Permalink
Browse files

many improvements to right_aws SDB adapter... now uses the SDB.select…

… method opposed to depricated query and query with attrs
  • Loading branch information...
1 parent 8f38022 commit d6f181ba919cc298eb98d9f6f60758ef0fcf3a4f Dan Mayer committed Jun 15, 2009
View
@@ -3,6 +3,7 @@ require 'spec'
require 'spec/rake/spectask'
require 'pathname'
require "rake/gempackagetask"
+require 'devver'
ROOT = Pathname(__FILE__).dirname.expand_path
require ROOT + 'lib/simpledb_adapter'
@@ -26,6 +27,14 @@ Spec::Rake::SpecTask.new(:spec) do |t|
end
end
+desc 'Run specifications without Rcov'
+Spec::Rake::SpecTask.new(:spec_no_rcov) do |t|
+ if File.exists?('spec/spec.opts')
+ t.spec_opts << '--options' << 'spec/spec.opts'
+ end
+ t.spec_files = Pathname.glob((ROOT + 'spec/**/*_spec.rb').to_s)
+end
+
spec = Gem::Specification.new do |s|
s.name = "dm-adapter-simpledb"
@@ -55,4 +64,4 @@ task :gemspec do
File.open("dm-adapter-simpledb.gemspec", "w") do |file|
file.puts spec.to_ruby
end
-end
+end
View
@@ -0,0 +1,3 @@
+1_YOUR_ACCESS_KEY_2
+I_YOUR_SECRET_KEY_0
+
@@ -35,11 +35,11 @@ def read_many(query)
conditions = set_conditions(query, sdb_type)
order = set_sort_order(query, conditions)
results = get_results(query, conditions, order)
-
+
Collection.new(query) do |collection|
results.each do |result|
data = query.fields.map do |property|
- value = result[:attributes][property.field.to_s]
+ value = result.values[0][property.field.to_s]
if value != nil
if value.size > 1
value.map {|v| property.typecast(v) }
@@ -81,7 +81,7 @@ def domain
#sets the conditions for the SDB query
def set_conditions(query, sdb_type)
- conditions = ["['simpledb_type' = '#{sdb_type}']"]
+ conditions = ["simpledb_type = '#{sdb_type}'"]
if query.conditions.size > 0
conditions += query.conditions.map do |condition|
operator = case condition[0]
@@ -93,7 +93,7 @@ def set_conditions(query, sdb_type)
when :lte then '<='
else raise "Invalid query operator: #{operator.inspect}"
end
- "['#{condition[1].name.to_s}' #{operator} '#{condition[2].to_s}']"
+ "#{condition[1].name.to_s} #{operator} '#{condition[2].to_s}'"
end
end
conditions
@@ -103,30 +103,24 @@ def set_conditions(query, sdb_type)
def set_sort_order(query, conditions)
if query.order!=nil && query.order.length > 0
query_object = query.order[0]
- conditions << "['#{query_object.property.name.to_s}'> '']" #anything sorted on must be a condition
- order = "sort '#{query_object.property.name.to_s}' #{query_object.direction==:desc ? 'DESC' : 'ASC'}"
+ #anything sorted on must be a condition for SDB
+ conditions << "#{query_object.property.name.to_s} > ''"
+ order = "order by #{query_object.property.name.to_s} #{query_object.direction==:desc ? 'desc' : 'asc'}"
else
order = ""
end
end
#gets all results or proper number of results depending on the :limit
def get_results(query, conditions, order)
- results = sdb.query(domain, "#{conditions.compact.join(' intersection ')} #{order}")
- if query.limit!=nil && query.limit <= results[:items].length
- results[:items] = results[:items][0...query.limit]
- else
- sdb_continuation_key = results[:next_token]
- #this means there are more results to retrieve from SDB
- while (sdb_continuation_key!=nil) do
- old_results = results
- results = sdb.query(domain, "#{conditions.compact.join(' intersection ')} #{order}", nil, sdb_continuation_key)
- results[:items] = old_results[:items] + results[:items]
- sdb_continuation_key = results[:next_token]
- end
- end
- #todo use newer SDB batch get attributes
- results = results[:items].map {|d| sdb.get_attributes(domain, d) }
+
+ #TODO add support for continuation keys (again)
+ query_call = "select * from #{domain} "
+ query_call = query_call + "where #{conditions.compact.join(' and ')}" if conditions.length > 0
+ query_call = query_call + " #{order}"
+ query_call = query_call + " limit #{query.limit}" if query.limit!=nil
+ results = sdb.select(query_call)
+ results = results[:items]
end
# Creates an item name for a query
@@ -0,0 +1,16 @@
+require 'pathname'
+require Pathname(__FILE__).dirname.expand_path + 'spec_helper'
+require 'ruby-debug'
+
+describe 'associations' do
+ it 'should work with belongs_to associations'
+ it 'should work with has n associations'
+end
+
+describe 'STI' do
+ it 'should override default type'
+ it 'should load descendents on parent.all'
+ it 'should raise an error if you have a column named couchdb_type'
+end
+
+
View
@@ -0,0 +1,52 @@
+require 'pathname'
+require Pathname(__FILE__).dirname.expand_path + 'spec_helper'
+
+class Professor
+ include DataMapper::Resource
+
+ property :id, String, :key => true
+ property :name, String, :key => true
+ property :age, Integer
+ property :wealth, Float
+ property :birthday, Date
+ property :created_at, DateTime
+
+end
+
+describe 'with multiple records saved' do
+ before(:each) do
+ @person_attrs = { :id => "person-#{Time.now.to_f.to_s}", :name => 'Jeremy Boles', :age => 25, :wealth => 25.00, :birthday => Date.today }
+ @jeremy = Professor.create(@person_attrs.merge(:id => Time.now.to_f.to_s, :name => "Jeremy Boles", :age => 25))
+ @danielle = Professor.create(@person_attrs.merge(:id => Time.now.to_f.to_s, :name => "Danille Boles", :age => 26))
+ @keegan = Professor.create(@person_attrs.merge(:id => Time.now.to_f.to_s, :name => "Keegan Jones", :age => 20))
+ sleep(0.4) #or the get calls might not have these created yet
+ end
+
+ after(:each) do
+ @jeremy.destroy
+ @danielle.destroy
+ @keegan.destroy
+ sleep(0.4) #or might not be destroyed by the next test
+ end
+
+ it 'should handle DateTime' do
+ pending 'Need to figure out how to coerce DateTime'
+ time = Time.now
+ @jeremy.created_at = time
+ @jeremy.save
+ sleep(0.2)
+ person = Professor.get!(@jeremy.id, @jeremy.name)
+ person.created_at.should == time
+ end
+
+ it 'should handle Date' do
+ person = Professor.get!(@jeremy.id, @jeremy.name)
+ person.birthday.should == @jeremy.birthday
+ end
+
+ it 'should match with Data' do
+ people = Professor.all(:birthday => Date.today)
+ people.length.should == 3
+ end
+
+end
@@ -0,0 +1,102 @@
+require 'pathname'
+require Pathname(__FILE__).dirname.expand_path + 'spec_helper'
+
+class Hero
+ include DataMapper::Resource
+
+ property :id, String, :key => true
+ property :name, String, :key => true
+ property :age, Integer
+ property :wealth, Float
+ property :birthday, Date
+ property :created_at, DateTime
+
+ belongs_to :company
+end
+
+describe 'with multiple records saved' do
+ before(:each) do
+ @person_attrs = { :id => "person-#{Time.now.to_f.to_s}", :name => 'Jeremy Boles', :age => 25, :wealth => 25.00, :birthday => Date.today }
+ @jeremy = Hero.create(@person_attrs.merge(:id => Time.now.to_f.to_s, :name => "Jeremy Boles", :age => 25))
+ @danielle = Hero.create(@person_attrs.merge(:id => Time.now.to_f.to_s, :name => "Danille Boles", :age => 26))
+ @keegan = Hero.create(@person_attrs.merge(:id => Time.now.to_f.to_s, :name => "Keegan Jones", :age => 20, :wealth => 15.00))
+ sleep(0.4) #or the get calls might not have these created yet
+ end
+
+ after(:each) do
+ @jeremy.destroy
+ @danielle.destroy
+ @keegan.destroy
+ sleep(0.4) #or might not be destroyed by the next test
+ end
+
+ it 'should handle limit one case' do
+ persons = Hero.all(:limit => 1)
+ persons.length.should ==1
+ end
+
+ it 'should handle max item limit case' do
+ persons = Hero.all(:limit => 3)
+ persons.length.should ==3
+ end
+
+ it 'should handle max item if limit is large case' do
+ persons = Hero.all(:limit => 150)
+ persons.length.should ==3
+ end
+
+ #it would be really slow to create over 100 entires to test this until we have batch creation
+ it 'should handle limits over the default SDB 100 results limit'
+
+ #it would be really slow to create over 100 entires to test this until we have batch creation
+ it 'should get all results over the default SDB 100 results limit'
+
+ it 'should handle ordering asc results with a limit' do
+ persons = Hero.all(:order => [:age.asc], :limit => 2)
+ persons.inspect #can't access via array until loaded? Weird
+ persons.length.should ==2
+ persons[0].should == @keegan
+ persons[1].should == @jeremy
+ end
+
+ it 'should handle ordering asc results' do
+ persons = Hero.all(:order => [:age.asc])
+ persons.inspect #can't access via array until loaded? Weird
+ persons[0].should == @keegan
+ persons[1].should == @jeremy
+ persons[2].should == @danielle
+ end
+
+ it 'should handle ordering desc results' do
+ persons = Hero.all(:order => [:age.desc])
+ persons.inspect #can't access via array until loaded? Weird
+ persons[0].should == @danielle
+ persons[1].should == @jeremy
+ persons[2].should == @keegan
+ end
+
+ it 'should handle ordering results with multiple conditionss' do
+ persons = Hero.all(:age.gt => 20, :wealth.gt => 20, :order => [:age.desc])
+ persons.inspect #can't access via array until loaded? Weird
+ persons.length.should ==2
+ persons[0].should == @danielle
+ persons[1].should == @jeremy
+ end
+
+ it 'should handle ordering results with ordered by conditions' do
+ persons = Hero.all(:age.gt => 20, :order => [:age.desc])
+ persons.inspect #can't access via array until loaded? Weird
+ persons.length.should ==2
+ persons[0].should == @danielle
+ persons[1].should == @jeremy
+ end
+
+ it 'should handle ordering results with unorder by conditions' do
+ persons = Hero.all(:wealth.gt => 20.00, :order => [:age.desc])
+ persons.inspect #can't access via array until loaded? Weird
+ persons.length.should ==2
+ persons[0].should == @danielle
+ persons[1].should == @jeremy
+ end
+
+end
@@ -0,0 +1,42 @@
+require 'pathname'
+require Pathname(__FILE__).dirname.expand_path + 'spec_helper'
+require 'ruby-debug'
+
+describe 'support migrations' do
+
+ #TODO do this on different storage
+ class Person
+ include DataMapper::Resource
+
+ property :id, String, :key => true
+ property :name, String, :key => true
+ property :age, Integer
+ property :wealth, Float
+ property :birthday, Date
+ property :created_at, DateTime
+
+ belongs_to :company
+ end
+
+ before do
+ @adapter = repository(:default).adapter
+ end
+
+# test can't be run simultanious make it delete a throwawaable storage model
+# instead of the one used by all the tests
+# it "should destroy model storage" do
+# ENV['destroy']='true'
+# @adapter.destroy_model_storage(repository(:default), Person)
+# @adapter.storage_exists?("missionaries").should == false
+# ENV['destroy']='false'
+# @adapter.create_model_storage(repository(:default), Person)
+# @adapter.storage_exists?("missionaries").should == true
+# end
+
+ it "should create model storage" do
+ Person.auto_migrate!
+ @adapter.storage_exists?("missionaries").should == true
+ end
+
+end
+
Oops, something went wrong.

0 comments on commit d6f181b

Please sign in to comment.