riot-mongoid 1.1.2 → 1.1.3

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.
@@ -0,0 +1,41 @@
1
+ # riot-mongoid
2
+
3
+ Riot assertions for Mongoid
4
+
5
+ ## Examples
6
+
7
+ context "Photo Model" do
8
+
9
+ context 'definition' do
10
+ setup { Photo.new }
11
+
12
+ # field associations
13
+ asserts_topic.has_field :title, :type => String
14
+ asserts_topic.has_field :caption, :type => String, :default => ""
15
+
16
+ # association assertions
17
+ asserts_topic.has_association :belongs_to_related, :account
18
+ asserts_topic.has_association :has_many_related, :comments
19
+
20
+ # validation assertions
21
+ asserts_topic.has_validation :validates_presence_of, :caption
22
+
23
+ # key assertions
24
+ asserts_topic.has_key :title, :caption
25
+ end
26
+ end
27
+
28
+
29
+ ## Note on Patches/Pull Requests
30
+
31
+ * Fork the project.
32
+ * Make your feature addition or bug fix.
33
+ * Add tests for it. This is important so I don't break it in a
34
+ future version unintentionally.
35
+ * Commit, do not mess with rakefile, version, or history.
36
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
37
+ * Send me a pull request. Bonus points for topic branches.
38
+
39
+ ## Copyright
40
+
41
+ Copyright (c) 2010 gabrielg. See LICENSE for details.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.2
1
+ 1.1.3
@@ -1,45 +1,4 @@
1
1
  require 'riot'
2
2
  require 'mongoid'
3
3
 
4
- Mongoid::Field.module_eval do
5
- attr_reader :accessible
6
- end
7
-
8
- module RiotMongoid
9
- class HasFieldAssertion < Riot::AssertionMacro
10
- register :has_field
11
-
12
- def evaluate(model, *macro_info)
13
- field_name, options = macro_info
14
- field = model.fields[field_name.to_s]
15
- if options.nil? || options.empty?
16
- fail("field options must be specified with this assertion macro")
17
- elsif field.nil?
18
- fail("expected #{model} to have field #{field_name}")
19
- else
20
- options_valid = options.all? { |key,value| field.send(key) == value }
21
- options_valid ? pass("#{model} has field '#{field_name}' with options #{options.inspect}") :
22
- fail("expected model to have options #{options.inspect} on field #{field_name}")
23
- end
24
- end
25
- end # HasFieldAssertion
26
-
27
- class HasAssociationAssertion < Riot::AssertionMacro
28
- register :has_association
29
-
30
- def evaluate(model, *association_macro_info)
31
- assoc_type, assoc_name, options = association_macro_info
32
- assoc = model.associations[assoc_name.to_s]
33
- options ||= {}
34
- if assoc_name.nil?
35
- fail("association name and potential options must be specified with this assertion macro")
36
- elsif assoc.nil? || assoc.macro != assoc_type.to_sym
37
- fail("expected #{model} to have association #{assoc_name} of type #{assoc_type}")
38
- else
39
- options_valid = options.all? { |key,value| assoc.send(key) == value }
40
- options_valid ? pass("#{model} has '#{assoc_type}' association '#{assoc_name}' with options #{options.inspect}") :
41
- fail("expected model to have options #{options.inspect} on association #{assoc_name}")
42
- end
43
- end
44
- end # HasAssociationAssertion
45
- end # RiotMongoid
4
+ Dir[File.dirname(__FILE__) + '/riot-mongoid/*.rb'].each {|file| require file }
@@ -0,0 +1,20 @@
1
+ module RiotMongoid
2
+ class HasAssociationAssertion < Riot::AssertionMacro
3
+ register :has_association
4
+
5
+ def evaluate(model, *association_macro_info)
6
+ assoc_type, assoc_name, options = association_macro_info
7
+ assoc = model.associations[assoc_name.to_s]
8
+ options ||= {}
9
+ if assoc_name.nil?
10
+ fail("association name and potential options must be specified with this assertion macro")
11
+ elsif assoc.nil? || assoc.macro != assoc_type.to_sym
12
+ fail("expected #{model} to have association #{assoc_name} of type #{assoc_type}")
13
+ else
14
+ options_valid = options.all? { |key,value| assoc.send(key) == value }
15
+ options_valid ? pass("#{model} has '#{assoc_type}' association '#{assoc_name}' with options #{options.inspect}") :
16
+ fail("expected model to have options #{options.inspect} on association #{assoc_name}")
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,23 @@
1
+ Mongoid::Field.module_eval do
2
+ attr_reader :accessible
3
+ end
4
+
5
+ module RiotMongoid
6
+ class HasFieldAssertion < Riot::AssertionMacro
7
+ register :has_field
8
+
9
+ def evaluate(model, *macro_info)
10
+ field_name, options = macro_info
11
+ field = model.fields[field_name.to_s]
12
+ if options.nil? || options.empty?
13
+ fail("field options must be specified with this assertion macro")
14
+ elsif field.nil?
15
+ fail("expected #{model} to have field #{field_name}")
16
+ else
17
+ options_valid = options.all? { |key,value| field.send(key) == value }
18
+ options_valid ? pass("#{model} has field '#{field_name}' with options #{options.inspect}") :
19
+ fail("expected model to have options #{options.inspect} on field #{field_name}")
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,15 @@
1
+ module RiotMongoid
2
+ class HasKeyAssertion < Riot::AssertionMacro
3
+ register :has_key
4
+
5
+ def evaluate(model, *key_macro_info)
6
+ if key_macro_info.nil?
7
+ fail("keys must be specified with this assertion macro")
8
+ else
9
+ valid = key_macro_info == model.primary_key
10
+ key = key_macro_info.join('-')
11
+ valid ? pass("#{model} has key #{key}") : fail("expected #{model} to have key #{key}")
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,23 @@
1
+ module RiotMongoid
2
+ class HasValidationAssertion < Riot::AssertionMacro
3
+ register :has_validation
4
+
5
+ def evaluate(model, *validation_macro_info)
6
+ validation_type, validation_name, options = validation_macro_info
7
+ validation = model.send(:all_validations).detect do |valid|
8
+ valid.key =~ %r{#{validation_type.to_s.camelize}} and valid.attribute == validation_name
9
+ end
10
+ options ||= {}
11
+ case
12
+ when validation_name.nil? || validation_type.nil?
13
+ fail("validation name, type and potential options must be specified with this assertion macro")
14
+ when validation.nil?
15
+ fail("expected #{model} to have validation on #{validation_name} of type #{validation_type}")
16
+ else
17
+ options_valid = options.all? { |key,value| validation.send(key) == value }
18
+ options_valid ? pass("#{model} has '#{validation_type}' validation '#{validation_name}' with options #{options.inspect}") :
19
+ fail("expected #{model} to have options #{options.inspect} on validation #{validation_type}")
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,73 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{riot-mongoid}
8
+ s.version = "1.1.3"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["gabrielg"]
12
+ s.date = %q{2010-06-10}
13
+ s.description = %q{A collection of assertion macros for testing Mongoid with Riot}
14
+ s.email = %q{gabriel.gironda@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.md",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/riot-mongoid.rb",
27
+ "lib/riot-mongoid/has_association.rb",
28
+ "lib/riot-mongoid/has_field.rb",
29
+ "lib/riot-mongoid/has_key.rb",
30
+ "lib/riot-mongoid/has_validation.rb",
31
+ "riot-mongoid.gemspec",
32
+ "test/has_association_test.rb",
33
+ "test/has_field_test.rb",
34
+ "test/has_key_test.rb",
35
+ "test/has_validation_test.rb",
36
+ "test/teststrap.rb"
37
+ ]
38
+ s.homepage = %q{http://github.com/gabrielg/riot-mongoid}
39
+ s.rdoc_options = ["--charset=UTF-8"]
40
+ s.require_paths = ["lib"]
41
+ s.rubygems_version = %q{1.3.6}
42
+ s.summary = %q{Riot assertions for Mongoid}
43
+ s.test_files = [
44
+ "test/has_association_test.rb",
45
+ "test/has_field_test.rb",
46
+ "test/has_key_test.rb",
47
+ "test/has_validation_test.rb",
48
+ "test/teststrap.rb"
49
+ ]
50
+
51
+ if s.respond_to? :specification_version then
52
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
53
+ s.specification_version = 3
54
+
55
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
56
+ s.add_development_dependency(%q<riot>, [">= 0"])
57
+ s.add_development_dependency(%q<yard>, [">= 0"])
58
+ s.add_runtime_dependency(%q<mongoid>, [">= 1.2.7"])
59
+ s.add_runtime_dependency(%q<riot>, [">= 0.10.12"])
60
+ else
61
+ s.add_dependency(%q<riot>, [">= 0"])
62
+ s.add_dependency(%q<yard>, [">= 0"])
63
+ s.add_dependency(%q<mongoid>, [">= 1.2.7"])
64
+ s.add_dependency(%q<riot>, [">= 0.10.12"])
65
+ end
66
+ else
67
+ s.add_dependency(%q<riot>, [">= 0"])
68
+ s.add_dependency(%q<yard>, [">= 0"])
69
+ s.add_dependency(%q<mongoid>, [">= 1.2.7"])
70
+ s.add_dependency(%q<riot>, [">= 0.10.12"])
71
+ end
72
+ end
73
+
@@ -0,0 +1,32 @@
1
+ require File.join(File.dirname(__FILE__),'teststrap')
2
+
3
+ context "has_association macro" do
4
+ setup do
5
+ test = mock_model do
6
+ embeds_many :things
7
+ embedded_in :another_thing, :inverse_of => :word
8
+ has_many_related :relations
9
+ end
10
+ end
11
+
12
+ asserts "passes when the association options are specified for a has_many" do
13
+ RiotMongoid::HasAssociationAssertion.new.evaluate(topic, :embeds_many, :things).first
14
+ end.equals(:pass)
15
+
16
+ asserts "passes when the association options are specified for a belongs_to" do
17
+ RiotMongoid::HasAssociationAssertion.new.evaluate(topic, :embedded_in, :another_thing, :inverse_of => :word).first
18
+ end.equals(:pass)
19
+
20
+ asserts "returns useful message" do
21
+ RiotMongoid::HasAssociationAssertion.new.evaluate(topic, :embedded_in, :another_thing, :inverse_of => :word).last
22
+ end.matches(/has 'embedded_in' association 'another_thing' with options \{:inverse_of=>:word\}/)
23
+
24
+ asserts "passes when the association options are specified for a has_many_related" do
25
+ RiotMongoid::HasAssociationAssertion.new.evaluate(topic, :has_many_related, :relations).first
26
+ end.equals(:pass)
27
+
28
+ asserts "fails when no association name is specified" do
29
+ RiotMongoid::HasAssociationAssertion.new.evaluate(topic, :has_many).first
30
+ end.equals(:fail)
31
+
32
+ end
@@ -0,0 +1,33 @@
1
+ require File.join(File.dirname(__FILE__),'teststrap')
2
+
3
+ context "has_field macro" do
4
+ setup do
5
+ mock_model do
6
+ field :name, :type => String
7
+ field :rad, :type => Boolean, :default => false
8
+ end
9
+ end
10
+ asserts "passes when the field options are specified" do
11
+ RiotMongoid::HasFieldAssertion.new.evaluate(topic, :name, :type => String).first
12
+ end.equals(:pass)
13
+
14
+ asserts "returns useful message" do
15
+ RiotMongoid::HasFieldAssertion.new.evaluate(topic, :name, :type => String).last
16
+ end.matches(/has field 'name' with options \{:type=>String\}/)
17
+
18
+ asserts "fails when the field options are not specified" do
19
+ RiotMongoid::HasFieldAssertion.new.evaluate(topic, :name).first
20
+ end.equals(:fail)
21
+
22
+ asserts "fails when invalid field options are specified" do
23
+ RiotMongoid::HasFieldAssertion.new.evaluate(topic, :name, :type => Date).first
24
+ end.equals(:fail)
25
+
26
+ asserts "passes with a slightly more complex field setup" do
27
+ RiotMongoid::HasFieldAssertion.new.evaluate(topic, :rad, :type => Boolean, :default => false).first
28
+ end.equals(:pass)
29
+
30
+ asserts "fails with a slightly more complex field setup and a bad assertion" do
31
+ RiotMongoid::HasFieldAssertion.new.evaluate(topic, :rad, :type => Boolean, :default => true).first
32
+ end.equals(:fail)
33
+ end
@@ -0,0 +1,26 @@
1
+ require File.join(File.dirname(__FILE__),'teststrap')
2
+
3
+ context "has_key macro" do
4
+ setup do
5
+ mock_model do
6
+ field :name, :type => String
7
+ field :rad, :type => Boolean, :default => false
8
+ field :surname, :type => String
9
+
10
+ key :name, :rad, :surname
11
+ end
12
+ end
13
+
14
+ asserts "passes when the key is specified" do
15
+ RiotMongoid::HasKeyAssertion.new.evaluate(topic, :name, :rad, :surname).first
16
+ end.equals(:pass)
17
+
18
+ asserts "fails when the key doesn't match" do
19
+ RiotMongoid::HasKeyAssertion.new.evaluate(topic, :name, :surname).first
20
+ end.equals(:fail)
21
+
22
+ asserts "returns a helpful message" do
23
+ RiotMongoid::HasKeyAssertion.new.evaluate(topic, :name, :rad, :surname).last
24
+ end.matches %r{has key name-rad-surname}
25
+
26
+ end
@@ -0,0 +1,43 @@
1
+ require File.join(File.dirname(__FILE__),'teststrap')
2
+
3
+ context "has_validation macro" do
4
+ setup do
5
+ mock_model do
6
+ field :name, :type => String
7
+ field :rad, :type => Boolean, :default => false
8
+ field :surname, :type => String
9
+
10
+ validates_presence_of :name, :surname
11
+ validates_length_of :surname, :within => 4..40
12
+ validates_uniqueness_of :rad
13
+ end
14
+ end
15
+
16
+ asserts "passes when the validation is specified" do
17
+ RiotMongoid::HasValidationAssertion.new.evaluate(topic, :validates_presence_of, :name).first
18
+ end.equals(:pass)
19
+
20
+ asserts "passes when the validation is specified" do
21
+ RiotMongoid::HasValidationAssertion.new.evaluate(topic, :validates_presence_of, :surname).first
22
+ end.equals(:pass)
23
+
24
+ asserts "returns useful message" do
25
+ RiotMongoid::HasValidationAssertion.new.evaluate(topic, :validates_presence_of, :name).last
26
+ end.matches(/has 'validates_presence_of' validation 'name'/)
27
+
28
+ asserts "passes when the validation options is specified" do
29
+ RiotMongoid::HasValidationAssertion.new.evaluate(topic, :validates_length_of, :surname, :within => 4..40).first
30
+ end.equals(:pass)
31
+
32
+ asserts "passes when the validation options is specified and doesn't match" do
33
+ RiotMongoid::HasValidationAssertion.new.evaluate(topic, :validates_length_of, :surname, :within => 3..30).first
34
+ end.equals(:fail)
35
+
36
+ asserts "fails when invalid field options are specified" do
37
+ RiotMongoid::HasValidationAssertion.new.evaluate(topic, :validates_length_of, :type => Date).first
38
+ end.equals(:fail)
39
+
40
+ asserts "passes when another validation is specified" do
41
+ RiotMongoid::HasValidationAssertion.new.evaluate(topic, :validates_uniqueness_of, :rad).first
42
+ end.equals(:pass)
43
+ end
@@ -1,4 +1,14 @@
1
1
  require 'rubygems'
2
2
  require 'riot'
3
- require 'riot-mongoid'
4
- require 'mongoid'
3
+ require 'mongoid'
4
+ require File.join(File.dirname(__FILE__),'..','lib','riot-mongoid')
5
+
6
+
7
+ class Riot::Situation
8
+ def mock_model(&block)
9
+ mock = Class.new
10
+ mock.class_eval { include Mongoid::Document }
11
+ mock.class_eval(&block)
12
+ mock
13
+ end
14
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 1
8
- - 2
9
- version: 1.1.2
8
+ - 3
9
+ version: 1.1.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - gabrielg
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-03-29 00:00:00 -05:00
17
+ date: 2010-06-10 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -77,16 +77,24 @@ extensions: []
77
77
 
78
78
  extra_rdoc_files:
79
79
  - LICENSE
80
- - README.rdoc
80
+ - README.md
81
81
  files:
82
82
  - .document
83
83
  - .gitignore
84
84
  - LICENSE
85
- - README.rdoc
85
+ - README.md
86
86
  - Rakefile
87
87
  - VERSION
88
88
  - lib/riot-mongoid.rb
89
- - test/riot-mongoid_test.rb
89
+ - lib/riot-mongoid/has_association.rb
90
+ - lib/riot-mongoid/has_field.rb
91
+ - lib/riot-mongoid/has_key.rb
92
+ - lib/riot-mongoid/has_validation.rb
93
+ - riot-mongoid.gemspec
94
+ - test/has_association_test.rb
95
+ - test/has_field_test.rb
96
+ - test/has_key_test.rb
97
+ - test/has_validation_test.rb
90
98
  - test/teststrap.rb
91
99
  has_rdoc: true
92
100
  homepage: http://github.com/gabrielg/riot-mongoid
@@ -119,5 +127,8 @@ signing_key:
119
127
  specification_version: 3
120
128
  summary: Riot assertions for Mongoid
121
129
  test_files:
122
- - test/riot-mongoid_test.rb
130
+ - test/has_association_test.rb
131
+ - test/has_field_test.rb
132
+ - test/has_key_test.rb
133
+ - test/has_validation_test.rb
123
134
  - test/teststrap.rb
@@ -1,17 +0,0 @@
1
- = riot-mongoid
2
-
3
- Description goes here.
4
-
5
- == Note on Patches/Pull Requests
6
-
7
- * Fork the project.
8
- * Make your feature addition or bug fix.
9
- * Add tests for it. This is important so I don't break it in a
10
- future version unintentionally.
11
- * Commit, do not mess with rakefile, version, or history.
12
- (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
- * Send me a pull request. Bonus points for topic branches.
14
-
15
- == Copyright
16
-
17
- Copyright (c) 2010 gabrielg. See LICENSE for details.
@@ -1,68 +0,0 @@
1
- require 'teststrap'
2
-
3
- context "riot-mongoid" do
4
-
5
- setup do
6
- test_class = Class.new
7
- test_class.class_eval do
8
- include Mongoid::Document
9
- field :name, :type => String
10
- field :rad, :type => Boolean, :default => false
11
-
12
- has_many :things
13
- belongs_to :another_thing, :inverse_of => :word
14
- has_many_related :relations
15
- end
16
- test_class
17
- end
18
-
19
- context "has_field" do
20
- asserts "the macro passes when the field options are specified" do
21
- RiotMongoid::HasFieldAssertion.new.evaluate(topic, :name, :type => String).first
22
- end.equals(:pass)
23
-
24
- asserts "the macro pass message is useful" do
25
- RiotMongoid::HasFieldAssertion.new.evaluate(topic, :name, :type => String).last
26
- end.matches(/has field 'name' with options \{:type=>String\}/)
27
-
28
- asserts "the macro fails when the field options are not specified" do
29
- RiotMongoid::HasFieldAssertion.new.evaluate(topic, :name).first
30
- end.equals(:fail)
31
-
32
- asserts "the macro fails when invalid field options are specified" do
33
- RiotMongoid::HasFieldAssertion.new.evaluate(topic, :name, :type => Date).first
34
- end.equals(:fail)
35
-
36
- asserts "the macro passes with a slightly more complex field setup" do
37
- RiotMongoid::HasFieldAssertion.new.evaluate(topic, :rad, :type => Boolean, :default => false).first
38
- end.equals(:pass)
39
-
40
- asserts "the macro fails with a slightly more complex field setup and a bad assertion" do
41
- RiotMongoid::HasFieldAssertion.new.evaluate(topic, :rad, :type => Boolean, :default => true).first
42
- end.equals(:fail)
43
- end # has_field
44
-
45
- context "has_association" do
46
-
47
- asserts "the macro passes when the association options are specified for a has_many" do
48
- RiotMongoid::HasAssociationAssertion.new.evaluate(topic, :has_many, :things).first
49
- end.equals(:pass)
50
-
51
- asserts "the macro passes when the association options are specified for a belongs_to" do
52
- RiotMongoid::HasAssociationAssertion.new.evaluate(topic, :belongs_to, :another_thing, :inverse_of => :word).first
53
- end.equals(:pass)
54
-
55
- asserts "the macro pass message is useful" do
56
- RiotMongoid::HasAssociationAssertion.new.evaluate(topic, :belongs_to, :another_thing, :inverse_of => :word).last
57
- end.matches(/has 'belongs_to' association 'another_thing' with options \{:inverse_of=>:word\}/)
58
-
59
- asserts "the macro passes when the association options are specified for a has_many_related" do
60
- RiotMongoid::HasAssociationAssertion.new.evaluate(topic, :has_many_related, :relations).first
61
- end.equals(:pass)
62
-
63
- asserts "the macro fails when no association name is specified" do
64
- RiotMongoid::HasAssociationAssertion.new.evaluate(topic, :has_many).first
65
- end.equals(:fail)
66
-
67
- end # has_association
68
- end # riot-mongoid