redis_orm 0.5.1 → 0.8

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.
@@ -1,306 +0,0 @@
1
- require File.dirname(File.expand_path(__FILE__)) + '/test_helper.rb'
2
-
3
- class Article < RedisOrm::Base
4
- property :title, String
5
- has_many :comments
6
- has_many :categories
7
- end
8
-
9
- class Comment < RedisOrm::Base
10
- property :body, String
11
-
12
- belongs_to :article
13
- end
14
-
15
- class Profile < RedisOrm::Base
16
- property :title, String
17
- has_one :city
18
- end
19
-
20
- class City < RedisOrm::Base
21
- property :name, String
22
- has_many :profiles
23
- end
24
-
25
- class Category < RedisOrm::Base
26
- property :name, String
27
- has_many :articles
28
- end
29
-
30
- class User < RedisOrm::Base
31
- property :name, String
32
- index :name
33
- has_many :users, :as => :friends
34
- end
35
-
36
- class Message < RedisOrm::Base
37
- property :text, String
38
- has_one :message, :as => :replay_to
39
- end
40
-
41
- describe "check associations" do
42
- before(:each) do
43
- @article = Article.new :title => "DHH drops OpenID on 37signals"
44
- @article.save
45
-
46
- @article.should be
47
- @article.title.should == "DHH drops OpenID on 37signals"
48
-
49
- @comment1 = Comment.new :body => "test"
50
- @comment1.save
51
- @comment1.should be
52
- @comment1.body.should == "test"
53
-
54
- @comment2 = Comment.new :body => "test #2"
55
- @comment2.save
56
- @comment2.should be
57
- @comment2.body.should == "test #2"
58
- end
59
-
60
- it "should assign properly from belongs_to side" do
61
- @comment1.article.should == nil
62
- @comment1.article = @article
63
- @comment1.article.id.should == @article.id
64
- @article.comments.count.should == 1
65
- @article.comments[0].id.should == @comment1.id
66
-
67
- @comment2.article.should == nil
68
- @comment2.article = @article
69
- @comment2.article.id.should == @article.id
70
- @article.comments.count.should == 2
71
- @article.comments[0].id.should == @comment2.id
72
- end
73
-
74
- it "should correctly resets associations when nil/[] provided" do
75
- # from has_many proxy side
76
- @article.comments << [@comment1, @comment2]
77
- @article.comments.count.should == 2
78
- @comment1.article.id.should == @article.id
79
- @comment2.article.id.should == @article.id
80
-
81
- # clear
82
- @article.comments = []
83
- @article.comments.count.should == 0
84
- @comment1.article.should == nil
85
- @comment2.article.should == nil
86
-
87
- # from belongs_to side
88
- @article.comments << [@comment1, @comment2]
89
- @article.comments.count.should == 2
90
- @comment1.article.id.should == @article.id
91
-
92
- # clear
93
- @comment1.article = nil
94
- @article.comments.count.should == 1
95
- @comment1.article.should == nil
96
-
97
- # from has_one side
98
- profile = Profile.create :title => "test"
99
- chicago = City.create :name => "Chicago"
100
-
101
- profile.city = chicago
102
- profile.city.name.should == "Chicago"
103
- chicago.profiles.count.should == 1
104
- chicago.profiles[0].id.should == profile.id
105
-
106
- # clear
107
- profile.city = nil
108
- profile.city.should == nil
109
- chicago.profiles.count.should == 0
110
- end
111
-
112
- it "should return array of records for has_many association" do
113
- @article.comments << []
114
- @article.comments.count.should == 0
115
-
116
- @article.comments = []
117
- @article.comments.count.should == 0
118
-
119
- @article.comments << [@comment1, @comment2]
120
- #@article.comments.should be_kind_of(Array)
121
-
122
- @article.comments.count.should == 2
123
- @article.comments.size.should == 2
124
-
125
- @comment1.article.should be
126
- @comment2.article.should be
127
-
128
- @comment1.article.id.should == @comment2.article.id
129
- end
130
-
131
- it "should behave as active_record (proxy couldn't return records w/o #all call) += and << behave differently" do
132
- @article.comments << @comment1 << @comment2
133
- @article.comments.count.should == 2
134
-
135
- comments = @article.comments
136
- comments.count.should == 2
137
-
138
- comments = []
139
- comments += @article.comments
140
- comments.count.should == 2
141
- comments.collect{|c| c.id}.should include(@comment1.id)
142
- comments.collect{|c| c.id}.should include(@comment2.id)
143
-
144
- comments = []
145
- comments << @article.comments.all
146
- comments.flatten.count.should == 2
147
-
148
- comments = []
149
- comments << @article.comments
150
- comments.count.should == 1
151
- end
152
-
153
- it "should return 1 comment when second was deleted" do
154
- Comment.count.should == 2
155
- @article.comments << [@comment1, @comment2]
156
- #@article.comments.should be_kind_of(Array)
157
- @article.comments.size.should == 2
158
-
159
- @comment1.destroy
160
-
161
- @article.comments.size.should == 1
162
- @article.comments.count.should == 1
163
- Comment.count.should == 1
164
- end
165
-
166
- it "should leave associations when parent has been deleted (nullify assocs)" do
167
- Comment.count.should == 2
168
- @article.comments << [@comment1, @comment2]
169
- @comment1.article.id.should == @article.id
170
- @comment2.article.id.should == @article.id
171
- #@article.comments.should be_kind_of(Array)
172
- @article.comments.size.should == 2
173
- @article.comments.count.should == 2
174
-
175
- @article.destroy
176
-
177
- Article.count.should == 0
178
- Comment.count.should == 2
179
- end
180
-
181
- it "should replace associations when '=' is used instead of '<<' " do
182
- Comment.count.should == 2
183
- @article.comments << [@comment1, @comment2]
184
- @comment1.article.id.should == @article.id
185
- @comment2.article.id.should == @article.id
186
- @article.comments.size.should == 2
187
- @article.comments.count.should == 2
188
-
189
- @article.comments = [@comment1]
190
- @article.comments.count.should == 1
191
- @article.comments.first.id.should == @comment1.id
192
-
193
- @comment1.article.id.should == @article.id
194
- end
195
-
196
- it "should correctly use many-to-many associations both with '=' and '<<' " do
197
- @cat1 = Category.create :name => "Nature"
198
- @cat2 = Category.create :name => "Art"
199
- @cat3 = Category.create :name => "Web"
200
-
201
- @cat1.name.should == "Nature"
202
- @cat2.name.should == "Art"
203
- @cat3.name.should == "Web"
204
-
205
- @article.categories << [@cat1, @cat2]
206
-
207
- @cat1.articles.count.should == 1
208
- @cat1.articles[0].id.should == @article.id
209
- @cat2.articles.count.should == 1
210
- @cat2.articles[0].id.should == @article.id
211
-
212
- @article.categories.size.should == 2
213
- @article.categories.count.should == 2
214
-
215
- @article.categories = [@cat1, @cat3]
216
- @article.categories.count.should == 2
217
- @article.categories.map{|c| c.id}.include?(@cat1.id).should be
218
- @article.categories.map{|c| c.id}.include?(@cat3.id).should be
219
-
220
- @cat1.articles.count.should == 1
221
- @cat1.articles[0].id.should == @article.id
222
-
223
- @cat3.articles.count.should == 1
224
- @cat3.articles[0].id.should == @article.id
225
-
226
- @cat2.articles.count.should == 0
227
-
228
- @cat1.destroy
229
- Category.count.should == 2
230
- @article.categories.count.should == 1
231
- end
232
-
233
- it "should remove old associations and create new ones" do
234
- profile = Profile.new
235
- profile.title = "test"
236
- profile.save
237
-
238
- chicago = City.new
239
- chicago.name = "Chicago"
240
- chicago.save
241
-
242
- washington = City.new
243
- washington.name = "Washington"
244
- washington.save
245
-
246
- profile.city = chicago
247
- profile.city.name.should == "Chicago"
248
- chicago.profiles.count.should == 1
249
- washington.profiles.count.should == 0
250
- chicago.profiles[0].id.should == profile.id
251
-
252
- profile.city = washington
253
- profile.city.name.should == "Washington"
254
- chicago.profiles.count.should == 0
255
- washington.profiles.count.should == 1
256
- washington.profiles[0].id.should == profile.id
257
- end
258
-
259
- it "should maintain correct self referencing link" do
260
- me = User.create :name => "german"
261
- friend1 = User.create :name => "friend1"
262
- friend2 = User.create :name => "friend2"
263
-
264
- me.friends << [friend1, friend2]
265
-
266
- me.friends.count.should == 2
267
- friend1.friends.count.should == 0
268
- friend2.friends.count.should == 0
269
- end
270
-
271
- it "should delete one specific record from an array with associated records" do
272
- me = User.create :name => "german"
273
- friend1 = User.create :name => "friend1"
274
- friend2 = User.create :name => "friend2"
275
-
276
- me.friends << [friend1, friend2]
277
-
278
- me = User.find_by_name 'german'
279
- me.friends.count.should == 2
280
- friend1 = User.find_by_name 'friend1'
281
- friend1.friends.count.should == 0
282
- friend2 = User.find_by_name 'friend2'
283
- friend2.friends.count.should == 0
284
-
285
- me.friends.delete(friend1.id)
286
- me.friends.count.should == 1
287
- me.friends[0].id == friend2.id
288
- User.count.should == 3
289
- end
290
-
291
- it "should create self-referencing link for has_one association" do
292
- m = Message.create :text => "it should create self-referencing link for has_one association"
293
-
294
- r = Message.create :text => "replay"
295
-
296
- r.replay_to = m
297
-
298
- Message.count.should == 2
299
- r.replay_to.should be
300
- r.replay_to.id.should == m.id
301
-
302
- rf = Message.last
303
- rf.replay_to.should be
304
- rf.replay_to.id.should == Message.first.id
305
- end
306
- end
@@ -1,64 +0,0 @@
1
- require File.dirname(File.expand_path(__FILE__)) + '/test_helper.rb'
2
-
3
- class Article < RedisOrm::Base
4
- property :title, String
5
- property :karma, Integer
6
- end
7
-
8
- describe "check atomicity" do
9
- =begin
10
- it "should properly increment property's value" do
11
- @article = Article.new :title => "Simple test atomicity with multiple threads", :karma => 1
12
- @article.save
13
-
14
- @threads = []
15
-
16
- 50.times do |i|
17
- @threads << Thread.new(i) do
18
- sleep(0.2)
19
- @article.update_attribute :karma, (@article.karma + 1)
20
- end
21
- end
22
-
23
- @threads.each{|thread| thread.join}
24
-
25
- Article.first.karma.should == 51
26
- end
27
- =end
28
- it "should properly increment property's value" do
29
- threads = []
30
-
31
- 50.times do |i|
32
- id = i
33
- threads << Thread.new(i) do
34
- if id % 2 == 0
35
- art = Article.create :title => "article ##{id}", :karma => id
36
- puts "article.last.id - #{art.id}, article.last.karma - #{art.karma}"
37
- else
38
- puts "id - #{id}, (id / 2) + 1 - #{(id / 2) + 1}"
39
- Article.find((id / 2) + 1).destroy
40
- end
41
- end
42
- end
43
-
44
- threads.each{|thread| thread.join}
45
- Article.count.should == 0
46
- end
47
- =begin
48
- it "should properly increment/decrement property's value" do
49
- article = Article.create :title => "article #1", :karma => 10
50
- threads = []
51
-
52
- 10.times do
53
- threads << Thread.new{ article.update_attribute(:karma, (article.karma + 2)) }
54
- end
55
-
56
- 15.times do
57
- threads << Thread.new{ article.update_attribute(:karma, (article.karma - 1)) }
58
- end
59
-
60
- threads.each{|thread| thread.join}
61
- article.karma.should == 15
62
- end
63
- =end
64
- end
@@ -1,173 +0,0 @@
1
- require File.dirname(File.expand_path(__FILE__)) + '/test_helper.rb'
2
-
3
- class User < RedisOrm::Base
4
- property :name, String
5
- property :age, Integer
6
- property :wage, Float
7
- property :male, RedisOrm::Boolean
8
-
9
- property :created_at, Time
10
- property :modified_at, Time
11
- end
12
-
13
- class DefaultUser < RedisOrm::Base
14
- property :name, String, :default => "german"
15
- property :age, Integer, :default => 26
16
- property :wage, Float, :default => 256.25
17
- property :male, RedisOrm::Boolean, :default => true
18
- property :admin, RedisOrm::Boolean, :default => false
19
-
20
- property :created_at, Time
21
- property :modified_at, Time
22
- end
23
-
24
- class TimeStamp < RedisOrm::Base
25
- timestamps
26
- end
27
-
28
- describe "check basic functionality" do
29
- it "test_simple_creation" do
30
- User.count.should == 0
31
-
32
- user = User.new :name => "german"
33
- user.save
34
-
35
- user.should be
36
-
37
- user.name.should == "german"
38
-
39
- User.count.should == 1
40
- User.first.name.should == "german"
41
- end
42
-
43
- it "should test different ways to update a record" do
44
- User.count.should == 0
45
-
46
- user = User.new :name => "german"
47
- user.should be
48
- user.save
49
-
50
- user.name.should == "german"
51
-
52
- user.name = "nobody"
53
- user.save
54
-
55
- User.count.should == 1
56
- User.first.name.should == "nobody"
57
-
58
- u = User.first
59
- u.should be
60
- u.update_attribute :name, "root"
61
- User.first.name.should == "root"
62
-
63
- u = User.first
64
- u.should be
65
- u.update_attributes :name => "german"
66
- User.first.name.should == "german"
67
- end
68
-
69
- it "test_deletion" do
70
- User.count.should == 0
71
-
72
- user = User.new :name => "german"
73
- user.save
74
- user.should be
75
-
76
- user.name.should == "german"
77
-
78
- User.count.should == 1
79
- id = user.id
80
-
81
- user.destroy
82
- User.count.should == 0
83
- $redis.zrank("user:ids", id).should == nil
84
- $redis.hgetall("user:#{id}").should == {}
85
- end
86
-
87
- it "should return first and last objects" do
88
- User.count.should == 0
89
- User.first.should == nil
90
- User.last.should == nil
91
-
92
- user1 = User.new :name => "german"
93
- user1.save
94
- user1.should be
95
- user1.name.should == "german"
96
-
97
- user2 = User.new :name => "nobody"
98
- user2.save
99
- user2.should be
100
- user2.name.should == "nobody"
101
-
102
- User.count.should == 2
103
-
104
- User.first.should be
105
- User.last.should be
106
-
107
- User.first.id.should == user1.id
108
- User.last.id.should == user2.id
109
- end
110
-
111
- it "should return values with correct classes" do
112
- user = User.new
113
- user.name = "german"
114
- user.age = 26
115
- user.wage = 124.34
116
- user.male = true
117
- user.save
118
-
119
- user.should be
120
-
121
- u = User.first
122
-
123
- u.created_at.class.should == Time
124
- u.modified_at.class.should == Time
125
- u.wage.class.should == Float
126
- u.male.class.to_s.should match(/TrueClass|FalseClass/)
127
- u.age.class.to_s.should match(/Integer|Fixnum/)
128
-
129
- u.name.should == "german"
130
- u.wage.should == 124.34
131
- u.age.should == 26
132
- u.male.should == true
133
- end
134
-
135
- it "should return correct saved defaults" do
136
- DefaultUser.count.should == 0
137
- DefaultUser.create
138
- DefaultUser.count.should == 1
139
-
140
- u = DefaultUser.first
141
-
142
- u.created_at.class.should == Time
143
- u.modified_at.class.should == Time
144
- u.wage.class.should == Float
145
- u.male.class.to_s.should match(/TrueClass|FalseClass/)
146
- u.admin.class.to_s.should match(/TrueClass|FalseClass/)
147
- u.age.class.to_s.should match(/Integer|Fixnum/)
148
-
149
- u.name.should == "german"
150
- u.male.should == true
151
- u.age.should == 26
152
- u.wage.should == 256.25
153
- u.admin.should == false
154
-
155
- du = DefaultUser.new
156
- du.name = "germaninthetown"
157
- du.save
158
-
159
- du_saved = DefaultUser.last
160
- du_saved.name.should == "germaninthetown"
161
- du_saved.admin.should == false
162
- end
163
-
164
- it "should expand timestamps declaration properly" do
165
- t = TimeStamp.new
166
- t.save
167
-
168
- t.created_at.should be
169
- t.modified_at.should be
170
- t.created_at.day.should == Time.now.day
171
- t.modified_at.day.should == Time.now.day
172
- end
173
- end