mongoid-minitest 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,3 +1,16 @@
1
+ ### 0.0.3 - 04/14/2012
2
+
3
+ + Added `have_index_for(indexed_field)` document matcher.
4
+ + Added `embedded_in(association_name)` association matcher.
5
+ + Added `embed_many(association_name)` association matcher.
6
+ + Fixed issue check `association_type` if metadata is not nil.
7
+ + Added `embed_one(association_name)` association matcher.
8
+ + Added `have_and_belong_to_many(association_name)` association matcher.
9
+ + Added `validate_associated(association_name)` validation matcher.
10
+ + Added `accept_with(value)` chain matcher to `validate_acceptance_of`.
11
+ + Added `validate_acceptance_of(field)` validation matcher.
12
+ + Added `validate_confirmation_of(field)` validation matcher.
13
+
1
14
  ### 0.0.2 - 04/13/2012
2
15
 
3
16
  + Added `have_one(association_name)` association matcher.
@@ -11,7 +24,7 @@
11
24
  + Added `validate_inclusion_of(field)` validation matcher.
12
25
  + Added `belong_to(association_name)` association matcher.
13
26
 
14
- ### 0.0.1 - 04/11/2012
27
+ ### 0.0.1 - 04/12/2012
15
28
 
16
29
  + Added `of_type(type)` chain matcher to association matcher.
17
30
  + Added `have_many(association_name)` association matcher.
data/README.md CHANGED
@@ -2,14 +2,16 @@
2
2
 
3
3
  MiniTest matchers for Mongoid.
4
4
 
5
- * Compatible with Ruby >=1.9.2.
6
- * Tested on MiniTest 2.x
5
+ ## Requirements
6
+
7
+ * Ruby 1.9.x
8
+ * Mongoid 2.x
7
9
 
8
10
  ## Installation
9
11
 
10
12
  Add this line to your application's Gemfile:
11
13
 
12
- gem 'mongoid-minitest'
14
+ gem "mongoid-minitest"
13
15
 
14
16
  And then execute:
15
17
 
@@ -93,6 +95,8 @@ See the following examples:
93
95
  it { must have_fields(:name, :login).of_type(String) }
94
96
  it { must have_fields(:name, :login).with_default_value("me") }
95
97
  it { must have_fields(:name, :login).of_type(String).with_default_value("me") }
98
+
99
+ it { must have_index_for(:name) }
96
100
  end
97
101
 
98
102
  ### Validation Matchers
@@ -101,7 +105,7 @@ See the following examples:
101
105
  subject { Person }
102
106
 
103
107
  it { must validate_presence_of(:name) }
104
-
108
+
105
109
  it { must validate_uniqueness_of(:login).case_insensitive }
106
110
  it { must validate_uniqueness_of(:login).scoped_to(:site) }
107
111
 
@@ -119,6 +123,11 @@ See the following examples:
119
123
  it { must validate_inclusion_of(:role).to_allow("user", "admin") }
120
124
  it { must validate_exclusion_of(:email).to_not_allow("foo@bar.com", "fizz@buzz.com") }
121
125
 
126
+ it { must validate_confirmation_of(:password) }
127
+ it { must validate_acceptance_of(:terms_of_use).accept_with("1") }
128
+
129
+ it { must validate_associated(:pets) }
130
+
122
131
  # Testing validators custom messages
123
132
  it { must validate_presence_of(:role).with_message("no role") }
124
133
  it { must validate_length_of(:password).with_min(8).with_message("len >= 8") }
@@ -130,20 +139,24 @@ See the following examples:
130
139
  describe Person do
131
140
  subject { Person }
132
141
 
133
- it { must have_one(:account).of_type(Account) }
142
+ it { must have_one(:account) }
134
143
  it { must have_many(:pets).of_type(Pet) }
144
+ it { must have_and_belong_to_many(:friends) }
145
+
146
+ it { must embed_one(:profile) }
147
+ it { must embed_many(:sites) }
135
148
  end
136
149
 
137
150
  describe Pet do
138
151
  subject { Pet }
139
152
 
140
- it { must belong_to(:person).of_type(Person) }
153
+ it { must belong_to(:person) }
141
154
  end
142
155
 
143
- describe Account do
144
- subject { Account }
156
+ describe Site do
157
+ subject { Site }
145
158
 
146
- it { must belong_to(:person).of_type(Person) }
159
+ it { must embedded_in(:person) }
147
160
  end
148
161
  end
149
162
 
@@ -159,31 +172,6 @@ See the following examples:
159
172
 
160
173
  To run unit tests, run `bundle exec rake` or `bundle exec guard` to run in auto mode.
161
174
 
162
- ## License
163
-
164
- Copyright (c) 2012 Francesco Rodriguez
165
-
166
- MIT License
167
-
168
- Permission is hereby granted, free of charge, to any person obtaining
169
- a copy of this software and associated documentation files (the
170
- "Software"), to deal in the Software without restriction, including
171
- without limitation the rights to use, copy, modify, merge, publish,
172
- distribute, sublicense, and/or sell copies of the Software, and to
173
- permit persons to whom the Software is furnished to do so, subject to
174
- the following conditions:
175
-
176
- The above copyright notice and this permission notice shall be
177
- included in all copies or substantial portions of the Software.
178
-
179
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
180
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
181
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
182
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
183
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
184
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
185
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
186
-
187
175
  ## Last words
188
176
 
189
177
  At [EpicLabs](https://github.com/epiclabs/), We're migrating all the tests from `rspec` to `minitest`. I've been using
@@ -1,9 +1,13 @@
1
1
  module Mongoid
2
2
  module Matchers
3
3
  module Associations
4
- HAS_ONE = Mongoid::Relations::Referenced::One
5
- HAS_MANY = Mongoid::Relations::Referenced::Many
4
+ HAS_ONE = Mongoid::Relations::Referenced::One
5
+ HAS_MANY = Mongoid::Relations::Referenced::Many
6
+ HAS_AND_BELONGS_TO_MANY = Mongoid::Relations::Referenced::ManyToMany
6
7
  BELONGS_TO = Mongoid::Relations::Referenced::In
8
+ EMBEDS_ONE = Mongoid::Relations::Embedded::One
9
+ EMBEDS_MANY = Mongoid::Relations::Embedded::Many
10
+ EMBEDDED_IN = Mongoid::Relations::Embedded::In
7
11
 
8
12
  class HaveAssociationMatcher
9
13
  include Helpers
@@ -45,16 +49,15 @@ module Mongoid
45
49
 
46
50
  def check_association_name
47
51
  if @metadata.nil?
48
- @negative_message = "no association named #{@association[:name]}"
52
+ @negative_message = "no association named #{@association[:name].inspect}"
49
53
  @result = false
50
54
  else
51
- @positive_message = "association named #{@association[:name]}"
55
+ @positive_message = "association named #{@association[:name].inspect}"
52
56
  end
53
57
  end
54
58
 
55
59
  def check_association_type
56
- @relation = @metadata.relation
57
- if @relation != @association[:type]
60
+ if !@metadata.nil? && @metadata.relation != @association[:type]
58
61
  @negative_message = association_type_failure_message
59
62
  @result = false
60
63
  else
@@ -64,7 +67,7 @@ module Mongoid
64
67
 
65
68
  def association_type_failure_message
66
69
  msg = "#{@klass.inspect}"
67
- msg << " #{type_description(@relation, false)}"
70
+ msg << " #{type_description(@association[:type], false)}"
68
71
  msg << " #{@association[:name].inspect}"
69
72
 
70
73
  msg
@@ -86,8 +89,16 @@ module Mongoid
86
89
  (passive ? "reference" : "references") << " one"
87
90
  when HAS_MANY.name
88
91
  (passive ? "reference" : "references") << " many"
92
+ when HAS_AND_BELONGS_TO_MANY.name
93
+ (passive ? "reference" : "references") << " and referenced in many"
89
94
  when BELONGS_TO.name
90
95
  (passive ? "be referenced" : "referenced") << " in"
96
+ when EMBEDS_ONE.name
97
+ (passive ? "embed" : "embeds") << " one"
98
+ when EMBEDS_MANY.name
99
+ (passive ? "embed" : "embeds") << " many"
100
+ when EMBEDDED_IN.name
101
+ (passive ? "be" : "is") << " embedded in"
91
102
  else
92
103
  raise "Unknown association type #{type}"
93
104
  end
@@ -102,9 +113,25 @@ module Mongoid
102
113
  HaveAssociationMatcher.new(association_name, HAS_MANY)
103
114
  end
104
115
 
116
+ def have_and_belong_to_many(association_name)
117
+ HaveAssociationMatcher.new(association_name, HAS_AND_BELONGS_TO_MANY)
118
+ end
119
+
105
120
  def belong_to(association_name)
106
121
  HaveAssociationMatcher.new(association_name, BELONGS_TO)
107
122
  end
123
+
124
+ def embed_one(association_name)
125
+ HaveAssociationMatcher.new(association_name, EMBEDS_ONE)
126
+ end
127
+
128
+ def embed_many(association_name)
129
+ HaveAssociationMatcher.new(association_name, EMBEDS_MANY)
130
+ end
131
+
132
+ def embedded_in(association_name)
133
+ HaveAssociationMatcher.new(association_name, EMBEDDED_IN)
134
+ end
108
135
  end
109
136
  end
110
137
  end
@@ -0,0 +1,39 @@
1
+ module Mongoid
2
+ module Matchers
3
+ class HaveIndexMatcher
4
+ include Helpers
5
+
6
+ def initialize(field)
7
+ @field = field.to_s
8
+ end
9
+
10
+ def matches?(subject)
11
+ @klass = class_of(subject)
12
+
13
+ if !@klass.index_options[@field.to_sym]
14
+ @error = "no index for #{@field.inspect}"
15
+
16
+ return false
17
+ end
18
+
19
+ true
20
+ end
21
+
22
+ def failure_message
23
+ "#{@klass} to #{description}, got #{@error}"
24
+ end
25
+
26
+ def negative_failure_message
27
+ "#{@klass} to not #{description}, got #{@klass} to #{description}"
28
+ end
29
+
30
+ def description
31
+ "have an index for #{@field.inspect}"
32
+ end
33
+ end
34
+
35
+ def have_index_for(field)
36
+ HaveIndexMatcher.new(field)
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,47 @@
1
+ module Mongoid
2
+ module Matchers
3
+ module Validations
4
+ class ValidateAcceptanceMatcher < HaveValidationMatcher
5
+ def initialize(field)
6
+ super(field, :acceptance)
7
+ end
8
+
9
+ def accept_with(value)
10
+ @accepted = value
11
+ self
12
+ end
13
+
14
+ def matches?(subject)
15
+ return false unless @result = super(subject)
16
+
17
+ check_accepted_value if @accepted
18
+
19
+ @result
20
+ end
21
+
22
+ def description
23
+ desc = []
24
+ desc << " accept with #{@accepted.inspect}" if @accepted
25
+ super << desc.to_sentence
26
+ end
27
+
28
+ private
29
+
30
+ def check_accepted_value
31
+ actual = @validator.options[:accept]
32
+
33
+ if actual == @accepted
34
+ @positive_message << " accept with #{actual.inspect}"
35
+ else
36
+ @negative_message << " accept with #{actual.inspect}"
37
+ @result = false
38
+ end
39
+ end
40
+ end
41
+
42
+ def validate_acceptance_of(field)
43
+ ValidateAcceptanceMatcher.new(field)
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,19 @@
1
+ module Mongoid
2
+ module Matchers
3
+ module Validations
4
+ class ValidateAssociated < HaveValidationMatcher
5
+ def initialize(association_name)
6
+ super(association_name, :associated)
7
+ end
8
+
9
+ def description
10
+ "validate associated #{@field.inspect}"
11
+ end
12
+ end
13
+
14
+ def validate_associated(association_name)
15
+ ValidateAssociated.new(association_name)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,9 @@
1
+ module Mongoid
2
+ module Matchers
3
+ module Validations
4
+ def validate_confirmation_of(field)
5
+ HaveValidationMatcher.new(field, :confirmation)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module MiniTest
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
@@ -7,13 +7,17 @@ require "matchers/helpers"
7
7
  require "matchers/document/document"
8
8
  require "matchers/document/have_field"
9
9
  require "matchers/document/be_stored_in"
10
+ require "matchers/document/have_index"
10
11
  require "matchers/validations/validations"
11
- require "matchers/validations/presence_of"
12
- require "matchers/validations/uniqueness_of"
13
- require "matchers/validations/length_of"
14
- require "matchers/validations/format_of"
15
- require "matchers/validations/inclusion_of"
16
- require "matchers/validations/exclusion_of"
12
+ require "matchers/validations/presence"
13
+ require "matchers/validations/uniqueness"
14
+ require "matchers/validations/length"
15
+ require "matchers/validations/format"
16
+ require "matchers/validations/inclusion"
17
+ require "matchers/validations/exclusion"
18
+ require "matchers/validations/confirmation"
19
+ require "matchers/validations/acceptance"
20
+ require "matchers/validations/associated"
17
21
  require "matchers/associations/associations"
18
22
 
19
23
  module Mongoid
@@ -7,6 +7,10 @@ describe "Associations" do
7
7
 
8
8
  it { must have_many(:pets).of_type(Pet) }
9
9
  it { must have_one(:account).of_type(Account) }
10
+ it { must have_and_belong_to_many(:friends).of_type(Person) }
11
+
12
+ it { must embed_one(:profile) }
13
+ it { must embed_many(:sites) }
10
14
  end
11
15
 
12
16
  describe Pet do
@@ -21,4 +25,10 @@ describe "Associations" do
21
25
  it { must belong_to(:person).of_type(Person) }
22
26
  end
23
27
 
28
+ describe Site do
29
+ subject { Site }
30
+
31
+ it { must embedded_in(:person) }
32
+ end
33
+
24
34
  end
@@ -13,5 +13,7 @@ describe "Document" do
13
13
 
14
14
  it { must have_field(:name).of_type(String).with_default_value("me") }
15
15
  it { must have_fields(:name, :login).of_type(String).with_default_value("me") }
16
+
17
+ it { must have_index_for(:name) }
16
18
  end
17
19
  end
@@ -24,5 +24,10 @@ describe "Validations" do
24
24
 
25
25
  it { must validate_inclusion_of(:role).to_allow("user", "admin") }
26
26
  it { must validate_exclusion_of(:email).to_not_allow("foo@bar.com", "fizz@buzz.com") }
27
+
28
+ it { must validate_confirmation_of(:password) }
29
+ it { must validate_acceptance_of(:terms_of_use).accept_with("1") }
30
+
31
+ it { must validate_associated(:pets) }
27
32
  end
28
33
  end
@@ -12,16 +12,29 @@ class Person
12
12
  field :role, type: String
13
13
  field :site, type: String
14
14
 
15
- has_many :pets
15
+ index :name
16
+
16
17
  has_one :account
18
+ has_many :pets
19
+ has_and_belongs_to_many :friends, class_name: "Person"
20
+
21
+ embeds_one :profile
22
+ embeds_many :sites
17
23
 
18
24
  validates_presence_of(:name)
19
25
  validates_presence_of(:role, message: "no role")
26
+
27
+ validates_confirmation_of(:password)
28
+ validates_acceptance_of(:terms_of_use)
29
+
20
30
  validates_uniqueness_of(:email)
21
31
  validates_uniqueness_of(:login, scope: :site, case_sensitive: false)
32
+
22
33
  validates_length_of(:password, minimum: 8, maximum: 16)
23
34
  validates_length_of(:login, in: 5..12)
35
+
24
36
  validates_format_of(:email, with: /^([^\s]+)((?:[-a-z0-9]\.)[a-z]{2,})$/i)
37
+
25
38
  validates_inclusion_of(:role, in: ["admin", "user"])
26
39
  validates_exclusion_of(:email, in: ["foo@bar.com", "fizz@buzz.com"])
27
40
  end
@@ -37,3 +50,13 @@ class Account
37
50
 
38
51
  belongs_to :person
39
52
  end
53
+
54
+ class Profile
55
+ include Mongoid::Document
56
+ end
57
+
58
+ class Site
59
+ include Mongoid::Document
60
+
61
+ embedded_in :person
62
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid-minitest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-13 00:00:00.000000000 Z
12
+ date: 2012-04-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: minitest
16
- requirement: &70256836430000 !ruby/object:Gem::Requirement
16
+ requirement: &70194736091660 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 2.12.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70256836430000
24
+ version_requirements: *70194736091660
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: mongoid
27
- requirement: &70256836423540 !ruby/object:Gem::Requirement
27
+ requirement: &70194736084360 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 2.4.7
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70256836423540
35
+ version_requirements: *70194736084360
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: bson_ext
38
- requirement: &70256836425720 !ruby/object:Gem::Requirement
38
+ requirement: &70194736087380 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 1.6.1
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70256836425720
46
+ version_requirements: *70194736087380
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: guard-minitest
49
- requirement: &70256836427820 !ruby/object:Gem::Requirement
49
+ requirement: &70194736089500 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70256836427820
57
+ version_requirements: *70194736089500
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rake
60
- requirement: &70256836328980 !ruby/object:Gem::Requirement
60
+ requirement: &70194752344020 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
@@ -65,7 +65,7 @@ dependencies:
65
65
  version: 0.9.2
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70256836328980
68
+ version_requirements: *70194752344020
69
69
  description: Minitest matchers for Mongoid
70
70
  email:
71
71
  - lrodriguezsanc@gmail.com
@@ -86,13 +86,17 @@ files:
86
86
  - lib/matchers/document/be_stored_in.rb
87
87
  - lib/matchers/document/document.rb
88
88
  - lib/matchers/document/have_field.rb
89
+ - lib/matchers/document/have_index.rb
89
90
  - lib/matchers/helpers.rb
90
- - lib/matchers/validations/exclusion_of.rb
91
- - lib/matchers/validations/format_of.rb
92
- - lib/matchers/validations/inclusion_of.rb
93
- - lib/matchers/validations/length_of.rb
94
- - lib/matchers/validations/presence_of.rb
95
- - lib/matchers/validations/uniqueness_of.rb
91
+ - lib/matchers/validations/acceptance.rb
92
+ - lib/matchers/validations/associated.rb
93
+ - lib/matchers/validations/confirmation.rb
94
+ - lib/matchers/validations/exclusion.rb
95
+ - lib/matchers/validations/format.rb
96
+ - lib/matchers/validations/inclusion.rb
97
+ - lib/matchers/validations/length.rb
98
+ - lib/matchers/validations/presence.rb
99
+ - lib/matchers/validations/uniqueness.rb
96
100
  - lib/matchers/validations/validations.rb
97
101
  - lib/minitest/matchers.rb
98
102
  - lib/mongoid-minitest.rb