activerecord-simpledb-adapter 0.4.11 → 0.4.12

Sign up to get free protection for your applications and to get access to all the features.
@@ -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,36 @@
1
+ require 'spec_helper'
2
+
3
+ describe "SimpleDBAdapter ActiveRecord default values for 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
+ class ModelWithDefaults < ActiveRecord::Base
16
+ columns_definition do |c|
17
+ c.string :p1, :default => 'payload_string1'
18
+ c.string :p2, :default => lambda { "payload_string2" }
19
+ c.integer :p3, :default => 10
20
+ c.float :p4, :default => 10.05
21
+ c.boolean :p5, :default => true
22
+ c.string :p6
23
+ end
24
+ end
25
+
26
+ it "should correct sets after create new instance of model" do
27
+ m = ModelWithDefaults.new
28
+ m.p1.should == "payload_string1"
29
+ m.p2.should == "payload_string2"
30
+ m.p3.should == 10
31
+ m.p4.should == 10.05
32
+ m.p5.should be_true
33
+ m.p6.should be_nil
34
+ end
35
+
36
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+ describe "SimpleDBAdapter ActiveRecord column with json type" do
3
+ class RecordWithJson < ActiveRecord::Base
4
+ columns_definition do |t|
5
+ t.json :foo
6
+ end
7
+ end
8
+
9
+ before :each do
10
+ ActiveRecord::Base.logger = Logger.new(STDOUT)
11
+ ActiveRecord::Base.establish_connection($config)
12
+ ActiveRecord::Base.connection.create_domain($config[:domain_name])
13
+ end
14
+
15
+ after :each do
16
+ ActiveRecord::Base.connection.delete_domain($config[:domain_name])
17
+ end
18
+
19
+ def check_value val
20
+ t = RecordWithJson.create!(:foo => val)
21
+ t.reload
22
+ t.foo.should == val
23
+ end
24
+
25
+ it "should correct convert/unconvert nil, string, int, float, array, hash values" do
26
+ [nil, 'test', 123, 123.01, [1,2], { 'test' => 'value'}].each do |val|
27
+ check_value val
28
+ end
29
+ end
30
+ 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,24 @@
1
+ require 'spec_helper'
2
+ #testes
3
+ describe "SimpleDBAdapter ActiveRecord offset operation" 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
+ 50.times { |i| Person.create!({:login => "login#{i}"}) }
10
+ end
11
+
12
+ after :each do
13
+ ActiveRecord::Base.connection.delete_domain($config[:domain_name])
14
+ end
15
+
16
+ it "should work" do
17
+ count, offset = 10, 20
18
+ persons = Person.limit(count).offset(offset).all
19
+ persons.count.should == count
20
+ persons.each_with_index do |person, i|
21
+ person.login.should == "login#{i + offset}"
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,111 @@
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
+ it "should update attributes with update_attributes" do
107
+ p1 = Person.create_valid
108
+ p1.update_attributes!({:login =>"123"})
109
+ p1.login.should == "123"
110
+ end
111
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe "SimpleDBAdapter ActiveRecord types convertation" do
4
+ before :each do
5
+ ActiveRecord::Base.logger = Logger.new(STDOUT)
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
+
14
+ it "should correct represent nil values for all types of columns" do
15
+ p = Person.create!
16
+ Person.valid_params.each do |name, value|
17
+ p.try(name).should be_nil
18
+ p.try(name).class.should == NilClass
19
+ end
20
+ end
21
+
22
+ it "should correct convert types from sdb to local" do
23
+ p = Person.create!(Person.valid_params)
24
+ p1 = Person.find p.id
25
+ Person.valid_params.each do |name, value|
26
+ p.try(name).should == p1.try(name)
27
+ p.try(name).should.class == p1.try(name).class
28
+ end
29
+ end
30
+ 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
@@ -0,0 +1 @@
1
+ Person.create!(:login => "admin")
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+ require 'rails'
3
+ require 'rails/generators'
4
+ require 'generators/active_record/model/model_generator'
5
+ require 'genspec'
6
+ describe :model do
7
+ context "with a name argument" do
8
+ with_args "foo", "name:string", "year:integer", "--orm=active_record"
9
+
10
+ it "should generate a model called 'foo' with two columns" do
11
+ subject.should generate("app/models/foo.rb") { |content|
12
+ content.should =~ /class Foo < ActiveRecord\:\:Base/
13
+ content.should =~ /columns_definition do \|c\|\n c.string :name\n c.integer :year\n end/
14
+ }
15
+ end
16
+ end
17
+ end