riot-datamapper 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 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,111 @@
1
+ # riot-datamapper #
2
+
3
+ Riot assertion macros for datamapper.
4
+
5
+ [Riot](https://github.com/thumblemonks/riot) is a fast, expressive, and contextual ruby unit testing framework.
6
+
7
+ [DataMapper](http://datamapper.org/) is an ORM which is fast, thread-safe and feature rich.
8
+
9
+ These macros provide an easy way to test some properties of your
10
+ DataMapper models.
11
+
12
+ ## Installation ##
13
+
14
+ Install it via rubygems:
15
+
16
+ ```
17
+ gem install riot-datamapper
18
+ ```
19
+
20
+ Or stick it in your Gemfile:
21
+
22
+ ```ruby
23
+ # Gemfile
24
+
25
+ group :test do
26
+ gem 'riot-datamapper'
27
+ end
28
+ ```
29
+
30
+ ## Examples / Usage ##
31
+
32
+ Given a model like:
33
+
34
+ ```ruby
35
+ # foo_bar.rb
36
+
37
+ class User
38
+ include DataMapper::Resource
39
+
40
+ property :foo, String
41
+ property :bar, Serial
42
+ property :monkey, Boolean, :default => false
43
+ property :name, String, :default => 'monkey', :required => true
44
+
45
+ has n, :comments
46
+ has 1, :address
47
+ end
48
+
49
+ class Address
50
+ property :street, String
51
+ property :city, String
52
+
53
+ belongs_to :user
54
+ end
55
+
56
+ class Comment
57
+
58
+ property :title, String
59
+ property :body, Text
60
+
61
+ belongs_to :user
62
+ end
63
+ ```
64
+
65
+ You can test this like so:
66
+
67
+ ```ruby
68
+ # foo_bar_test.rb
69
+
70
+ context "User Model" do
71
+ setup { User }
72
+
73
+ asserts_topic.has_property :foo
74
+ asserts_topic.has_property :bar, 'Serial'
75
+ asserts_topic.has_property :monkey, 'Boolean', :default => false
76
+ asserts_topic.has_property :name, 'String', :default => 'monkey', :required => true
77
+
78
+ asserts_topic.has_association :has_n, :comments
79
+ asserts_topic.has_association :has 1, :address
80
+ end
81
+
82
+ context "Address Model" do
83
+ setup { Address }
84
+
85
+ asserts_topic.has_property :street, String
86
+ asserts_topic.has_property :city, String
87
+
88
+ asserts_topic.has_association :belongs_to, :user
89
+ end
90
+
91
+ context "Comment Model" do
92
+ setup { Comment }
93
+
94
+ asserts_topic.has_property :title, String
95
+ asserts_topic.has_property :body, Text
96
+
97
+ asserts_topic.has_association :belongs_to, :user
98
+ end
99
+ ```
100
+
101
+ ## TODO ##
102
+
103
+ * Add more macros
104
+ * Add validation macros so you can do something like #has_validation :validates_presence_of
105
+
106
+ ## Copyright
107
+
108
+ Copyright © 2011 Arthur Chiu. See [MIT-LICENSE](https://github.com/achiu/riot-datamapper/blob/master/MIT-LICENSE) for details.
109
+
110
+
111
+
@@ -0,0 +1,32 @@
1
+ module Riot
2
+ module DataMapper
3
+ class HasAssociation < Riot::AssertionMacro
4
+ register :has_association
5
+
6
+ MAPPINGS = {
7
+ :has_n => 'OneToMany',
8
+ :has_1 => 'OneToOne',
9
+ :belongs_to => 'ManyToOne',
10
+ }
11
+
12
+ def evaluate(model, type, assoc, options = {})
13
+ relationship = model.relationships[assoc]
14
+ fail_msg = "expected #{model} to have an association with :#{assoc}"
15
+ pass_msg = "#{model} has an association with :#{assoc}"
16
+ type_msg = " of type '#{type}'"
17
+ options_msg = " with options #{options.inspect}"
18
+
19
+ return fail(fail_msg) if relationship.nil?
20
+
21
+ if relationship.class.to_s.include? MAPPINGS[type]
22
+ pass_msg << type_msg
23
+ else
24
+ return fail(fail_msg + type_msg)
25
+ end
26
+
27
+ pass(pass_msg)
28
+ end
29
+
30
+ end
31
+ end
32
+ end
@@ -1,5 +1,5 @@
1
1
  module Riot
2
2
  module Datamapper
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
@@ -0,0 +1,64 @@
1
+ require File.expand_path('../teststrap', __FILE__)
2
+
3
+ context "has_association macro" do
4
+ setup do
5
+ Object.send(:remove_const, :Post) if Object.const_defined?(:Post)
6
+ Object.send(:remove_const, :User) if Object.const_defined?(:User)
7
+ Object.send(:remove_const, :Comment) if Object.const_defined?(:Comment)
8
+
9
+ class Post
10
+ include DataMapper::Resource
11
+
12
+ property :id, Serial
13
+ property :foo, String
14
+
15
+ has n, :comments
16
+ has n, :users, :through => :comments
17
+ end
18
+
19
+ class User
20
+ include DataMapper::Resource
21
+
22
+ property :id, Serial
23
+ has 1, :comment
24
+ end
25
+
26
+ class Comment
27
+ include DataMapper::Resource
28
+ property :id, Serial
29
+
30
+ belongs_to :user
31
+ belongs_to :post
32
+ end
33
+ DataMapper.finalize
34
+ end
35
+
36
+ asserts "that it passes when the model has n :comments" do
37
+ Riot::DataMapper::HasAssociation.new.evaluate(Post,:has_n, :comments).first
38
+ end.equals :pass
39
+
40
+ asserts "that it fails when the model has n :foos" do
41
+ Riot::DataMapper::HasAssociation.new.evaluate(Post,:has_n, :foos).first
42
+ end.equals :fail
43
+
44
+ asserts "that it passes when the model belongs_to :post" do
45
+ Riot::DataMapper::HasAssociation.new.evaluate(Comment, :belongs_to, :post).first
46
+ end.equals :pass
47
+
48
+ asserts "that it fails when the model belongs_to :foo" do
49
+ Riot::DataMapper::HasAssociation.new.evaluate(Comment, :belongs_to, :foo).first
50
+ end.equals :fail
51
+
52
+ asserts "that it passes when the model has 1 :comment" do
53
+ Riot::DataMapper::HasAssociation.new.evaluate(User, :has_1, :comment).first
54
+ end.equals :pass
55
+
56
+ asserts "that it passes when the model has 1 :foo" do
57
+ Riot::DataMapper::HasAssociation.new.evaluate(User, :has_1, :foo).first
58
+ end.equals :fail
59
+
60
+ asserts "that it passes when the model belongs_to :user" do
61
+ Riot::DataMapper::HasAssociation.new.evaluate(Comment, :belongs_to, :user).first
62
+ end.equals :pass
63
+
64
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: riot-datamapper
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.1
5
+ version: 0.0.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Arthur Chiu
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-07-13 00:00:00 Z
13
+ date: 2011-07-15 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: riot
@@ -57,12 +57,16 @@ extra_rdoc_files: []
57
57
  files:
58
58
  - .gitignore
59
59
  - Gemfile
60
+ - MIT-LICENSE
61
+ - README.md
60
62
  - Rakefile
61
63
  - lib/riot-datamapper.rb
64
+ - lib/riot-datamapper/has_association.rb
62
65
  - lib/riot-datamapper/has_property.rb
63
66
  - lib/riot-datamapper/has_validation.rb
64
67
  - lib/riot-datamapper/version.rb
65
68
  - riot-datamapper.gemspec
69
+ - test/has_association_test.rb
66
70
  - test/has_property_test.rb
67
71
  - test/teststrap.rb
68
72
  homepage: https://www.github.com/achiu/riot-datamapper
@@ -93,5 +97,6 @@ signing_key:
93
97
  specification_version: 3
94
98
  summary: Riot Assertion Macros for DataMapper
95
99
  test_files:
100
+ - test/has_association_test.rb
96
101
  - test/has_property_test.rb
97
102
  - test/teststrap.rb