Skip to content
This repository has been archived by the owner on Jul 7, 2019. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
many improvements to right_aws SDB adapter... now uses the SDB.select…
… method opposed to depricated query and query with attrs
  • Loading branch information
Dan Mayer committed Jun 15, 2009
1 parent 8f38022 commit d6f181b
Show file tree
Hide file tree
Showing 11 changed files with 438 additions and 239 deletions.
11 changes: 10 additions & 1 deletion Rakefile
Expand Up @@ -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'
Expand All @@ -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"
Expand Down Expand Up @@ -55,4 +64,4 @@ task :gemspec do
File.open("dm-adapter-simpledb.gemspec", "w") do |file|
file.puts spec.to_ruby
end
end
end
3 changes: 3 additions & 0 deletions aws_config.sample
@@ -0,0 +1,3 @@
1_YOUR_ACCESS_KEY_2
I_YOUR_SECRET_KEY_0

36 changes: 15 additions & 21 deletions lib/simpledb_adapter.rb
Expand Up @@ -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) }
Expand Down Expand Up @@ -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]
Expand All @@ -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
Expand All @@ -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
Expand Down
16 changes: 16 additions & 0 deletions spec/associations_spec.rb
@@ -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


52 changes: 52 additions & 0 deletions spec/date_spec.rb
@@ -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
102 changes: 102 additions & 0 deletions spec/limit_and_order_spec.rb
@@ -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
42 changes: 42 additions & 0 deletions spec/migrations_spec.rb
@@ -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

0 comments on commit d6f181b

Please sign in to comment.