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/model_with_enc.rb
DELETED
data/test/my_base_model.rb
DELETED
data/test/my_child_model.rb
DELETED
@@ -1,35 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + "/../lib/simple_record")
|
2
|
-
require_relative 'my_model'
|
3
|
-
|
4
|
-
class MyChildModel < SimpleRecord::Base
|
5
|
-
belongs_to :my_model
|
6
|
-
belongs_to :x, :class_name=>"MyModel"
|
7
|
-
has_attributes :name, :child_attr
|
8
|
-
|
9
|
-
end
|
10
|
-
|
11
|
-
|
12
|
-
=begin
|
13
|
-
|
14
|
-
|
15
|
-
puts 'word'
|
16
|
-
|
17
|
-
mm = MyModel.new
|
18
|
-
puts 'word2'
|
19
|
-
|
20
|
-
mcm = MyChildModel.new
|
21
|
-
|
22
|
-
puts 'mcm instance methods=' + MyChildModel.instance_methods(true).inspect
|
23
|
-
#puts 'mcm=' + mcm.instance_methods(false)
|
24
|
-
puts 'mcm class vars = ' + mcm.class.class_variables.inspect
|
25
|
-
puts mcm.class == MyChildModel
|
26
|
-
puts 'saved? ' + mm.save.to_s
|
27
|
-
puts mm.errors.inspect
|
28
|
-
|
29
|
-
puts "mm attributes=" + MyModel.defined_attributes.inspect
|
30
|
-
puts "mcm attributes=" + MyChildModel.defined_attributes.inspect
|
31
|
-
|
32
|
-
mcm2 = MyChildModel.new
|
33
|
-
puts "mcm2 attributes=" + MyChildModel.defined_attributes.inspect
|
34
|
-
|
35
|
-
=end
|
data/test/my_model.rb
DELETED
@@ -1,101 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + "/../lib/simple_record")
|
2
|
-
require_relative 'my_base_model'
|
3
|
-
require_relative 'my_sharded_model'
|
4
|
-
|
5
|
-
class MyModel < MyBaseModel
|
6
|
-
|
7
|
-
has_strings :name, :nickname, :s1, :s2
|
8
|
-
has_ints :age, :save_count
|
9
|
-
has_booleans :cool
|
10
|
-
has_dates :birthday, :date1, :date2, :date3
|
11
|
-
|
12
|
-
# validates_presence_of :name
|
13
|
-
|
14
|
-
# validate :validate
|
15
|
-
# before_create :validate_on_create
|
16
|
-
# before_update :validate_on_update
|
17
|
-
|
18
|
-
belongs_to :my_sharded_model
|
19
|
-
|
20
|
-
has_clobs :clob1, :clob2
|
21
|
-
|
22
|
-
attr_accessor :attr_before_save, :attr_after_save, :attr_before_create, :attr_after_create, :attr_after_update
|
23
|
-
|
24
|
-
#callbacks
|
25
|
-
before_create :set_nickname
|
26
|
-
after_create :after_create
|
27
|
-
|
28
|
-
before_save :before_save
|
29
|
-
|
30
|
-
after_save :after_save
|
31
|
-
after_update :after_update
|
32
|
-
|
33
|
-
def set_nickname
|
34
|
-
puts 'before_create set nickname'
|
35
|
-
@attr_before_create = true
|
36
|
-
self.nickname = name if self.nickname.blank?
|
37
|
-
end
|
38
|
-
|
39
|
-
def before_save
|
40
|
-
puts 'before_save'
|
41
|
-
@attr_before_save = true
|
42
|
-
end
|
43
|
-
|
44
|
-
def after_create
|
45
|
-
puts 'after_create'
|
46
|
-
@attr_after_create = true
|
47
|
-
end
|
48
|
-
|
49
|
-
def after_save
|
50
|
-
puts "after_save"
|
51
|
-
@attr_after_save = true
|
52
|
-
bump_save_count
|
53
|
-
end
|
54
|
-
def after_update
|
55
|
-
puts "after_update"
|
56
|
-
@attr_after_update = true
|
57
|
-
end
|
58
|
-
|
59
|
-
def bump_save_count
|
60
|
-
puts 'after_save bump save_count=' + save_count.to_s
|
61
|
-
if save_count.nil?
|
62
|
-
self.save_count = 1
|
63
|
-
else
|
64
|
-
self.save_count += 1
|
65
|
-
end
|
66
|
-
# puts 'save_count=' + self.save_count.to_s
|
67
|
-
end
|
68
|
-
|
69
|
-
def validate
|
70
|
-
puts 'MyModel.validate'
|
71
|
-
errors.add("name", "can't be empty.") if name.blank?
|
72
|
-
# errors.add("nickname", "can't be empty.") if nickname.blank?
|
73
|
-
end
|
74
|
-
|
75
|
-
def validate_on_create
|
76
|
-
puts 'MyModel.validate_on_create'
|
77
|
-
errors.add("save_count", "should be zero.") if !save_count.blank? && save_count > 0
|
78
|
-
end
|
79
|
-
|
80
|
-
def validate_on_update
|
81
|
-
puts 'MyModel.validate_on_update'
|
82
|
-
# puts 'save_count = ' + save_count.to_s
|
83
|
-
# errors.add("save_count", "should not be zero.") if save_count.blank? || save_count == 0
|
84
|
-
end
|
85
|
-
|
86
|
-
def atts
|
87
|
-
@@attributes
|
88
|
-
end
|
89
|
-
|
90
|
-
|
91
|
-
end
|
92
|
-
|
93
|
-
|
94
|
-
class SingleClobClass < SimpleRecord::Base
|
95
|
-
|
96
|
-
sr_config :single_clob=>true
|
97
|
-
|
98
|
-
has_strings :name
|
99
|
-
|
100
|
-
has_clobs :clob1, :clob2
|
101
|
-
end
|
data/test/my_sharded_model.rb
DELETED
@@ -1,46 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + "/../lib/simple_record")
|
2
|
-
|
3
|
-
require File.expand_path(File.dirname(__FILE__) + "/../lib/simple_record")
|
4
|
-
|
5
|
-
class MyShardedModel < SimpleRecord::Base
|
6
|
-
|
7
|
-
shard :shards=>:my_shards, :map=>:my_mapping_function
|
8
|
-
|
9
|
-
has_strings :name
|
10
|
-
|
11
|
-
def self.num_shards
|
12
|
-
4
|
13
|
-
end
|
14
|
-
|
15
|
-
def self.my_shards
|
16
|
-
Array(0...self.num_shards)
|
17
|
-
end
|
18
|
-
|
19
|
-
def my_mapping_function
|
20
|
-
shard_num = SimpleRecord::Sharding::Hashing.sdbm_hash(self.id) % self.class.num_shards
|
21
|
-
puts "shard_num=" + shard_num.inspect
|
22
|
-
shard_num
|
23
|
-
end
|
24
|
-
|
25
|
-
def self.shard_for_find(id)
|
26
|
-
shard_num = SimpleRecord::Sharding::Hashing.sdbm_hash(id) % self.num_shards
|
27
|
-
end
|
28
|
-
|
29
|
-
end
|
30
|
-
|
31
|
-
|
32
|
-
class MyShardedByFieldModel < SimpleRecord::Base
|
33
|
-
|
34
|
-
shard :shards=>:my_shards, :map=>:my_mapping_function
|
35
|
-
|
36
|
-
has_strings :name, :state
|
37
|
-
|
38
|
-
def self.my_shards
|
39
|
-
['AL', 'CA', 'FL', 'NY']
|
40
|
-
end
|
41
|
-
|
42
|
-
def my_mapping_function
|
43
|
-
state
|
44
|
-
end
|
45
|
-
|
46
|
-
end
|
data/test/my_simple_model.rb
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + "/../lib/simple_record")
|
2
|
-
require_relative 'my_base_model'
|
3
|
-
require_relative 'my_sharded_model'
|
4
|
-
|
5
|
-
class MySimpleModel < SimpleRecord::Base
|
6
|
-
|
7
|
-
has_strings :name, :nickname, :s1, :s2
|
8
|
-
has_ints :age, :save_count
|
9
|
-
has_booleans :cool
|
10
|
-
has_dates :birthday, :date1, :date2, :date3
|
11
|
-
|
12
|
-
|
13
|
-
end
|
data/test/paging_array_test.rb
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
|
2
|
-
require File.expand_path(File.dirname(__FILE__) + "/../lib/results_array")
|
3
|
-
|
4
|
-
array = SimpleRecord::ResultsArray.new()
|
5
|
-
#array.extend(ResultsArray)
|
6
|
-
|
7
|
-
500.times do |i|
|
8
|
-
array << "_ob_" + i.to_s
|
9
|
-
end
|
10
|
-
|
11
|
-
array.each do |v|
|
12
|
-
puts v.to_s
|
13
|
-
end
|
14
|
-
|
15
|
-
puts array[10]
|
data/test/temp.rb
DELETED
data/test/temp_test.rb
DELETED
@@ -1,63 +0,0 @@
|
|
1
|
-
# rubymine won't run 1.9 tests
|
2
|
-
|
3
|
-
require 'minitest/unit'
|
4
|
-
require File.expand_path(File.dirname(__FILE__) + "/../lib/simple_record")
|
5
|
-
require "yaml"
|
6
|
-
require 'aws'
|
7
|
-
require 'my_model'
|
8
|
-
require 'my_child_model'
|
9
|
-
|
10
|
-
|
11
|
-
def setup
|
12
|
-
@config = YAML::load(File.read('test-config.yml'))
|
13
|
-
puts 'akey=' + @config['amazon']['access_key']
|
14
|
-
puts 'skey=' + @config['amazon']['secret_key']
|
15
|
-
SimpleRecord.establish_connection(@config['amazon']['access_key'], @config['amazon']['secret_key'], :port=>80, :protocol=>"http")
|
16
|
-
SimpleRecord::Base.set_domain_prefix("simplerecord_tests_")
|
17
|
-
end
|
18
|
-
|
19
|
-
def teardown
|
20
|
-
SimpleRecord.close_connection()
|
21
|
-
end
|
22
|
-
|
23
|
-
def test_dates
|
24
|
-
mm = MyModel.new
|
25
|
-
mm.name = "Travis"
|
26
|
-
mm.age = 32
|
27
|
-
mm.cool = true
|
28
|
-
mm.created = DateTime.now - 10
|
29
|
-
mm.updated = DateTime.now
|
30
|
-
mm.birthday = Time.now - (3600 * 24 * 365 * 10)
|
31
|
-
puts 'before save=' + mm.inspect
|
32
|
-
mm.save
|
33
|
-
puts 'after save=' + mm.inspect
|
34
|
-
|
35
|
-
mms = MyModel.find(:all, :conditions => ["created > ?", DateTime.now - 1])
|
36
|
-
puts 'mms=' + mms.inspect
|
37
|
-
|
38
|
-
end
|
39
|
-
|
40
|
-
def test_date_comparisons
|
41
|
-
|
42
|
-
t = SimpleRecord::Base.pad_and_offset(Time.now)
|
43
|
-
puts t
|
44
|
-
dt = SimpleRecord::Base.pad_and_offset(DateTime.now)
|
45
|
-
puts dt
|
46
|
-
dt_tomorrow = SimpleRecord::Base.pad_and_offset(DateTime.now + 1)
|
47
|
-
|
48
|
-
|
49
|
-
puts 'compare=' + (t <=> dt).to_s
|
50
|
-
puts 'compare=' + (t <=> dt_tomorrow).to_s
|
51
|
-
|
52
|
-
dts = DateTime.parse(dt_tomorrow)
|
53
|
-
puts dts.to_s
|
54
|
-
ts = Time.parse(dt)
|
55
|
-
puts ts.to_s
|
56
|
-
end
|
57
|
-
|
58
|
-
setup
|
59
|
-
|
60
|
-
#test_dates
|
61
|
-
test_date_comparisons
|
62
|
-
|
63
|
-
teardown
|
data/test/test_base.rb
DELETED
@@ -1,67 +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 "yaml"
|
5
|
-
require 'aws'
|
6
|
-
require_relative 'my_model'
|
7
|
-
require_relative 'my_child_model'
|
8
|
-
require 'active_support'
|
9
|
-
|
10
|
-
class TestBase < Test::Unit::TestCase
|
11
|
-
|
12
|
-
|
13
|
-
def setup
|
14
|
-
reset_connection()
|
15
|
-
|
16
|
-
end
|
17
|
-
|
18
|
-
def teardown
|
19
|
-
SimpleRecord.close_connection
|
20
|
-
end
|
21
|
-
|
22
|
-
def delete_all(clz)
|
23
|
-
puts 'delete_all ' + clz.name
|
24
|
-
obs = clz.find(:all)
|
25
|
-
obs.each do |o|
|
26
|
-
o.delete
|
27
|
-
end
|
28
|
-
# @@sdb.select("select * from #{domain}") do |o|
|
29
|
-
# o.delete
|
30
|
-
# end
|
31
|
-
end
|
32
|
-
|
33
|
-
def reset_connection(options={})
|
34
|
-
puts 'reset_connection'
|
35
|
-
@config = YAML::load(File.open(File.expand_path("~/.test_configs/simple_record.yml")))
|
36
|
-
#puts 'inspecting config = ' + @config.inspect
|
37
|
-
|
38
|
-
SimpleRecord.enable_logging
|
39
|
-
|
40
|
-
SimpleRecord::Base.set_domain_prefix("simplerecord_tests_")
|
41
|
-
SimpleRecord.establish_connection(@config['amazon']['access_key'], @config['amazon']['secret_key'],
|
42
|
-
{:connection_mode=>:single}.merge(options))
|
43
|
-
|
44
|
-
|
45
|
-
# Establish AWS connection directly
|
46
|
-
@@sdb = Aws::SdbInterface.new(@config['amazon']['access_key'], @config['amazon']['secret_key'],
|
47
|
-
{:connection_mode => :single}.merge(options))
|
48
|
-
|
49
|
-
end
|
50
|
-
|
51
|
-
|
52
|
-
# Use to populate db
|
53
|
-
def create_my_models(count)
|
54
|
-
batch = []
|
55
|
-
count.times do |i|
|
56
|
-
mm = MyModel.new(:name=>"model_" + i.to_s)
|
57
|
-
mm.age = i
|
58
|
-
batch << mm
|
59
|
-
end
|
60
|
-
MyModel.batch_save batch
|
61
|
-
|
62
|
-
sleep 2
|
63
|
-
|
64
|
-
end
|
65
|
-
|
66
|
-
|
67
|
-
end
|
data/test/test_dirty.rb
DELETED
@@ -1,79 +0,0 @@
|
|
1
|
-
require 'test/unit'
|
2
|
-
require File.expand_path(File.dirname(__FILE__) + "/../lib/simple_record")
|
3
|
-
require "yaml"
|
4
|
-
require 'aws'
|
5
|
-
require 'my_model'
|
6
|
-
require 'my_child_model'
|
7
|
-
require 'active_support'
|
8
|
-
require 'test_base'
|
9
|
-
|
10
|
-
|
11
|
-
class Person < SimpleRecord::Base
|
12
|
-
has_strings :name, :i_as_s
|
13
|
-
has_ints :age
|
14
|
-
end
|
15
|
-
class DirtyTest < TestBase
|
16
|
-
|
17
|
-
def setup
|
18
|
-
super
|
19
|
-
|
20
|
-
Person.create_domain
|
21
|
-
@person = Person.new(:name => 'old', :age => 70)
|
22
|
-
@person.save
|
23
|
-
|
24
|
-
assert !@person.changed?
|
25
|
-
assert !@person.name_changed?
|
26
|
-
end
|
27
|
-
|
28
|
-
def teardown
|
29
|
-
Person.delete_domain
|
30
|
-
SimpleRecord.close_connection
|
31
|
-
end
|
32
|
-
|
33
|
-
def test_same_value_are_not_dirty
|
34
|
-
@person.name = "old"
|
35
|
-
|
36
|
-
assert !@person.changed?
|
37
|
-
assert !@person.name_changed?
|
38
|
-
|
39
|
-
@person.age = 70
|
40
|
-
puts 'age_change2=' + @person.age_change.inspect
|
41
|
-
assert !@person.changed?
|
42
|
-
assert !@person.age_changed?
|
43
|
-
end
|
44
|
-
|
45
|
-
def test_reverted_changes_are_not_dirty
|
46
|
-
@person.name = "new"
|
47
|
-
assert @person.changed?
|
48
|
-
assert @person.name_changed?
|
49
|
-
|
50
|
-
@person.name = "old"
|
51
|
-
assert !@person.changed?
|
52
|
-
assert !@person.name_changed?
|
53
|
-
|
54
|
-
@person.age = 15
|
55
|
-
assert @person.changed?
|
56
|
-
assert @person.age_changed?
|
57
|
-
|
58
|
-
@person.age = 70
|
59
|
-
puts 'age_change2=' + @person.age_change.inspect
|
60
|
-
assert !@person.changed?
|
61
|
-
assert !@person.age_changed?
|
62
|
-
end
|
63
|
-
|
64
|
-
def test_storing_int_as_string
|
65
|
-
@person.i_as_s = 5
|
66
|
-
assert @person.changed?
|
67
|
-
assert @person.i_as_s_changed?
|
68
|
-
@person.save
|
69
|
-
|
70
|
-
sleep 2
|
71
|
-
|
72
|
-
@person.i_as_s = 5
|
73
|
-
puts 'i_as_s_changed=' + @person.i_as_s_change.inspect
|
74
|
-
# Maybe this should fail? What do we expect this behavior to be?
|
75
|
-
# assert !@person.changed?
|
76
|
-
# assert !@person.i_as_s_changed?
|
77
|
-
|
78
|
-
end
|
79
|
-
end
|