activerecord-simpledb-adapter 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -33,7 +33,7 @@ model example:
33
33
  t.string :login
34
34
  t.integer :year, :limit => 4
35
35
  t.boolean :active
36
- t.string :from, :to => 'from_property'
36
+ t.string :from
37
37
  t.float :price
38
38
  t.integer :lock_version
39
39
 
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{activerecord-simpledb-adapter}
8
- s.version = "0.1.1"
8
+ s.version = "0.1.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Ilia Ablamonov", "Alex Gorkunov", "Cloud Castle Inc."]
@@ -30,7 +30,12 @@ Gem::Specification.new do |s|
30
30
  s.rubygems_version = %q{1.3.7}
31
31
  s.summary = %q{ActiveRecord SimpleDB adapter}
32
32
  s.test_files = [
33
- "spec/activerecord-simpledb-adapter_spec.rb",
33
+ "spec/active_record/connection_adapters/simpledb_adapter_spec.rb",
34
+ "spec/active_record/core_actions_spec.rb",
35
+ "spec/active_record/locking_spec.rb",
36
+ "spec/active_record/record_attributes_spec.rb",
37
+ "spec/active_record/validates_spec.rb",
38
+ "spec/active_record/where_spec.rb",
34
39
  "spec/spec_helper.rb"
35
40
  ]
36
41
 
@@ -58,6 +58,22 @@ class Aws::SdbInterface
58
58
  end
59
59
 
60
60
  module ActiveRecord
61
+
62
+ class SimpleDBLogger
63
+ def initialize(logger)
64
+ @logger = logger
65
+ end
66
+
67
+ def info *args
68
+ #skip noisy info messages from aws interface
69
+ end
70
+
71
+ def method_missing m, *args
72
+ @logger.send(m, args)
73
+ end
74
+
75
+ end
76
+
61
77
  class Base
62
78
  def self.simpledb_connection(config) # :nodoc:
63
79
  require 'aws'
@@ -72,7 +88,7 @@ module ActiveRecord
72
88
  :server => config[:host],
73
89
  :port => config[:port],
74
90
  :protocol => config[:protocol],
75
- :logger => logger
91
+ :logger => SimpleDBLogger.new(logger)
76
92
  },
77
93
  config
78
94
  end
@@ -318,8 +334,8 @@ module ActiveRecord
318
334
  end
319
335
 
320
336
  def execute sql, name = nil, skip_logging = false
321
- p sql
322
- case sql[:action]
337
+ log sql.inspect, "SimpleDB" do
338
+ case sql[:action]
323
339
  when :insert
324
340
  item_name = get_id sql[:attrs]
325
341
  item_name = sql[:attrs][:id] = generate_id unless item_name
@@ -333,6 +349,7 @@ module ActiveRecord
333
349
  @connection.delete_attributes domain_name, item_name, nil, sql[:wheres]
334
350
  else
335
351
  raise "Unsupported action: #{sql[:action].inspect}"
352
+ end
336
353
  end
337
354
  end
338
355
 
@@ -342,30 +359,29 @@ module ActiveRecord
342
359
  alias :create :insert_sql
343
360
 
344
361
  def select sql, name = nil
345
- puts sql
346
- result = []
347
- response = @connection.select(sql, nil, true)
348
- collection_name = get_collection_column_and_name(sql)
349
- columns = columns_definition(collection_name)
350
-
351
- response[:items].each do |item|
352
- item.each do |id, attrs|
353
- ritem = {}
354
- ritem['id'] = id unless id == 'Domain' && attrs['Count'] # unless count(*) result
355
- attrs.each {|k, vs|
356
- column = columns[k]
357
- if column.present?
358
- ritem[column.name] = column.unquote_number(vs.first)
359
- else
360
- ritem[k] = vs.first
361
- end
362
- }
363
- puts ritem.inspect
364
- result << ritem
362
+ log sql, "SimpleDB" do
363
+ result = []
364
+ response = @connection.select(sql, nil, true)
365
+ collection_name = get_collection_column_and_name(sql)
366
+ columns = columns_definition(collection_name)
367
+
368
+ response[:items].each do |item|
369
+ item.each do |id, attrs|
370
+ ritem = {}
371
+ ritem['id'] = id unless id == 'Domain' && attrs['Count'] # unless count(*) result
372
+ attrs.each {|k, vs|
373
+ column = columns[k]
374
+ if column.present?
375
+ ritem[column.name] = column.unquote_number(vs.first)
376
+ else
377
+ ritem[k] = vs.first
378
+ end
379
+ }
380
+ result << ritem
381
+ end
365
382
  end
383
+ result
366
384
  end
367
- # puts "Box usage: #{response[:box_usage].to_f}"
368
- result
369
385
  end
370
386
 
371
387
  # Executes the update statement and returns the number of rows affected.
@@ -414,7 +430,7 @@ module ActiveRecord
414
430
  raise PreparedStatementInvalid, "collection column '#{@@ccn.values.join(" or ")}' not found in the WHERE section in query"
415
431
  end
416
432
  end
417
-
418
433
  end
434
+
419
435
  end
420
436
  end
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ describe "SimpleDBAdapter establish connection" do
4
+
5
+ it "should connect to database" do
6
+ ActiveRecord::Base.establish_connection(CONNECTION_PARAMS)
7
+ ActiveRecord::Base.connection.should_not be_nil
8
+ ActiveRecord::Base.connection.class.should == ActiveRecord::ConnectionAdapters::SimpleDBAdapter
9
+ end
10
+
11
+ it "should be active after connection to database" do
12
+ ActiveRecord::Base.establish_connection(CONNECTION_PARAMS)
13
+ ActiveRecord::Base.connection.should be_active
14
+ end
15
+
16
+ it "should not be active after disconnection to database" do
17
+ ActiveRecord::Base.establish_connection(CONNECTION_PARAMS)
18
+ ActiveRecord::Base.connection.disconnect!
19
+ ActiveRecord::Base.connection.should_not be_active
20
+ end
21
+
22
+ it "should be active after reconnection to database" do
23
+ ActiveRecord::Base.establish_connection(CONNECTION_PARAMS)
24
+ ActiveRecord::Base.connection.reconnect!
25
+ ActiveRecord::Base.connection.should be_active
26
+ end
27
+
28
+ end
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+
3
+ describe "SimpleDBAdapter ActiveRecord core actions" do
4
+
5
+ before :each do
6
+ ActiveRecord::Base.logger = Logger.new(STDOUT)
7
+ ActiveRecord::Base.establish_connection($config)
8
+ ActiveRecord::Base.connection.create_domain($config[:domain_name])
9
+ Person.create!(Person.valid_params)
10
+ end
11
+
12
+ after :each do
13
+ ActiveRecord::Base.connection.delete_domain($config[:domain_name])
14
+ end
15
+
16
+ it "should be correct work with .all statement" do
17
+ Person.all.should_not be_empty
18
+ Person.all.first.is_a?(Person).should be_true
19
+ end
20
+
21
+ it "should be correct work with .first statement" do
22
+ Person.first.is_a?(Person).should be_true
23
+ end
24
+
25
+ it "should be correct work with .last statement" do
26
+ Person.last.is_a?(Person).should be_true
27
+ end
28
+
29
+ it "should be correct work with .count statement" do
30
+ Person.count.should > 0
31
+ end
32
+
33
+ it "should be correct work with .find statement" do
34
+ p1 = Person.create!
35
+ p2 = Person.find p1.id
36
+ p1.should == p2
37
+ end
38
+
39
+ it "should be correct work with .destroy statement" do
40
+ p1 = Person.new
41
+ p1.save
42
+ id = p1.id
43
+ p1.destroy
44
+ p2 = Person.find_by_id id
45
+ p2.should be_nil
46
+ end
47
+ end
@@ -0,0 +1,50 @@
1
+ require 'spec_helper'
2
+
3
+ describe "SimpleDBAdapter ActiveRecord with optimistic locking" do
4
+
5
+ before :each do
6
+ ActiveRecord::Base.logger = Logger.new(STDOUT)
7
+ ActiveRecord::Base.establish_connection($config)
8
+ ActiveRecord::Base.connection.create_domain($config[:domain_name])
9
+ end
10
+
11
+ after :each do
12
+ ActiveRecord::Base.connection.delete_domain($config[:domain_name])
13
+ end
14
+
15
+ it "should be have not nil lock version value after save" do
16
+ p = Person.new
17
+ p.save
18
+ p.reload
19
+ p.lock_version.should_not be_nil
20
+ end
21
+
22
+ it "should change lock version value after sevarals saves" do
23
+ p = Person.new
24
+ p.save
25
+ old_lock_version = p.lock_version
26
+ p.login = "blabla"
27
+ p.save
28
+ p.reload
29
+ p.lock_version.should_not == old_lock_version
30
+ end
31
+
32
+ it "should raise error when tried save model with non-actual values (changed another place and time)" do
33
+ p = Person.new
34
+ p.save
35
+ p1 = Person.find p.id
36
+ p1.login = "blabla"
37
+ p.login = "bla"
38
+ p1.save
39
+ lambda {p.save!}.should raise_error(ActiveRecord::StaleObjectError)
40
+ end
41
+
42
+ it "should raise error when tried destroy item with changed state" do
43
+ p = Person.new
44
+ p.save
45
+ p1 = Person.find p.id
46
+ p1.login = "blabla"
47
+ p1.save
48
+ lambda {p.destroy}.should raise_error(ActiveRecord::StaleObjectError)
49
+ end
50
+ end
@@ -0,0 +1,106 @@
1
+ require 'spec_helper'
2
+
3
+ describe "SimpleDBAdapter ActiveRecord attributes" do
4
+
5
+ before :each do
6
+ ActiveRecord::Base.logger = Logger.new(STDOUT)
7
+ ActiveRecord::Base.establish_connection($config)
8
+ ActiveRecord::Base.connection.create_domain($config[:domain_name])
9
+ end
10
+
11
+ after :each do
12
+ ActiveRecord::Base.connection.delete_domain($config[:domain_name])
13
+ end
14
+
15
+ it "should correct auto-generated properites (id, created_at, updated_at)" do
16
+ p = Person.new
17
+ p.id.should be_nil
18
+ p.created_at.should be_nil
19
+ p.updated_at.should be_nil
20
+ p.save!
21
+ p.id.should_not be_nil
22
+ p.created_at.should_not be_nil
23
+ p.updated_at.should_not be_nil
24
+ end
25
+
26
+ it "should be different ids for different model instances after create" do
27
+ p1 = Person.create!
28
+ p2 = Person.create!
29
+ p1.id.should_not == p2.id
30
+ end
31
+
32
+ it "should correct saved and loaded" do
33
+ p = Person.create!(Person.valid_params)
34
+
35
+ r = Person.find p.id
36
+ Person.valid_params.each do |key, value|
37
+ r.try(key).should == value
38
+ end
39
+ end
40
+
41
+ it "should correct save and load integer values" do
42
+ p = Person.new
43
+ p.year = 2010
44
+ p.save
45
+ p.year = p.year + 1
46
+ p.save
47
+ p.reload
48
+ p.year.is_a?(Fixnum).should be_true
49
+ p.year.should == 2011
50
+ end
51
+
52
+ it "should correct save and load float values" do
53
+ p = Person.new
54
+ p.price = 10.04
55
+ p.save
56
+ p.price = p.price + 1.2
57
+ p.save
58
+ p.reload
59
+ p.price.is_a?(Float).should be_true
60
+ p.price.should == 11.24
61
+ end
62
+
63
+ it "should corrert save and load negative values" do
64
+ p = Person.new
65
+ p.price = -10.04
66
+ p.year = -200
67
+ p.save
68
+ p.price = p.price - 1.02
69
+ p.year = p.year - 2
70
+ p.save
71
+ p.reload
72
+ p.price.should == -11.06
73
+ p.year.should == -202
74
+ end
75
+
76
+ it "should correct save and load boolean values" do
77
+ p = Person.new
78
+ p.active = true
79
+ p.save
80
+ p.reload
81
+ p.active.should be_true
82
+ p.active = false
83
+ p.save
84
+ p.reload
85
+ p.active.should be_false
86
+ end
87
+
88
+ it "should correct save and load string values" do
89
+ p = Person.new
90
+ p.login = "superman"
91
+ p.save
92
+ p.login = p.login + "123"
93
+ p.save
94
+ p.reload
95
+ p.login.should == "superman123"
96
+ end
97
+
98
+ it "should update updated_at property when invoke .touch" do
99
+ p1 = Person.new
100
+ p1.save
101
+ p2 = Person.find p1.id
102
+ p1.touch
103
+ p1.updated_at.class.should == Time
104
+ p1.updated_at.should > p2.updated_at
105
+ end
106
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ describe "SimpleDBAdapter ActiveRecord validates" do
4
+
5
+ before :each do
6
+ ActiveRecord::Base.logger = Logger.new(STDOUT)
7
+ ActiveRecord::Base.establish_connection($config)
8
+ ActiveRecord::Base.connection.create_domain($config[:domain_name])
9
+ end
10
+
11
+ after :each do
12
+ ActiveRecord::Base.connection.delete_domain($config[:domain_name])
13
+ end
14
+
15
+ it "should be correct work with validates presense of" do
16
+ class PersonWithValidates < Person
17
+ validates_presence_of :login
18
+ end
19
+
20
+ lambda {
21
+ PersonWithValidates.create!
22
+ }.should raise_error(ActiveRecord::RecordInvalid, "Validation failed: Login can't be blank")
23
+ end
24
+
25
+ it "should be correct work with validates uniqueness of" do
26
+ class PersonWithValidates < Person
27
+ validates_uniqueness_of :login
28
+ end
29
+
30
+ PersonWithValidates.create!(Person.valid_params)
31
+
32
+ lambda {
33
+ PersonWithValidates.create!(Person.valid_params)
34
+ }.should raise_error(ActiveRecord::RecordInvalid, "Validation failed: Login has already been taken")
35
+ end
36
+ end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ describe "SimpleDBAdapter ActiveRecord where statement" do
4
+
5
+ before :each do
6
+ ActiveRecord::Base.logger = Logger.new(STDOUT)
7
+ ActiveRecord::Base.establish_connection($config)
8
+ ActiveRecord::Base.connection.create_domain($config[:domain_name])
9
+ Person.create!(Person.valid_params)
10
+ end
11
+
12
+ after :each do
13
+ ActiveRecord::Base.connection.delete_domain($config[:domain_name])
14
+ end
15
+
16
+ it "should be return items by conditions with different type values" do
17
+ Person.valid_params.each do |name, value|
18
+ result = Person.where(name => value).first
19
+ result.try(name).should == value
20
+ end
21
+ end
22
+
23
+ it "should be return items by condition with table name prefix" do
24
+ result = Person.where("people.state" => 'paid').first
25
+ result.state.should == 'paid'
26
+ end
27
+ end
@@ -38,7 +38,7 @@ class Person < ActiveRecord::Base
38
38
  t.string :login
39
39
  t.integer :year, :limit => 4
40
40
  t.boolean :active
41
- t.string :from
41
+ t.string :state
42
42
  t.float :price
43
43
  t.integer :lock_version
44
44
 
@@ -51,7 +51,7 @@ class Person < ActiveRecord::Base
51
51
  :year => 2010,
52
52
  :active => true,
53
53
  :price => 10.04,
54
- :from => 'paid'
54
+ :state => 'paid'
55
55
  }
56
56
  end
57
57
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-simpledb-adapter
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 31
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 1
10
- version: 0.1.1
9
+ - 2
10
+ version: 0.1.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ilia Ablamonov
@@ -205,7 +205,12 @@ files:
205
205
  - lib/activerecord-simpledb-adapter.rb
206
206
  - lib/arel/visitors/simpledb.rb
207
207
  - lib/tasks/simpledb.rake
208
- - spec/activerecord-simpledb-adapter_spec.rb
208
+ - spec/active_record/connection_adapters/simpledb_adapter_spec.rb
209
+ - spec/active_record/core_actions_spec.rb
210
+ - spec/active_record/locking_spec.rb
211
+ - spec/active_record/record_attributes_spec.rb
212
+ - spec/active_record/validates_spec.rb
213
+ - spec/active_record/where_spec.rb
209
214
  - spec/spec_helper.rb
210
215
  has_rdoc: true
211
216
  homepage: http://github.com/cloudcastle/activerecord-simpledb-adapter
@@ -242,5 +247,10 @@ signing_key:
242
247
  specification_version: 3
243
248
  summary: ActiveRecord SimpleDB adapter
244
249
  test_files:
245
- - spec/activerecord-simpledb-adapter_spec.rb
250
+ - spec/active_record/connection_adapters/simpledb_adapter_spec.rb
251
+ - spec/active_record/core_actions_spec.rb
252
+ - spec/active_record/locking_spec.rb
253
+ - spec/active_record/record_attributes_spec.rb
254
+ - spec/active_record/validates_spec.rb
255
+ - spec/active_record/where_spec.rb
246
256
  - spec/spec_helper.rb
@@ -1,149 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
-
3
- describe "model with active_record_simple_db" do
4
-
5
- before :each do
6
- ActiveRecord::Base.establish_connection($config)
7
- ActiveRecord::Base.connection.create_domain($config[:domain_name])
8
- end
9
-
10
- after :each do
11
- ActiveRecord::Base.connection.delete_domain($config[:domain_name])
12
- end
13
- #test .new
14
- it "should be without id, created_at and updated_at when model is new" do
15
- p = Person.new
16
- p.id.should be_nil
17
- p.created_at.should be_nil
18
- p.updated_at.should be_nil
19
- end
20
-
21
- #test .create and .save
22
- it "should be with id, created_at and updated_at when saved when new model was saved or created immediately" do
23
- p1 = Person.new
24
- p1.save
25
- p1.id.should_not be_nil
26
- p1.created_at.should_not be_nil
27
- p1.updated_at.should_not be_nil
28
- p2 = Person.create!
29
- p2.id.should_not be_nil
30
- p2.created_at.should_not be_nil
31
- p2.updated_at.should_not be_nil
32
- end
33
-
34
- #test correct saving/loading properties
35
- it "should correct save and load all properties" do
36
- p = Person.create!(Person.valid_params)
37
-
38
- r = Person.find p.id
39
- Person.valid_params.each do |key, value|
40
- r.try(key).should == value
41
- end
42
- end
43
-
44
- #test validates instructions
45
- it "should be correct work with validates instruction" do
46
- class PersonWithValidates < Person
47
- validates_presence_of :login
48
- validates_uniqueness_of :login
49
- end
50
-
51
- lambda {
52
- PersonWithValidates.create!
53
- }.should raise_error(ActiveRecord::RecordInvalid, "Validation failed: Login can't be blank")
54
-
55
- PersonWithValidates.create!(Person.valid_params)
56
-
57
- lambda {
58
- PersonWithValidates.create!(Person.valid_params)
59
- }.should raise_error(ActiveRecord::RecordInvalid, "Validation failed: Login has already been taken")
60
- end
61
-
62
- #test .create
63
- it "should be different ids for different model instances" do
64
- p1 = Person.create!
65
- p2 = Person.create!
66
- p1.id.should_not == p2.id
67
- end
68
-
69
- #test .find
70
- it "should be found entry by id" do
71
- p1 = Person.create!
72
- p2 = Person.find p1.id
73
- p1.should == p2
74
- end
75
-
76
- #test .all
77
- it "should return collection of models when invoke .all" do
78
- p = Person.new
79
- p.save
80
- r = Person.all
81
- r.should_not be_nil
82
- r.should_not be_empty
83
- end
84
-
85
- #test .first
86
- it "should return valid entry when invoke .first" do
87
- p = Person.new
88
- p.save
89
- p1 = Person.first
90
- p1.should_not be_nil
91
- end
92
-
93
- #test .first
94
- it "should return valid entry when invoke .last" do
95
- p = Person.new
96
- p.save
97
- p1 = Person.last
98
- p1.should_not be_nil
99
- end
100
-
101
- #test .touch
102
- it "should update updated_at property when invoke .touch" do
103
- p1 = Person.new
104
- p1.save
105
- p2 = Person.find p1.id
106
- p1.touch
107
- p1.updated_at.should > p2.updated_at
108
- end
109
-
110
- #test .destroy
111
- it "should be destroy entry when invoke destroy" do
112
- p1 = Person.new
113
- p1.save
114
- id = p1.id
115
- p1.destroy
116
- p2 = Person.find_by_id id
117
- p2.should be_nil
118
- end
119
-
120
- #test optimistic locking
121
- it "should be throw exception when invoke destroy twice for same object" do
122
- p1 = Person.new
123
- p1.save
124
- p2 = Person.find p1.id
125
- p1.year = 2008
126
- p1.save
127
- lambda {p2.destroy}.should raise_error(ActiveRecord::StaleObjectError)
128
- end
129
-
130
- #test where selection
131
- it "should be correct work with where statment with equalities" do
132
- p1 = Person.new
133
- p1.year = 2008
134
- p1.save
135
- p2 = Person.new
136
- p2.year = 2010
137
- p2.save
138
- p = Person.where(:year => 2010).all
139
- p.should_not be_empty
140
- end
141
-
142
- it "should be correct work with where statment with \"from\" column" do
143
- p1 = Person.new
144
- p1.from = 'test'
145
- p1.save
146
- p = Person.where(:from => 'test').all
147
- p.should_not be_empty
148
- end
149
- end