mongoid-rspec 4.1.0 → 4.3.0
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.
- checksums.yaml +4 -4
- data/README.md +54 -7
- data/Rakefile +3 -4
- data/lib/matchers/accept_nested_attributes.rb +2 -0
- data/lib/matchers/allow_mass_assignment.rb +2 -0
- data/lib/matchers/associations.rb +25 -8
- data/lib/matchers/be_dynamic_document.rb +2 -0
- data/lib/matchers/be_mongoid_document.rb +2 -0
- data/lib/matchers/be_stored_in.rb +2 -0
- data/lib/matchers/have_field.rb +5 -3
- data/lib/matchers/have_timestamps.rb +5 -3
- data/lib/matchers/indexes/have_index_for.rb +2 -0
- data/lib/matchers/indexes/v3/have_index_for.rb +4 -2
- data/lib/matchers/indexes/v4/have_index_for.rb +3 -1
- data/lib/matchers/validations/absence_of.rb +2 -0
- data/lib/matchers/validations/acceptance_of.rb +2 -0
- data/lib/matchers/validations/associated.rb +2 -0
- data/lib/matchers/validations/confirmation_of.rb +2 -0
- data/lib/matchers/validations/custom_validation_of.rb +4 -2
- data/lib/matchers/validations/exclusion_of.rb +5 -3
- data/lib/matchers/validations/format_of.rb +2 -0
- data/lib/matchers/validations/inclusion_of.rb +2 -0
- data/lib/matchers/validations/length_of.rb +2 -0
- data/lib/matchers/validations/numericality_of.rb +2 -0
- data/lib/matchers/validations/presence_of.rb +2 -0
- data/lib/matchers/validations/uniqueness_of.rb +2 -0
- data/lib/matchers/validations.rb +43 -7
- data/lib/mongoid/rspec/version.rb +3 -1
- data/lib/mongoid/rspec.rb +2 -0
- data/lib/mongoid-rspec.rb +2 -0
- metadata +58 -95
- data/spec/models/article.rb +0 -32
- data/spec/models/comment.rb +0 -6
- data/spec/models/log.rb +0 -9
- data/spec/models/message.rb +0 -17
- data/spec/models/movie_article.rb +0 -7
- data/spec/models/permalink.rb +0 -5
- data/spec/models/person.rb +0 -10
- data/spec/models/profile.rb +0 -16
- data/spec/models/record.rb +0 -5
- data/spec/models/site.rb +0 -13
- data/spec/models/user.rb +0 -38
- data/spec/spec_helper.rb +0 -35
- data/spec/unit/accept_nested_attributes_spec.rb +0 -12
- data/spec/unit/associations_spec.rb +0 -54
- data/spec/unit/be_dynamic_document_spec.rb +0 -21
- data/spec/unit/be_mongoid_document_spec.rb +0 -25
- data/spec/unit/be_stored_in.rb +0 -54
- data/spec/unit/document_spec.rb +0 -17
- data/spec/unit/have_index_for_spec.rb +0 -46
- data/spec/unit/have_timestamps_spec.rb +0 -71
- data/spec/unit/validations_spec.rb +0 -61
- data/spec/validators/ssn_validator.rb +0 -16
|
@@ -1,46 +0,0 @@
|
|
|
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) if Mongoid::Compatibility::Version.mongoid4_or_newer?
|
|
45
|
-
end
|
|
46
|
-
end
|
|
@@ -1,71 +0,0 @@
|
|
|
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
|
|
@@ -1,61 +0,0 @@
|
|
|
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(%w[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(%i[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(%i[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(%i[approved rejected]).on(:update) }
|
|
42
|
-
it { is_expected.to validate_absence_of(:deletion_date) } if Mongoid::Compatibility::Version.mongoid4_or_newer?
|
|
43
|
-
end
|
|
44
|
-
|
|
45
|
-
describe MovieArticle do
|
|
46
|
-
it { is_expected.to validate_numericality_of(:rating).greater_than(0) }
|
|
47
|
-
it { is_expected.to validate_numericality_of(:rating).to_allow(greater_than: 0).less_than_or_equal_to(5) }
|
|
48
|
-
it { is_expected.to validate_numericality_of(:classification).to_allow(even: true, only_integer: true, nil: false) }
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
describe Person do
|
|
52
|
-
it { is_expected.to custom_validate(:ssn).with_validator(SsnValidator) }
|
|
53
|
-
it { is_expected.not_to custom_validate(:name) }
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
describe Message do
|
|
57
|
-
it { is_expected.to validate_uniqueness_of(:identifier).with_message('uniqueness') }
|
|
58
|
-
it { is_expected.to validate_presence_of(:from).with_message('required') }
|
|
59
|
-
it { is_expected.to validate_format_of(:to).with_message('format') }
|
|
60
|
-
end
|
|
61
|
-
end
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
class SsnValidator < ActiveModel::EachValidator
|
|
2
|
-
def validate_each(record, attribute, value)
|
|
3
|
-
unless valid_ssn?(record, attribute, value)
|
|
4
|
-
record.errors[attribute] << "#{value} is not a valid Social Security Number"
|
|
5
|
-
end
|
|
6
|
-
end
|
|
7
|
-
|
|
8
|
-
def self.kind
|
|
9
|
-
:custom
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
def valid_ssn?(_record, _attribute, _value)
|
|
13
|
-
# irrelevant here how validation is done
|
|
14
|
-
true
|
|
15
|
-
end
|
|
16
|
-
end
|