simple_record 2.1.3 → 2.1.6
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +23 -2
- data/lib/simple_record.rb +50 -47
- data/lib/simple_record/active_sdb.rb +941 -938
- data/lib/simple_record/json.rb +37 -57
- data/lib/simple_record/sharding.rb +48 -20
- data/lib/simple_record/validations.rb +1 -1
- metadata +47 -82
- data/test/conversions_test.rb +0 -44
- data/test/model_with_enc.rb +0 -6
- data/test/my_base_model.rb +0 -8
- data/test/my_child_model.rb +0 -35
- data/test/my_model.rb +0 -101
- data/test/my_sharded_model.rb +0 -46
- data/test/my_simple_model.rb +0 -13
- data/test/paging_array_test.rb +0 -15
- data/test/temp.rb +0 -9
- data/test/temp_test.rb +0 -63
- data/test/test_base.rb +0 -67
- data/test/test_dirty.rb +0 -79
- data/test/test_encodings.rb +0 -43
- data/test/test_global_options.rb +0 -63
- data/test/test_helpers.rb +0 -15
- data/test/test_json.rb +0 -82
- data/test/test_lobs.rb +0 -118
- data/test/test_marshalled.rb +0 -43
- data/test/test_pagination.rb +0 -31
- data/test/test_rails3.rb +0 -24
- data/test/test_results_array.rb +0 -57
- data/test/test_shards.rb +0 -148
- data/test/test_simple_record.rb +0 -693
- data/test/test_usage.rb +0 -40
- data/test/test_validations.rb +0 -55
data/test/test_shards.rb
DELETED
@@ -1,148 +0,0 @@
|
|
1
|
-
require 'test/unit'
|
2
|
-
require File.join(File.dirname(__FILE__), "/../lib/simple_record")
|
3
|
-
require File.join(File.dirname(__FILE__), "./test_helpers")
|
4
|
-
require File.join(File.dirname(__FILE__), "./test_base")
|
5
|
-
require "yaml"
|
6
|
-
require 'aws'
|
7
|
-
require_relative 'my_sharded_model'
|
8
|
-
|
9
|
-
# Tests for SimpleRecord
|
10
|
-
#
|
11
|
-
class TestShards < TestBase
|
12
|
-
|
13
|
-
def setup
|
14
|
-
super
|
15
|
-
delete_all MyShardedModel
|
16
|
-
delete_all MyShardedByFieldModel
|
17
|
-
end
|
18
|
-
|
19
|
-
def teardown
|
20
|
-
super
|
21
|
-
|
22
|
-
end
|
23
|
-
|
24
|
-
# We'll want to shard based on ID's, user decides how many shards and some mapping function will
|
25
|
-
# be used to select the shard.
|
26
|
-
def test_id_sharding
|
27
|
-
|
28
|
-
mm = MyShardedModel.new(:name=>"single")
|
29
|
-
mm.save
|
30
|
-
sleep 1
|
31
|
-
puts 'finding by id'
|
32
|
-
mm2 = MyShardedModel.find(mm.id)
|
33
|
-
p mm2
|
34
|
-
assert_equal mm.id, mm2.id
|
35
|
-
puts 'deleting'
|
36
|
-
mm2.delete
|
37
|
-
sleep 1
|
38
|
-
mm3 = MyShardedModel.find(mm.id)
|
39
|
-
assert_nil mm3
|
40
|
-
|
41
|
-
puts "saving 20 now"
|
42
|
-
saved = []
|
43
|
-
20.times do |i|
|
44
|
-
mm = MyShardedModel.new(:name=>"name #{i}")
|
45
|
-
mm.save
|
46
|
-
saved << mm
|
47
|
-
end
|
48
|
-
|
49
|
-
# todo: assert that we're actually sharding
|
50
|
-
|
51
|
-
puts "finding them all"
|
52
|
-
found = []
|
53
|
-
rs = MyShardedModel.find(:all)
|
54
|
-
rs.each do |m|
|
55
|
-
p m
|
56
|
-
found << m
|
57
|
-
end
|
58
|
-
saved.each do |so|
|
59
|
-
assert(found.find { |m1| m1.id == so.id })
|
60
|
-
end
|
61
|
-
|
62
|
-
puts "deleting all of them"
|
63
|
-
found.each do |fo|
|
64
|
-
fo.delete
|
65
|
-
end
|
66
|
-
|
67
|
-
puts "Now ensure that all are deleted"
|
68
|
-
rs = MyShardedModel.find(:all)
|
69
|
-
assert rs.size == 0
|
70
|
-
|
71
|
-
puts "Testing belongs_to sharding"
|
72
|
-
|
73
|
-
|
74
|
-
end
|
75
|
-
|
76
|
-
def test_field_sharding
|
77
|
-
|
78
|
-
states = MyShardedByFieldModel.shards
|
79
|
-
puts "states=" + states.inspect
|
80
|
-
|
81
|
-
mm = MyShardedByFieldModel.new(:name=>"single", :state=>"CA")
|
82
|
-
mm.save
|
83
|
-
sleep 1
|
84
|
-
puts 'finding by id'
|
85
|
-
mm2 = MyShardedByFieldModel.find(mm.id)
|
86
|
-
p mm2
|
87
|
-
assert_equal mm.id, mm2.id
|
88
|
-
puts 'deleting'
|
89
|
-
mm2.delete
|
90
|
-
sleep 1
|
91
|
-
mm3 = MyShardedByFieldModel.find(mm.id)
|
92
|
-
assert_nil mm3
|
93
|
-
|
94
|
-
puts "saving 20 now"
|
95
|
-
saved = []
|
96
|
-
20.times do |i|
|
97
|
-
mm = MyShardedByFieldModel.new(:name=>"name #{i}", :state=>states[i % states.size])
|
98
|
-
mm.save
|
99
|
-
p mm
|
100
|
-
saved << mm
|
101
|
-
end
|
102
|
-
|
103
|
-
sleep 1
|
104
|
-
# todo: assert that we're actually sharding
|
105
|
-
|
106
|
-
puts "finding them all"
|
107
|
-
found = []
|
108
|
-
rs = MyShardedByFieldModel.find(:all)
|
109
|
-
rs.each do |m|
|
110
|
-
p m
|
111
|
-
found << m
|
112
|
-
end
|
113
|
-
saved.each do |so|
|
114
|
-
assert(found.find { |m1| m1.id == so.id })
|
115
|
-
end
|
116
|
-
|
117
|
-
rs = MyShardedByFieldModel.find(:all)
|
118
|
-
rs.each do |m|
|
119
|
-
p m
|
120
|
-
found << m
|
121
|
-
end
|
122
|
-
saved.each do |so|
|
123
|
-
assert(found.find { |m1| m1.id == so.id })
|
124
|
-
end
|
125
|
-
|
126
|
-
# Try to find on a specific known shard
|
127
|
-
selects = SimpleRecord.stats.selects
|
128
|
-
cali_models = MyShardedByFieldModel.find(:all, :shard => "CA")
|
129
|
-
puts 'cali_models=' + cali_models.inspect
|
130
|
-
assert_equal(5, cali_models.size)
|
131
|
-
assert_equal(selects + 1, SimpleRecord.stats.selects)
|
132
|
-
|
133
|
-
puts "deleting all of them"
|
134
|
-
found.each do |fo|
|
135
|
-
fo.delete
|
136
|
-
end
|
137
|
-
sleep 1
|
138
|
-
|
139
|
-
puts "Now ensure that all are deleted"
|
140
|
-
rs = MyShardedByFieldModel.find(:all)
|
141
|
-
assert rs.size == 0
|
142
|
-
end
|
143
|
-
|
144
|
-
def test_time_sharding
|
145
|
-
|
146
|
-
end
|
147
|
-
|
148
|
-
end
|
data/test/test_simple_record.rb
DELETED
@@ -1,693 +0,0 @@
|
|
1
|
-
require 'test/unit'
|
2
|
-
require File.join(File.dirname(__FILE__), "/../lib/simple_record")
|
3
|
-
require File.join(File.dirname(__FILE__), "./test_helpers")
|
4
|
-
require_relative "test_base"
|
5
|
-
require "yaml"
|
6
|
-
require 'aws'
|
7
|
-
require_relative 'my_model'
|
8
|
-
require_relative 'my_child_model'
|
9
|
-
require_relative 'model_with_enc'
|
10
|
-
require_relative 'my_simple_model'
|
11
|
-
|
12
|
-
# Tests for SimpleRecord
|
13
|
-
#
|
14
|
-
|
15
|
-
class TestSimpleRecord < TestBase
|
16
|
-
|
17
|
-
|
18
|
-
def test_save_get
|
19
|
-
mm = MyModel.new
|
20
|
-
mm.name = "Travis"
|
21
|
-
mm.age = 32
|
22
|
-
mm.cool = true
|
23
|
-
mm.save
|
24
|
-
sleep 1
|
25
|
-
|
26
|
-
assert !mm.created.nil?
|
27
|
-
assert !mm.updated.nil?
|
28
|
-
assert !mm.id.nil?
|
29
|
-
assert mm.age == 32
|
30
|
-
assert mm.cool == true
|
31
|
-
assert mm.name == "Travis"
|
32
|
-
|
33
|
-
id = mm.id
|
34
|
-
puts 'id=' + id.to_s
|
35
|
-
# Get the object back
|
36
|
-
mm2 = MyModel.find(id)
|
37
|
-
#puts 'got=' + mm2.name + ' and he/she is ' + mm2.age.to_s + ' years old and he/she is cool? ' + mm2.cool.to_s
|
38
|
-
#puts mm2.cool.class.name
|
39
|
-
assert mm2.id == mm.id
|
40
|
-
assert mm2.age == mm.age
|
41
|
-
assert mm2.cool == mm.cool
|
42
|
-
assert mm2.age == 32
|
43
|
-
assert mm2.cool == true
|
44
|
-
assert mm2.name == "Travis"
|
45
|
-
assert mm2.created.is_a? DateTime
|
46
|
-
|
47
|
-
# test nilification
|
48
|
-
mm2.age = nil
|
49
|
-
mm2.save
|
50
|
-
puts mm2.errors.inspect
|
51
|
-
sleep 1
|
52
|
-
mm2 = MyModel.find(id)
|
53
|
-
puts mm2.inspect
|
54
|
-
assert mm2.age.nil?, "doh, age should be nil, but it's " + mm2.age.inspect
|
55
|
-
end
|
56
|
-
|
57
|
-
|
58
|
-
def test_updates
|
59
|
-
mm = MyModel.new
|
60
|
-
mm.name = "Travis"
|
61
|
-
mm.age = 32
|
62
|
-
mm.cool = true
|
63
|
-
mm.s1 = "Initial value"
|
64
|
-
mm.save
|
65
|
-
id = mm.id
|
66
|
-
sleep 1
|
67
|
-
|
68
|
-
mm = MyModel.find(id)
|
69
|
-
mm.name = "Travis2"
|
70
|
-
mm.age = 10
|
71
|
-
mm.cool = false
|
72
|
-
mm.s1 = "" # test blank string
|
73
|
-
|
74
|
-
puts 'mm=' + mm.inspect
|
75
|
-
mm.save
|
76
|
-
sleep 1
|
77
|
-
|
78
|
-
puts 'mm2=' + mm.inspect
|
79
|
-
|
80
|
-
assert mm.s1 == "", "mm.s1 is not empty string, it is " + mm.s1.inspect
|
81
|
-
|
82
|
-
mm = MyModel.find(id)
|
83
|
-
assert mm.name == "Travis2", "Name was not Travis2, it was #{mm.name}"
|
84
|
-
assert mm.age == 10
|
85
|
-
assert mm.cool == false
|
86
|
-
assert mm.s1 == "", "mm.s1 is not empty string, it is #{mm.s1.inspect}"
|
87
|
-
|
88
|
-
end
|
89
|
-
|
90
|
-
def test_funky_values
|
91
|
-
mm = MyModel.new(:name=>"Funky")
|
92
|
-
mm.s1 = "other/2009-11-10/04/84.eml" # reported here: http://groups.google.com/group/simple-record/browse_thread/thread/3659e82491d03a2c?hl=en
|
93
|
-
assert mm.save
|
94
|
-
assert mm.errors.size == 0
|
95
|
-
|
96
|
-
mm2 = MyModel.find(mm.id)
|
97
|
-
puts 'mm2=' + mm2.inspect
|
98
|
-
|
99
|
-
end
|
100
|
-
|
101
|
-
|
102
|
-
def test_create
|
103
|
-
mm = MyModel.create(:name=>"Travis", :age=>32, :cool=>true)
|
104
|
-
puts 'mm=' + mm.inspect
|
105
|
-
assert !mm.id.nil?
|
106
|
-
end
|
107
|
-
|
108
|
-
def test_bad_query
|
109
|
-
assert_raise Aws::AwsError do
|
110
|
-
mm2 = MyModel.find(:all, :conditions=>["name =4?", "1"])
|
111
|
-
end
|
112
|
-
end
|
113
|
-
|
114
|
-
def test_batch_save
|
115
|
-
items = []
|
116
|
-
mm = MyModel.new
|
117
|
-
mm.name = "Travis"
|
118
|
-
mm.age = 32
|
119
|
-
mm.cool = true
|
120
|
-
items << mm
|
121
|
-
mm = MyModel.new
|
122
|
-
mm.name = "Tritt"
|
123
|
-
mm.age = 44
|
124
|
-
mm.cool = false
|
125
|
-
items << mm
|
126
|
-
MyModel.batch_save(items)
|
127
|
-
sleep 2
|
128
|
-
items.each do |item|
|
129
|
-
puts 'id=' + item.id
|
130
|
-
new_item = MyModel.find(item.id)
|
131
|
-
#puts 'new=' + new_item.inspect
|
132
|
-
assert item.id == new_item.id
|
133
|
-
assert item.name == new_item.name
|
134
|
-
assert item.cool == new_item.cool
|
135
|
-
end
|
136
|
-
end
|
137
|
-
|
138
|
-
# Testing getting the association ID without materializing the obejct
|
139
|
-
def test_get_belongs_to_id
|
140
|
-
mm = MyModel.new
|
141
|
-
mm.name = "Parent"
|
142
|
-
mm.age = 55
|
143
|
-
mm.cool = true
|
144
|
-
mm.save
|
145
|
-
|
146
|
-
child = MyChildModel.new
|
147
|
-
child.name = "Child"
|
148
|
-
child.my_model = mm
|
149
|
-
puts 'c1=' + child.inspect
|
150
|
-
puts 'mmid1=' + child.my_model_id.to_s
|
151
|
-
assert child.my_model_id == mm.id
|
152
|
-
child.save
|
153
|
-
puts 'mmid2=' + child.my_model_id.to_s
|
154
|
-
|
155
|
-
puts "child=" + child.inspect
|
156
|
-
sleep 1
|
157
|
-
|
158
|
-
child = MyChildModel.find(child.id)
|
159
|
-
puts "child find=" + child.inspect
|
160
|
-
puts "child.my_model_id = " + child.my_model_id.to_s
|
161
|
-
assert !child.my_model_id.nil?
|
162
|
-
assert !child.my_model.nil?
|
163
|
-
assert child.my_model_id == mm.id
|
164
|
-
end
|
165
|
-
|
166
|
-
def test_callbacks
|
167
|
-
|
168
|
-
|
169
|
-
mm = MyModel.new
|
170
|
-
assert !mm.save
|
171
|
-
assert mm.errors.count == 1 # name is required
|
172
|
-
|
173
|
-
# test queued callback before_create
|
174
|
-
mm.name = "Travis"
|
175
|
-
assert mm.save
|
176
|
-
sleep 1
|
177
|
-
# now nickname should be set on before_create
|
178
|
-
assert mm.nickname == mm.name
|
179
|
-
|
180
|
-
mm2 = MyModel.find(mm.id)
|
181
|
-
assert_equal mm2.nickname, mm.nickname
|
182
|
-
assert_equal mm2.name, mm.name
|
183
|
-
|
184
|
-
|
185
|
-
end
|
186
|
-
|
187
|
-
def test_dirty
|
188
|
-
mm = MyModel.new
|
189
|
-
mm.name = "Travis"
|
190
|
-
mm.age = 32
|
191
|
-
mm.cool = true
|
192
|
-
mm.save
|
193
|
-
id = mm.id
|
194
|
-
puts 'id=' + id.to_s
|
195
|
-
sleep 1
|
196
|
-
# Get the object back
|
197
|
-
mm2 = MyModel.find(id)
|
198
|
-
puts 'mm2=' + mm2.inspect
|
199
|
-
puts 'got=' + mm2.name.to_s + ' and he/she is ' + mm2.age.to_s + ' years old and he/she is cool? ' + mm2.cool.to_s
|
200
|
-
assert mm2.id == mm.id
|
201
|
-
assert mm2.age == mm.age
|
202
|
-
assert mm2.cool == mm.cool
|
203
|
-
|
204
|
-
mm2.name = "Travis 2"
|
205
|
-
mm2.save(:dirty=>true)
|
206
|
-
|
207
|
-
# todo: how do we assert this? perhaps change a value directly in sdb and see that it doesn't get overwritten.
|
208
|
-
# or check stats and ensure only 1 attribute was put
|
209
|
-
|
210
|
-
# Test to ensure that if an item is not dirty, sdb doesn't get hit
|
211
|
-
puts SimpleRecord.stats.saves.to_s
|
212
|
-
SimpleRecord.stats.clear
|
213
|
-
mm2.save(:dirty=>true)
|
214
|
-
puts SimpleRecord.stats.saves.to_s
|
215
|
-
assert SimpleRecord.stats.saves == 0
|
216
|
-
|
217
|
-
mmc = MyChildModel.new
|
218
|
-
mmc.my_model = mm
|
219
|
-
mmc.x = mm
|
220
|
-
mmc.save
|
221
|
-
|
222
|
-
sleep 1
|
223
|
-
|
224
|
-
mmc2 = MyChildModel.find(mmc.id)
|
225
|
-
assert mmc2.my_model_id == mmc.my_model_id, "mm2.my_model_id=#{mmc2.my_model_id} mmc.my_model_id=#{mmc.my_model_id}"
|
226
|
-
puts 'setting my_model to nil'
|
227
|
-
mmc2.my_model = nil
|
228
|
-
mmc2.x = nil
|
229
|
-
puts 'saving my_model to nil'
|
230
|
-
SimpleRecord.stats.clear
|
231
|
-
assert mmc2.save(:dirty=>true)
|
232
|
-
assert SimpleRecord.stats.saves == 1, "saves is #{SimpleRecord.stats.saves}" # 1 put only for updated, should have a count of attributes saved in stats
|
233
|
-
assert SimpleRecord.stats.deletes == 1, "deletes is #{SimpleRecord.stats.deletes}"
|
234
|
-
assert mmc2.id == mmc.id
|
235
|
-
assert mmc2.my_model_id == nil
|
236
|
-
assert mmc2.my_model == nil, "my_model not nil? #{mmc2.my_model.inspect}"
|
237
|
-
|
238
|
-
sleep 1
|
239
|
-
|
240
|
-
mmc3 = MyChildModel.find(mmc.id)
|
241
|
-
puts "mmc3 1 =" + mmc3.inspect
|
242
|
-
assert mmc3.my_model_id == nil, "my_model_id not nil? #{mmc3.my_model_id.inspect}"
|
243
|
-
assert mmc3.my_model == nil
|
244
|
-
|
245
|
-
mm3 = MyModel.new(:name=>"test")
|
246
|
-
assert mm3.save
|
247
|
-
sleep 1
|
248
|
-
|
249
|
-
mmc3.my_model = mm3
|
250
|
-
assert mmc3.my_model_changed?
|
251
|
-
assert mmc3.save(:dirty=>true)
|
252
|
-
assert mmc3.my_model_id == mm3.id
|
253
|
-
assert mmc3.my_model.id == mm3.id
|
254
|
-
|
255
|
-
sleep 1
|
256
|
-
mmc3 = MyChildModel.find(mmc3.id)
|
257
|
-
puts "mmc3=" + mmc3.inspect
|
258
|
-
assert mmc3.my_model_id == mm3.id, "my_model_id=#{mmc3.my_model_id.inspect} mm3.id=#{mm3.id.inspect}"
|
259
|
-
assert mmc3.my_model.id == mm3.id
|
260
|
-
|
261
|
-
mmc3 = MyChildModel.find(mmc3.id)
|
262
|
-
mmc3.my_model_id = mm2.id
|
263
|
-
assert mmc3.my_model_id == mm2.id
|
264
|
-
assert mmc3.changed?
|
265
|
-
assert mmc3.my_model_changed?
|
266
|
-
assert mmc3.my_model.id == mm2.id
|
267
|
-
|
268
|
-
end
|
269
|
-
|
270
|
-
# http://api.rubyonrails.org/classes/ActiveRecord/Dirty.html#M002136
|
271
|
-
def test_changed
|
272
|
-
mm = MySimpleModel.new
|
273
|
-
mm.name = "Travis"
|
274
|
-
mm.age = 32
|
275
|
-
mm.cool = true
|
276
|
-
mm.save
|
277
|
-
|
278
|
-
puts 'changed?=' + mm.changed?.to_s
|
279
|
-
p mm.changed
|
280
|
-
assert !mm.changed?
|
281
|
-
assert mm.changed.size == 0
|
282
|
-
assert mm.changes.size == 0
|
283
|
-
assert !mm.name_changed?
|
284
|
-
|
285
|
-
mm.name = "Jim"
|
286
|
-
assert mm.changed?
|
287
|
-
assert mm.changed.size == 1
|
288
|
-
assert mm.changed[0] == "name"
|
289
|
-
|
290
|
-
assert mm.changes.size == 1
|
291
|
-
puts 'CHANGES=' + mm.changes.inspect
|
292
|
-
assert mm.changes["name"][0] == "Travis"
|
293
|
-
assert mm.changes["name"][1] == "Jim"
|
294
|
-
|
295
|
-
assert mm.name_changed?
|
296
|
-
assert mm.name_was == "Travis", "was #{mm.name_was}"
|
297
|
-
assert mm.name_change[0] == "Travis"
|
298
|
-
assert mm.name_change[1] == "Jim"
|
299
|
-
|
300
|
-
end
|
301
|
-
|
302
|
-
def test_count
|
303
|
-
|
304
|
-
SimpleRecord.stats.clear
|
305
|
-
|
306
|
-
count = MyModel.find(:count) # select 1
|
307
|
-
assert count > 0
|
308
|
-
|
309
|
-
mms = MyModel.find(:all) # select 2
|
310
|
-
puts 'mms=' + mms.inspect
|
311
|
-
assert mms.size > 0 # select 3
|
312
|
-
puts 'mms=' + mms.inspect
|
313
|
-
assert mms.size == count, "size != count! size=" + mms.size.to_s + " count=" + count.to_s
|
314
|
-
assert SimpleRecord.stats.selects == 3, "should have been 3 select, but was actually #{SimpleRecord.stats.selects}" # count should not have been called twice
|
315
|
-
|
316
|
-
count = MyModel.find(:count, :conditions=>["name=?", "Travis"])
|
317
|
-
assert count > 0
|
318
|
-
|
319
|
-
mms = MyModel.find(:all, :conditions=>["name=?", "Travis"])
|
320
|
-
assert mms.size > 0
|
321
|
-
assert mms.size == count
|
322
|
-
|
323
|
-
end
|
324
|
-
|
325
|
-
def test_attributes_correct
|
326
|
-
# child should contain child class attributes + parent class attributes
|
327
|
-
|
328
|
-
#MyModel.defined_attributes.each do |a|
|
329
|
-
#
|
330
|
-
#end
|
331
|
-
#MyChildModel.defined_attributes.inspect
|
332
|
-
|
333
|
-
end
|
334
|
-
|
335
|
-
def test_results_array
|
336
|
-
mms = MyModel.find(:all) # select 2
|
337
|
-
assert !mms.first.nil?
|
338
|
-
assert !mms.last.nil?
|
339
|
-
assert !mms.empty?
|
340
|
-
assert mms.include?(mms[0])
|
341
|
-
|
342
|
-
assert mms[2, 2].size == 2
|
343
|
-
assert mms[2..5].size == 4
|
344
|
-
assert mms[2...5].size == 3
|
345
|
-
|
346
|
-
end
|
347
|
-
|
348
|
-
def test_random_index
|
349
|
-
create_my_models(120)
|
350
|
-
mms = MyModel.find(:all)
|
351
|
-
o = mms[85]
|
352
|
-
puts 'o=' + o.inspect
|
353
|
-
assert !o.nil?
|
354
|
-
o = mms[111]
|
355
|
-
puts 'o=' + o.inspect
|
356
|
-
assert !o.nil?
|
357
|
-
end
|
358
|
-
|
359
|
-
def test_objects_in_constructor
|
360
|
-
mm = MyModel.new(:name=>"model1")
|
361
|
-
mm.save
|
362
|
-
# my_model should be treated differently since it's a belong_to
|
363
|
-
mcm = MyChildModel.new(:name=>"johnny", :my_model=>mm)
|
364
|
-
mcm.save
|
365
|
-
sleep 1
|
366
|
-
|
367
|
-
assert mcm.my_model != nil
|
368
|
-
|
369
|
-
mcm = MyChildModel.find(mcm.id)
|
370
|
-
puts 'mcm=' + mcm.inspect
|
371
|
-
assert mcm.my_model != nil
|
372
|
-
|
373
|
-
end
|
374
|
-
|
375
|
-
|
376
|
-
def test_nil_attr_deletion
|
377
|
-
mm = MyModel.new
|
378
|
-
mm.name = "Chad"
|
379
|
-
mm.age = 30
|
380
|
-
mm.cool = false
|
381
|
-
mm.save
|
382
|
-
|
383
|
-
sleep 1
|
384
|
-
|
385
|
-
# Should have 1 age attribute
|
386
|
-
sdb_atts = @@sdb.get_attributes('simplerecord_tests_my_models', mm.id, 'age')
|
387
|
-
puts 'sdb_atts=' + sdb_atts.inspect
|
388
|
-
assert sdb_atts[:attributes].size == 1, "hmmm, not size 1: " + sdb_atts[:attributes].size.to_s
|
389
|
-
|
390
|
-
mm.age = nil
|
391
|
-
mm.save
|
392
|
-
sleep 1
|
393
|
-
|
394
|
-
# Should be NIL
|
395
|
-
assert mm.age == nil, "age is #{mm.age}"
|
396
|
-
|
397
|
-
# Should have NO age attributes
|
398
|
-
assert @@sdb.get_attributes('simplerecord_tests_my_models', mm.id, 'age')[:attributes].size == 0
|
399
|
-
end
|
400
|
-
|
401
|
-
def test_null
|
402
|
-
puts Time.now.to_i.to_s
|
403
|
-
TestHelpers.clear_out_my_models
|
404
|
-
|
405
|
-
mm = MyModel.new(:name=>"birthay is null")
|
406
|
-
mm.save
|
407
|
-
mm2 = MyModel.new(:name=>"birthday is not null")
|
408
|
-
mm2.birthday = Time.now
|
409
|
-
mm2.save
|
410
|
-
sleep 2
|
411
|
-
mms = MyModel.find(:all, :conditions=>["birthday is null"])
|
412
|
-
mms.each do |m|
|
413
|
-
puts m.inspect
|
414
|
-
end
|
415
|
-
assert mms.size == 1
|
416
|
-
assert mms[0].id = mm.id
|
417
|
-
mms = MyModel.find(:all, :conditions=>["birthday is not null"])
|
418
|
-
mms.each do |m|
|
419
|
-
puts m.inspect
|
420
|
-
end
|
421
|
-
assert mms.size == 1
|
422
|
-
assert mms[0].id = mm2.id
|
423
|
-
|
424
|
-
end
|
425
|
-
|
426
|
-
# Test to add support for IN
|
427
|
-
def test_in_clause
|
428
|
-
# mms = MyModel.find(:all)
|
429
|
-
|
430
|
-
# mms2 = MyModel.find(:all, :conditions=>["id in ?"])
|
431
|
-
|
432
|
-
end
|
433
|
-
|
434
|
-
def test_base_attributes
|
435
|
-
mm = MyModel.new()
|
436
|
-
mm.name = "test name"
|
437
|
-
mm.base_string = "in base class"
|
438
|
-
mm.save_with_validation!
|
439
|
-
sleep 1
|
440
|
-
|
441
|
-
mm2 = MyModel.find(mm.id)
|
442
|
-
assert mm2.base_string == mm.base_string
|
443
|
-
|
444
|
-
mm2.base_string = "changed base string"
|
445
|
-
mm2.save_with_validation!
|
446
|
-
sleep 1
|
447
|
-
|
448
|
-
mm3 = MyModel.find(mm2.id)
|
449
|
-
assert mm3.base_string == mm2.base_string
|
450
|
-
puts mm3.inspect
|
451
|
-
|
452
|
-
|
453
|
-
end
|
454
|
-
|
455
|
-
def test_dates
|
456
|
-
mm = MyModel.new()
|
457
|
-
mm.name = "test name"
|
458
|
-
mm.date1 = Date.today
|
459
|
-
mm.date2 = Time.now
|
460
|
-
mm.date3 = DateTime.now
|
461
|
-
mm.save
|
462
|
-
|
463
|
-
sleep 1
|
464
|
-
|
465
|
-
mm = MyModel.find(:first, :conditions=>["date1 >= ?", 1.days.ago.to_date])
|
466
|
-
puts 'mm=' + mm.inspect
|
467
|
-
assert mm.is_a? MyModel
|
468
|
-
|
469
|
-
mm = MyModel.find(:first, :conditions=>["date2 >= ?", 1.minutes.ago])
|
470
|
-
puts 'mm=' + mm.inspect
|
471
|
-
assert mm.is_a? MyModel
|
472
|
-
|
473
|
-
mm = MyModel.find(:first, :conditions=>["date3 >= ?", 1.minutes.ago])
|
474
|
-
puts 'mm=' + mm.inspect
|
475
|
-
assert mm.is_a? MyModel
|
476
|
-
|
477
|
-
end
|
478
|
-
|
479
|
-
def test_attr_encrypted
|
480
|
-
require_relative 'model_with_enc'
|
481
|
-
ssn = "123456789"
|
482
|
-
password = "my_password"
|
483
|
-
|
484
|
-
ob = ModelWithEnc.new
|
485
|
-
ob.name = "my name"
|
486
|
-
ob.ssn = ssn
|
487
|
-
ob.password = password
|
488
|
-
puts "ob before save=" + ob.inspect
|
489
|
-
assert ssn == ob.ssn, "#{ssn} != #{ob.ssn} apparently!?"
|
490
|
-
puts "#{ob.password.class.name} ob.password=#{ob.password} password=#{password}"
|
491
|
-
assert password != ob.password # we know this doesn't work right
|
492
|
-
assert ob.password == password, "#{ob.password.class.name} ob.password=#{ob.password} password=#{password}"
|
493
|
-
ob.save
|
494
|
-
|
495
|
-
# try also with constructor, just to be safe
|
496
|
-
ob = ModelWithEnc.create(:ssn=>ssn, :name=>"my name", :password=>password)
|
497
|
-
assert ssn == ob.ssn, "#{ssn} != #{ob.ssn} apparently!?"
|
498
|
-
puts "#{ob.password.class.name} ob.password=#{ob.password} password=#{password}"
|
499
|
-
assert password != ob.password # we know this doesn't work right
|
500
|
-
assert ob.password == password, "#{ob.password.class.name} ob.password=#{ob.password} password=#{password}"
|
501
|
-
puts "ob after save=" + ob.inspect
|
502
|
-
assert ssn == ob.ssn
|
503
|
-
assert ob.password == password, "#{ob.password.class.name} ob.password=#{ob.password} password=#{password}"
|
504
|
-
|
505
|
-
sleep 2
|
506
|
-
|
507
|
-
ob2 = ModelWithEnc.find(ob.id)
|
508
|
-
puts 'ob2=' + ob2.inspect
|
509
|
-
assert ob2.name == ob.name, "#{ob2.name} vs #{ob.name}"
|
510
|
-
assert ob2.ssn == ob.ssn, "#{ob2.ssn} vs #{ob.ssn}"
|
511
|
-
assert ob2.ssn == ssn, "#{ob2.ssn} vs #{ssn}"
|
512
|
-
assert ob2.password == password, "#{ob2.password} vs #{password}"
|
513
|
-
assert ob2.attributes["password"] != password
|
514
|
-
assert ob2.password == ob.password, "#{ob2.password} vs #{ob.password}"
|
515
|
-
|
516
|
-
end
|
517
|
-
|
518
|
-
def test_non_persistent_attributes
|
519
|
-
mm = MyModel.new({:some_np_att=>"word"})
|
520
|
-
mm = MyModel.new({"some_other_np_att"=>"up"})
|
521
|
-
|
522
|
-
end
|
523
|
-
|
524
|
-
def test_atts_using_strings_and_symbols
|
525
|
-
mm = MyModel.new({:name=>"myname"})
|
526
|
-
mm2 = MyModel.new({"name"=>"myname"})
|
527
|
-
assert_equal(mm.name, mm2.name)
|
528
|
-
|
529
|
-
mm.save
|
530
|
-
mm2.save
|
531
|
-
sleep 1
|
532
|
-
|
533
|
-
mm = MyModel.find(mm.id)
|
534
|
-
mm2 = MyModel.find(mm2.id)
|
535
|
-
assert_equal mm.name, mm2.name
|
536
|
-
end
|
537
|
-
|
538
|
-
def test_constructor_using_belongs_to_ids
|
539
|
-
mm = MyModel.new({:name=>"myname"})
|
540
|
-
mm.save
|
541
|
-
sleep 1
|
542
|
-
|
543
|
-
mm2 = MyChildModel.new({"name"=>"myname2", :my_model_id=>mm.id})
|
544
|
-
puts 'mm2=' + mm2.inspect
|
545
|
-
assert_equal mm.id, mm2.my_model_id, "#{mm.id} != #{mm2.my_model_id}"
|
546
|
-
mm3 = mm2.my_model
|
547
|
-
puts 'mm3=' + mm3.inspect
|
548
|
-
assert_equal mm.name, mm3.name
|
549
|
-
|
550
|
-
mm3 = MyChildModel.create(:my_model_id=>mm.id, :name=>"myname3")
|
551
|
-
|
552
|
-
sleep 2
|
553
|
-
mm4 = MyChildModel.find(mm3.id)
|
554
|
-
assert_equal mm4.my_model_id, mm.id
|
555
|
-
assert !mm4.my_model.nil?
|
556
|
-
|
557
|
-
end
|
558
|
-
|
559
|
-
def test_update_attributes
|
560
|
-
mm = MyModel.new({:name=>"myname"})
|
561
|
-
mm.save
|
562
|
-
|
563
|
-
now = Time.now
|
564
|
-
mm.update_attributes(:name=>"name2", :age=>21, "date2"=>now)
|
565
|
-
assert mm.name == "name2", "Name is #{mm.name}"
|
566
|
-
assert mm.age == 21
|
567
|
-
# assert mm.date2.to_time.utc == now.utc, "#{mm.date2.class.name} #{mm.date2.to_time.inspect} != #{now.inspect}"
|
568
|
-
sleep 1
|
569
|
-
|
570
|
-
mm = MyModel.find(mm.id)
|
571
|
-
assert mm.name == "name2", "Name is #{mm.name}"
|
572
|
-
assert mm.age == 21, "Age is not 21, it is #{mm.age}"
|
573
|
-
# assert mm.date2 == now, "Date is not correct, it is #{mm.date2}"
|
574
|
-
end
|
575
|
-
|
576
|
-
def test_explicit_class_name
|
577
|
-
mm = MyModel.new({:name=>"myname"})
|
578
|
-
mm.save
|
579
|
-
sleep 1
|
580
|
-
|
581
|
-
mm2 = MyChildModel.new({"name"=>"myname2"})
|
582
|
-
mm2.x = mm
|
583
|
-
assert mm2.x.id == mm.id
|
584
|
-
mm2.save
|
585
|
-
sleep 1
|
586
|
-
|
587
|
-
mm3 = MyChildModel.find(mm2.id)
|
588
|
-
puts "mm3.x=" + mm3.x.inspect
|
589
|
-
assert mm3.x.id == mm.id
|
590
|
-
end
|
591
|
-
|
592
|
-
def test_storage_format
|
593
|
-
|
594
|
-
mm = MyModel.new({:name=>"myname"})
|
595
|
-
mm.date1 = Time.now
|
596
|
-
mm.date2 = DateTime.now
|
597
|
-
mm.save
|
598
|
-
sleep 1
|
599
|
-
|
600
|
-
raw = @@sdb.get_attributes(MyModel.domain, mm.id)
|
601
|
-
puts "raw=" + raw.inspect
|
602
|
-
assert raw[:attributes]["updated"][0].size == "2010-01-06T16:04:23".size
|
603
|
-
assert raw[:attributes]["date1"][0].size == "2010-01-06T16:04:23".size
|
604
|
-
assert raw[:attributes]["date2"][0].size == "2010-01-06T16:04:23".size
|
605
|
-
|
606
|
-
end
|
607
|
-
|
608
|
-
def test_empty_initialize
|
609
|
-
mm = MyModel.new
|
610
|
-
|
611
|
-
mme = ModelWithEnc.new
|
612
|
-
mme = ModelWithEnc.new(:ssn=>"", :password=>"") # this caused encryptor errors
|
613
|
-
mme = ModelWithEnc.new(:ssn=>nil, :password=>nil)
|
614
|
-
end
|
615
|
-
|
616
|
-
def test_string_ints
|
617
|
-
mm = MyModel.new
|
618
|
-
mm.name = "whatever"
|
619
|
-
mm.age = "1"
|
620
|
-
puts mm.inspect
|
621
|
-
|
622
|
-
mm2 = MyModel.new
|
623
|
-
mm2.name = "whatever2"
|
624
|
-
mm2.age = 1
|
625
|
-
params = {:name=>"scooby", :age=>"123"}
|
626
|
-
mm3 = MyModel.new(params)
|
627
|
-
|
628
|
-
|
629
|
-
assert mm.age == 1, "mm.age=#{mm.age}"
|
630
|
-
assert mm2.age == 1
|
631
|
-
assert mm3.age == 123
|
632
|
-
|
633
|
-
mm.save!
|
634
|
-
mm2.save!
|
635
|
-
mm3.save!
|
636
|
-
sleep 1
|
637
|
-
|
638
|
-
assert mm.age == 1
|
639
|
-
assert mm2.age == 1
|
640
|
-
assert mm3.age == 123
|
641
|
-
|
642
|
-
mmf1 = MyModel.find(mm.id)
|
643
|
-
mmf2 = MyModel.find(mm2.id)
|
644
|
-
mmf3 = MyModel.find(mm3.id)
|
645
|
-
|
646
|
-
assert mmf1.age == 1
|
647
|
-
assert mmf2.age == 1
|
648
|
-
assert mmf3.age == 123
|
649
|
-
|
650
|
-
mmf1.update_attributes({:age=>"456"})
|
651
|
-
|
652
|
-
mmf1.age == 456
|
653
|
-
|
654
|
-
end
|
655
|
-
|
656
|
-
def test_box_usage
|
657
|
-
mm = MyModel.new
|
658
|
-
mm.name = "whatever"
|
659
|
-
mm.age = "1"
|
660
|
-
mm.save
|
661
|
-
sleep 1
|
662
|
-
|
663
|
-
mms = MyModel.all
|
664
|
-
|
665
|
-
assert mms.box_usage && mms.box_usage > 0
|
666
|
-
assert mms.request_id
|
667
|
-
|
668
|
-
end
|
669
|
-
|
670
|
-
def test_multi_value_attributes
|
671
|
-
|
672
|
-
val = ['a', 'b', 'c']
|
673
|
-
val2 = [1, 2, 3]
|
674
|
-
|
675
|
-
mm = MyModel.new
|
676
|
-
mm.name = val
|
677
|
-
mm.age = val2
|
678
|
-
assert_equal val, mm.name
|
679
|
-
assert_equal val2, mm.age
|
680
|
-
mm.save
|
681
|
-
|
682
|
-
sleep 1
|
683
|
-
mm = MyModel.find(mm.id)
|
684
|
-
# Values are not returned in order
|
685
|
-
assert_equal val, mm.name.sort
|
686
|
-
assert_equal val2, mm.age.sort
|
687
|
-
|
688
|
-
|
689
|
-
|
690
|
-
|
691
|
-
end
|
692
|
-
|
693
|
-
end
|