mongoid-rspec 1.2.1 → 1.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,9 +1 @@
1
- # A sample Gemfile
2
- source :gemcutter
3
-
4
- gem "mongoid", '>= 2.0.0.beta.15'
5
- gem 'bson_ext', '>= 1.0.4'
6
-
7
- gem 'redgreen'
8
-
9
- gem 'rspec', ">= 2.0.0.beta.19"
1
+ gemspec
data/Gemfile.lock CHANGED
@@ -1,41 +1,43 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ mongoid-rspec (1.3.0)
5
+ mongoid (~> 2.0.0)
6
+ rspec (~> 2)
7
+
1
8
  GEM
2
- remote: http://rubygems.org/
3
9
  specs:
4
- activemodel (3.0.0.rc)
5
- activesupport (= 3.0.0.rc)
10
+ activemodel (3.0.1)
11
+ activesupport (= 3.0.1)
6
12
  builder (~> 2.1.2)
7
13
  i18n (~> 0.4.1)
8
- activesupport (3.0.0.rc)
9
- bson (1.0.4)
10
- bson_ext (1.0.4)
14
+ activesupport (3.0.1)
15
+ bson (1.1.2)
11
16
  builder (2.1.2)
12
17
  diff-lcs (1.1.2)
13
- i18n (0.4.1)
14
- mongo (1.0.7)
15
- bson (>= 1.0.4)
16
- mongoid (2.0.0.beta.16)
17
- activemodel (= 3.0.0.rc)
18
- bson (= 1.0.4)
19
- mongo (= 1.0.7)
20
- tzinfo (= 0.3.22)
18
+ i18n (0.4.2)
19
+ mongo (1.1.2)
20
+ bson (>= 1.1.1)
21
+ mongoid (2.0.0.beta.20)
22
+ activemodel (~> 3.0)
23
+ mongo (~> 1.1)
24
+ tzinfo (~> 0.3.22)
21
25
  will_paginate (~> 3.0.pre)
22
- redgreen (1.2.2)
23
- rspec (2.0.0.beta.19)
24
- rspec-core (= 2.0.0.beta.19)
25
- rspec-expectations (= 2.0.0.beta.19)
26
- rspec-mocks (= 2.0.0.beta.19)
27
- rspec-core (2.0.0.beta.19)
28
- rspec-expectations (2.0.0.beta.19)
29
- diff-lcs (>= 1.1.2)
30
- rspec-mocks (2.0.0.beta.19)
31
- tzinfo (0.3.22)
26
+ rspec (2.1.0)
27
+ rspec-core (~> 2.1.0)
28
+ rspec-expectations (~> 2.1.0)
29
+ rspec-mocks (~> 2.1.0)
30
+ rspec-core (2.1.0)
31
+ rspec-expectations (2.1.0)
32
+ diff-lcs (~> 1.1.2)
33
+ rspec-mocks (2.1.0)
34
+ tzinfo (0.3.23)
32
35
  will_paginate (3.0.pre2)
33
36
 
34
37
  PLATFORMS
35
38
  ruby
36
39
 
37
40
  DEPENDENCIES
38
- bson_ext (>= 1.0.4)
39
- mongoid (>= 2.0.0.beta.15)
40
- redgreen
41
- rspec (>= 2.0.0.beta.19)
41
+ mongoid (~> 2.0.0)
42
+ mongoid-rspec!
43
+ rspec (~> 2)
data/README.markdown CHANGED
@@ -37,6 +37,10 @@ Validation Matchers
37
37
  it { should validate_inclusion_of(:role).to_allow("admin", "member") }
38
38
  it { should validate_numericality_of(:age) }
39
39
  end
40
+
41
+ describe Article do
42
+ it { should validate_length_of(:title) }
43
+ end
40
44
 
41
45
  Others
42
46
  -
@@ -47,4 +51,16 @@ Others
47
51
 
48
52
  # useful if you use factory_girl and have Factory(:user) defined for User
49
53
  it { should save }
50
- end
54
+ end
55
+
56
+ Use
57
+ -
58
+ add in Gemfile
59
+
60
+ gem 'mongoid-rspec'
61
+
62
+ drop in existing or dedicated support file in spec/support (spec/support/mongoid.rb)
63
+
64
+ RSpec.configure do |configuration|
65
+ configuration.include Mongoid::Matchers
66
+ end
data/Rakefile CHANGED
@@ -10,10 +10,8 @@ begin
10
10
  gem.email = "evansagge@gmail.com"
11
11
  gem.homepage = "http://github.com/evansagge/mongoid-rspec"
12
12
  gem.authors = ["Evan Sagge"]
13
- gem.add_dependency "mongoid", ">= 2.0.0.beta.15"
14
- gem.add_dependency "bson_ext", ">= 1.0.4"
15
- gem.add_dependency "rspec", ">= 2.0.0.beta.19"
16
- # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
13
+ gem.add_dependency "mongoid", "~> 2.0.0"
14
+ gem.add_dependency "rspec", "~> 2"
17
15
  end
18
16
  Jeweler::GemcutterTasks.new
19
17
  rescue LoadError
@@ -25,11 +23,11 @@ require 'rspec/core/rake_task'
25
23
 
26
24
  task :default => :spec
27
25
 
28
- Rspec::Core::RakeTask.new(:spec) do |spec|
26
+ RSpec::Core::RakeTask.new(:spec) do |spec|
29
27
  spec.pattern = "./spec/**/*_spec.rb"
30
28
  end
31
29
 
32
- Rspec::Core::RakeTask.new(:rcov) do |spec|
30
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
33
31
  spec.pattern = "./spec/**/*_spec.rb"
34
32
  spec.rcov = true
35
33
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.2.1
1
+ 1.3.2
@@ -100,3 +100,27 @@ RSpec::Matchers.define :have_instance_method do |name|
100
100
  "have instance method #{name.to_s}"
101
101
  end
102
102
  end
103
+
104
+ RSpec::Matchers.define :be_mongoid_document do
105
+ match do |doc|
106
+ doc.class.included_modules.should include(Mongoid::Document)
107
+ end
108
+ end
109
+
110
+ RSpec::Matchers.define :be_versioned_document do
111
+ match do |doc|
112
+ doc.class.included_modules.should include(Mongoid::Versioning)
113
+ end
114
+ end
115
+
116
+ RSpec::Matchers.define :be_timestamped_document do
117
+ match do |doc|
118
+ doc.class.included_modules.should include(Mongoid::Timestamps)
119
+ end
120
+ end
121
+
122
+ RSpec::Matchers.define :be_paranoid_document do
123
+ match do |doc|
124
+ doc.class.included_modules.should include(Mongoid::Paranoia)
125
+ end
126
+ end
@@ -0,0 +1,9 @@
1
+ module Mongoid
2
+ module Matchers
3
+ module Validations
4
+ def validate_length_of(field)
5
+ HaveValidationMatcher.new(field, :length)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -12,36 +12,83 @@ module Mongoid
12
12
  end
13
13
  alias_method :scoped_on, :scoped_to
14
14
 
15
+ def case_insensitive
16
+ @case_insensitive = true
17
+ self
18
+ end
19
+
15
20
  def allow_blank?(allow_blank)
16
21
  @allow_blank = allow_blank
22
+ self
17
23
  end
18
-
24
+
25
+ def with_message(message)
26
+ @expected_message = message
27
+ self
28
+ end
29
+
19
30
  def matches?(actual)
20
- return false unless result = super(actual)
21
-
22
- if [@validator.options[:scope]].flatten == @scope
23
- @positive_result_message = @positive_result_message << "scope to #{@validator.options[:scope]}"
24
- else
25
- @negative_result_message = @negative_result_message << "scope to #{@validator.options[:scope]}"
26
- result = false
27
- end if @scope
31
+ return false unless @result = super(actual)
28
32
 
29
- if @validator.options[:allow_blank] == @allow_blank
30
- @positive_result_message = @positive_result_message << " with blank values allowed"
31
- else
32
- @negative_result_message = @negative_result_message << " with no blank values allowed"
33
- result = false
34
- end if @allow_blank
33
+ check_scope if @scope
34
+ check_allow_blank if @allow_blank
35
+ check_case_sensitivity if @case_insensitive
36
+ check_expected_message if @expected_message
35
37
 
36
- result
37
- end
38
-
38
+ @result
39
+ end
40
+
39
41
  def description
40
42
  options_desc = []
41
43
  options_desc << " scoped to #{@scope.inspect}" if @scope
42
- options_desc << " allowing blank values" if @allow_blank
44
+ options_desc << " allowing blank values" if @allow_blank
45
+ options_desc << " allowing case insensitive values" if @case_insensitive
46
+ options_desc << " with message '#{@expected_message}'" if @case_insensitive
43
47
  super << options_desc.to_sentence
44
- end
48
+ end
49
+
50
+ private
51
+
52
+ def check_allow_blank
53
+ if @validator.options[:allow_blank] == @allow_blank
54
+ @positive_result_message << " with blank values allowed"
55
+ else
56
+ @negative_result_message << " with no blank values allowed"
57
+ @result = false
58
+ end
59
+ end
60
+
61
+ def check_scope
62
+ message = "scope to #{@validator.options[:scope]}"
63
+ if [@validator.options[:scope]].flatten == @scope
64
+ @positive_result_message << message
65
+ else
66
+ @negative_result_message << message
67
+ @result = false
68
+ end
69
+ end
70
+
71
+ def check_case_sensitivity
72
+ if @validator.options[:case_sensitive] == false
73
+ @positive_result_message << " with case insensitive values"
74
+ else
75
+ @negative_result_message << " without case insensitive values"
76
+ @result = false
77
+ end
78
+ end
79
+
80
+ def check_expected_message
81
+ actual_message = @validator.options[:message]
82
+ if actual_message.nil?
83
+ @negative_result_message << " with no custom message"
84
+ @result = false
85
+ elsif actual_message == @expected_message
86
+ @positive_result_message << " with custom message '#{@expected_message}'"
87
+ else
88
+ @negative_result_message << " got message '#{actual_message}'"
89
+ @result = false
90
+ end
91
+ end
45
92
  end
46
93
 
47
94
  def validate_uniqueness_of(field)
data/lib/mongoid-rspec.rb CHANGED
@@ -8,6 +8,7 @@ require 'matchers/validations'
8
8
  require 'matchers/validations/associated'
9
9
  require 'matchers/validations/format_of'
10
10
  require 'matchers/validations/inclusion_of'
11
+ require 'matchers/validations/length_of'
11
12
  require 'matchers/validations/numericality_of'
12
13
  require 'matchers/validations/presence_of'
13
14
  require 'matchers/validations/uniqueness_of'
@@ -5,62 +5,63 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongoid-rspec}
8
- s.version = "1.2.1"
8
+ s.version = "1.3.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Evan Sagge"]
12
- s.date = %q{2010-08-25}
12
+ s.date = %q{2010-11-11}
13
13
  s.description = %q{RSpec matches for Mongoid models, including association and validation matchers}
14
14
  s.email = %q{evansagge@gmail.com}
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE",
17
- "README.markdown"
17
+ "README.markdown"
18
18
  ]
19
19
  s.files = [
20
20
  ".bundle/config",
21
- ".document",
22
- ".gitignore",
23
- ".rvmrc",
24
- "Gemfile",
25
- "Gemfile.lock",
26
- "LICENSE",
27
- "README.markdown",
28
- "Rakefile",
29
- "VERSION",
30
- "lib/matchers/associations.rb",
31
- "lib/matchers/document.rb",
32
- "lib/matchers/validations.rb",
33
- "lib/matchers/validations/associated.rb",
34
- "lib/matchers/validations/format_of.rb",
35
- "lib/matchers/validations/inclusion_of.rb",
36
- "lib/matchers/validations/numericality_of.rb",
37
- "lib/matchers/validations/presence_of.rb",
38
- "lib/matchers/validations/uniqueness_of.rb",
39
- "lib/mongoid-rspec.rb",
40
- "mongoid-rspec.gemspec",
41
- "spec/models/article.rb",
42
- "spec/models/comment.rb",
43
- "spec/models/profile.rb",
44
- "spec/models/user.rb",
45
- "spec/spec_helper.rb",
46
- "spec/unit/associations_spec.rb",
47
- "spec/unit/document_spec.rb",
48
- "spec/unit/validations_spec.rb"
21
+ ".document",
22
+ ".rvmrc",
23
+ "Gemfile",
24
+ "Gemfile.lock",
25
+ "LICENSE",
26
+ "README.markdown",
27
+ "Rakefile",
28
+ "VERSION",
29
+ "lib/matchers/associations.rb",
30
+ "lib/matchers/document.rb",
31
+ "lib/matchers/validations.rb",
32
+ "lib/matchers/validations/associated.rb",
33
+ "lib/matchers/validations/format_of.rb",
34
+ "lib/matchers/validations/inclusion_of.rb",
35
+ "lib/matchers/validations/length_of.rb",
36
+ "lib/matchers/validations/numericality_of.rb",
37
+ "lib/matchers/validations/presence_of.rb",
38
+ "lib/matchers/validations/uniqueness_of.rb",
39
+ "lib/mongoid-rspec.rb",
40
+ "mongoid-rspec.gemspec",
41
+ "spec/models/article.rb",
42
+ "spec/models/comment.rb",
43
+ "spec/models/profile.rb",
44
+ "spec/models/site.rb",
45
+ "spec/models/user.rb",
46
+ "spec/spec_helper.rb",
47
+ "spec/unit/associations_spec.rb",
48
+ "spec/unit/document_spec.rb",
49
+ "spec/unit/validations_spec.rb"
49
50
  ]
50
51
  s.homepage = %q{http://github.com/evansagge/mongoid-rspec}
51
- s.rdoc_options = ["--charset=UTF-8"]
52
52
  s.require_paths = ["lib"]
53
53
  s.rubygems_version = %q{1.3.7}
54
54
  s.summary = %q{RSpec matchers for Mongoid}
55
55
  s.test_files = [
56
56
  "spec/models/article.rb",
57
- "spec/models/comment.rb",
58
- "spec/models/profile.rb",
59
- "spec/models/user.rb",
60
- "spec/spec_helper.rb",
61
- "spec/unit/associations_spec.rb",
62
- "spec/unit/document_spec.rb",
63
- "spec/unit/validations_spec.rb"
57
+ "spec/models/comment.rb",
58
+ "spec/models/profile.rb",
59
+ "spec/models/site.rb",
60
+ "spec/models/user.rb",
61
+ "spec/spec_helper.rb",
62
+ "spec/unit/associations_spec.rb",
63
+ "spec/unit/document_spec.rb",
64
+ "spec/unit/validations_spec.rb"
64
65
  ]
65
66
 
66
67
  if s.respond_to? :specification_version then
@@ -68,18 +69,15 @@ Gem::Specification.new do |s|
68
69
  s.specification_version = 3
69
70
 
70
71
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
71
- s.add_runtime_dependency(%q<mongoid>, [">= 2.0.0.beta.15"])
72
- s.add_runtime_dependency(%q<bson_ext>, [">= 1.0.4"])
73
- s.add_runtime_dependency(%q<rspec>, [">= 2.0.0.beta.19"])
72
+ s.add_runtime_dependency(%q<mongoid>, ["~> 2.0.0"])
73
+ s.add_runtime_dependency(%q<rspec>, ["~> 2"])
74
74
  else
75
- s.add_dependency(%q<mongoid>, [">= 2.0.0.beta.15"])
76
- s.add_dependency(%q<bson_ext>, [">= 1.0.4"])
77
- s.add_dependency(%q<rspec>, [">= 2.0.0.beta.19"])
75
+ s.add_dependency(%q<mongoid>, ["~> 2.0.0"])
76
+ s.add_dependency(%q<rspec>, ["~> 2"])
78
77
  end
79
78
  else
80
- s.add_dependency(%q<mongoid>, [">= 2.0.0.beta.15"])
81
- s.add_dependency(%q<bson_ext>, [">= 1.0.4"])
82
- s.add_dependency(%q<rspec>, [">= 2.0.0.beta.19"])
79
+ s.add_dependency(%q<mongoid>, ["~> 2.0.0"])
80
+ s.add_dependency(%q<rspec>, ["~> 2"])
83
81
  end
84
82
  end
85
83
 
@@ -9,5 +9,7 @@ class Article
9
9
  referenced_in :user, :inverse_of => :articles
10
10
 
11
11
  validates :title, :presence => true
12
+
13
+ validates_length_of :title, :minimum => 8, :maximum => 16
12
14
  end
13
15
 
@@ -0,0 +1,10 @@
1
+ class Site
2
+ include Mongoid::Document
3
+
4
+ field :name
5
+
6
+ references_many :users, :inverse_of => :site
7
+
8
+ validates :name, :presence => true, :uniqueness => true
9
+
10
+ end
data/spec/models/user.rb CHANGED
@@ -5,14 +5,15 @@ class User
5
5
  field :email
6
6
  field :role
7
7
 
8
+ referenced_in :site, :inverse_of => :users
8
9
  references_many :articles
9
10
  references_many :comments
10
11
  references_many :children, :stored_as => :array, :class_name => "User"
11
12
 
12
13
  embeds_one :profile
13
14
 
14
- validates :login, :presence => true, :uniqueness => true, :format => { :with => /^[\w\-]+$/ }
15
- validates :email, :presence => true, :uniqueness => true
15
+ validates :login, :presence => true, :uniqueness => { :scope => :site }, :format => { :with => /^[\w\-]+$/ }
16
+ validates :email, :uniqueness => { :case_sensitive => false, :scope => :site, :message => "is already taken" }
16
17
  validates :role, :presence => true, :inclusion => { :in => ["admin", "moderator", "member"]}
17
18
  validates :profile, :presence => true, :associated => true
18
19
 
@@ -1,9 +1,15 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe "Validations" do
4
+ describe Site do
5
+ it { should validate_presence_of(:name) }
6
+ it { should validate_uniqueness_of(:name) }
7
+ end
8
+
4
9
  describe User do
5
10
  it { should validate_presence_of(:login) }
6
- it { should validate_uniqueness_of(:login) }
11
+ it { should validate_uniqueness_of(:login).scoped_to(:site) }
12
+ it { should validate_uniqueness_of(:email).case_insensitive.with_message("is already taken") }
7
13
  it { should validate_format_of(:login).to_allow("valid_login").not_to_allow("invalid login") }
8
14
  it { should validate_associated(:profile) }
9
15
  it { should validate_inclusion_of(:role).to_allow("admin", "member") }
@@ -12,4 +18,8 @@ describe "Validations" do
12
18
  describe Profile do
13
19
  it { should validate_numericality_of(:age) }
14
20
  end
21
+
22
+ describe Article do
23
+ it { should validate_length_of(:title) }
24
+ end
15
25
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 1
7
+ - 3
7
8
  - 2
8
- - 1
9
- version: 1.2.1
9
+ version: 1.3.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Evan Sagge
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-08-25 00:00:00 +08:00
17
+ date: 2010-11-11 00:00:00 +08:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -23,49 +23,28 @@ dependencies:
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
24
  none: false
25
25
  requirements:
26
- - - ">="
26
+ - - ~>
27
27
  - !ruby/object:Gem::Version
28
28
  segments:
29
29
  - 2
30
30
  - 0
31
31
  - 0
32
- - beta
33
- - 15
34
- version: 2.0.0.beta.15
32
+ version: 2.0.0
35
33
  type: :runtime
36
34
  version_requirements: *id001
37
- - !ruby/object:Gem::Dependency
38
- name: bson_ext
39
- prerelease: false
40
- requirement: &id002 !ruby/object:Gem::Requirement
41
- none: false
42
- requirements:
43
- - - ">="
44
- - !ruby/object:Gem::Version
45
- segments:
46
- - 1
47
- - 0
48
- - 4
49
- version: 1.0.4
50
- type: :runtime
51
- version_requirements: *id002
52
35
  - !ruby/object:Gem::Dependency
53
36
  name: rspec
54
37
  prerelease: false
55
- requirement: &id003 !ruby/object:Gem::Requirement
38
+ requirement: &id002 !ruby/object:Gem::Requirement
56
39
  none: false
57
40
  requirements:
58
- - - ">="
41
+ - - ~>
59
42
  - !ruby/object:Gem::Version
60
43
  segments:
61
44
  - 2
62
- - 0
63
- - 0
64
- - beta
65
- - 19
66
- version: 2.0.0.beta.19
45
+ version: "2"
67
46
  type: :runtime
68
- version_requirements: *id003
47
+ version_requirements: *id002
69
48
  description: RSpec matches for Mongoid models, including association and validation matchers
70
49
  email: evansagge@gmail.com
71
50
  executables: []
@@ -78,7 +57,6 @@ extra_rdoc_files:
78
57
  files:
79
58
  - .bundle/config
80
59
  - .document
81
- - .gitignore
82
60
  - .rvmrc
83
61
  - Gemfile
84
62
  - Gemfile.lock
@@ -92,6 +70,7 @@ files:
92
70
  - lib/matchers/validations/associated.rb
93
71
  - lib/matchers/validations/format_of.rb
94
72
  - lib/matchers/validations/inclusion_of.rb
73
+ - lib/matchers/validations/length_of.rb
95
74
  - lib/matchers/validations/numericality_of.rb
96
75
  - lib/matchers/validations/presence_of.rb
97
76
  - lib/matchers/validations/uniqueness_of.rb
@@ -100,6 +79,7 @@ files:
100
79
  - spec/models/article.rb
101
80
  - spec/models/comment.rb
102
81
  - spec/models/profile.rb
82
+ - spec/models/site.rb
103
83
  - spec/models/user.rb
104
84
  - spec/spec_helper.rb
105
85
  - spec/unit/associations_spec.rb
@@ -110,8 +90,8 @@ homepage: http://github.com/evansagge/mongoid-rspec
110
90
  licenses: []
111
91
 
112
92
  post_install_message:
113
- rdoc_options:
114
- - --charset=UTF-8
93
+ rdoc_options: []
94
+
115
95
  require_paths:
116
96
  - lib
117
97
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -141,6 +121,7 @@ test_files:
141
121
  - spec/models/article.rb
142
122
  - spec/models/comment.rb
143
123
  - spec/models/profile.rb
124
+ - spec/models/site.rb
144
125
  - spec/models/user.rb
145
126
  - spec/spec_helper.rb
146
127
  - spec/unit/associations_spec.rb
data/.gitignore DELETED
@@ -1,21 +0,0 @@
1
- ## MAC OS
2
- .DS_Store
3
-
4
- ## TEXTMATE
5
- *.tmproj
6
- tmtags
7
-
8
- ## EMACS
9
- *~
10
- \#*
11
- .\#*
12
-
13
- ## VIM
14
- *.swp
15
-
16
- ## PROJECT::GENERAL
17
- coverage
18
- rdoc
19
- pkg
20
-
21
- ## PROJECT::SPECIFIC