Dynamoid 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,150 @@
1
+ require 'dynamoid/adapter/local'
2
+ require File.expand_path(File.dirname(__FILE__) + '../../../spec_helper')
3
+
4
+ describe Dynamoid::Adapter::Local do
5
+
6
+ unless ENV['ACCESS_KEY'] && ENV['SECRET_KEY']
7
+
8
+ # BatchGetItem
9
+ it 'performs BatchGetItem with singular keys' do
10
+ Dynamoid::Adapter.create_table('table1', :id)
11
+ Dynamoid::Adapter.put_item('table1', {:id => '1', :name => 'Josh'})
12
+ Dynamoid::Adapter.create_table('table2', :id)
13
+ Dynamoid::Adapter.put_item('table2', {:id => '1', :name => 'Justin'})
14
+
15
+ results = Dynamoid::Adapter.batch_get_item('table1' => '1', 'table2' => '1')
16
+ results.size.should == 2
17
+ results['table1'].should include({:name => 'Josh', :id => '1'})
18
+ results['table2'].should include({:name => 'Justin', :id => '1'})
19
+ end
20
+
21
+ it 'performs BatchGetItem with multiple keys' do
22
+ Dynamoid::Adapter.create_table('table1', :id)
23
+ Dynamoid::Adapter.put_item('table1', {:id => '1', :name => 'Josh'})
24
+ Dynamoid::Adapter.put_item('table1', {:id => '2', :name => 'Justin'})
25
+
26
+ results = Dynamoid::Adapter.batch_get_item('table1' => ['1', '2'])
27
+ results.size.should == 1
28
+ results['table1'].should include({:name => 'Josh', :id => '1'})
29
+ results['table1'].should include({:name => 'Justin', :id => '2'})
30
+ end
31
+
32
+ # CreateTable
33
+ it 'performs CreateTable' do
34
+ Dynamoid::Adapter.create_table('Test Table', :id)
35
+
36
+ Dynamoid::Adapter.list_tables.should include 'Test Table'
37
+ end
38
+
39
+ # DeleteItem
40
+ it 'performs DeleteItem' do
41
+ Dynamoid::Adapter.create_table('table1', :id)
42
+ Dynamoid::Adapter.put_item('table1', {:id => '1', :name => 'Josh'})
43
+
44
+ Dynamoid::Adapter.delete_item('table1', '1')
45
+
46
+ Dynamoid::Adapter.data['table1'][:data].should be_empty
47
+ end
48
+
49
+ it 'performs DeleteItem for an item that does not exist' do
50
+ Dynamoid::Adapter.create_table('table1', :id)
51
+
52
+ Dynamoid::Adapter.delete_item('table1', '1')
53
+
54
+ Dynamoid::Adapter.data['table1'][:data].should be_empty
55
+ end
56
+
57
+ # DeleteTable
58
+ it 'performs DeleteTable' do
59
+ Dynamoid::Adapter.create_table('table1', :id)
60
+
61
+ Dynamoid::Adapter.delete_table('table1')
62
+
63
+ Dynamoid::Adapter.data['table1'].should be_nil
64
+ end
65
+
66
+ # DescribeTable
67
+
68
+ # GetItem
69
+ it "performs GetItem for an item that does not exist" do
70
+ Dynamoid::Adapter.create_table('Test Table', :id)
71
+
72
+ Dynamoid::Adapter.get_item('Test Table', '1').should be_nil
73
+ end
74
+
75
+ it "performs GetItem for an item that does exist" do
76
+ Dynamoid::Adapter.create_table('Test Table', :id)
77
+ Dynamoid::Adapter.put_item('Test Table', {:id => '1', :name => 'Josh'})
78
+
79
+ Dynamoid::Adapter.get_item('Test Table', '1').should == {:id => '1', :name => 'Josh'}
80
+ end
81
+
82
+ # ListTables
83
+ it 'performs ListTables' do
84
+ Dynamoid::Adapter.create_table('Table1', :id)
85
+ Dynamoid::Adapter.create_table('Table2', :id)
86
+
87
+ Dynamoid::Adapter.list_tables.should include 'Table1'
88
+ Dynamoid::Adapter.list_tables.should include 'Table2'
89
+ end
90
+
91
+ # PutItem
92
+ it 'performs PutItem for an item that does not exist' do
93
+ Dynamoid::Adapter.create_table('Test Table', :id)
94
+ Dynamoid::Adapter.put_item('Test Table', {:id => '1', :name => 'Josh'})
95
+
96
+ Dynamoid::Adapter.data['Test Table'].should == { :id => :id, :data => { '1' => { :id=> '1', :name=>"Josh" }}}
97
+ end
98
+
99
+ # Query
100
+ it 'performs query on a table and returns items' do
101
+ Dynamoid::Adapter.create_table('Test Table', :id)
102
+ Dynamoid::Adapter.put_item('Test Table', {:id => '1', :name => 'Josh'})
103
+
104
+ Dynamoid::Adapter.query('Test Table', '1').should == { :id=> '1', :name=>"Josh" }
105
+ end
106
+
107
+ it 'performs query on a table and returns items if there are multiple items' do
108
+ Dynamoid::Adapter.create_table('Test Table', :id)
109
+ Dynamoid::Adapter.put_item('Test Table', {:id => '1', :name => 'Josh'})
110
+ Dynamoid::Adapter.put_item('Test Table', {:id => '2', :name => 'Justin'})
111
+
112
+ Dynamoid::Adapter.query('Test Table', '1').should == { :id=> '1', :name=>"Josh" }
113
+ end
114
+
115
+ # Scan
116
+ it 'performs scan on a table and returns items' do
117
+ Dynamoid::Adapter.create_table('Test Table', :id)
118
+ Dynamoid::Adapter.put_item('Test Table', {:id => '1', :name => 'Josh'})
119
+
120
+ Dynamoid::Adapter.scan('Test Table', :name => 'Josh').should == [{ :id=> '1', :name=>"Josh" }]
121
+ end
122
+
123
+ it 'performs scan on a table and returns items if there are multiple items but only one match' do
124
+ Dynamoid::Adapter.create_table('Test Table', :id)
125
+ Dynamoid::Adapter.put_item('Test Table', {:id => '1', :name => 'Josh'})
126
+ Dynamoid::Adapter.put_item('Test Table', {:id => '2', :name => 'Justin'})
127
+
128
+ Dynamoid::Adapter.scan('Test Table', :name => 'Josh').should == [{ :id=> '1', :name=>"Josh" }]
129
+ end
130
+
131
+ it 'performs scan on a table and returns multiple items if there are multiple matches' do
132
+ Dynamoid::Adapter.create_table('Test Table', :id)
133
+ Dynamoid::Adapter.put_item('Test Table', {:id => '1', :name => 'Josh'})
134
+ Dynamoid::Adapter.put_item('Test Table', {:id => '2', :name => 'Josh'})
135
+
136
+ Dynamoid::Adapter.scan('Test Table', :name => 'Josh').should == [{ :id=> '1', :name=>"Josh" }, { :id=> '2', :name=>"Josh" }]
137
+ end
138
+
139
+ # UpdateItem
140
+
141
+ # UpdateTable
142
+
143
+ protected
144
+
145
+ def setup_value(table, key, value)
146
+ Dynamoid::Adapter.data[table][key] = value
147
+ end
148
+
149
+ end
150
+ end
@@ -0,0 +1,13 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe "Dynamoid::Adapter" do
4
+
5
+ it 'extends itself automatically' do
6
+ lambda {Dynamoid::Adapter.list_tables}.should_not raise_error
7
+ end
8
+
9
+ it 'raises nomethod if we try a method that is not on the child' do
10
+ lambda {Dynamoid::Adapter.foobar}.should raise_error
11
+ end
12
+
13
+ end
@@ -0,0 +1,31 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe "Dynamoid::Attributes" do
4
+
5
+ before do
6
+ @address = Address.new
7
+ end
8
+
9
+ it 'should write an attribute correctly' do
10
+ @address.write_attribute(:city, 'Chicago')
11
+ end
12
+
13
+ it 'should write an attribute with the alias' do
14
+ @address[:city] = 'Chicago'
15
+ end
16
+
17
+ it 'should read a written attribute' do
18
+ @address.write_attribute(:city, 'Chicago')
19
+ @address.read_attribute(:city).should == 'Chicago'
20
+ end
21
+
22
+ it 'should read a written attribute with the alias' do
23
+ @address.write_attribute(:city, 'Chicago')
24
+ @address[:city].should == 'Chicago'
25
+ end
26
+
27
+ it 'returns all attributes' do
28
+ Address.attributes.should == [:id, :city]
29
+ end
30
+
31
+ end
@@ -0,0 +1,38 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe "Dynamoid::Document" do
4
+
5
+ it 'initializes a new document' do
6
+ @address = Address.new
7
+
8
+ @address.new_record.should be_true
9
+ @address.attributes.should == {:id => nil, :city => nil}
10
+ end
11
+
12
+ it 'initializes a new document with attributes' do
13
+ @address = Address.new(:city => 'Chicago')
14
+
15
+ @address.new_record.should be_true
16
+
17
+ @address.attributes.should == {:id => nil, :city => 'Chicago'}
18
+ end
19
+
20
+ it 'creates a new document' do
21
+ @address = Address.create(:city => 'Chicago')
22
+
23
+ @address.new_record.should be_false
24
+ @address.id.should_not be_nil
25
+ end
26
+
27
+ it 'tests equivalency with itself' do
28
+ @addres.should == @address
29
+ end
30
+
31
+ it 'is not equivalent to another document' do
32
+ @address.should_not == Address.create
33
+ end
34
+
35
+ it 'is not equivalent to another object' do
36
+ @address.should_not == "test"
37
+ end
38
+ end
@@ -0,0 +1,26 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe "Dynamoid::Fields" do
4
+
5
+ before do
6
+ @address = Address.new
7
+ end
8
+
9
+ it 'declares read attributes' do
10
+ @address.city.should be_nil
11
+ end
12
+
13
+ it 'declares write attributes' do
14
+ @address.city = 'Chicago'
15
+ @address.city.should == 'Chicago'
16
+ end
17
+
18
+ it 'declares a query attribute' do
19
+ @address.city?.should be_false
20
+
21
+ @address.city = 'Chicago'
22
+
23
+ @address.city?.should be_true
24
+ end
25
+
26
+ end
@@ -0,0 +1,110 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe "Dynamoid::Finders" do
4
+
5
+ before do
6
+ @address = Address.create(:city => 'Chicago')
7
+ end
8
+
9
+ it 'finds an existing address' do
10
+ found = Address.find(@address.id)
11
+
12
+ found.should == @address
13
+ found.city.should == 'Chicago'
14
+ end
15
+
16
+ it 'is not a new object' do
17
+ found = Address.find(@address.id)
18
+
19
+ found.new_record.should_not be_true
20
+ end
21
+
22
+ it 'finds multiple ids' do
23
+ @address2 = Address.create(:city => 'Illinois')
24
+
25
+ Address.find(@address.id, @address2.id).should include @address, @address2
26
+ end
27
+
28
+ context 'with users' do
29
+ before do
30
+ User.create_indexes
31
+ end
32
+
33
+ it 'finds using method_missing for attributes' do
34
+ array = Address.find_by_city('Chicago')
35
+
36
+ array.should == @address
37
+ end
38
+
39
+ it 'finds using method_missing for multiple attributes' do
40
+ @user = User.create(:name => 'Josh', :email => 'josh@joshsymonds.com')
41
+
42
+ array = User.find_all_by_name_and_email('Josh', 'josh@joshsymonds.com')
43
+
44
+ array.should == [@user]
45
+ end
46
+
47
+ it 'finds using method_missing for single attributes and multiple results' do
48
+ @user1 = User.create(:name => 'Josh', :email => 'josh@joshsymonds.com')
49
+ @user2 = User.create(:name => 'Josh', :email => 'josh@joshsymonds.com')
50
+
51
+ array = User.find_all_by_name('Josh')
52
+
53
+ array.size.should == 2
54
+ array.should include @user1
55
+ array.should include @user2
56
+ end
57
+
58
+ it 'finds using method_missing for multiple attributes and multiple results' do
59
+ @user1 = User.create(:name => 'Josh', :email => 'josh@joshsymonds.com')
60
+ @user2 = User.create(:name => 'Josh', :email => 'josh@joshsymonds.com')
61
+
62
+ array = User.find_all_by_name_and_email('Josh', 'josh@joshsymonds.com')
63
+
64
+ array.size.should == 2
65
+ array.should include @user1
66
+ array.should include @user2
67
+ end
68
+
69
+ it 'finds using method_missing for multiple attributes and no results' do
70
+ @user1 = User.create(:name => 'Josh', :email => 'josh@joshsymonds.com')
71
+ @user2 = User.create(:name => 'Justin', :email => 'justin@joshsymonds.com')
72
+
73
+ array = User.find_all_by_name_and_email('Gaga','josh@joshsymonds.com')
74
+
75
+ array.should be_empty
76
+ end
77
+
78
+ it 'finds using method_missing for a single attribute and no results' do
79
+ @user1 = User.create(:name => 'Josh', :email => 'josh@joshsymonds.com')
80
+ @user2 = User.create(:name => 'Justin', :email => 'justin@joshsymonds.com')
81
+
82
+ array = User.find_all_by_name('Gaga')
83
+
84
+ array.should be_empty
85
+ end
86
+
87
+ it 'should find on a query that is not indexed' do
88
+ @user = User.create(:password => 'Test')
89
+
90
+ array = User.find_all_by_password('Test')
91
+
92
+ array.should == [@user]
93
+ end
94
+
95
+ it 'should find on a query on multiple attributes that are not indexed' do
96
+ @user = User.create(:password => 'Test', :name => 'Josh')
97
+
98
+ array = User.find_all_by_password_and_name('Test', 'Josh')
99
+
100
+ array.should == [@user]
101
+ end
102
+
103
+ it 'should return an empty array when fields exist but nothing is found' do
104
+ array = User.find_all_by_password('Test')
105
+
106
+ array.should be_empty
107
+ end
108
+
109
+ end
110
+ end
@@ -0,0 +1,54 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe "Dynamoid::Indexes" do
4
+
5
+ before do
6
+ User.create_indexes
7
+ end
8
+
9
+ it 'raises an error if a field does not exist' do
10
+ lambda {User.send(:index, :test)}.should raise_error(Dynamoid::Errors::InvalidField)
11
+ end
12
+
13
+ it 'adds indexes to the index array' do
14
+ User.indexes.should == [[:name], [:email], [:email, :name]]
15
+ end
16
+
17
+ it 'reorders index names alphabetically' do
18
+ User.indexes.last.should == [:email, :name]
19
+ end
20
+
21
+ it 'creates a name for a table index' do
22
+ User.index_table_name([:email, :name]).should == 'dynamoid_tests_index_user_emails_and_names'
23
+ end
24
+
25
+ it 'creates a key name for a table index' do
26
+ User.index_key_name([:email, :name]).should == 'user_emails_and_names'
27
+ end
28
+
29
+ it 'creates a table after an index is created' do
30
+ User.send(:index, :password)
31
+
32
+ User.table_exists?(User.index_table_name([:password])).should be_true
33
+ end
34
+
35
+ it 'assembles a hashed value for the key of an index' do
36
+ @user = User.create(:name => 'Josh', :email => 'josh@joshsymonds.com')
37
+
38
+ @user.key_for_index([:email, :name]).should == (Digest::SHA2.new << 'Josh' << 'josh@joshsymonds.com').to_s
39
+ end
40
+
41
+ it 'saves indexes to their tables' do
42
+ @user = User.create(:name => 'Josh')
43
+
44
+ Dynamoid::Adapter.get_item("dynamoid_tests_index_user_names", @user.key_for_index([:name])).should == {@user.class.index_key_name([:name]).to_sym => @user.key_for_index([:name]), :ids => Set[@user.id]}
45
+ end
46
+
47
+ it 'saves multiple indexes with the same value as an array' do
48
+ @user1 = User.create(:name => 'Josh')
49
+ @user2 = User.create(:name => 'Josh')
50
+
51
+ Dynamoid::Adapter.get_item("dynamoid_tests_index_user_names", @user1.key_for_index([:name])).should == {@user1.class.index_key_name([:name]).to_sym => @user1.key_for_index([:name]), :ids => Set[@user2.id, @user1.id]}
52
+ end
53
+
54
+ end
@@ -0,0 +1,55 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe "Dynamoid::Persistence" do
4
+
5
+ before do
6
+ @address = Address.new
7
+ end
8
+
9
+ context 'without AWS keys' do
10
+ unless ENV['ACCESS_KEY'] && ENV['SECRET_KEY']
11
+ before do
12
+ Dynamoid::Adapter.delete_table(Address.table_name) if Dynamoid::Adapter.list_tables.include?(Address.table_name)
13
+ end
14
+
15
+ it 'creates a table' do
16
+ Address.create_table(Address.table_name)
17
+
18
+ Dynamoid::Adapter.list_tables.should include 'dynamoid_tests_addresses'
19
+ end
20
+
21
+ it 'checks if a table already exists' do
22
+ Address.create_table(Address.table_name)
23
+
24
+ Address.table_exists?(Address.table_name).should be_true
25
+ Address.table_exists?('crazytable').should be_false
26
+ end
27
+ end
28
+ end
29
+
30
+
31
+ it 'assigns itself an id on save' do
32
+ @address.save
33
+
34
+ Dynamoid::Adapter.get_item("dynamoid_tests_addresses", @address.id)[:id].should == @address.id
35
+ end
36
+
37
+ it 'assigns itself an id on save only if it does not have one' do
38
+ @address.id = 'test123'
39
+ @address.save
40
+
41
+ Dynamoid::Adapter.get_item("dynamoid_tests_addresses", 'test123').should_not be_empty
42
+ end
43
+
44
+ it 'has a table name' do
45
+ Address.table_name.should == 'dynamoid_tests_addresses'
46
+ end
47
+
48
+ it 'saves indexes along with itself' do
49
+ @user = User.new(:name => 'Josh')
50
+
51
+ @user.expects(:save_indexes).once.returns(true)
52
+ @user.save
53
+ end
54
+
55
+ end