appoxy-simple_record 1.1.2 → 1.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/lib/results_array.rb CHANGED
@@ -22,10 +22,12 @@ module SimpleRecord
22
22
  end
23
23
 
24
24
  def size
25
+ # puts 'SIZE count=' + @count.inspect
25
26
  return @count if @count
26
27
  params_for_count = params.dup
27
28
  params_for_count[0] = :count
28
- @count = clz.find(*params)
29
+ @count = clz.find(*params_for_count)
30
+ # puts '@count=' + @count.to_s
29
31
  @count
30
32
  end
31
33
 
data/lib/simple_record.rb CHANGED
@@ -25,11 +25,17 @@
25
25
 
26
26
  require 'right_aws'
27
27
  require 'sdb/active_sdb'
28
- #require 'results_array'
28
+ #require 'results_array' # why the heck isn't this picking up???
29
29
  require File.expand_path(File.dirname(__FILE__) + "/results_array")
30
+ require File.expand_path(File.dirname(__FILE__) + "/stats")
30
31
 
31
32
  module SimpleRecord
32
33
 
34
+ @@stats = SimpleRecord::Stats.new
35
+
36
+ def self.stats
37
+ @@stats
38
+ end
33
39
 
34
40
  # Create a new handle to an Sdb account. All handles share the same per process or per thread
35
41
  # HTTP connection to Amazon Sdb. Each handle is for a specific account.
@@ -75,6 +81,8 @@ module SimpleRecord
75
81
  include SimpleRecord::Callbacks
76
82
 
77
83
 
84
+
85
+
78
86
  # todo: move into Callbacks module
79
87
  #this bit of code creates a "run_blank" function for everything value in the @@callbacks array.
80
88
  #this function can then be inserted in the appropriate place in the save, new, destroy, etc overrides
@@ -864,6 +872,7 @@ This is done on getters now
864
872
  results = q_type == :all ? [] : nil
865
873
  begin
866
874
  results=super(*params)
875
+ SimpleRecord.stats.selects += 1
867
876
  if q_type != :count
868
877
  cache_results(results)
869
878
  if results.is_a?(Array)
data/lib/stats.rb ADDED
@@ -0,0 +1,10 @@
1
+ module SimpleRecord
2
+ class Stats
3
+ attr_accessor :selects, :puts
4
+
5
+ def clear
6
+ self.selects = 0
7
+ self.puts = 0
8
+ end
9
+ end
10
+ end
@@ -12,6 +12,7 @@ class TestSimpleRecord < Test::Unit::TestCase
12
12
  #puts @config.inspect
13
13
  SimpleRecord.establish_connection(@config['amazon']['access_key'], @config['amazon']['secret_key'], :port=>80, :protocol=>"http")
14
14
  SimpleRecord::Base.set_domain_prefix("simplerecord_tests_")
15
+ SimpleRecord.stats.clear
15
16
  end
16
17
 
17
18
  def teardown
@@ -19,159 +20,22 @@ class TestSimpleRecord < Test::Unit::TestCase
19
20
  end
20
21
 
21
22
 
22
- def test_save_get
23
- mm = MyModel.new
24
- mm.name = "Travis"
25
- mm.age = 32
26
- mm.cool = true
27
- mm.save
28
-
29
- assert !mm.created.nil?
30
- assert !mm.updated.nil?
31
- assert !mm.id.nil?
32
- assert mm.age == 32
33
- assert mm.cool = true
34
- assert mm.name = "Travis"
35
-
36
- id = mm.id
37
- puts 'id=' + id.to_s
38
- # Get the object back
39
- mm2 = MyModel.find(id)
40
- #puts 'got=' + mm2.name + ' and he/she is ' + mm2.age.to_s + ' years old and he/she is cool? ' + mm2.cool.to_s
41
- #puts mm2.cool.class.name
42
- assert mm2.id == mm.id
43
- assert mm2.age == mm.age
44
- assert mm2.cool == mm.cool
45
- assert mm2.age == 32
46
- assert mm2.cool = true
47
- assert mm2.name = "Travis"
48
- assert mm2.created.is_a? DateTime
49
- end
50
-
51
- def test_bad_query
52
- assert_raise RightAws::AwsError do
53
- mm2 = MyModel.find(:all, :conditions=>["name =4?", "1"])
54
- end
55
- end
56
-
57
- def test_batch_save
58
- items = []
59
- mm = MyModel.new
60
- mm.name = "Travis"
61
- mm.age = 32
62
- mm.cool = true
63
- items << mm
64
- mm = MyModel.new
65
- mm.name = "Tritt"
66
- mm.age = 44
67
- mm.cool = false
68
- items << mm
69
- MyModel.batch_save(items)
70
- items.each do |item|
71
- puts 'id=' + item.id
72
- new_item = MyModel.find(item.id)
73
- puts 'new=' + new_item.inspect
74
- assert item.id == new_item.id
75
- assert item.name == new_item.name
76
- assert item.cool == new_item.cool
77
- end
78
- end
79
-
80
- # Testing getting the association ID without materializing the obejct
81
- def test_get_belongs_to_id
82
- mm = MyModel.new
83
- mm.name = "Parent"
84
- mm.age = 55
85
- mm.cool = true
86
- mm.save
87
-
88
- child = MyChildModel.new
89
- child.name = "Child"
90
- child.my_model = mm
91
- child.save
92
-
93
- child = MyChildModel.find(child.id)
94
- puts child.my_model_id
95
- assert child.my_model_id == mm.id
96
- end
97
-
98
- def test_callbacks
99
-
100
-
101
- mm = MyModel.new
102
- assert !mm.save
103
- assert mm.errors.count == 1 # name is required
104
-
105
- # test queued callback before_create
106
- mm.name = "Travis"
107
- assert mm.save
108
- # now nickname should be set on before_create
109
- assert mm.nickname == mm.name
110
-
111
- mm2 = MyModel.find(mm.id)
112
- assert mm2.nickname = mm.nickname
113
- assert mm2.name = mm.name
114
-
115
-
116
-
117
- end
118
-
119
- def test_dirty
120
- mm = MyModel.new
121
- mm.name = "Travis"
122
- mm.age = 32
123
- mm.cool = true
124
- mm.save
125
- id = mm.id
126
- puts 'id=' + id.to_s
127
- # Get the object back
128
- mm2 = MyModel.find(id)
129
- puts 'got=' + mm2.name + ' and he/she is ' + mm2.age.to_s + ' years old and he/she is cool? ' + mm2.cool.to_s
130
- assert mm2.id == mm.id
131
- assert mm2.age == mm.age
132
- assert mm2.cool == mm.cool
133
-
134
- mm2.name = "Travis 2"
135
- mm2.save(:dirty=>true)
136
-
137
- # todo: how do we assert this?
138
-
139
- end
140
-
141
- # http://api.rubyonrails.org/classes/ActiveRecord/Dirty.html#M002136
142
- def test_changed
143
- mm = MyModel.new
144
- mm.name = "Travis"
145
- mm.age = 32
146
- mm.cool = true
147
- mm.save
148
-
149
- puts 'changed?=' + mm.changed?.to_s
150
- assert !mm.changed?
151
- assert mm.changed.size == 0
152
- assert mm.changes.size == 0
153
- assert !mm.name_changed?
154
-
155
- mm.name = "Jim"
156
- assert mm.changed?
157
- assert mm.changed.size == 1
158
- assert mm.changed[0] == "name"
23
+ def test_count
24
+ count = MyModel.find(:count) # select 1
25
+ assert count > 0
159
26
 
160
- assert mm.changes.size == 1
161
- puts 'CHANGES=' + mm.changes.inspect
162
- assert mm.changes["name"][0] == "Travis"
163
- assert mm.changes["name"][1] == "Jim"
27
+ mms = MyModel.find(:all) # select 2
28
+ assert mms.size > 0 # select 3
29
+ assert mms.size == count # skipped
30
+ assert SimpleRecord.stats.selects == 3, "should have been 3 select, but was actually #{SimpleRecord.stats.selects}" # count should not have been called twice
164
31
 
165
- assert mm.name_changed?
166
- assert mm.name_was == "Travis"
167
- assert mm.name_change[0] == "Travis"
168
- assert mm.name_change[1] == "Jim"
32
+ count = MyModel.find(:count, :conditions=>["name=?", "Travis"])
33
+ assert count > 0
169
34
 
170
- end
35
+ mms = MyModel.find(:all, :conditions=>["name=?", "Travis"])
36
+ assert mms.size > 0
37
+ assert mms.size == count
171
38
 
172
- def test_count
173
- count = MyModel.find(:count)
174
- assert count > 0
175
39
  end
176
40
 
177
41
  def test_attributes_correct
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: appoxy-simple_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ version: 1.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Travis Reeder
@@ -25,6 +25,7 @@ extra_rdoc_files:
25
25
  files:
26
26
  - lib/results_array.rb
27
27
  - lib/simple_record.rb
28
+ - lib/stats.rb
28
29
  - README.markdown
29
30
  has_rdoc: true
30
31
  homepage: http://github.com/appoxy/simple_record/