riot-mongo_mapper 0.0.1

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.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Arthur Chiu
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # riot-mongo_mapper
2
+
3
+ Riot assertions for MongoMapper
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_key :title, String
14
+ asserts_topic.has_key :caption, String, :default => ""
15
+
16
+ # association assertions
17
+ asserts_topic.has_association :many, :accounts
18
+ asserts_topic.has_association :one, :comments
19
+ asserts_topic.has_association :many, :persons, :in => :persons_id
20
+
21
+ # validation assertions
22
+ asserts_topic.has_validation :validates_presence_of, :caption
23
+ end
24
+ end
25
+
26
+
27
+ ## Note on Patches/Pull Requests
28
+
29
+ * Fork the project.
30
+ * Make your feature addition or bug fix.
31
+ * Add tests for it. This is important so I don't break it in a
32
+ future version unintentionally.
33
+ * Commit, do not mess with rakefile, version, or history.
34
+ (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)
35
+ * Send me a pull request. Bonus points for topic branches.
36
+
37
+ ## Copyright
38
+
39
+ Copyright (c) 2010 Arthur Chiu. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,36 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'mg'
4
+
5
+ MG.new(File.join(File.dirname(__FILE__),"riot-mongo_mapper.gemspec"))
6
+
7
+ require 'rake/testtask'
8
+ Rake::TestTask.new(:test) do |test|
9
+ test.libs << 'lib' << 'test'
10
+ test.pattern = 'test/**/*_test.rb'
11
+ test.verbose = true
12
+ end
13
+
14
+ begin
15
+ require 'rcov/rcovtask'
16
+ Rcov::RcovTask.new do |test|
17
+ test.libs << 'test'
18
+ test.pattern = 'test/**/*_test.rb'
19
+ test.verbose = true
20
+ end
21
+ rescue LoadError
22
+ task :rcov do
23
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
24
+ end
25
+ end
26
+
27
+ task :default => :test
28
+
29
+ begin
30
+ require 'yard'
31
+ YARD::Rake::YardocTask.new
32
+ rescue LoadError
33
+ task :yardoc do
34
+ abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
35
+ end
36
+ end
@@ -0,0 +1,18 @@
1
+ module RiotMongoMapper
2
+ class HasAssociationAssertion < Riot::AssertionMacro
3
+ register :has_association
4
+
5
+ def evaluate(model, *association_macro_info)
6
+ association_name, key_name, options = association_macro_info
7
+ association = model.associations[key_name.to_s]
8
+ if association.nil?
9
+ fail("expected #{model} to have association #{association}")
10
+ else
11
+ association_valid = association.type == association_name.to_sym
12
+ options_valid = options ? options.all? { |field,value| association.options[field] == value } : true
13
+ options_valid && association_valid ? pass("#{model} has association '#{association_name}' on '#{key_name}' with options #{options.inspect}") :
14
+ fail("expected #{model} to have association #{association_name} with options #{options.inspect} on key #{key_name}")
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ module RiotMongoMapper
2
+ class HasKeyAssertion < Riot::AssertionMacro
3
+ register :has_key
4
+
5
+ def evaluate(model, *key_macro_info)
6
+ key_name, key_type, options = key_macro_info
7
+ key = model.keys[key_name.to_sym]
8
+ if key.nil?
9
+ fail("expected #{model} to have field #{key_name}")
10
+ else
11
+ type_valid = (key.type == key_type)
12
+ options_valid = options ? options.all? { |field,value| key.options[field] == value } : true
13
+ options_valid && type_valid ? pass("#{model} has key '#{key_name}' with options #{options.inspect}") :
14
+ fail("expected #{model} to have options #{options.inspect} on key #{key_name}")
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,23 @@
1
+ module RiotMongoMapper
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,4 @@
1
+ require 'riot'
2
+ require 'mongo_mapper'
3
+
4
+ Dir[File.dirname(__FILE__) + '/riot-mongo_mapper/*.rb'].each {|file| require file }
@@ -0,0 +1,20 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "riot-mongo_mapper"
3
+ s.version = "0.0.1"
4
+ s.required_rubygems_version = ">= 1.3.6"
5
+ s.authors = ["Arthur Chiu"]
6
+ s.date = Time.now.strftime("%Y-%m-%d")
7
+ s.description = "A collection of assertion macros for testing MongoMapper with Riot"
8
+ s.email = "mr.arthur.chiu@gmail.com"
9
+ s.extra_rdoc_files = ["README.md"]
10
+ s.files = %w{LICENSE README.md Rakefile riot-mongo_mapper.gemspec} + Dir.glob("{lib,test}/**/*")
11
+ s.homepage = %q{http://github.com/achiu/riot-mongo_mapper}
12
+ s.rdoc_options = ["--charset=UTF-8"]
13
+ s.require_paths = ["lib"]
14
+ s.summary = %q{Riot assertions for MongoMapper}
15
+ s.add_development_dependency(%q<riot>, [">= 0"])
16
+ s.add_development_dependency(%q<yard>, [">= 0"])
17
+ s.add_runtime_dependency(%q<mongo_mapper>, [">= 0.8.2"])
18
+ s.add_runtime_dependency(%q<riot>, [">= 0.11.2"])
19
+ end
20
+
@@ -0,0 +1,32 @@
1
+ require File.join(File.dirname(__FILE__),'teststrap')
2
+
3
+ context "has_association macro" do
4
+ setup{ @assertion = RiotMongoMapper::HasAssociationAssertion.new }
5
+ setup do
6
+ mock_model do
7
+ many :persons
8
+ many :categories, :in => :category_ids, :class_name => 'Tags'
9
+ end
10
+ end
11
+
12
+ asserts "passes when has association" do
13
+ @assertion.evaluate(topic, :many, :persons).first
14
+ end.equals(:pass)
15
+
16
+ asserts "fails when has association" do
17
+ @assertion.evaluate(topic, :many, :dogs).first
18
+ end.equals(:fail)
19
+
20
+ asserts "passes when association options is specified" do
21
+ @assertion.evaluate(topic, :many, :categories, :in => :category_ids, :class_name => 'Tags').first
22
+ end.equals(:pass)
23
+
24
+ asserts "fails when the options don't match" do
25
+ @assertion.evaluate(topic, :many, :categories, :foreign_id => 'wtf').first
26
+ end.equals(:fail)
27
+
28
+ asserts "returns a message" do
29
+ @assertion.evaluate(topic, :many, :persons).last
30
+ end.matches %r{has association 'many' on 'persons' with options}
31
+
32
+ end
@@ -0,0 +1,34 @@
1
+ require File.join(File.dirname(__FILE__),'teststrap')
2
+
3
+ context "has_key macro" do
4
+ setup { @assertion = RiotMongoMapper::HasKeyAssertion.new }
5
+ setup do
6
+ mock_model do
7
+ key :name, String
8
+ key :foo, Boolean, :default => false
9
+ key :age, Integer, :required => true, :default => 18
10
+ end
11
+ end
12
+
13
+ asserts "passes when the key is specified" do
14
+ @assertion.evaluate(topic, :name, String).first
15
+ end.equals(:pass)
16
+
17
+ asserts "fails when the key doesn't match" do
18
+ @assertion.evaluate(topic, :name, Array).first
19
+ end.equals(:fail)
20
+
21
+ asserts "passes when options are specified" do
22
+ @assertion.evaluate(topic, :age, Integer, :required => true, :default => 18).first
23
+ end.equals(:pass)
24
+
25
+ asserts "fails when the options specifed don't match" do
26
+ @assertion.evaluate(topic, :foo, Boolean, :default => true).first
27
+ end.equals(:fail)
28
+
29
+ asserts "returns a message" do
30
+ @assertion.evaluate(topic, :name, String).last
31
+ end.matches %r{has key 'name' with options}
32
+
33
+
34
+ end
@@ -0,0 +1,44 @@
1
+ require File.join(File.dirname(__FILE__),'teststrap')
2
+
3
+ context "has_validation macro" do
4
+ setup { @assertion = RiotMongoMapper::HasValidationAssertion.new }
5
+ setup do
6
+ mock_model do
7
+ key :name, String
8
+ key :rad, Boolean, :default => false
9
+ key :surname, String
10
+
11
+ validates_presence_of :name, :surname
12
+ validates_length_of :surname, :within => 4..40
13
+ validates_uniqueness_of :rad
14
+ end
15
+ end
16
+
17
+ asserts "passes when the validation is specified" do
18
+ @assertion.evaluate(topic, :validates_presence_of, :name).first
19
+ end.equals(:pass)
20
+
21
+ asserts "passes when the validation is specified" do
22
+ @assertion.evaluate(topic, :validates_presence_of, :surname).first
23
+ end.equals(:pass)
24
+
25
+ asserts "returns useful message" do
26
+ @assertion.evaluate(topic, :validates_presence_of, :name).last
27
+ end.matches(/has 'validates_presence_of' validation 'name'/)
28
+
29
+ asserts "passes when the validation options is specified" do
30
+ @assertion.evaluate(topic, :validates_length_of, :surname, :within => 4..40).first
31
+ end.equals(:pass)
32
+
33
+ asserts "passes when the validation options is specified and doesn't match" do
34
+ @assertion.evaluate(topic, :validates_length_of, :surname, :within => 3..30).first
35
+ end.equals(:fail)
36
+
37
+ asserts "fails when invalid field options are specified" do
38
+ @assertion.evaluate(topic, :validates_length_of, :name, :type => Date).first
39
+ end.equals(:fail)
40
+
41
+ asserts "passes when another validation is specified" do
42
+ @assertion.evaluate(topic, :validates_uniqueness_of, :rad).first
43
+ end.equals(:pass)
44
+ end
data/test/teststrap.rb ADDED
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'riot'
3
+ require 'mongo_mapper'
4
+ require File.join(File.dirname(__FILE__),'..','lib','riot-mongo_mapper')
5
+
6
+
7
+ class Riot::Situation
8
+ def mock_model(&block)
9
+ mock = Class.new
10
+ mock.class_eval { include MongoMapper::Document }
11
+ mock.class_eval(&block)
12
+ mock
13
+ end
14
+ end
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: riot-mongo_mapper
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Arthur Chiu
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-07-04 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: riot
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: yard
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ name: mongo_mapper
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 59
58
+ segments:
59
+ - 0
60
+ - 8
61
+ - 2
62
+ version: 0.8.2
63
+ type: :runtime
64
+ version_requirements: *id003
65
+ - !ruby/object:Gem::Dependency
66
+ name: riot
67
+ prerelease: false
68
+ requirement: &id004 !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ hash: 55
74
+ segments:
75
+ - 0
76
+ - 11
77
+ - 2
78
+ version: 0.11.2
79
+ type: :runtime
80
+ version_requirements: *id004
81
+ description: A collection of assertion macros for testing MongoMapper with Riot
82
+ email: mr.arthur.chiu@gmail.com
83
+ executables: []
84
+
85
+ extensions: []
86
+
87
+ extra_rdoc_files:
88
+ - README.md
89
+ files:
90
+ - LICENSE
91
+ - README.md
92
+ - Rakefile
93
+ - riot-mongo_mapper.gemspec
94
+ - lib/riot-mongo_mapper/has_association.rb
95
+ - lib/riot-mongo_mapper/has_key.rb
96
+ - lib/riot-mongo_mapper/has_validation.rb
97
+ - lib/riot-mongo_mapper.rb
98
+ - test/has_association_test.rb
99
+ - test/has_key_test.rb
100
+ - test/has_validation_test.rb
101
+ - test/teststrap.rb
102
+ has_rdoc: true
103
+ homepage: http://github.com/achiu/riot-mongo_mapper
104
+ licenses: []
105
+
106
+ post_install_message:
107
+ rdoc_options:
108
+ - --charset=UTF-8
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ none: false
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ hash: 3
117
+ segments:
118
+ - 0
119
+ version: "0"
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ hash: 23
126
+ segments:
127
+ - 1
128
+ - 3
129
+ - 6
130
+ version: 1.3.6
131
+ requirements: []
132
+
133
+ rubyforge_project:
134
+ rubygems_version: 1.3.7
135
+ signing_key:
136
+ specification_version: 3
137
+ summary: Riot assertions for MongoMapper
138
+ test_files: []
139
+