sequel_spec 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +18 -0
  3. data/.rspec +2 -0
  4. data/Gemfile +4 -0
  5. data/LICENSE +22 -0
  6. data/README.md +91 -0
  7. data/Rakefile +1 -0
  8. data/lib/sequel_spec.rb +8 -0
  9. data/lib/sequel_spec/association.rb +13 -0
  10. data/lib/sequel_spec/association/association_matcher.rb +32 -0
  11. data/lib/sequel_spec/association/have_many_to_many_matcher.rb +15 -0
  12. data/lib/sequel_spec/association/have_many_to_one_matcher.rb +15 -0
  13. data/lib/sequel_spec/association/have_one_through_one_matcher.rb +15 -0
  14. data/lib/sequel_spec/association/have_one_to_many_matcher.rb +15 -0
  15. data/lib/sequel_spec/association/have_one_to_one_matcher.rb +15 -0
  16. data/lib/sequel_spec/base.rb +45 -0
  17. data/lib/sequel_spec/integration/rspec.rb +6 -0
  18. data/lib/sequel_spec/validation.rb +18 -0
  19. data/lib/sequel_spec/validation/validate_format_matcher.rb +42 -0
  20. data/lib/sequel_spec/validation/validate_includes_matcher.rb +43 -0
  21. data/lib/sequel_spec/validation/validate_integer_matcher.rb +23 -0
  22. data/lib/sequel_spec/validation/validate_length_matcher.rb +81 -0
  23. data/lib/sequel_spec/validation/validate_matcher.rb +67 -0
  24. data/lib/sequel_spec/validation/validate_not_null_matcher.rb +21 -0
  25. data/lib/sequel_spec/validation/validate_numeric_matcher.rb +23 -0
  26. data/lib/sequel_spec/validation/validate_presence_matcher.rb +23 -0
  27. data/lib/sequel_spec/validation/validate_schema_types_matcher.rb +23 -0
  28. data/lib/sequel_spec/validation/validate_type_matcher.rb +42 -0
  29. data/lib/sequel_spec/validation/validate_unique_matcher.rb +31 -0
  30. data/lib/sequel_spec/version.rb +3 -0
  31. data/sequel_spec.gemspec +25 -0
  32. data/spec/migrations/001_create_tables.rb +26 -0
  33. data/spec/sequel_spec/association/have_many_to_many_matcher_spec.rb +57 -0
  34. data/spec/sequel_spec/association/have_many_to_one_matcher_spec.rb +57 -0
  35. data/spec/sequel_spec/association/have_one_through_one_matcher_spec.rb +57 -0
  36. data/spec/sequel_spec/association/have_one_to_many_matcher_spec.rb +57 -0
  37. data/spec/sequel_spec/association/have_one_to_one_matcher_spec.rb +57 -0
  38. data/spec/sequel_spec/validation/validate_format_matcher_spec.rb +88 -0
  39. data/spec/sequel_spec/validation/validate_includes_matcher_spec.rb +88 -0
  40. data/spec/sequel_spec/validation/validate_integer_matcher_spec.rb +76 -0
  41. data/spec/sequel_spec/validation/validate_length_matcher_spec.rb +252 -0
  42. data/spec/sequel_spec/validation/validate_not_null_matcher_spec.rb +83 -0
  43. data/spec/sequel_spec/validation/validate_numeric_matcher_spec.rb +76 -0
  44. data/spec/sequel_spec/validation/validate_presence_matcher_spec.rb +77 -0
  45. data/spec/sequel_spec/validation/validate_schema_types_matcher_spec.rb +83 -0
  46. data/spec/sequel_spec/validation/validate_type_matcher_spec.rb +88 -0
  47. data/spec/sequel_spec/validation/validate_unique_matcher_spec.rb +83 -0
  48. data/spec/spec_helper.rb +33 -0
  49. data/spec/support/test_helpers.rb +19 -0
  50. metadata +164 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1d1cdbc693387bd8389f02b56356ba409fa38eea
4
+ data.tar.gz: 66ff755f8c42070f4c558a4ca2098802f97c4bb3
5
+ SHA512:
6
+ metadata.gz: 9e7b4b4065e4b43515e78d05bee8228ff083c20858c0adce136eb763294c1bc2bbb507bb5a4a5f482bfd375dd32b80b969c265d2d274b9620df8682d104730ba
7
+ data.tar.gz: 2c02bc59848bd3d52c9f81df7347cbfcdcf78cb222247365265fd7d9cc85a667fbd5d9dec2543330abe625eaf58c805d0a7356d313ee1c3e42133a8cc54dd54c
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.db
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sequel_spec.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Adrià Planas, Jonathan Tron, Joseph Halter
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,91 @@
1
+ # SequelSpec
2
+
3
+ SequelSpec is a refactor of [rspec_sequel_matchers](https://github.com/openhood/rspec_sequel_matchers) borrowing concepts from [shoulda-matchers](https://github.com/thoughtbot/shoulda-matchers). In contrast with rspec_sequel_matchers it provides a sweeter syntax and it doesn't rely on a specific mocking framework.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'sequel_spec', :group => :test
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install sequel_spec
18
+
19
+ ## Usage
20
+
21
+ The matchers are automatically included on RSpec. Just use them at will.
22
+
23
+ ### Association
24
+
25
+ ```ruby
26
+ it { should have_many_to_many :items }
27
+ it { should have_many_to_one :item }
28
+ it { should have_one_to_many :comments }
29
+ it { should have_one_to_one :photo }
30
+ it { should have_one_through_one :author }
31
+ ```
32
+
33
+ You can specify options using ```#with_options```
34
+
35
+ ```ruby
36
+ it { should have_one_through_one(:author).with_options :key => :user_id }
37
+ ```
38
+
39
+ ### Validation
40
+
41
+ ```ruby
42
+ # Format
43
+ it { should validate_format_of(:name).with(/\w+/) }
44
+
45
+ # Inclusion
46
+ it { should ensure_inclusion_of(:name).in(['John', 'Smith']) }
47
+
48
+ # Integrity (valid integer)
49
+ it { should validate_integrity_of(:age) }
50
+
51
+ # Length
52
+ it { should validate_length_of(:name).is(20) }
53
+ it { should validate_length_of(:name).is_at_least(20) }
54
+ it { should validate_length_of(:name).is_at_most(20) }
55
+ it { should validate_length_of(:name).is_between(2..20) }
56
+
57
+ # Not null
58
+ it { should validate_not_null(:category_id) }
59
+
60
+ # Numericality (valid float)
61
+ it { should validate_numericality_of(:price) }
62
+
63
+ # Type
64
+ it { should validate_type_of(:birthdate).is Date }
65
+
66
+ # Uniqueness
67
+ it { should validate_uniqueness_of(:username) }
68
+ ```
69
+
70
+ This matchers accepts options through ```#with_options``` as well
71
+
72
+ ```ruby
73
+ it { should validate_uniqueness_of(:username).with_options :message => "This username is already taken" }
74
+ ```
75
+
76
+ Or with the specialized helpers: ```#allowing_nil```, ```#allowing_blank```, ```#allowing_missing```, and ```#with_message```
77
+
78
+ ```ruby
79
+ it { should validate_length_of(:name).is(20).allowing_nil }
80
+ it { should validate_length_of(:name).is(20).allowing_blank }
81
+ it { should validate_length_of(:name).is(20).allowing_missing }
82
+ it { should validate_length_of(:name).is(20).with_message "Name should be 20 chars long" }
83
+ ```
84
+
85
+ ## Contributing
86
+
87
+ 1. Fork it ( http://github.com/<my-github-username>/sequel_spec/fork )
88
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
89
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
90
+ 4. Push to the branch (`git push origin my-new-feature`)
91
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,8 @@
1
+ require 'sequel_spec/version'
2
+ require 'sequel_spec/base'
3
+ require 'sequel_spec/association'
4
+ require 'sequel_spec/validation'
5
+
6
+ if defined?(RSpec)
7
+ require 'sequel_spec/integration/rspec'
8
+ end
@@ -0,0 +1,13 @@
1
+ require 'sequel_spec/association/association_matcher'
2
+ require 'sequel_spec/association/have_many_to_many_matcher'
3
+ require 'sequel_spec/association/have_many_to_one_matcher'
4
+ require 'sequel_spec/association/have_one_to_many_matcher'
5
+ require 'sequel_spec/association/have_one_to_one_matcher'
6
+ require 'sequel_spec/association/have_one_through_one_matcher'
7
+
8
+ module SequelSpec
9
+ module Matchers
10
+ module Association
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,32 @@
1
+ module SequelSpec
2
+ module Matchers
3
+ module Association
4
+ class AssociationMatcher < Base
5
+ def description
6
+ desc = "have a #{association_type} association #{@attribute.inspect}"
7
+ desc << " with option(s) #{hash_to_nice_string @options}" unless @options.empty?
8
+ desc
9
+ end
10
+
11
+ def valid?(db, instance, klass, attribute, options)
12
+ @association = klass.association_reflection(attribute) || {}
13
+
14
+ if @association.empty?
15
+ @suffix << "(no association #{@attribute.inspect} found)"
16
+ false
17
+ else
18
+ matching = @association[:type] == association_type
19
+ options.each do |key, value|
20
+ if @association[key] != value
21
+ @suffix << "expected #{key.inspect} == #{value.inspect} but found #{@association[key].inspect} instead"
22
+ matching = false
23
+ end
24
+ end
25
+
26
+ matching
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,15 @@
1
+ module SequelSpec
2
+ module Matchers
3
+ module Association
4
+ class HaveManyToManyMatcher < AssociationMatcher
5
+ def association_type
6
+ :many_to_many
7
+ end
8
+ end
9
+
10
+ def have_many_to_many(*args)
11
+ HaveManyToManyMatcher.new(*args)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module SequelSpec
2
+ module Matchers
3
+ module Association
4
+ class HaveManyToOneMatcher < AssociationMatcher
5
+ def association_type
6
+ :many_to_one
7
+ end
8
+ end
9
+
10
+ def have_many_to_one(*args)
11
+ HaveManyToOneMatcher.new(*args)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module SequelSpec
2
+ module Matchers
3
+ module Association
4
+ class HaveOneThroughOneMatcher < AssociationMatcher
5
+ def association_type
6
+ :one_through_one
7
+ end
8
+ end
9
+
10
+ def have_one_through_one(*args)
11
+ HaveOneThroughOneMatcher.new(*args)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module SequelSpec
2
+ module Matchers
3
+ module Association
4
+ class HaveOneToManyMatcher < AssociationMatcher
5
+ def association_type
6
+ :one_to_many
7
+ end
8
+ end
9
+
10
+ def have_one_to_many(*args)
11
+ HaveOneToManyMatcher.new(*args)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module SequelSpec
2
+ module Matchers
3
+ module Association
4
+ class HaveOneToOneMatcher < AssociationMatcher
5
+ def association_type
6
+ :one_to_one
7
+ end
8
+ end
9
+
10
+ def have_one_to_one(*args)
11
+ HaveOneToOneMatcher.new(*args)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,45 @@
1
+ module SequelSpec
2
+ module Matchers
3
+ class Base
4
+ def initialize(attribute)
5
+ raise ArgumentError, "Attribute not specified" unless attribute
6
+ @attribute = attribute
7
+ @description = []
8
+ @options = {}
9
+
10
+ self
11
+ end
12
+
13
+ def matches?(subject)
14
+ @suffix = []
15
+
16
+ if subject.is_a?(Sequel::Model)
17
+ @prefix = "expected #{subject.inspect} to"
18
+ valid?(subject.db, subject, subject.class, @attribute, @options)
19
+ else
20
+ @prefix = "expected #{subject.table_name.to_s.classify} to"
21
+ valid?(subject.db, subject.new, subject, @attribute, @options)
22
+ end
23
+ end
24
+
25
+ def with_options(opts)
26
+ raise ArgumentError, "#with_options expects a hash, #{opts.class} given" unless opts.is_a? Hash
27
+
28
+ @options.merge! opts
29
+ self
30
+ end
31
+
32
+ def failure_message
33
+ [@prefix, description, @suffix].flatten.compact.join(" ")
34
+ end
35
+
36
+ def negative_failure_message
37
+ [@prefix, "not", description, @suffix].flatten.compact.join(" ")
38
+ end
39
+
40
+ def hash_to_nice_string(hash)
41
+ hash.sort{ |a,b| a[0].to_s<=>b[0].to_s }.collect{ |param| param.collect{ |v| v.inspect }.join(" => ") }.join(", ")
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,6 @@
1
+ require 'rspec/core'
2
+
3
+ RSpec.configure do |config|
4
+ config.include SequelSpec::Matchers::Validation
5
+ config.include SequelSpec::Matchers::Association
6
+ end
@@ -0,0 +1,18 @@
1
+ require 'sequel_spec/validation/validate_matcher'
2
+ require 'sequel_spec/validation/validate_length_matcher'
3
+ require 'sequel_spec/validation/validate_format_matcher'
4
+ require 'sequel_spec/validation/validate_includes_matcher'
5
+ require 'sequel_spec/validation/validate_integer_matcher'
6
+ require 'sequel_spec/validation/validate_numeric_matcher'
7
+ require 'sequel_spec/validation/validate_presence_matcher'
8
+ require 'sequel_spec/validation/validate_unique_matcher'
9
+ require 'sequel_spec/validation/validate_not_null_matcher'
10
+ require 'sequel_spec/validation/validate_type_matcher'
11
+ require 'sequel_spec/validation/validate_schema_types_matcher'
12
+
13
+ module SequelSpec
14
+ module Matchers
15
+ module Validation
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,42 @@
1
+ module SequelSpec
2
+ module Matchers
3
+ module Validation
4
+ class ValidateFormatMatcher < ValidateMatcher
5
+ def description
6
+ desc = "validate format of #{@attribute.inspect} against #{@additionnal.inspect}"
7
+ desc << " with option(s) #{hash_to_nice_string @options}" unless @options.empty?
8
+ desc
9
+ end
10
+
11
+ def additionnal_param_required?
12
+ true
13
+ end
14
+
15
+ def additionnal_param_check
16
+ unless @additionnal
17
+ raise ArgumentError, "You should specify the format using #with"
18
+ end
19
+ end
20
+
21
+ def with(value)
22
+ unless value.is_a?(Regexp)
23
+ raise ArgumentError, "#with expects a Regexp"
24
+ end
25
+
26
+ @additionnal = value
27
+ self
28
+ end
29
+
30
+ def validation_type
31
+ :validates_format
32
+ end
33
+ end
34
+
35
+ def validate_format(attribute)
36
+ ValidateFormatMatcher.new(attribute)
37
+ end
38
+
39
+ alias :validate_format_of :validate_format
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,43 @@
1
+ module SequelSpec
2
+ module Matchers
3
+ module Validation
4
+ class ValidateIncludesMatcher < ValidateMatcher
5
+ def description
6
+ desc = "validate that #{@attribute.inspect} is included in #{@additionnal.inspect}"
7
+ desc << " with option(s) #{hash_to_nice_string @options}" unless @options.empty?
8
+ desc
9
+ end
10
+
11
+ # temp workaround
12
+ def additionnal_param_required?
13
+ true
14
+ end
15
+
16
+ def additionnal_param_check
17
+ unless @additionnal
18
+ raise ArgumentError, "You should specify inclusion using #in"
19
+ end
20
+ end
21
+
22
+ def in(value)
23
+ unless value.is_a?(Enumerable)
24
+ raise ArgumentError, "#in expects a #{Enumerable}"
25
+ end
26
+
27
+ @additionnal = value
28
+ self
29
+ end
30
+
31
+ def validation_type
32
+ :validates_includes
33
+ end
34
+ end
35
+
36
+ def validate_includes(attribute)
37
+ ValidateIncludesMatcher.new(attribute)
38
+ end
39
+
40
+ alias :ensure_inclusion_of :validate_includes
41
+ end
42
+ end
43
+ end