dynamoid 0.3.2 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Dynamoid.gemspec +3 -2
- data/README.markdown +39 -3
- data/VERSION +1 -1
- data/lib/dynamoid.rb +2 -0
- data/lib/dynamoid/adapter.rb +9 -8
- data/lib/dynamoid/adapter/aws_sdk.rb +15 -9
- data/lib/dynamoid/adapter/local.rb +39 -14
- data/lib/dynamoid/associations.rb +5 -6
- data/lib/dynamoid/associations/association.rb +23 -1
- data/lib/dynamoid/associations/belongs_to.rb +0 -1
- data/lib/dynamoid/associations/has_and_belongs_to_many.rb +0 -1
- data/lib/dynamoid/associations/has_many.rb +0 -1
- data/lib/dynamoid/associations/many_association.rb +10 -7
- data/lib/dynamoid/associations/single_association.rb +2 -1
- data/lib/dynamoid/components.rb +1 -0
- data/lib/dynamoid/config.rb +1 -0
- data/lib/dynamoid/criteria.rb +2 -2
- data/lib/dynamoid/criteria/chain.rb +118 -43
- data/lib/dynamoid/document.rb +58 -6
- data/lib/dynamoid/fields.rb +18 -3
- data/lib/dynamoid/finders.rb +14 -7
- data/lib/dynamoid/indexes.rb +3 -2
- data/lib/dynamoid/indexes/index.rb +2 -2
- data/lib/dynamoid/persistence.rb +29 -14
- data/spec/app/models/address.rb +4 -0
- data/spec/app/models/camel_case.rb +13 -0
- data/spec/app/models/tweet.rb +9 -0
- data/spec/dynamoid/adapter/aws_sdk_spec.rb +5 -5
- data/spec/dynamoid/adapter/local_spec.rb +79 -79
- data/spec/dynamoid/adapter_spec.rb +6 -6
- data/spec/dynamoid/associations/association_spec.rb +26 -12
- data/spec/dynamoid/criteria/chain_spec.rb +64 -21
- data/spec/dynamoid/criteria_spec.rb +28 -0
- data/spec/dynamoid/document_spec.rb +29 -0
- data/spec/dynamoid/fields_spec.rb +5 -0
- data/spec/dynamoid/finders_spec.rb +6 -1
- data/spec/dynamoid/indexes/index_spec.rb +1 -1
- data/spec/dynamoid/persistence_spec.rb +9 -17
- data/spec/spec_helper.rb +1 -0
- metadata +4 -3
data/lib/dynamoid/indexes.rb
CHANGED
@@ -40,8 +40,9 @@ module Dynamoid #:nodoc:
|
|
40
40
|
# @since 0.2.0
|
41
41
|
def create_indexes
|
42
42
|
self.indexes.each do |name, index|
|
43
|
-
opts = index.
|
44
|
-
|
43
|
+
opts = {:table_name => index.table_name, :id => :id}
|
44
|
+
opts[:range_key] = { :range => :number } if index.range_key?
|
45
|
+
self.create_table(opts)
|
45
46
|
end
|
46
47
|
end
|
47
48
|
end
|
@@ -81,7 +81,7 @@ module Dynamoid #:nodoc:
|
|
81
81
|
self.delete(obj, true)
|
82
82
|
values = values(obj)
|
83
83
|
return true if values[:hash_value].blank? || (!values[:range_value].nil? && values[:range_value].blank?)
|
84
|
-
existing = Dynamoid::Adapter.read(self.table_name, values[:hash_value], values[:range_value])
|
84
|
+
existing = Dynamoid::Adapter.read(self.table_name, values[:hash_value], { :range_key => values[:range_value] })
|
85
85
|
ids = ((existing and existing[:ids]) or Set.new)
|
86
86
|
Dynamoid::Adapter.write(self.table_name, {:id => values[:hash_value], :ids => ids.merge([obj.id]), :range => values[:range_value]})
|
87
87
|
end
|
@@ -93,7 +93,7 @@ module Dynamoid #:nodoc:
|
|
93
93
|
def delete(obj, changed_attributes = false)
|
94
94
|
values = values(obj, changed_attributes)
|
95
95
|
return true if values[:hash_value].blank? || (!values[:range_value].nil? && values[:range_value].blank?)
|
96
|
-
existing = Dynamoid::Adapter.read(self.table_name, values[:hash_value], values[:range_value])
|
96
|
+
existing = Dynamoid::Adapter.read(self.table_name, values[:hash_value], { :range_key => values[:range_value]})
|
97
97
|
return true unless existing && existing[:ids] && existing[:ids].include?(obj.id)
|
98
98
|
Dynamoid::Adapter.write(self.table_name, {:id => values[:hash_value], :ids => (existing[:ids] - Set[obj.id]), :range => values[:range_value]})
|
99
99
|
end
|
data/lib/dynamoid/persistence.rb
CHANGED
@@ -17,14 +17,31 @@ module Dynamoid
|
|
17
17
|
#
|
18
18
|
# @since 0.2.0
|
19
19
|
def table_name
|
20
|
-
"#{Dynamoid::Config.namespace}_#{self.
|
20
|
+
"#{Dynamoid::Config.namespace}_#{options[:name] ? options[:name] : self.name.downcase.pluralize}"
|
21
21
|
end
|
22
22
|
|
23
|
-
# Creates a table
|
23
|
+
# Creates a table.
|
24
24
|
#
|
25
|
-
# @
|
26
|
-
|
27
|
-
|
25
|
+
# @param [Hash] options options to pass for table creation
|
26
|
+
# @option options [Symbol] :id the id field for the table
|
27
|
+
# @option options [Symbol] :table_name the actual name for the table
|
28
|
+
# @option options [Integer] :read_capacity set the read capacity for the table; does not work on existing tables
|
29
|
+
# @option options [Integer] :write_capacity set the write capacity for the table; does not work on existing tables
|
30
|
+
# @option options [Hash] {range_key => :type} a hash of the name of the range key and a symbol of its type
|
31
|
+
#
|
32
|
+
# @since 0.4.0
|
33
|
+
def create_table(options = {})
|
34
|
+
options = {
|
35
|
+
:id => self.hash_key,
|
36
|
+
:table_name => self.table_name,
|
37
|
+
:write_capacity => self.write_capacity,
|
38
|
+
:read_capacity => self.read_capacity,
|
39
|
+
:range_key => self.range_key ? {range_key => attributes[range_key][:type]} : nil
|
40
|
+
}.merge(options)
|
41
|
+
|
42
|
+
return true if table_exists?(options[:table_name])
|
43
|
+
|
44
|
+
Dynamoid::Adapter.tables << options[:table_name] if Dynamoid::Adapter.create_table(options[:table_name], options[:id], options)
|
28
45
|
end
|
29
46
|
|
30
47
|
# Does a table with this name exist?
|
@@ -38,12 +55,12 @@ module Dynamoid
|
|
38
55
|
#
|
39
56
|
# @since 0.2.0
|
40
57
|
def undump(incoming = nil)
|
41
|
-
incoming
|
42
|
-
incoming = incoming.symbolize_keys
|
58
|
+
incoming = (incoming || {}).symbolize_keys
|
43
59
|
Hash.new.tap do |hash|
|
44
60
|
self.attributes.each do |attribute, options|
|
45
61
|
hash[attribute] = undump_field(incoming[attribute], options[:type])
|
46
62
|
end
|
63
|
+
incoming.each {|attribute, value| hash[attribute] ||= value }
|
47
64
|
end
|
48
65
|
end
|
49
66
|
|
@@ -84,11 +101,6 @@ module Dynamoid
|
|
84
101
|
|
85
102
|
end
|
86
103
|
|
87
|
-
# Create the table if it doesn't exist already upon loading the class.
|
88
|
-
included do
|
89
|
-
self.create_table(self.table_name) unless self.table_exists?(self.table_name)
|
90
|
-
end
|
91
|
-
|
92
104
|
# Is this object persisted in the datastore? Required for some ActiveModel integration stuff.
|
93
105
|
#
|
94
106
|
# @since 0.2.0
|
@@ -100,6 +112,8 @@ module Dynamoid
|
|
100
112
|
#
|
101
113
|
# @since 0.2.0
|
102
114
|
def save(options = {})
|
115
|
+
self.class.create_table
|
116
|
+
|
103
117
|
@previously_changed = changes
|
104
118
|
|
105
119
|
if new_record?
|
@@ -175,10 +189,11 @@ module Dynamoid
|
|
175
189
|
# @since 0.2.0
|
176
190
|
def persist
|
177
191
|
run_callbacks(:save) do
|
178
|
-
self.
|
192
|
+
self.hash_key = SecureRandom.uuid if self.hash_key.nil? || self.hash_key.blank?
|
179
193
|
Dynamoid::Adapter.write(self.class.table_name, self.dump)
|
180
194
|
save_indexes
|
181
|
-
|
195
|
+
@new_record = false
|
196
|
+
true
|
182
197
|
end
|
183
198
|
end
|
184
199
|
|
data/spec/app/models/address.rb
CHANGED
@@ -7,5 +7,18 @@ class CamelCase
|
|
7
7
|
has_many :users
|
8
8
|
has_one :sponsor
|
9
9
|
has_and_belongs_to_many :subscriptions
|
10
|
+
|
11
|
+
before_create :doing_before_create
|
12
|
+
after_create :doing_after_create
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def doing_before_create
|
17
|
+
true
|
18
|
+
end
|
19
|
+
|
20
|
+
def doing_after_create
|
21
|
+
true
|
22
|
+
end
|
10
23
|
|
11
24
|
end
|
@@ -8,7 +8,7 @@ describe Dynamoid::Adapter::AwsSdk do
|
|
8
8
|
context 'without a preexisting table' do
|
9
9
|
# CreateTable and DeleteTable
|
10
10
|
it 'performs CreateTable and DeleteTable' do
|
11
|
-
table = Dynamoid::Adapter.create_table('CreateTable', :id, :range_key => :created_at)
|
11
|
+
table = Dynamoid::Adapter.create_table('CreateTable', :id, :range_key => { :created_at => :number })
|
12
12
|
|
13
13
|
Dynamoid::Adapter.connection.tables.collect{|t| t.name}.should include 'CreateTable'
|
14
14
|
|
@@ -20,7 +20,7 @@ describe Dynamoid::Adapter::AwsSdk do
|
|
20
20
|
before(:all) do
|
21
21
|
Dynamoid::Adapter.create_table('dynamoid_tests_TestTable1', :id) unless Dynamoid::Adapter.list_tables.include?('dynamoid_tests_TestTable1')
|
22
22
|
Dynamoid::Adapter.create_table('dynamoid_tests_TestTable2', :id) unless Dynamoid::Adapter.list_tables.include?('dynamoid_tests_TestTable2')
|
23
|
-
Dynamoid::Adapter.create_table('dynamoid_tests_TestTable3', :id, :range_key => :range) unless Dynamoid::Adapter.list_tables.include?('dynamoid_tests_TestTable3')
|
23
|
+
Dynamoid::Adapter.create_table('dynamoid_tests_TestTable3', :id, :range_key => { :range => :number }) unless Dynamoid::Adapter.list_tables.include?('dynamoid_tests_TestTable3')
|
24
24
|
end
|
25
25
|
|
26
26
|
# GetItem, PutItem and DeleteItem
|
@@ -41,11 +41,11 @@ describe Dynamoid::Adapter::AwsSdk do
|
|
41
41
|
it 'performs GetItem for an item that does exist with a range key' do
|
42
42
|
Dynamoid::Adapter.put_item('dynamoid_tests_TestTable3', {:id => '1', :name => 'Josh', :range => 2.0})
|
43
43
|
|
44
|
-
Dynamoid::Adapter.get_item('dynamoid_tests_TestTable3', '1', 2.0).should == {:name => 'Josh', :id => '1', :range => 2.0}
|
44
|
+
Dynamoid::Adapter.get_item('dynamoid_tests_TestTable3', '1', :range_key => 2.0).should == {:name => 'Josh', :id => '1', :range => 2.0}
|
45
45
|
|
46
|
-
Dynamoid::Adapter.delete_item('dynamoid_tests_TestTable3', '1', 2.0)
|
46
|
+
Dynamoid::Adapter.delete_item('dynamoid_tests_TestTable3', '1', :range_key => 2.0)
|
47
47
|
|
48
|
-
Dynamoid::Adapter.get_item('dynamoid_tests_TestTable3', '1', 2.0).should be_nil
|
48
|
+
Dynamoid::Adapter.get_item('dynamoid_tests_TestTable3', '1', :range_key => 2.0).should be_nil
|
49
49
|
end
|
50
50
|
|
51
51
|
it 'performs DeleteItem for an item that does not exist' do
|
@@ -4,239 +4,239 @@ require File.expand_path(File.dirname(__FILE__) + '../../../spec_helper')
|
|
4
4
|
describe Dynamoid::Adapter::Local do
|
5
5
|
|
6
6
|
unless ENV['ACCESS_KEY'] && ENV['SECRET_KEY']
|
7
|
-
|
7
|
+
|
8
8
|
# BatchGetItem
|
9
9
|
it 'performs BatchGetItem with singular keys' do
|
10
10
|
Dynamoid::Adapter.create_table('table1', :id)
|
11
11
|
Dynamoid::Adapter.put_item('table1', {:id => '1', :name => 'Josh'})
|
12
12
|
Dynamoid::Adapter.create_table('table2', :id)
|
13
|
-
Dynamoid::Adapter.put_item('table2', {:id => '1', :name => 'Justin'})
|
14
|
-
|
13
|
+
Dynamoid::Adapter.put_item('table2', {:id => '1', :name => 'Justin'})
|
14
|
+
|
15
15
|
results = Dynamoid::Adapter.batch_get_item('table1' => '1', 'table2' => '1')
|
16
16
|
results.size.should == 2
|
17
17
|
results['table1'].should include({:name => 'Josh', :id => '1'})
|
18
18
|
results['table2'].should include({:name => 'Justin', :id => '1'})
|
19
19
|
end
|
20
|
-
|
20
|
+
|
21
21
|
it 'performs BatchGetItem with multiple keys' do
|
22
22
|
Dynamoid::Adapter.create_table('table1', :id)
|
23
23
|
Dynamoid::Adapter.put_item('table1', {:id => '1', :name => 'Josh'})
|
24
24
|
Dynamoid::Adapter.put_item('table1', {:id => '2', :name => 'Justin'})
|
25
|
-
|
25
|
+
|
26
26
|
results = Dynamoid::Adapter.batch_get_item('table1' => ['1', '2'])
|
27
27
|
results.size.should == 1
|
28
28
|
results['table1'].should include({:name => 'Josh', :id => '1'})
|
29
29
|
results['table1'].should include({:name => 'Justin', :id => '2'})
|
30
30
|
end
|
31
|
-
|
31
|
+
|
32
32
|
it 'performs BatchGetItem with range keys' do
|
33
|
-
Dynamoid::Adapter.create_table('table1', :id, :range_key => :range)
|
33
|
+
Dynamoid::Adapter.create_table('table1', :id, :range_key => { :range => :string })
|
34
34
|
Dynamoid::Adapter.put_item('table1', {:id => '1', :range => 1.0})
|
35
35
|
Dynamoid::Adapter.put_item('table1', {:id => '2', :range => 2.0})
|
36
|
-
|
36
|
+
|
37
37
|
results = Dynamoid::Adapter.batch_get_item('table1' => [['1', 1.0], ['2', 2.0]])
|
38
38
|
results.size.should == 1
|
39
39
|
results['table1'].should include({:id => '1', :range => 1.0})
|
40
40
|
results['table1'].should include({:id => '2', :range => 2.0})
|
41
41
|
end
|
42
|
-
|
42
|
+
|
43
43
|
it 'performs BatchGetItem with range keys on one primary key' do
|
44
|
-
Dynamoid::Adapter.create_table('table1', :id, :range_key => :range)
|
44
|
+
Dynamoid::Adapter.create_table('table1', :id, :range_key => { :range => :string })
|
45
45
|
Dynamoid::Adapter.put_item('table1', {:id => '1', :range => 1.0})
|
46
46
|
Dynamoid::Adapter.put_item('table1', {:id => '1', :range => 2.0})
|
47
|
-
|
47
|
+
|
48
48
|
results = Dynamoid::Adapter.batch_get_item('table1' => [['1', 1.0], ['1', 2.0]])
|
49
49
|
results.size.should == 1
|
50
50
|
results['table1'].should include({:id => '1', :range => 1.0})
|
51
51
|
results['table1'].should include({:id => '1', :range => 2.0})
|
52
52
|
end
|
53
|
-
|
53
|
+
|
54
54
|
# CreateTable
|
55
55
|
it 'performs CreateTable' do
|
56
56
|
Dynamoid::Adapter.create_table('Test Table', :id)
|
57
|
-
|
57
|
+
|
58
58
|
Dynamoid::Adapter.list_tables.should include 'Test Table'
|
59
59
|
end
|
60
|
-
|
60
|
+
|
61
61
|
# DeleteItem
|
62
62
|
it 'performs DeleteItem' do
|
63
63
|
Dynamoid::Adapter.create_table('table1', :id)
|
64
64
|
Dynamoid::Adapter.put_item('table1', {:id => '1', :name => 'Josh'})
|
65
|
-
|
65
|
+
|
66
66
|
Dynamoid::Adapter.delete_item('table1', '1')
|
67
|
-
|
67
|
+
|
68
68
|
Dynamoid::Adapter.data['table1'][:data].should be_empty
|
69
69
|
end
|
70
|
-
|
70
|
+
|
71
71
|
it 'performs DeleteItem for an item that does not exist' do
|
72
72
|
Dynamoid::Adapter.create_table('table1', :id)
|
73
|
-
|
73
|
+
|
74
74
|
Dynamoid::Adapter.delete_item('table1', '1')
|
75
|
-
|
75
|
+
|
76
76
|
Dynamoid::Adapter.data['table1'][:data].should be_empty
|
77
77
|
end
|
78
|
-
|
78
|
+
|
79
79
|
# DeleteTable
|
80
80
|
it 'performs DeleteTable' do
|
81
81
|
Dynamoid::Adapter.create_table('table1', :id)
|
82
|
-
|
82
|
+
|
83
83
|
Dynamoid::Adapter.delete_table('table1')
|
84
|
-
|
84
|
+
|
85
85
|
Dynamoid::Adapter.data['table1'].should be_nil
|
86
86
|
end
|
87
|
-
|
87
|
+
|
88
88
|
# DescribeTable
|
89
|
-
|
89
|
+
|
90
90
|
# GetItem
|
91
91
|
it "performs GetItem for an item that does not exist" do
|
92
92
|
Dynamoid::Adapter.create_table('Test Table', :id)
|
93
|
-
|
93
|
+
|
94
94
|
Dynamoid::Adapter.get_item('Test Table', '1').should be_nil
|
95
95
|
end
|
96
|
-
|
96
|
+
|
97
97
|
it "performs GetItem for an item that does exist" do
|
98
98
|
Dynamoid::Adapter.create_table('Test Table', :id)
|
99
99
|
Dynamoid::Adapter.put_item('Test Table', {:id => '1', :name => 'Josh'})
|
100
|
-
|
100
|
+
|
101
101
|
Dynamoid::Adapter.get_item('Test Table', '1').should == {:id => '1', :name => 'Josh'}
|
102
102
|
end
|
103
|
-
|
103
|
+
|
104
104
|
it "performs GetItem for an item with a range key" do
|
105
|
-
Dynamoid::Adapter.create_table('Test Table', :id, :range_key => :range)
|
105
|
+
Dynamoid::Adapter.create_table('Test Table', :id, :range_key => { :range => :number })
|
106
106
|
Dynamoid::Adapter.put_item('Test Table', {:id => '1', :range => 1.0})
|
107
|
-
|
107
|
+
|
108
108
|
Dynamoid::Adapter.get_item('Test Table', '1').should be_nil
|
109
|
-
Dynamoid::Adapter.get_item('Test Table', '1', 1.0).should == {:id => '1', :range => 1.0}
|
109
|
+
Dynamoid::Adapter.get_item('Test Table', '1', :range_key => 1.0).should == {:id => '1', :range => 1.0}
|
110
110
|
end
|
111
|
-
|
111
|
+
|
112
112
|
# ListTables
|
113
113
|
it 'performs ListTables' do
|
114
114
|
Dynamoid::Adapter.create_table('Table1', :id)
|
115
115
|
Dynamoid::Adapter.create_table('Table2', :id)
|
116
|
-
|
116
|
+
|
117
117
|
Dynamoid::Adapter.list_tables.should include 'Table1'
|
118
118
|
Dynamoid::Adapter.list_tables.should include 'Table2'
|
119
119
|
end
|
120
|
-
|
120
|
+
|
121
121
|
# PutItem
|
122
122
|
it 'performs PutItem for an item that does not exist' do
|
123
123
|
Dynamoid::Adapter.create_table('Test Table', :id)
|
124
124
|
Dynamoid::Adapter.put_item('Test Table', {:id => '1', :name => 'Josh'})
|
125
|
-
|
125
|
+
|
126
126
|
Dynamoid::Adapter.data['Test Table'].should == {:hash_key=>:id, :range_key=>nil, :data=>{"1."=>{:id=>"1", :name=>"Josh"}}}
|
127
127
|
end
|
128
|
-
|
128
|
+
|
129
129
|
it 'puts an item twice and overwrites an existing item' do
|
130
130
|
Dynamoid::Adapter.create_table('Test Table', :id)
|
131
131
|
Dynamoid::Adapter.put_item('Test Table', {:id => '1', :name => 'Josh'})
|
132
132
|
Dynamoid::Adapter.put_item('Test Table', {:id => '1', :name => 'Justin'})
|
133
|
-
|
134
|
-
Dynamoid::Adapter.data['Test Table'].should == {:hash_key=>:id, :range_key=>nil, :data=>{"1."=>{:id=>"1", :name=>"Justin"}}}
|
133
|
+
|
134
|
+
Dynamoid::Adapter.data['Test Table'].should == {:hash_key=>:id, :range_key=>nil, :data=>{"1."=>{:id=>"1", :name=>"Justin"}}}
|
135
135
|
end
|
136
|
-
|
136
|
+
|
137
137
|
it 'puts an item twice and does not overwrite an existing item if the range key is not the same' do
|
138
|
-
Dynamoid::Adapter.create_table('Test Table', :id, :range_key => :range)
|
138
|
+
Dynamoid::Adapter.create_table('Test Table', :id, :range_key => { :range => :number })
|
139
139
|
Dynamoid::Adapter.put_item('Test Table', {:id => '1', :name => 'Justin', :range => 1.0})
|
140
140
|
Dynamoid::Adapter.put_item('Test Table', {:id => '1', :name => 'Justin', :range => 2.0})
|
141
|
-
|
141
|
+
|
142
142
|
Dynamoid::Adapter.data['Test Table'].should == {:hash_key=>:id, :range_key=>:range, :data=>{"1.1.0"=>{:id=>"1", :name=>"Justin", :range => 1.0}, "1.2.0" => {:id=>"1", :name=>"Justin", :range => 2.0}}}
|
143
143
|
end
|
144
|
-
|
144
|
+
|
145
145
|
it 'puts an item twice and does overwrite an existing item if the range key is the same' do
|
146
|
-
Dynamoid::Adapter.create_table('Test Table', :id, :range_key => :range)
|
146
|
+
Dynamoid::Adapter.create_table('Test Table', :id, :range_key => { :range => :number })
|
147
147
|
Dynamoid::Adapter.put_item('Test Table', {:id => '1', :name => 'Josh', :range => 1.0})
|
148
148
|
Dynamoid::Adapter.put_item('Test Table', {:id => '1', :name => 'Justin', :range => 1.0})
|
149
|
-
|
150
|
-
Dynamoid::Adapter.data['Test Table'].should == {:hash_key=>:id, :range_key=>:range, :data=>{"1.1.0"=>{:id=>"1", :name=>"Justin", :range => 1.0}}}
|
149
|
+
|
150
|
+
Dynamoid::Adapter.data['Test Table'].should == {:hash_key=>:id, :range_key=>:range, :data=>{"1.1.0"=>{:id=>"1", :name=>"Justin", :range => 1.0}}}
|
151
151
|
end
|
152
|
-
|
153
|
-
|
152
|
+
|
153
|
+
|
154
154
|
# Query
|
155
155
|
it 'performs query on a table and returns items' do
|
156
156
|
Dynamoid::Adapter.create_table('Test Table', :id)
|
157
157
|
Dynamoid::Adapter.put_item('Test Table', {:id => '1', :name => 'Josh'})
|
158
|
-
|
159
|
-
Dynamoid::Adapter.query('Test Table', :hash_value => '1').should == { :id=> '1', :name=>"Josh" }
|
158
|
+
|
159
|
+
Dynamoid::Adapter.query('Test Table', :hash_value => '1').should == [{ :id=> '1', :name=>"Josh" }]
|
160
160
|
end
|
161
|
-
|
161
|
+
|
162
162
|
it 'performs query on a table and returns items if there are multiple items' do
|
163
163
|
Dynamoid::Adapter.create_table('Test Table', :id)
|
164
164
|
Dynamoid::Adapter.put_item('Test Table', {:id => '1', :name => 'Josh'})
|
165
165
|
Dynamoid::Adapter.put_item('Test Table', {:id => '2', :name => 'Justin'})
|
166
|
-
|
167
|
-
Dynamoid::Adapter.query('Test Table', :hash_value => '1').should == { :id=> '1', :name=>"Josh" }
|
166
|
+
|
167
|
+
Dynamoid::Adapter.query('Test Table', :hash_value => '1').should == [{ :id=> '1', :name=>"Josh" }]
|
168
168
|
end
|
169
|
-
|
169
|
+
|
170
170
|
context 'range queries' do
|
171
171
|
before do
|
172
|
-
Dynamoid::Adapter.create_table('Test Table', :id, :range_key => :range)
|
172
|
+
Dynamoid::Adapter.create_table('Test Table', :id, :range_key => { :range => :number })
|
173
173
|
Dynamoid::Adapter.put_item('Test Table', {:id => '1', :range => 1.0})
|
174
|
-
Dynamoid::Adapter.put_item('Test Table', {:id => '1', :range => 2.0})
|
174
|
+
Dynamoid::Adapter.put_item('Test Table', {:id => '1', :range => 2.0})
|
175
175
|
end
|
176
|
-
|
177
|
-
it 'performs query on a table with a range and selects items in a range' do
|
176
|
+
|
177
|
+
it 'performs query on a table with a range and selects items in a range' do
|
178
178
|
Dynamoid::Adapter.query('Test Table', :hash_value => '1', :range_value => 0.0..3.0).should =~ [{:id => '1', :range => 1.0}, {:id => '1', :range => 2.0}]
|
179
179
|
end
|
180
|
-
|
181
|
-
it 'performs query on a table with a range and selects items greater than' do
|
180
|
+
|
181
|
+
it 'performs query on a table with a range and selects items greater than' do
|
182
182
|
Dynamoid::Adapter.query('Test Table', :hash_value => '1', :range_greater_than => 1.0).should =~ [{:id => '1', :range => 2.0}]
|
183
183
|
end
|
184
|
-
|
185
|
-
it 'performs query on a table with a range and selects items less than' do
|
184
|
+
|
185
|
+
it 'performs query on a table with a range and selects items less than' do
|
186
186
|
Dynamoid::Adapter.query('Test Table', :hash_value => '1', :range_less_than => 2.0).should =~ [{:id => '1', :range => 1.0}]
|
187
187
|
end
|
188
|
-
|
189
|
-
it 'performs query on a table with a range and selects items gte' do
|
188
|
+
|
189
|
+
it 'performs query on a table with a range and selects items gte' do
|
190
190
|
Dynamoid::Adapter.query('Test Table', :hash_value => '1', :range_gte => 1.0).should =~ [{:id => '1', :range => 1.0}, {:id => '1', :range => 2.0}]
|
191
191
|
end
|
192
|
-
|
193
|
-
it 'performs query on a table with a range and selects items lte' do
|
192
|
+
|
193
|
+
it 'performs query on a table with a range and selects items lte' do
|
194
194
|
Dynamoid::Adapter.query('Test Table', :hash_value => '1', :range_lte => 2.0).should =~ [{:id => '1', :range => 1.0}, {:id => '1', :range => 2.0}]
|
195
195
|
end
|
196
|
-
|
196
|
+
|
197
197
|
end
|
198
|
-
|
198
|
+
|
199
199
|
# Scan
|
200
200
|
it 'performs scan on a table and returns items' do
|
201
201
|
Dynamoid::Adapter.create_table('Test Table', :id)
|
202
202
|
Dynamoid::Adapter.put_item('Test Table', {:id => '1', :name => 'Josh'})
|
203
|
-
|
203
|
+
|
204
204
|
Dynamoid::Adapter.scan('Test Table', :name => 'Josh').should == [{ :id=> '1', :name=>"Josh" }]
|
205
205
|
end
|
206
|
-
|
206
|
+
|
207
207
|
it 'performs scan on a table and returns items if there are multiple items but only one match' do
|
208
208
|
Dynamoid::Adapter.create_table('Test Table', :id)
|
209
209
|
Dynamoid::Adapter.put_item('Test Table', {:id => '1', :name => 'Josh'})
|
210
210
|
Dynamoid::Adapter.put_item('Test Table', {:id => '2', :name => 'Justin'})
|
211
|
-
|
211
|
+
|
212
212
|
Dynamoid::Adapter.scan('Test Table', :name => 'Josh').should == [{ :id=> '1', :name=>"Josh" }]
|
213
213
|
end
|
214
|
-
|
214
|
+
|
215
215
|
it 'performs scan on a table and returns multiple items if there are multiple matches' do
|
216
216
|
Dynamoid::Adapter.create_table('Test Table', :id)
|
217
217
|
Dynamoid::Adapter.put_item('Test Table', {:id => '1', :name => 'Josh'})
|
218
218
|
Dynamoid::Adapter.put_item('Test Table', {:id => '2', :name => 'Josh'})
|
219
|
-
|
219
|
+
|
220
220
|
Dynamoid::Adapter.scan('Test Table', :name => 'Josh').should == [{ :id=> '1', :name=>"Josh" }, { :id=> '2', :name=>"Josh" }]
|
221
221
|
end
|
222
|
-
|
222
|
+
|
223
223
|
it 'performs scan on a table and returns all items if no criteria are specified' do
|
224
224
|
Dynamoid::Adapter.create_table('Test Table', :id)
|
225
225
|
Dynamoid::Adapter.put_item('Test Table', {:id => '1', :name => 'Josh'})
|
226
226
|
Dynamoid::Adapter.put_item('Test Table', {:id => '2', :name => 'Josh'})
|
227
|
-
|
227
|
+
|
228
228
|
Dynamoid::Adapter.scan('Test Table', {}).should == [{ :id=> '1', :name=>"Josh" }, { :id=> '2', :name=>"Josh" }]
|
229
229
|
end
|
230
|
-
|
230
|
+
|
231
231
|
# UpdateItem
|
232
|
-
|
232
|
+
|
233
233
|
# UpdateTable
|
234
|
-
|
234
|
+
|
235
235
|
protected
|
236
|
-
|
236
|
+
|
237
237
|
def setup_value(table, key, value)
|
238
238
|
Dynamoid::Adapter.data[table][key] = value
|
239
239
|
end
|
240
|
-
|
240
|
+
|
241
241
|
end
|
242
242
|
end
|