riot-mongoid 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0
1
+ 1.1.0
data/lib/riot-mongoid.rb CHANGED
@@ -16,4 +16,23 @@ module RiotMongoid
16
16
  end
17
17
  end
18
18
  end # HasFieldAssertion
19
+
20
+ class HasAssociationAssertion < Riot::AssertionMacro
21
+ register :has_association
22
+
23
+ def evaluate(model, *association_macro_info)
24
+ assoc_type, assoc_name, options = association_macro_info
25
+ assoc = model.associations[assoc_name.to_s]
26
+ options ||= {}
27
+ if assoc_name.nil?
28
+ fail("association name and potential options must be specified with this assertion macro")
29
+ elsif assoc.nil? || assoc.macro != assoc_type.to_sym
30
+ fail("expected #{model} to have association #{assoc_name} of type #{assoc_type}")
31
+ else
32
+ options_valid = options.all? { |key,value| assoc.send(key) == value }
33
+ options_valid ? pass("#{model} has '#{assoc_type}' association '#{assoc_name}' with options #{options.inspect}") :
34
+ fail("expected model to have options #{options.inspect} on association #{assoc_name}")
35
+ end
36
+ end
37
+ end # HasAssociationAssertion
19
38
  end # RiotMongoid
@@ -8,31 +8,61 @@ context "riot-mongoid" do
8
8
  include Mongoid::Document
9
9
  field :name, :type => String
10
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
11
15
  end
12
16
  test_class
13
17
  end
14
18
 
15
- asserts "the macro passes when the field options are specified" do
16
- RiotMongoid::HasFieldAssertion.new.evaluate(topic, :name, :type => String).first
17
- end.equals(:pass)
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)
18
50
 
19
- asserts "the macro pass message is useful" do
20
- RiotMongoid::HasFieldAssertion.new.evaluate(topic, :name, :type => String).last
21
- end.matches(/has field 'name' with options \{:type=>String\}/)
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)
22
54
 
23
- asserts "the macro fails when the field options are not specified" do
24
- RiotMongoid::HasFieldAssertion.new.evaluate(topic, :name).first
25
- end.equals(:fail)
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\}/)
26
58
 
27
- asserts "the macro fails when invalid field options are specified" do
28
- RiotMongoid::HasFieldAssertion.new.evaluate(topic, :name, :type => Date).first
29
- end.equals(:fail)
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)
30
62
 
31
- asserts "the macro passes with a slightly more complex field setup" do
32
- RiotMongoid::HasFieldAssertion.new.evaluate(topic, :rad, :type => Boolean, :default => false).first
33
- end.equals(:pass)
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)
34
66
 
35
- asserts "the macro fails with a slightly more complex field setup and a bad assertion" do
36
- RiotMongoid::HasFieldAssertion.new.evaluate(topic, :rad, :type => Boolean, :default => true).first
37
- end.equals(:fail)
38
- end
67
+ end # has_association
68
+ end # riot-mongoid
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: riot-mongoid
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - gabrielg
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-03-05 00:00:00 -06:00
12
+ date: 2010-03-08 00:00:00 -06:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency