mongoid-rspec 1.5.5 → 1.5.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/matchers/document.rb +12 -0
- data/lib/matchers/indexes.rb +8 -8
- data/lib/matchers/validations.rb +1 -1
- data/lib/matchers/validations/inclusion_of.rb +12 -12
- data/lib/matchers/validations/length_of.rb +48 -19
- data/lib/mongoid-rspec/version.rb +1 -1
- data/spec/models/article.rb +3 -2
- data/spec/unit/document_spec.rb +2 -0
- data/spec/unit/validations_spec.rb +2 -0
- metadata +4 -3
data/lib/matchers/document.rb
CHANGED
@@ -5,6 +5,11 @@ module Mongoid
|
|
5
5
|
@attributes = attrs.collect(&:to_s)
|
6
6
|
end
|
7
7
|
|
8
|
+
def localized
|
9
|
+
@localized = true
|
10
|
+
self
|
11
|
+
end
|
12
|
+
|
8
13
|
def of_type(type)
|
9
14
|
@type = type
|
10
15
|
self
|
@@ -43,6 +48,13 @@ module Mongoid
|
|
43
48
|
end
|
44
49
|
|
45
50
|
@errors.push("field #{attr.inspect}" << error) unless error.blank?
|
51
|
+
|
52
|
+
if @localized
|
53
|
+
unless @klass.fields[attr].localized?
|
54
|
+
@errors.push "is not localized #{attr.inspect}"
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
46
58
|
else
|
47
59
|
@errors.push "no field named #{attr.inspect}"
|
48
60
|
end
|
data/lib/matchers/indexes.rb
CHANGED
@@ -4,31 +4,31 @@ module Mongoid
|
|
4
4
|
def initialize(index_fields)
|
5
5
|
@index_fields = index_fields.symbolize_keys!
|
6
6
|
end
|
7
|
-
|
8
|
-
def with_options(options = {})
|
7
|
+
|
8
|
+
def with_options(options = { })
|
9
9
|
@options = options
|
10
10
|
self
|
11
11
|
end
|
12
|
-
|
12
|
+
|
13
13
|
def matches?(klass)
|
14
|
-
@klass
|
14
|
+
@klass = klass.is_a?(Class) ? klass : klass.class
|
15
15
|
@errors = []
|
16
|
-
|
16
|
+
|
17
17
|
unless @klass.index_options[@index_fields]
|
18
18
|
@errors.push "no index for #{@index_fields}"
|
19
19
|
else
|
20
20
|
if !@options.nil? && !@options.empty?
|
21
21
|
@options.each do |option, option_value|
|
22
|
-
if @klass.index_options[@index_fields]
|
22
|
+
if @klass.index_options[@index_fields][option] != option_value
|
23
23
|
@errors.push "index for #{@index_fields.inspect} with options of #{@klass.index_options[@index_fields].inspect}"
|
24
24
|
end
|
25
25
|
end
|
26
26
|
end
|
27
27
|
end
|
28
|
-
|
28
|
+
|
29
29
|
@errors.empty?
|
30
30
|
end
|
31
|
-
|
31
|
+
|
32
32
|
def failure_message_for_should
|
33
33
|
"Expected #{@klass.inspect} to #{description}, got #{@errors.to_sentence}"
|
34
34
|
end
|
data/lib/matchers/validations.rb
CHANGED
@@ -1,23 +1,23 @@
|
|
1
1
|
module Mongoid
|
2
2
|
module Matchers
|
3
|
-
module Validations
|
3
|
+
module Validations
|
4
4
|
class ValidateInclusionOfMatcher < HaveValidationMatcher
|
5
5
|
def initialize(name)
|
6
6
|
super(name, :inclusion)
|
7
7
|
end
|
8
|
-
|
8
|
+
|
9
9
|
def to_allow(*values)
|
10
10
|
@allowed_values = values.map(&:to_a).flatten
|
11
11
|
self
|
12
12
|
end
|
13
|
-
|
13
|
+
|
14
14
|
def matches?(actual)
|
15
15
|
return false unless result = super(actual)
|
16
16
|
|
17
17
|
if @allowed_values
|
18
18
|
raw_validator_allowed_values = @validator.options[:in]
|
19
19
|
|
20
|
-
validator_allowed_values = case raw_validator_allowed_values
|
20
|
+
validator_allowed_values = case raw_validator_allowed_values
|
21
21
|
when Range then raw_validator_allowed_values.to_a
|
22
22
|
when Proc then raw_validator_allowed_values.call(actual)
|
23
23
|
else raw_validator_allowed_values end
|
@@ -26,24 +26,24 @@ module Mongoid
|
|
26
26
|
if not_allowed_values.empty?
|
27
27
|
@positive_result_message = @positive_result_message << " allowing all values mentioned"
|
28
28
|
else
|
29
|
-
@negative_result_message = @negative_result_message << " not allowing
|
29
|
+
@negative_result_message = @negative_result_message << " not allowing these values: #{not_allowed_values.inspect}"
|
30
30
|
result = false
|
31
31
|
end
|
32
32
|
end
|
33
|
-
|
34
|
-
result
|
33
|
+
|
34
|
+
result
|
35
35
|
end
|
36
|
-
|
36
|
+
|
37
37
|
def description
|
38
38
|
options_desc = []
|
39
|
-
options_desc << " allowing
|
39
|
+
options_desc << " allowing these values: #{@allowed_values}" if @allowed_values
|
40
40
|
super << options_desc.to_sentence
|
41
|
-
end
|
41
|
+
end
|
42
42
|
end
|
43
43
|
|
44
44
|
def validate_inclusion_of(field)
|
45
45
|
ValidateInclusionOfMatcher.new(field)
|
46
|
-
end
|
46
|
+
end
|
47
47
|
end
|
48
48
|
end
|
49
|
-
end
|
49
|
+
end
|
@@ -10,11 +10,13 @@ module Mongoid
|
|
10
10
|
@maximum = value
|
11
11
|
self
|
12
12
|
end
|
13
|
+
alias :less_than :with_maximum
|
13
14
|
|
14
15
|
def with_minimum(value)
|
15
16
|
@minimum = value
|
16
17
|
self
|
17
18
|
end
|
19
|
+
alias :greater_than :with_minimum
|
18
20
|
|
19
21
|
def within(value)
|
20
22
|
@within = value
|
@@ -41,55 +43,82 @@ module Mongoid
|
|
41
43
|
|
42
44
|
def description
|
43
45
|
options_desc = []
|
44
|
-
options_desc << "
|
45
|
-
options_desc << "
|
46
|
-
options_desc << "
|
47
|
-
options_desc << "
|
48
|
-
super << options_desc.to_sentence
|
46
|
+
options_desc << "with minimum of #{@minimum}" if @minimum
|
47
|
+
options_desc << "with maximum of #{@maximum}" if @maximum
|
48
|
+
options_desc << "within the range of #{@within}" if @within
|
49
|
+
options_desc << "as exactly #{@is}" if @is
|
50
|
+
super << " #{options_desc.to_sentence}"
|
49
51
|
end
|
50
52
|
|
51
53
|
private
|
52
54
|
|
53
55
|
def check_maximum
|
54
|
-
|
55
|
-
|
56
|
+
if actual_max.nil?
|
57
|
+
@negative_result_message << " with no maximum"
|
58
|
+
@result = false
|
59
|
+
elsif actual_max == @maximum
|
56
60
|
@positive_result_message << " with maximum of #{@maximum}"
|
57
61
|
else
|
58
|
-
@negative_result_message << " with maximum of #{
|
62
|
+
@negative_result_message << " with maximum of #{actual_max}"
|
59
63
|
@result = false
|
60
64
|
end
|
61
65
|
end
|
62
66
|
|
63
67
|
def check_minimum
|
64
|
-
|
65
|
-
|
68
|
+
if actual_min.nil?
|
69
|
+
@negative_result_message << " with no minimum"
|
70
|
+
@result = false
|
71
|
+
elsif actual_min == @minimum
|
66
72
|
@positive_result_message << " with minimum of #{@minimum}"
|
67
73
|
else
|
68
|
-
@negative_result_message << " with minimum of #{
|
74
|
+
@negative_result_message << " with minimum of #{actual_min}"
|
69
75
|
@result = false
|
70
76
|
end
|
71
77
|
end
|
72
78
|
|
73
79
|
def check_range
|
74
80
|
min, max = [@within.min, @within.max]
|
75
|
-
|
76
|
-
|
77
|
-
@
|
81
|
+
if !actual_min.nil? and actual_max.nil?
|
82
|
+
@negative_result_message << " with no minimum but with maximum of #{actual_max}"
|
83
|
+
@result = false
|
84
|
+
elsif actual_min.nil? and !actual_max.nil?
|
85
|
+
@negative_result_message << " with minimum_of #{actual_min} but no maximum"
|
86
|
+
@result = false
|
87
|
+
elsif actual_min.nil? and actual_max.nil?
|
88
|
+
@negative_result_message << " with no minimum and maximum"
|
89
|
+
@result = false
|
90
|
+
elsif actual_min == min && actual_max == max
|
91
|
+
@positive_result_message << " within the range of #{@within.inspect}"
|
78
92
|
else
|
79
|
-
@negative_result_message << "
|
93
|
+
@negative_result_message << " within the range of #{(actual_min..actual_max).inspect}"
|
80
94
|
@result = false
|
81
95
|
end
|
82
96
|
end
|
83
97
|
|
84
98
|
def check_exact
|
85
|
-
|
86
|
-
|
87
|
-
@positive_result_message << " is exactly #{@is}"
|
99
|
+
if actual_is == @is
|
100
|
+
@positive_result_message << " as exactly #{@is}"
|
88
101
|
else
|
89
|
-
@negative_result_message << "
|
102
|
+
@negative_result_message << " as exactly #{actual_is}"
|
90
103
|
@result = false
|
91
104
|
end
|
92
105
|
end
|
106
|
+
|
107
|
+
def actual_is
|
108
|
+
actual_is = @validator.options[:is]
|
109
|
+
end
|
110
|
+
|
111
|
+
def actual_min
|
112
|
+
@validator.options[:minimum] || ((@validator.options[:in] || @validator.options[:within]).try(&:min)).tap do |x|
|
113
|
+
puts "minimum: #{x}, #{@validator.options}"
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
def actual_max
|
118
|
+
@validator.options[:maximum] || ((@validator.options[:in] || @validator.options[:within]).try(&:max)).tap do |x|
|
119
|
+
puts "maximum: #{x}"
|
120
|
+
end
|
121
|
+
end
|
93
122
|
end
|
94
123
|
|
95
124
|
def validate_length_of(field)
|
data/spec/models/article.rb
CHANGED
@@ -5,7 +5,7 @@ class Article
|
|
5
5
|
include Mongoid::Versioning
|
6
6
|
include Mongoid::MultiParameterAttributes
|
7
7
|
|
8
|
-
field :title
|
8
|
+
field :title, :localize => true
|
9
9
|
field :content
|
10
10
|
field :published, :type => Boolean, :default => false
|
11
11
|
field :allow_comments, :type => Boolean, :default => true
|
@@ -17,7 +17,8 @@ class Article
|
|
17
17
|
|
18
18
|
validates :title, :presence => true
|
19
19
|
|
20
|
-
validates_length_of :title, :
|
20
|
+
validates_length_of :title, :within => 8..16
|
21
|
+
validates_length_of :content, :minimum => 200
|
21
22
|
|
22
23
|
index({ title: 1 }, { unique: true, background: true })
|
23
24
|
index({ published: 1 })
|
data/spec/unit/document_spec.rb
CHANGED
@@ -11,6 +11,8 @@ describe "Document" do
|
|
11
11
|
describe Article do
|
12
12
|
it { should have_field(:published).of_type(Boolean).with_default_value_of(false) }
|
13
13
|
it { should have_field(:allow_comments).of_type(Boolean).with_default_value_of(true) }
|
14
|
+
it { should belong_to(:author) }
|
15
|
+
it { should have_field(:title).localized }
|
14
16
|
it { should_not have_field(:allow_comments).of_type(Boolean).with_default_value_of(false) }
|
15
17
|
it { should_not have_field(:number_of_comments).of_type(Integer).with_default_value_of(1) }
|
16
18
|
it { should be_mongoid_document }
|
@@ -30,6 +30,8 @@ describe "Validations" do
|
|
30
30
|
|
31
31
|
describe Article do
|
32
32
|
it { should validate_length_of(:title).within(8..16) }
|
33
|
+
it { should_not validate_length_of(:content).greater_than(200).less_than(16) }
|
34
|
+
it { should validate_length_of(:content).greater_than(200) }
|
33
35
|
end
|
34
36
|
|
35
37
|
describe MovieArticle do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid-rspec
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.5.
|
4
|
+
version: 1.5.6
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-01-13 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
@@ -133,7 +133,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
133
133
|
version: '0'
|
134
134
|
requirements: []
|
135
135
|
rubyforge_project: mongoid-rspec
|
136
|
-
rubygems_version: 1.8.
|
136
|
+
rubygems_version: 1.8.23
|
137
137
|
signing_key:
|
138
138
|
specification_version: 3
|
139
139
|
summary: RSpec matchers for Mongoid
|
@@ -156,3 +156,4 @@ test_files:
|
|
156
156
|
- spec/unit/indexes_spec.rb
|
157
157
|
- spec/unit/validations_spec.rb
|
158
158
|
- spec/validators/ssn_validator.rb
|
159
|
+
has_rdoc:
|