mongoid-rspec 2.1.0 → 4.1.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.
Files changed (66) hide show
  1. checksums.yaml +5 -5
  2. data/LICENSE +1 -1
  3. data/README.md +222 -102
  4. data/Rakefile +8 -5
  5. data/lib/matchers/accept_nested_attributes.rb +22 -23
  6. data/lib/matchers/allow_mass_assignment.rb +12 -12
  7. data/lib/matchers/associations.rb +85 -49
  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 +55 -0
  11. data/lib/matchers/have_field.rb +90 -0
  12. data/lib/matchers/have_timestamps.rb +61 -0
  13. data/lib/matchers/indexes/have_index_for.rb +16 -0
  14. data/lib/matchers/indexes/v3/have_index_for.rb +59 -0
  15. data/lib/matchers/indexes/v4/have_index_for.rb +54 -0
  16. data/lib/matchers/validations.rb +30 -11
  17. data/lib/matchers/validations/absence_of.rb +11 -0
  18. data/lib/matchers/validations/associated.rb +1 -1
  19. data/lib/matchers/validations/confirmation_of.rb +7 -1
  20. data/lib/matchers/validations/custom_validation_of.rb +1 -4
  21. data/lib/matchers/validations/exclusion_of.rb +5 -5
  22. data/lib/matchers/validations/format_of.rb +2 -2
  23. data/lib/matchers/validations/inclusion_of.rb +5 -5
  24. data/lib/matchers/validations/length_of.rb +13 -35
  25. data/lib/matchers/validations/numericality_of.rb +32 -16
  26. data/lib/matchers/validations/presence_of.rb +1 -1
  27. data/lib/matchers/validations/uniqueness_of.rb +7 -10
  28. data/lib/mongoid-rspec.rb +1 -33
  29. data/lib/mongoid/rspec.rb +46 -0
  30. data/lib/mongoid/rspec/version.rb +5 -0
  31. data/spec/models/article.rb +9 -6
  32. data/spec/models/comment.rb +1 -1
  33. data/spec/models/log.rb +3 -3
  34. data/spec/models/message.rb +17 -0
  35. data/spec/models/movie_article.rb +1 -2
  36. data/spec/models/person.rb +1 -1
  37. data/spec/models/profile.rb +2 -2
  38. data/spec/models/record.rb +1 -1
  39. data/spec/models/site.rb +5 -1
  40. data/spec/models/user.rb +12 -10
  41. data/spec/spec_helper.rb +12 -10
  42. data/spec/unit/accept_nested_attributes_spec.rb +1 -1
  43. data/spec/unit/associations_spec.rb +19 -7
  44. data/spec/unit/be_dynamic_document_spec.rb +21 -0
  45. data/spec/unit/be_mongoid_document_spec.rb +25 -0
  46. data/spec/unit/be_stored_in.rb +54 -0
  47. data/spec/unit/document_spec.rb +5 -14
  48. data/spec/unit/have_index_for_spec.rb +46 -0
  49. data/spec/unit/have_timestamps_spec.rb +71 -0
  50. data/spec/unit/validations_spec.rb +23 -14
  51. data/spec/validators/ssn_validator.rb +6 -6
  52. metadata +119 -43
  53. data/.document +0 -5
  54. data/.gitignore +0 -6
  55. data/.ruby-gemset +0 -1
  56. data/.ruby-version +0 -1
  57. data/.travis.yml +0 -10
  58. data/Gemfile +0 -4
  59. data/lib/matchers/collections.rb +0 -9
  60. data/lib/matchers/document.rb +0 -173
  61. data/lib/matchers/indexes.rb +0 -69
  62. data/lib/matchers/validations/with_message.rb +0 -27
  63. data/lib/mongoid-rspec/version.rb +0 -5
  64. data/mongoid-rspec.gemspec +0 -25
  65. data/spec/unit/collections_spec.rb +0 -7
  66. data/spec/unit/indexes_spec.rb +0 -17
data/.document DELETED
@@ -1,5 +0,0 @@
1
- README.rdoc
2
- lib/**/*.rb
3
- bin/*
4
- features/**/*.feature
5
- LICENSE
data/.gitignore DELETED
@@ -1,6 +0,0 @@
1
- *.gem
2
- .rvmrc
3
- .bundle
4
- Gemfile.lock
5
- pkg/*
6
-
@@ -1 +0,0 @@
1
- mongoid-rspec
@@ -1 +0,0 @@
1
- 2.2.0
@@ -1,10 +0,0 @@
1
- services: mongodb
2
- language: ruby
3
- rvm:
4
- - 1.9.3
5
- - 2.1.3
6
- - 2.2.0
7
- - jruby-19mode
8
-
9
- gemfile:
10
- - Gemfile
data/Gemfile DELETED
@@ -1,4 +0,0 @@
1
- source "http://rubygems.org"
2
-
3
- # Specify your gem's dependencies in mongoid-rspec.gemspec
4
- gemspec
@@ -1,9 +0,0 @@
1
- RSpec::Matchers.define :be_stored_in do |collection_name|
2
- match do |doc|
3
- doc.class.collection_name == collection_name
4
- end
5
-
6
- description do
7
- "be stored in #{collection_name.to_s}"
8
- end
9
- end
@@ -1,173 +0,0 @@
1
- module Mongoid
2
- module Matchers
3
- class HaveFieldMatcher # :nodoc:
4
- def initialize(*attrs)
5
- @attributes = attrs.collect(&:to_s)
6
- end
7
-
8
- def localized
9
- @localized = true
10
- self
11
- end
12
-
13
- def of_type(type)
14
- @type = type
15
- self
16
- end
17
-
18
- def with_alias(field_alias)
19
- @field_alias = field_alias
20
- self
21
- end
22
-
23
- def with_default_value_of(default)
24
- @default = default
25
- self
26
- end
27
-
28
- def matches?(klass)
29
- @klass = klass.is_a?(Class) ? klass : klass.class
30
- @errors = []
31
- @attributes.each do |attr|
32
- if @klass.fields.include?(attr)
33
- error = ""
34
- if @type and @klass.fields[attr].type != @type
35
- error << " of type #{@klass.fields[attr].type}"
36
- end
37
-
38
- if !@default.nil?
39
- if @klass.fields[attr].default_val.nil?
40
- error << " with default not set"
41
- elsif @klass.fields[attr].default_val != @default
42
- error << " with default value of #{@klass.fields[attr].default_val}"
43
- end
44
- end
45
-
46
- if @field_alias and @klass.fields[attr].options[:as] != @field_alias
47
- error << " with alias #{@klass.fields[attr].options[:as]}"
48
- end
49
-
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
-
58
- else
59
- @errors.push "no field named #{attr.inspect}"
60
- end
61
- end
62
- @errors.empty?
63
- end
64
-
65
- def failure_message_for_should
66
- "Expected #{@klass.inspect} to #{description}, got #{@errors.to_sentence}"
67
- end
68
-
69
- def failure_message_for_should_not
70
- "Expected #{@klass.inspect} to not #{description}, got #{@klass.inspect} to #{description}"
71
- end
72
-
73
- alias :failure_message :failure_message_for_should
74
- alias :failure_message_when_negated :failure_message_for_should_not
75
-
76
- def description
77
- desc = "have #{@attributes.size > 1 ? 'fields' : 'field'} named #{@attributes.collect(&:inspect).to_sentence}"
78
- desc << " of type #{@type.inspect}" if @type
79
- desc << " with alias #{@field_alias}" if @field_alias
80
- desc << " with default value of #{@default.inspect}" if !@default.nil?
81
- desc
82
- end
83
- end
84
-
85
- def have_field(*args)
86
- HaveFieldMatcher.new(*args)
87
- end
88
- alias_method :have_fields, :have_field
89
- end
90
- end
91
-
92
- RSpec::Matchers.define :have_instance_method do |name|
93
- match do |klass|
94
- klass.instance_methods.include?(name.to_sym)
95
- end
96
-
97
- description do
98
- "have instance method #{name.to_s}"
99
- end
100
- end
101
-
102
- RSpec::Matchers.define :be_mongoid_document do
103
- match do |doc|
104
- doc.class.included_modules.include?(Mongoid::Document)
105
- end
106
-
107
- description do
108
- "be a Mongoid document"
109
- end
110
- end
111
-
112
- RSpec::Matchers.define :be_versioned_document do
113
- match do |doc|
114
- doc.class.included_modules.include?(Mongoid::Versioning)
115
- end
116
-
117
- description do
118
- "be a versioned Mongoid document"
119
- end
120
- end
121
-
122
- RSpec::Matchers.define :be_timestamped_document do
123
- match do |doc|
124
- if [*@timestamped_module].any?
125
- modules = [*@timestamped_module].map{|m| "Mongoid::Timestamps::#{m.to_s.classify}".constantize }
126
- (modules - doc.class.included_modules).empty?
127
- else
128
- doc.class.included_modules.include?(Mongoid::Timestamps) or
129
- doc.class.included_modules.include?(Mongoid::Timestamps::Created) or
130
- doc.class.included_modules.include?(Mongoid::Timestamps::Updated)
131
- end
132
- end
133
-
134
- chain :with do |timestamped_module|
135
- @timestamped_module = timestamped_module
136
- end
137
-
138
- description do
139
- desc = "be a timestamped Mongoid document"
140
- desc << " with #{@timestamped_module}" if @timestamped_module
141
- desc
142
- end
143
- end
144
-
145
- RSpec::Matchers.define :be_paranoid_document do
146
- match do |doc|
147
- doc.class.included_modules.include?(Mongoid::Paranoia)
148
- end
149
-
150
- description do
151
- "be a paranoid Mongoid document"
152
- end
153
- end
154
-
155
- RSpec::Matchers.define :be_multiparameted_document do
156
- match do |doc|
157
- doc.class.included_modules.include?(Mongoid::MultiParameterAttributes)
158
- end
159
-
160
- description do
161
- "be a multiparameted Mongoid document"
162
- end
163
- end
164
-
165
- RSpec::Matchers.define :be_dynamic_document do |_|
166
- match do |doc|
167
- doc.class.included_modules.include?(Mongoid::Attributes::Dynamic)
168
- end
169
-
170
- description do
171
- 'be a Mongoid document with dynamic attributes'
172
- end
173
- end
@@ -1,69 +0,0 @@
1
- module Mongoid
2
- module Matchers
3
- class HaveIndexForMatcher # :nodoc:
4
- def initialize(index_fields)
5
- @index_fields = index_fields.symbolize_keys!
6
- end
7
-
8
- def with_options(options = { })
9
- @options = options
10
- self
11
- end
12
-
13
- def matches?(klass)
14
- @klass = klass.is_a?(Class) ? klass : klass.class
15
- @errors = []
16
-
17
- index_specifications = @klass.index_specifications.find { |is| is.key == @index_fields }
18
- if index_specifications
19
- if !@options.nil? && !@options.empty?
20
- index_options = normalize_options(index_specifications.options)
21
- @options.each do |option, option_value|
22
- if index_options[option] != option_value
23
- @errors.push "index for #{@index_fields.inspect} with options of #{index_options.inspect}"
24
- end
25
- end
26
- end
27
- else
28
- @errors.push "no index for #{@index_fields}"
29
- end
30
-
31
- @errors.empty?
32
- end
33
-
34
- def failure_message_for_should
35
- "Expected #{@klass.inspect} to #{description}, got #{@errors.to_sentence}"
36
- end
37
-
38
- def failure_message_for_should_not
39
- "Expected #{@klass.inspect} to not #{description}, got #{@klass.inspect} to #{description}"
40
- end
41
-
42
- alias :failure_message :failure_message_for_should
43
- alias :failure_message_when_negated :failure_message_for_should_not
44
-
45
- def description
46
- desc = "have an index for #{@index_fields.inspect}"
47
- desc << " with options of #{@options.inspect}" if @options
48
- desc
49
- end
50
-
51
- private
52
- MAPPINGS = {
53
- dropDups: :drop_dups,
54
- expireAfterSeconds: :expire_after_seconds,
55
- bucketSize: :bucket_size
56
- }
57
-
58
- def normalize_options(options)
59
- options.transform_keys do |key|
60
- MAPPINGS[key] || key
61
- end
62
- end
63
- end
64
-
65
- def have_index_for(index_fields)
66
- HaveIndexForMatcher.new(index_fields)
67
- end
68
- end
69
- end
@@ -1,27 +0,0 @@
1
- module Mongoid
2
- module Matchers
3
- module Validations
4
- module WithMessage
5
- def with_message(message)
6
- @expected_message = message
7
- self
8
- end
9
-
10
- private
11
-
12
- def check_expected_message
13
- actual_message = @validator.options[:message]
14
- if actual_message.nil?
15
- @negative_result_message << " with no custom message"
16
- @result = false
17
- elsif actual_message == @expected_message
18
- @positive_result_message << " with custom message '#{@expected_message}'"
19
- else
20
- @negative_result_message << " got message '#{actual_message}'"
21
- @result = false
22
- end
23
- end
24
- end
25
- end
26
- end
27
- end
@@ -1,5 +0,0 @@
1
- module Mongoid
2
- module Rspec
3
- VERSION = "2.1.0"
4
- end
5
- end
@@ -1,25 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
- $:.push File.expand_path("../lib", __FILE__)
3
- require "mongoid-rspec/version"
4
-
5
- Gem::Specification.new do |s|
6
- s.name = "mongoid-rspec"
7
- s.version = Mongoid::Rspec::VERSION
8
- s.platform = Gem::Platform::RUBY
9
- s.authors = ["Evan Sagge", "Rodrigo Pinto"]
10
- s.email = %q{evansagge@gmail.com contato@rodrigopinto.me}
11
- s.homepage = %q{http://github.com/mongoid-rspec/mongoid-rspec}
12
- s.summary = %q{RSpec matchers for Mongoid}
13
- s.description = %q{RSpec matches for Mongoid models, including association and validation matchers}
14
-
15
- s.rubyforge_project = "mongoid-rspec"
16
-
17
- s.files = `git ls-files`.split("\n")
18
- s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
- s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
- s.require_paths = ["lib"]
21
-
22
- s.add_dependency 'rake'
23
- s.add_dependency 'mongoid', '~> 4.0.0'
24
- s.add_dependency 'rspec', '~> 3.1'
25
- end
@@ -1,7 +0,0 @@
1
- require 'spec_helper'
2
-
3
- RSpec.describe "Collections" do
4
- describe Log do
5
- it { is_expected.to be_stored_in :logs }
6
- end
7
- end
@@ -1,17 +0,0 @@
1
- require 'spec_helper'
2
-
3
- RSpec.describe "Indexes" do
4
- describe Article do
5
- it { is_expected.to have_index_for(published: 1) }
6
- it { is_expected.to have_index_for(title: 1).with_options(unique: true, background: true, drop_dups: true) }
7
- it { is_expected.to have_index_for('permalink._id' => 1) }
8
- end
9
-
10
- describe Profile do
11
- it { is_expected.to have_index_for(first_name: 1, last_name: 1) }
12
- end
13
-
14
- describe Log do
15
- it { is_expected.to have_index_for(created_at: 1).with_options(bucket_size: 100, expire_after_seconds: 3600) }
16
- end
17
- end