simply_stored 0.5.4 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,173 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/test_helper')
2
- require File.expand_path(File.dirname(__FILE__) + '/fixtures/couch')
3
-
4
- class BelongsToTest < Test::Unit::TestCase
5
- context "with associations" do
6
- setup do
7
- CouchPotato::Config.database_name = 'simply_stored_test'
8
- recreate_db
9
- end
10
-
11
- context "with belongs_to" do
12
- should "generate a view for the association" do
13
- assert Post.respond_to?(:association_post_belongs_to_user)
14
- end
15
-
16
- should "raise an error if another property with the same name already exists" do
17
- assert_raise(RuntimeError) do
18
- class ::DoubleBelongsToUser
19
- include SimplyStored::Couch
20
- property :user
21
- belongs_to :user
22
- end
23
- end
24
- end
25
-
26
- should "add the foreign key id to the referencing object" do
27
- user = User.create(:title => "Mr.")
28
- post = Post.create(:user => user)
29
-
30
- post = Post.find(post.id)
31
- assert_equal user.id, post.user_id
32
- end
33
-
34
- should "set also the foreign key id to nil if setting the referencing object to nil" do
35
- user = User.create(:title => "Mr.")
36
- post = Post.create(:user => user)
37
- post.user = nil
38
- post.save!
39
- assert_nil post.reload.user
40
- assert_nil post.reload.user_id
41
- end
42
-
43
- should "fetch the object from the database when requested through the getter" do
44
- user = User.create(:title => "Mr.")
45
- post = Post.create(:user => user)
46
-
47
- post = Post.find(post.id)
48
- assert_equal user, post.user
49
- end
50
-
51
- should "mark the referencing object as dirty" do
52
- user = User.create(:title => "Mr.")
53
- post = Post.create
54
- post.user = user
55
- assert post.dirty?
56
- end
57
-
58
- should "allow assigning a different object and store the id accordingly" do
59
- user = User.create(:title => "Mr.")
60
- user2 = User.create(:title => "Mrs.")
61
- post = Post.create(:user => user)
62
- post.user = user2
63
- post.save
64
-
65
- post = Post.find(post.id)
66
- assert_equal user2, post.user
67
- end
68
-
69
- should "check the class and raise an error if not matching in belongs_to setter" do
70
- post = Post.create
71
- assert_raise(ArgumentError, 'expected Post got String') do
72
- post.user = 'foo'
73
- end
74
- end
75
-
76
- should 'not query for the object twice in getter' do
77
- user = User.create(:title => "Mr.")
78
- post = Post.create(:user => user)
79
- post = Post.find(post.id)
80
- User.expects(:find).returns "user"
81
- post.user
82
- User.expects(:find).never
83
- post.user
84
- end
85
-
86
- should 'use cache in getter' do
87
- post = Post.create
88
- post.instance_variable_set("@user", 'foo')
89
- assert_equal 'foo', post.user
90
- end
91
-
92
- should "ignore the cache if force_reload is given as an option" do
93
- user = User.create(:name => 'Dude', :title => 'Mr.')
94
- post = Post.create(:user => user)
95
- post.reload
96
- post.instance_variable_set("@user", 'foo')
97
- assert_not_equal 'foo', post.user(:force_reload => true)
98
- end
99
-
100
- should 'set cache in setter' do
101
- post = Post.create
102
- user = User.create
103
- assert_nil post.instance_variable_get("@user")
104
- post.user = user
105
- assert_equal user, post.instance_variable_get("@user")
106
- end
107
-
108
- should "not hit the database when the id column is empty" do
109
- User.expects(:find).never
110
- post = Post.create
111
- post.user
112
- end
113
-
114
- should "know when the associated object changed" do
115
- post = Post.create(:user => User.create(:title => "Mr."))
116
- user2 = User.create(:title => "Mr.")
117
- post.user = user2
118
- assert post.user_changed?
119
- end
120
-
121
- should "not be changed when an association has not changed" do
122
- post = Post.create(:user => User.create(:title => "Mr."))
123
- assert !post.user_changed?
124
- end
125
-
126
- should "not be changed when assigned the same object" do
127
- user = User.create(:title => "Mr.")
128
- post = Post.create(:user => user)
129
- post.user = user
130
- assert !post.user_changed?
131
- end
132
-
133
- should "not be changed after saving" do
134
- user = User.create(:title => "Mr.")
135
- post = Post.new
136
- post.user = user
137
- assert post.user_changed?
138
- post.save!
139
- assert !post.user_changed?
140
- end
141
-
142
- should "handle a foreign_key of '' as nil" do
143
- post = Post.create
144
- post.user_id = ''
145
-
146
- assert_nothing_raised do
147
- assert_nil post.user
148
- end
149
- end
150
-
151
- context "with aliased associations" do
152
- should "allow different names for the same class" do
153
- editor = User.create(:name => 'Editor', :title => 'Dr.')
154
- author = User.create(:name => 'author', :title => 'Dr.')
155
- assert_not_nil editor.id, editor.errors.inspect
156
- assert_not_nil author.id, author.errors.inspect
157
-
158
- doc = Document.create(:editor => editor, :author => author)
159
- doc.save!
160
- assert_equal editor.id, doc.editor_id
161
- assert_equal author.id, doc.author_id
162
- doc = Document.find(doc.id)
163
- assert_not_nil doc.editor, doc.inspect
164
- assert_not_nil doc.author
165
- assert_equal editor.id, doc.editor.id
166
- assert_equal author.id, doc.author.id
167
- end
168
- end
169
- end
170
- end
171
-
172
-
173
- end
@@ -1,96 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/test_helper')
2
- require File.expand_path(File.dirname(__FILE__) + '/fixtures/couch')
3
-
4
- class ConflictHandlingTest < Test::Unit::TestCase
5
- context "when handling conflicts" do
6
- setup do
7
- CouchPotato::Config.database_name = 'simply_stored_test'
8
- recreate_db
9
- @original = User.create(:name => 'Mickey Mouse', :title => "Dr.", :homepage => 'www.gmx.de')
10
- @copy = User.find(@original.id)
11
- User.auto_conflict_resolution_on_save = true
12
- end
13
-
14
- should "be able to save without modifications" do
15
- assert @copy.save
16
- end
17
-
18
- should "be able to save when modification happen on different attributes" do
19
- @original.name = "Pluto"
20
- assert @original.save
21
-
22
- @copy.title = 'Prof.'
23
- assert_nothing_raised do
24
- assert @copy.save
25
- end
26
-
27
- assert_equal "Pluto", @copy.reload.name
28
- assert_equal "Prof.", @copy.reload.title
29
- assert_equal "www.gmx.de", @copy.reload.homepage
30
- end
31
-
32
- should "be able to save when modification happen on different, multiple attributes - remote" do
33
- @original.name = "Pluto"
34
- @original.homepage = 'www.google.com'
35
- assert @original.save
36
-
37
- @copy.title = 'Prof.'
38
- assert_nothing_raised do
39
- assert @copy.save
40
- end
41
-
42
- assert_equal "Pluto", @copy.reload.name
43
- assert_equal "Prof.", @copy.reload.title
44
- assert_equal "www.google.com", @copy.reload.homepage
45
- end
46
-
47
- should "be able to save when modification happen on different, multiple attributes locally" do
48
- @original.name = "Pluto"
49
- assert @original.save
50
-
51
- @copy.title = 'Prof.'
52
- @copy.homepage = 'www.google.com'
53
- assert_nothing_raised do
54
- assert @copy.save
55
- end
56
-
57
- assert_equal "Pluto", @copy.reload.name
58
- assert_equal "Prof.", @copy.reload.title
59
- assert_equal "www.google.com", @copy.reload.homepage
60
- end
61
-
62
- should "re-raise the conflict if there is no merge possible" do
63
- @original.name = "Pluto"
64
- assert @original.save
65
-
66
- @copy.name = 'Prof.'
67
- assert_raise(RestClient::Conflict) do
68
- assert @copy.save
69
- end
70
-
71
- assert_equal "Prof.", @copy.name
72
- assert_equal "Pluto", @copy.reload.name
73
- end
74
-
75
- should "re-raise the conflict if retried several times" do
76
- exception = RestClient::Conflict.new
77
- CouchPotato.database.expects(:save_document).raises(exception).times(3)
78
-
79
- @copy.name = 'Prof.'
80
- assert_raise(RestClient::Conflict) do
81
- assert @copy.save
82
- end
83
- end
84
-
85
- should "not try to merge and re-save if auto_conflict_resolution_on_save is disabled" do
86
- User.auto_conflict_resolution_on_save = false
87
- exception = RestClient::Conflict.new
88
- CouchPotato.database.expects(:save_document).raises(exception).times(1)
89
-
90
- @copy.name = 'Prof.'
91
- assert_raise(RestClient::Conflict) do
92
- assert @copy.save
93
- end
94
- end
95
- end
96
- end
data/test/finder_test.rb DELETED
@@ -1,188 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/test_helper')
2
- require File.expand_path(File.dirname(__FILE__) + '/fixtures/couch')
3
-
4
- class FinderTest < Test::Unit::TestCase
5
- context "when finding instances" do
6
- setup do
7
- CouchPotato::Config.database_name = 'simply_stored_test'
8
- recreate_db
9
- end
10
-
11
- context "with find(:all)" do
12
- setup do
13
- User.create(:title => "Mr.")
14
- User.create(:title => "Mrs.")
15
- end
16
-
17
- should "return all instances" do
18
- assert_equal 2, User.find(:all).size
19
- end
20
-
21
- should "allow a limit" do
22
- assert_equal 1, User.find(:all, :limit => 1).size
23
- end
24
-
25
- should "allow to order the results" do
26
- assert_not_equal User.find(:all).map(&:id), User.find(:all, :order => :desc).map(&:id)
27
- assert_equal User.find(:all).map(&:id).reverse, User.find(:all, :order => :desc).map(&:id)
28
- end
29
- end
30
-
31
- context "to find all instances" do
32
- should 'generate a default find_all view' do
33
- assert User.respond_to?(:all_documents)
34
- end
35
-
36
- should 'return all the users when calling all' do
37
- User.create(:title => "Mr.")
38
- User.create(:title => "Mrs.")
39
- assert_equal 2, User.all.size
40
- end
41
- end
42
-
43
- context "to find one instance" do
44
- should 'return one user when calling first' do
45
- user = User.create(:title => "Mr.")
46
- assert_equal user, User.first
47
- end
48
-
49
- should 'understand the order' do
50
- assert_nothing_raised do
51
- User.first(:order => :desc)
52
- end
53
- end
54
-
55
- should 'find the last as a reverse first' do
56
- User.expects(:find).with(:first, :order => :desc)
57
- User.last
58
- end
59
-
60
- should 'return nil when no user found' do
61
- assert_nil User.first
62
- end
63
- end
64
-
65
- context "when finding with just an identifier" do
66
- should "find just one instance" do
67
- user = User.create(:title => "Mr.")
68
- assert User.find(user.id).kind_of?(User)
69
- end
70
-
71
- should 'raise an error when no record was found' do
72
- assert_raises(SimplyStored::RecordNotFound) do
73
- User.find('abc')
74
- end
75
- end
76
-
77
- should 'tell you which class failed to load something' do
78
- exception = nil
79
- begin
80
- User.find('abc')
81
- rescue SimplyStored::RecordNotFound => e
82
- exception = e
83
- end
84
- assert_equal "User could not be found with \"abc\"", exception.message
85
- end
86
-
87
- should 'raise an error when nil was specified' do
88
- assert_raises(SimplyStored::Error) do
89
- User.find(nil)
90
- end
91
- end
92
-
93
- should 'raise an error when the record was not of the expected type' do
94
- post = Post.create
95
- assert_raises(SimplyStored::RecordNotFound) do
96
- User.find(post.id)
97
- end
98
- end
99
- end
100
-
101
- context "with a find_by prefix" do
102
- setup do
103
- recreate_db
104
- end
105
-
106
- should "create a view for the called finder" do
107
- User.find_by_name("joe")
108
- assert User.respond_to?(:by_name)
109
- end
110
-
111
- should 'not create the view when it already exists' do
112
- User.expects(:view).never
113
- User.find_by_name_and_created_at("joe", 'foo')
114
- end
115
-
116
- should "create a method to prevent future loops through method_missing" do
117
- assert !User.respond_to?(:find_by_title)
118
- User.find_by_title("Mr.")
119
- assert User.respond_to?(:find_by_title)
120
- end
121
-
122
- should "call the generated view and return the result" do
123
- user = User.create(:homepage => "http://www.peritor.com", :title => "Mr.")
124
- assert_equal user, User.find_by_homepage("http://www.peritor.com")
125
- end
126
-
127
- should 'find only one instance when using find_by' do
128
- User.create(:title => "Mr.")
129
- assert User.find_by_title("Mr.").is_a?(User)
130
- end
131
-
132
- should "raise an error if the parameters don't match" do
133
- assert_raise(ArgumentError) do
134
- User.find_by_title()
135
- end
136
-
137
- assert_raise(ArgumentError) do
138
- User.find_by_title(1,2,3,4,5)
139
- end
140
- end
141
- end
142
-
143
- context "with a find_all_by prefix" do
144
- should "create a view for the called finder" do
145
- User.find_all_by_name("joe")
146
- assert User.respond_to?(:by_name)
147
- end
148
-
149
- should 'not create the view when it already exists' do
150
- User.expects(:view).never
151
- User.find_all_by_name_and_created_at("joe", "foo")
152
- end
153
-
154
- should "create a method to prevent future loops through method_missing" do
155
- assert !User.respond_to?(:find_all_by_foo_attribute)
156
- User.find_all_by_foo_attribute("Mr.")
157
- assert User.respond_to?(:find_all_by_foo_attribute)
158
- end
159
-
160
- should "call the generated view and return the result" do
161
- user = User.create(:homepage => "http://www.peritor.com", :title => "Mr.")
162
- assert_equal [user], User.find_all_by_homepage("http://www.peritor.com")
163
- end
164
-
165
- should "return an emtpy array if none found" do
166
- recreate_db
167
- assert_equal [], User.find_all_by_title('Mr. Magoooo')
168
- end
169
-
170
- should 'find all instances when using find_all_by' do
171
- User.create(:title => "Mr.")
172
- User.create(:title => "Mr.")
173
- assert_equal 2, User.find_all_by_title("Mr.").size
174
- end
175
-
176
- should "raise an error if the parameters don't match" do
177
- assert_raise(ArgumentError) do
178
- User.find_all_by_title()
179
- end
180
-
181
- assert_raise(ArgumentError) do
182
- User.find_all_by_title(1,2,3,4,5)
183
- end
184
- end
185
- end
186
- end
187
-
188
- end