mongoid-spec 4.0.1

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.
Files changed (51) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +20 -0
  3. data/README.md +373 -0
  4. data/Rakefile +17 -0
  5. data/lib/matchers/accept_nested_attributes.rb +67 -0
  6. data/lib/matchers/allow_mass_assignment.rb +102 -0
  7. data/lib/matchers/associations.rb +330 -0
  8. data/lib/matchers/be_dynamic_document.rb +26 -0
  9. data/lib/matchers/be_mongoid_document.rb +26 -0
  10. data/lib/matchers/be_stored_in.rb +50 -0
  11. data/lib/matchers/have_field.rb +90 -0
  12. data/lib/matchers/have_index_for.rb +63 -0
  13. data/lib/matchers/have_timestamps.rb +61 -0
  14. data/lib/matchers/validations.rb +81 -0
  15. data/lib/matchers/validations/acceptance_of.rb +9 -0
  16. data/lib/matchers/validations/associated.rb +19 -0
  17. data/lib/matchers/validations/confirmation_of.rb +22 -0
  18. data/lib/matchers/validations/custom_validation_of.rb +47 -0
  19. data/lib/matchers/validations/exclusion_of.rb +49 -0
  20. data/lib/matchers/validations/format_of.rb +71 -0
  21. data/lib/matchers/validations/inclusion_of.rb +49 -0
  22. data/lib/matchers/validations/length_of.rb +147 -0
  23. data/lib/matchers/validations/numericality_of.rb +90 -0
  24. data/lib/matchers/validations/presence_of.rb +9 -0
  25. data/lib/matchers/validations/uniqueness_of.rb +82 -0
  26. data/lib/matchers/validations/with_message.rb +27 -0
  27. data/lib/mongoid-rspec.rb +1 -0
  28. data/lib/mongoid/rspec.rb +40 -0
  29. data/lib/mongoid/rspec/version.rb +5 -0
  30. data/spec/models/article.rb +29 -0
  31. data/spec/models/comment.rb +6 -0
  32. data/spec/models/log.rb +9 -0
  33. data/spec/models/movie_article.rb +8 -0
  34. data/spec/models/permalink.rb +5 -0
  35. data/spec/models/person.rb +10 -0
  36. data/spec/models/profile.rb +16 -0
  37. data/spec/models/record.rb +5 -0
  38. data/spec/models/site.rb +9 -0
  39. data/spec/models/user.rb +37 -0
  40. data/spec/spec_helper.rb +35 -0
  41. data/spec/unit/accept_nested_attributes_spec.rb +12 -0
  42. data/spec/unit/associations_spec.rb +42 -0
  43. data/spec/unit/be_dynamic_document_spec.rb +22 -0
  44. data/spec/unit/be_mongoid_document_spec.rb +25 -0
  45. data/spec/unit/be_stored_in.rb +54 -0
  46. data/spec/unit/document_spec.rb +16 -0
  47. data/spec/unit/have_index_for_spec.rb +46 -0
  48. data/spec/unit/have_timestamps_spec.rb +71 -0
  49. data/spec/unit/validations_spec.rb +54 -0
  50. data/spec/validators/ssn_validator.rb +16 -0
  51. metadata +171 -0
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Mongoid::Matchers::HaveIndexFor do
4
+ subject do
5
+ Class.new do
6
+ include Mongoid::Document
7
+
8
+ field :fizz, as: :buzz, type: String
9
+
10
+ index({ foo: 1 })
11
+ index({ bar: 1 }, { unique: true, background: true, drop_dups: true })
12
+ index({ foo: 1, bar: -1 })
13
+ index({ 'baz._id' => 1 })
14
+ index({ buzz: 1 })
15
+ end
16
+ end
17
+
18
+ it 'detects an index for singular field key' do
19
+ is_expected.to have_index_for(foo: 1)
20
+ end
21
+
22
+ it 'detects an index for multipple fields key' do
23
+ is_expected.to have_index_for(foo: 1, bar: -1)
24
+ end
25
+
26
+ it 'detects an index with options' do
27
+ is_expected
28
+ .to have_index_for(bar: 1)
29
+ .with_options(unique: true, background: true, drop_dups: true)
30
+ end
31
+
32
+ it 'detects an index with only part of options' do
33
+ is_expected
34
+ .to have_index_for(bar: 1)
35
+ .with_options(unique: true)
36
+ end
37
+
38
+ it 'detects an index for string key' do
39
+ is_expected.to have_index_for('baz._id' => 1)
40
+ end
41
+
42
+ it 'detect an index for aliased fields' do
43
+ is_expected.to have_index_for(fizz: 1)
44
+ is_expected.to have_index_for(buzz: 1)
45
+ end
46
+ end
@@ -0,0 +1,71 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Mongoid::Matchers::HaveTimestamps do
4
+ context 'when model includes Mongoid::Timestamps' do
5
+ subject do
6
+ Class.new do
7
+ include Mongoid::Document
8
+ include Mongoid::Timestamps
9
+ end
10
+ end
11
+
12
+ it { is_expected.to have_timestamps }
13
+ end
14
+
15
+ context 'when model includes Mongoid::Timestamps::Short' do
16
+ subject do
17
+ Class.new do
18
+ include Mongoid::Document
19
+ include Mongoid::Timestamps::Short
20
+ end
21
+ end
22
+
23
+ it { is_expected.to have_timestamps.shortened }
24
+ end
25
+
26
+ context 'when model includes Mongoid::Timestamps::Updated' do
27
+ subject do
28
+ Class.new do
29
+ include Mongoid::Document
30
+ include Mongoid::Timestamps::Updated
31
+ end
32
+ end
33
+
34
+ it { is_expected.to have_timestamps.for(:updating) }
35
+ end
36
+
37
+ context 'when model includes Mongoid::Timestamps::Updated::Short' do
38
+ subject do
39
+ Class.new do
40
+ include Mongoid::Document
41
+ include Mongoid::Timestamps::Updated::Short
42
+ end
43
+ end
44
+
45
+ it { is_expected.to have_timestamps.for(:updating).shortened }
46
+ it { is_expected.to have_timestamps.shortened.for(:updating) }
47
+ end
48
+
49
+ context 'when model includes Mongoid::Timestamps::Created' do
50
+ subject do
51
+ Class.new do
52
+ include Mongoid::Document
53
+ include Mongoid::Timestamps::Created
54
+ end
55
+ end
56
+
57
+ it { is_expected.to have_timestamps.for(:creating) }
58
+ end
59
+
60
+ context 'when model includes Mongoid::Timestamps::Created::Short' do
61
+ subject do
62
+ Class.new do
63
+ include Mongoid::Document
64
+ include Mongoid::Timestamps::Created::Short
65
+ end
66
+ end
67
+
68
+ it { is_expected.to have_timestamps.for(:creating).shortened }
69
+ it { is_expected.to have_timestamps.shortened.for(:creating) }
70
+ end
71
+ end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe "Validations" do
4
+ describe Site do
5
+ it { is_expected.to validate_presence_of(:name) }
6
+ it { is_expected.to validate_uniqueness_of(:name) }
7
+ end
8
+
9
+ describe User do
10
+ it { is_expected.to validate_presence_of(:login) }
11
+ it { is_expected.to validate_uniqueness_of(:login).scoped_to(:site) }
12
+ it { is_expected.to validate_uniqueness_of(:email).case_insensitive.with_message("is already taken") }
13
+ it { is_expected.to validate_format_of(:login).to_allow("valid_login").not_to_allow("invalid login") }
14
+ it { is_expected.to validate_associated(:profile) }
15
+ it { is_expected.to validate_exclusion_of(:login).to_not_allow("super", "index", "edit") }
16
+ it { is_expected.to validate_exclusion_of(:password).to_not_allow("password") }
17
+ it { is_expected.to validate_inclusion_of(:role).to_allow("admin", "member") }
18
+ it { is_expected.to validate_inclusion_of(:role).to_allow(["admin", "member"]) }
19
+ it { is_expected.to validate_confirmation_of(:email) }
20
+ it { is_expected.to validate_presence_of(:age).on(:create, :update) }
21
+ it { is_expected.to validate_numericality_of(:age).on(:create, :update) }
22
+ it { is_expected.to validate_inclusion_of(:age).to_allow(23..42).on([:create, :update]) }
23
+ it { is_expected.to validate_presence_of(:password).on(:create) }
24
+ it { is_expected.to validate_confirmation_of(:password).with_message("Password confirmation must match given password") }
25
+ it { is_expected.to validate_presence_of(:provider_uid).on(:create) }
26
+ it { is_expected.to validate_inclusion_of(:locale).to_allow([:en, :ru]) }
27
+ end
28
+
29
+ describe Profile do
30
+ it { is_expected.to validate_numericality_of(:age).greater_than(0) }
31
+ it { is_expected.not_to validate_numericality_of(:age).greater_than(0).only_integer(true) }
32
+ it { is_expected.to validate_acceptance_of(:terms_of_service) }
33
+ it { is_expected.to validate_length_of(:hobbies).with_minimum(1).with_message("requires at least one hobby") }
34
+ end
35
+
36
+ describe Article do
37
+ it { is_expected.to validate_length_of(:title).within(8..16) }
38
+ it { is_expected.not_to validate_length_of(:content).greater_than(200).less_than(16) }
39
+ it { is_expected.to validate_length_of(:content).greater_than(200) }
40
+ it { is_expected.to validate_inclusion_of(:status).to_allow([:pending]).on( :create ) }
41
+ it { is_expected.to validate_inclusion_of(:status).to_allow([:approved, :rejected]).on( :update ) }
42
+ end
43
+
44
+ describe MovieArticle do
45
+ it { is_expected.to validate_numericality_of(:rating).greater_than(0) }
46
+ it { is_expected.to validate_numericality_of(:rating).to_allow(:greater_than => 0).less_than_or_equal_to(5) }
47
+ it { is_expected.to validate_numericality_of(:classification).to_allow(:even => true, :only_integer => true, :nil => false) }
48
+ end
49
+
50
+ describe Person do
51
+ it { is_expected.to custom_validate(:ssn).with_validator(SsnValidator) }
52
+ it { is_expected.not_to custom_validate(:name) }
53
+ end
54
+ end
@@ -0,0 +1,16 @@
1
+ class SsnValidator < ActiveModel::EachValidator
2
+
3
+ def validate_each(record, attribute, value)
4
+ unless valid_ssn?(record, attribute, value)
5
+ record.errors[attribute] << "#{value} is not a valid Social Security Number"
6
+ end
7
+ end
8
+
9
+ def self.kind() :custom end
10
+
11
+ def valid_ssn?(record, attribute, value)
12
+ # irrelevant here how validation is done
13
+ true
14
+ end
15
+
16
+ end
metadata ADDED
@@ -0,0 +1,171 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid-spec
3
+ version: !ruby/object:Gem::Version
4
+ version: 4.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Evan Sagge
8
+ - Rodrigo Pinto
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2017-07-18 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mongoid
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '6.0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '6.0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rspec
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '3.3'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '3.3'
42
+ - !ruby/object:Gem::Dependency
43
+ name: activesupport
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: 4.0.0
49
+ type: :runtime
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: 4.0.0
56
+ - !ruby/object:Gem::Dependency
57
+ name: rake
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '10.0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '10.0'
70
+ description: RSpec matches for Mongoid models, including association and validation
71
+ matchers.
72
+ email: evansagge@gmail.com contato@rodrigopinto.me
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - LICENSE
78
+ - README.md
79
+ - Rakefile
80
+ - lib/matchers/accept_nested_attributes.rb
81
+ - lib/matchers/allow_mass_assignment.rb
82
+ - lib/matchers/associations.rb
83
+ - lib/matchers/be_dynamic_document.rb
84
+ - lib/matchers/be_mongoid_document.rb
85
+ - lib/matchers/be_stored_in.rb
86
+ - lib/matchers/have_field.rb
87
+ - lib/matchers/have_index_for.rb
88
+ - lib/matchers/have_timestamps.rb
89
+ - lib/matchers/validations.rb
90
+ - lib/matchers/validations/acceptance_of.rb
91
+ - lib/matchers/validations/associated.rb
92
+ - lib/matchers/validations/confirmation_of.rb
93
+ - lib/matchers/validations/custom_validation_of.rb
94
+ - lib/matchers/validations/exclusion_of.rb
95
+ - lib/matchers/validations/format_of.rb
96
+ - lib/matchers/validations/inclusion_of.rb
97
+ - lib/matchers/validations/length_of.rb
98
+ - lib/matchers/validations/numericality_of.rb
99
+ - lib/matchers/validations/presence_of.rb
100
+ - lib/matchers/validations/uniqueness_of.rb
101
+ - lib/matchers/validations/with_message.rb
102
+ - lib/mongoid-rspec.rb
103
+ - lib/mongoid/rspec.rb
104
+ - lib/mongoid/rspec/version.rb
105
+ - spec/models/article.rb
106
+ - spec/models/comment.rb
107
+ - spec/models/log.rb
108
+ - spec/models/movie_article.rb
109
+ - spec/models/permalink.rb
110
+ - spec/models/person.rb
111
+ - spec/models/profile.rb
112
+ - spec/models/record.rb
113
+ - spec/models/site.rb
114
+ - spec/models/user.rb
115
+ - spec/spec_helper.rb
116
+ - spec/unit/accept_nested_attributes_spec.rb
117
+ - spec/unit/associations_spec.rb
118
+ - spec/unit/be_dynamic_document_spec.rb
119
+ - spec/unit/be_mongoid_document_spec.rb
120
+ - spec/unit/be_stored_in.rb
121
+ - spec/unit/document_spec.rb
122
+ - spec/unit/have_index_for_spec.rb
123
+ - spec/unit/have_timestamps_spec.rb
124
+ - spec/unit/validations_spec.rb
125
+ - spec/validators/ssn_validator.rb
126
+ homepage: http://github.com/rrmartins/mongoid-rspec
127
+ licenses:
128
+ - MIT
129
+ metadata: {}
130
+ post_install_message:
131
+ rdoc_options: []
132
+ require_paths:
133
+ - lib
134
+ required_ruby_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '2.2'
139
+ required_rubygems_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: 1.3.6
144
+ requirements: []
145
+ rubyforge_project: mongoid-spec
146
+ rubygems_version: 2.6.10
147
+ signing_key:
148
+ specification_version: 4
149
+ summary: RSpec matchers for Mongoid
150
+ test_files:
151
+ - spec/models/article.rb
152
+ - spec/models/comment.rb
153
+ - spec/models/log.rb
154
+ - spec/models/movie_article.rb
155
+ - spec/models/permalink.rb
156
+ - spec/models/person.rb
157
+ - spec/models/profile.rb
158
+ - spec/models/record.rb
159
+ - spec/models/site.rb
160
+ - spec/models/user.rb
161
+ - spec/spec_helper.rb
162
+ - spec/unit/accept_nested_attributes_spec.rb
163
+ - spec/unit/associations_spec.rb
164
+ - spec/unit/be_dynamic_document_spec.rb
165
+ - spec/unit/be_mongoid_document_spec.rb
166
+ - spec/unit/be_stored_in.rb
167
+ - spec/unit/document_spec.rb
168
+ - spec/unit/have_index_for_spec.rb
169
+ - spec/unit/have_timestamps_spec.rb
170
+ - spec/unit/validations_spec.rb
171
+ - spec/validators/ssn_validator.rb