From 41e9724c1a5496e1518674b989450e778d777a61 Mon Sep 17 00:00:00 2001 From: Wesley Beary Date: Wed, 15 Jul 2009 11:53:32 -0700 Subject: [PATCH] spec cleanup/overhaul --- Rakefile | 1 - .../aws/simpledb/batch_put_attributes_spec.rb | 25 +++-------- spec/aws/simpledb/create_domain_spec.rb | 18 ++++---- spec/aws/simpledb/delete_domain_spec.rb | 12 ++---- spec/aws/simpledb/domain_metadata_spec.rb | 11 ++--- spec/aws/simpledb/get_attributes_spec.rb | 11 ++--- spec/aws/simpledb/list_domains_spec.rb | 9 ++-- spec/aws/simpledb/put_attributes_spec.rb | 15 ++----- spec/eventually.rb | 36 ++++++++++++++++ spec/spec_helper.rb | 42 ++----------------- 10 files changed, 76 insertions(+), 104 deletions(-) create mode 100644 spec/eventually.rb diff --git a/Rakefile b/Rakefile index bbce2080c..a613bc08f 100644 --- a/Rakefile +++ b/Rakefile @@ -29,7 +29,6 @@ Spec::Rake::SpecTask.new(:rcov) do |spec| spec.rcov = true end - task :default => :spec require 'rake/rdoctask' diff --git a/spec/aws/simpledb/batch_put_attributes_spec.rb b/spec/aws/simpledb/batch_put_attributes_spec.rb index 7992fefcc..904997491 100644 --- a/spec/aws/simpledb/batch_put_attributes_spec.rb +++ b/spec/aws/simpledb/batch_put_attributes_spec.rb @@ -3,33 +3,18 @@ require File.dirname(__FILE__) + '/../../spec_helper' describe 'SimpleDB.batch_put_attributes' do before(:all) do - sdb.create_domain('batch_put_attributes') + @domain_name = "fog_domain_#{Time.now.to_i}" + sdb.create_domain(@domain_name) end after(:all) do - sdb.delete_domain('batch_put_attributes') + sdb.delete_domain(@domain_name) end - it 'should have no attributes for y before batch_put_attributes' do - lambda { sdb.get_attributes('batch_put_attributes', 'a') }.should eventually { |expected| expected.body[:attributes].should be_empty } - end - - it 'should have no attributes for x before batch_put_attributes' do - lambda { sdb.get_attributes('batch_put_attributes', 'x') }.should eventually { |expected| expected.body[:attributes].should be_empty } - end - - it 'should return proper attributes from batch_put_attributes' do - actual = sdb.batch_put_attributes('batch_put_attributes', { 'a' => { 'b' => 'c' }, 'x' => { 'y' => 'z' } }) + it 'should return proper attributes' do + actual = sdb.batch_put_attributes(@domain_name, { 'a' => { 'b' => 'c' }, 'x' => { 'y' => 'z' } }) actual.body[:request_id].should be_a(String) actual.body[:box_usage].should be_a(Float) end - it 'should have correct attributes for a after batch_put_attributes' do - lambda { sdb.get_attributes('batch_put_attributes', 'a') }.should eventually { |expected| expected.body[:attributes].should == { 'b' => ['c'] } } - end - - it 'should have correct attributes for x after batch_put_attributes' do - lambda { sdb.get_attributes('batch_put_attributes', 'x') }.should eventually { |expected| expected.body[:attributes].should == { 'y' => ['z'] } } - end - end \ No newline at end of file diff --git a/spec/aws/simpledb/create_domain_spec.rb b/spec/aws/simpledb/create_domain_spec.rb index 1f834488d..123748749 100644 --- a/spec/aws/simpledb/create_domain_spec.rb +++ b/spec/aws/simpledb/create_domain_spec.rb @@ -2,22 +2,18 @@ require File.dirname(__FILE__) + '/../../spec_helper' describe 'SimpleDB.create_domain' do + before(:all) do + @domain_name = "fog_domain_#{Time.now.to_i}" + end + after(:all) do - sdb.delete_domain('create_domain') + sdb.delete_domain(@domain_name) end - it 'should not include create_domain in list_domains before create_domain' do - lambda { sdb.list_domains }.should_not eventually { |expected| expected.body[:domains].should_not include('create_domain') } - end - - it 'should return proper attributes from create_domain' do - actual = sdb.create_domain('create_domain') + it 'should return proper attributes' do + actual = sdb.create_domain(@domain_name) actual.body[:request_id].should be_a(String) actual.body[:box_usage].should be_a(Float) end - it 'should include create_domain in list_domains after create_domain' do - lambda { sdb.list_domains }.should eventually { |expected| expected.body[:domains].should include('create_domain') } - end - end diff --git a/spec/aws/simpledb/delete_domain_spec.rb b/spec/aws/simpledb/delete_domain_spec.rb index 4c6df54ec..f65761f4c 100644 --- a/spec/aws/simpledb/delete_domain_spec.rb +++ b/spec/aws/simpledb/delete_domain_spec.rb @@ -3,21 +3,17 @@ require File.dirname(__FILE__) + '/../../spec_helper' describe 'SimpleDB.delete_domain' do before(:all) do - sdb.create_domain('delete_domain') + @domain_name = "fog_domain_#{Time.now.to_i}" end - it 'should include delete_domain in list_domains before delete_domain' do - lambda { sdb.list_domains }.should eventually { |expected| expected.body[:domains].should include('delete_domain') } + before(:all) do + sdb.create_domain(@domain_name) end it 'should return proper attributes' do - actual = sdb.delete_domain('delete_domain') + actual = sdb.delete_domain(@domain_name) actual.body[:request_id].should be_a(String) actual.body[:box_usage].should be_a(Float) end - it 'should not include delete_domain in list_domains after delete_domain' do - lambda { sdb.list_domains }.should_not eventually { |expected| expected.body[:domains].should_not include('delete_domain') } - end - end \ No newline at end of file diff --git a/spec/aws/simpledb/domain_metadata_spec.rb b/spec/aws/simpledb/domain_metadata_spec.rb index d9a362980..c47707621 100644 --- a/spec/aws/simpledb/domain_metadata_spec.rb +++ b/spec/aws/simpledb/domain_metadata_spec.rb @@ -3,15 +3,16 @@ require File.dirname(__FILE__) + '/../../spec_helper' describe 'SimpleDB.domain_metadata' do before(:all) do - sdb.create_domain('domain_metadata') + @domain_name = "fog_domain_#{Time.now.to_i}" + sdb.create_domain(@domain_name) end after(:all) do - sdb.delete_domain('domain_metadata') + sdb.delete_domain(@domain_name) end it 'should return proper attributes when there are no items' do - results = sdb.domain_metadata('domain_metadata') + results = sdb.domain_metadata(@domain_name) results.body[:attribute_name_count].should == 0 results.body[:attribute_names_size_bytes].should == 0 results.body[:attribute_value_count].should == 0 @@ -24,8 +25,8 @@ describe 'SimpleDB.domain_metadata' do end it 'should return proper attributes with items' do - sdb.put_attributes('domain_metadata', 'foo', { :bar => :baz }) - results = sdb.domain_metadata('domain_metadata') + sdb.put_attributes(@domain_name, 'foo', { :bar => :baz }) + results = sdb.domain_metadata(@domain_name) results.body[:attribute_name_count].should == 1 results.body[:attribute_names_size_bytes].should == 3 results.body[:attribute_value_count].should == 1 diff --git a/spec/aws/simpledb/get_attributes_spec.rb b/spec/aws/simpledb/get_attributes_spec.rb index f6b50fc87..c307cd10f 100644 --- a/spec/aws/simpledb/get_attributes_spec.rb +++ b/spec/aws/simpledb/get_attributes_spec.rb @@ -3,20 +3,21 @@ require File.dirname(__FILE__) + '/../../spec_helper' describe 'SimpleDB.get_attributes' do before(:all) do - sdb.create_domain('get_attributes') + @domain_name = "fog_domain_#{Time.now.to_i}" + sdb.create_domain(@domain_name) end after(:all) do - sdb.delete_domain('get_attributes') + sdb.delete_domain(@domain_name) end it 'should have no attributes for foo before put_attributes' do - lambda { sdb.get_attributes('get_attributes', 'foo') }.should eventually { |expected| expected.body[:attributes].should be_empty } + lambda { sdb.get_attributes(@domain_name, 'foo') }.should eventually { |expected| expected.body[:attributes].should be_empty } end it 'should have attributes for foo after put_attributes' do - sdb.put_attributes('get_attributes', 'foo', { :bar => :baz }) - lambda { sdb.get_attributes('get_attributes', 'foo') }.should eventually { |expected| expected.body[:attributes].should == { 'bar' => ['baz'] } } + sdb.put_attributes(@domain_name, 'foo', { :bar => :baz }) + lambda { sdb.get_attributes(@domain_name, 'foo') }.should eventually { |expected| expected.body[:attributes].should == { 'bar' => ['baz'] } } end end diff --git a/spec/aws/simpledb/list_domains_spec.rb b/spec/aws/simpledb/list_domains_spec.rb index 14f1218a8..2947b5add 100644 --- a/spec/aws/simpledb/list_domains_spec.rb +++ b/spec/aws/simpledb/list_domains_spec.rb @@ -3,11 +3,11 @@ require File.dirname(__FILE__) + '/../../spec_helper' describe 'SimpleDB.list_domains' do before(:all) do - sdb.create_domain('list_domains') + @domain_name = "fog_domain_#{Time.now.to_i}" end after(:all) do - sdb.delete_domain('list_domains') + sdb.delete_domain(@domain_name) end it 'should return proper attributes' do @@ -17,8 +17,9 @@ describe 'SimpleDB.list_domains' do results.body[:request_id].should be_a(String) end - it 'should include list_domains in list_domains' do - lambda { sdb.list_domains }.should eventually { |expected| expected.body[:domains].should include('list_domains') } + it 'should include created domains' do + sdb.create_domain(@domain_name) + lambda { sdb.list_domains }.should eventually { |expected| expected.body[:domains].should include(@domain_name) } end end diff --git a/spec/aws/simpledb/put_attributes_spec.rb b/spec/aws/simpledb/put_attributes_spec.rb index 92f6485a3..2c1070d7f 100644 --- a/spec/aws/simpledb/put_attributes_spec.rb +++ b/spec/aws/simpledb/put_attributes_spec.rb @@ -3,25 +3,18 @@ require File.dirname(__FILE__) + '/../../spec_helper' describe 'SimpleDB.put_attributes' do before(:all) do - sdb.create_domain('put_attributes') + @domain_name = "fog_domain_#{Time.now.to_i}" + sdb.create_domain(@domain_name) end after(:all) do - sdb.delete_domain('put_attributes') - end - - it 'should have no attributes for foo before put_attributes' do - lambda { sdb.get_attributes('put_attributes', 'foo') }.should eventually { |expected| expected.body[:attributes].should be_empty } + sdb.delete_domain(@domain_name) end it 'should return proper attributes from put_attributes' do - actual = sdb.put_attributes('put_attributes', 'foo', { 'bar' => 'baz' }) + actual = sdb.put_attributes(@domain_name, 'foo', { 'bar' => 'baz' }) actual.body[:request_id].should be_a(String) actual.body[:box_usage].should be_a(Float) end - it 'should have attributes for foo after put_attributes' do - lambda { sdb.get_attributes('put_attributes', 'foo') }.should eventually { |expected| expected.body[:attributes].should == { 'bar' => ['baz'] } } - end - end diff --git a/spec/eventually.rb b/spec/eventually.rb new file mode 100644 index 000000000..630eb4b0d --- /dev/null +++ b/spec/eventually.rb @@ -0,0 +1,36 @@ +module Spec + module Matchers + class Eventually #:nodoc: + def initialize(&block) + @block = block + end + + def matches?(given_proc) + match = nil + [0,2,4,8,16].each do |delay| + begin + sleep(delay) + match = @block[given_proc.call] + break + rescue Spec::Expectations::ExpectationNotMetError => error + raise error if delay == 16 + end + end + match + end + end + + # :call-seq + # should eventually() { |expected| ... } + # Matches if block matches within 30 seconds + # + # == Examples + # + # lambda { do_something_eventually_returning_true }.should eventually {|expected| expected.should be_true } + # + # lambda { do_something_eventually_returning_false }.should_not eventually {|expected| expected.should_not be_true } + def eventually(&block) + Matchers::Eventually.new(&block) + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 45e1054c9..e307ee65f 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,6 +1,8 @@ require 'spec' -require "#{File.dirname(__FILE__)}/../lib/fog" +current_directory = File.dirname(__FILE__) +require "#{current_directory}/../lib/fog" +require "#{current_directory}/eventually.rb" Spec::Runner.configure do |config| end @@ -38,44 +40,6 @@ def s3 end end -module Spec - module Matchers - class Eventually #:nodoc: - def initialize(&block) - @block = block - end - - def matches?(given_proc) - match = nil - [0,2,4,8,16].each do |delay| - begin - sleep(delay) - match = @block[given_proc.call] - break - rescue Spec::Expectations::ExpectationNotMetError => error - raise error if delay == 16 - end - end - match - end - end - - # :call-seq - # should eventually() { |expected| ... } - # Matches if block matches within 30 seconds - # - # == Examples - # - # lambda { do_something_eventually_returning_true }.should eventually {|expected| expected.should be_true } - # - # lambda { do_something_eventually_returning_false }.should_not eventually {|expected| expected.should_not be_true } - def eventually(&block) - Matchers::Eventually.new(&block) - end - end -end - - class Eventually def initialize(result, delay) @result = result