Skip to content

Commit

Permalink
added shared examples
Browse files Browse the repository at this point in the history
added teardown method for adapters
gom-15: Handle missing setup.
  • Loading branch information
Philipp Brüll committed Feb 17, 2011
1 parent 8e964ba commit 0f50306
Show file tree
Hide file tree
Showing 13 changed files with 140 additions and 19 deletions.
1 change: 1 addition & 0 deletions lib/gom/spec.rb
@@ -1,5 +1,6 @@
require 'rspec'

require File.join(File.dirname(__FILE__), "spec", "acceptance", "adapter_that_needs_setup")
require File.join(File.dirname(__FILE__), "spec", "acceptance", "adapter_with_stateful_storage")
require File.join(File.dirname(__FILE__), "spec", "acceptance", "read_only_adapter_with_stateless_storage")

Expand Down
58 changes: 58 additions & 0 deletions lib/gom/spec/acceptance/adapter_that_needs_setup.rb
@@ -0,0 +1,58 @@

shared_examples_for "an adapter that needs setup" do

before :each do
GOM::Storage.teardown
end

describe "fetching an object" do

it "should raise a #{GOM::Storage::Adapter::NoSetupError}" do
lambda do
GOM::Storage.fetch "test_storage:object_1"
end.should raise_error(GOM::Storage::Adapter::NoSetupError)
end

end

describe "storing an object" do

it "should raise a #{GOM::Storage::Adapter::NoSetupError}" do
lambda do
GOM::Storage.store :object
end.should raise_error(GOM::Storage::Adapter::NoSetupError)
end

end

describe "removing an object" do

it "should raise a #{GOM::Storage::Adapter::NoSetupError}" do
lambda do
GOM::Storage.remove "test_storage:object_1"
end.should raise_error(GOM::Storage::Adapter::NoSetupError)
end

end

describe "fetching a class collection" do

it "should raise a #{GOM::Storage::Adapter::NoSetupError}" do
lambda do
GOM::Storage.collection :test_storage, :test_object_class_view
end.should raise_error(GOM::Storage::Adapter::NoSetupError)
end

end

describe "fetching a map collection" do

it "should raise a #{GOM::Storage::Adapter::NoSetupError}" do
lambda do
GOM::Storage.collection :test_storage, :test_map_view
end.should raise_error(GOM::Storage::Adapter::NoSetupError)
end

end

end
4 changes: 4 additions & 0 deletions lib/gom/spec/acceptance/adapter_with_stateful_storage.rb
Expand Up @@ -5,6 +5,10 @@
GOM::Storage.setup
end

after :all do
GOM::Storage.teardown
end

before :each do
@related_object = GOM::Spec::Object.new
@related_object.number = 16
Expand Down
Expand Up @@ -5,6 +5,10 @@
GOM::Storage.setup
end

after :all do
GOM::Storage.teardown
end

describe "fetching an object" do

it "should return the correct object" do
Expand Down
4 changes: 4 additions & 0 deletions lib/gom/storage.rb
Expand Up @@ -17,6 +17,10 @@ def self.setup
Configuration.setup_all
end

def self.teardown
Configuration.teardown_all
end

def self.fetch(id_string)
id = id_string.is_a?(String) ? GOM::Object::Id.new(id_string) : nil
Fetcher.new(id).object
Expand Down
25 changes: 8 additions & 17 deletions lib/gom/storage/adapter.rb
Expand Up @@ -6,6 +6,9 @@ module Storage
# Base class for a storage adapter
class Adapter

# If the adapter is used without a setup, this error is may raised.
class NoSetupError < StandardError; end

# If a view could not be found, this error is raised.
class ViewNotFoundError < StandardError; end

Expand All @@ -15,24 +18,12 @@ def initialize(configuration)
@configuration = configuration
end

def setup(*arguments)
not_implemented "setup"
end

def fetch(*arguments)
not_implemented "fetch"
end

def store(*arguments)
not_implemented "store"
end

def remove(*arguments)
not_implemented "remove"
end
[ :setup, :teardown, :fetch, :store, :remove, :collection ].each do |key|

def collection(*arguments)
not_implemented "collection"
define_method key do |*arguments|
not_implemented key
end

end

private
Expand Down
10 changes: 10 additions & 0 deletions lib/gom/storage/configuration.rb
Expand Up @@ -21,6 +21,10 @@ def setup
adapter.setup
end

def teardown
adapter.teardown
end

def adapter
@adapter ||= adapter_class.new self
end
Expand Down Expand Up @@ -77,6 +81,12 @@ def self.setup_all
end
end

def self.teardown_all
@configurations.values.each do |configuration|
configuration.teardown
end
end

def self.[](name)
@configurations ||= { }
@configurations[name.to_sym]
Expand Down
4 changes: 3 additions & 1 deletion spec/acceptance/adapter_spec.rb
Expand Up @@ -5,6 +5,8 @@

describe "fake adapter" do

it_should_behave_like "an adapter connected to a stateful storage"
it_should_behave_like "an adapter that needs setup"

# it_should_behave_like "an adapter connected to a stateful storage"

end
4 changes: 4 additions & 0 deletions spec/acceptance/object_spec.rb
Expand Up @@ -10,6 +10,10 @@
@object = Object.new
end

after :each do
GOM::Storage.teardown
end

describe "getting it's id" do

before :each do
Expand Down
8 changes: 8 additions & 0 deletions spec/fake_adapter.rb
Expand Up @@ -37,11 +37,17 @@ def setup
@store = { }
end

def teardown
@store = nil
end

def fetch(id)
raise GOM::Storage::Adapter::NoSetupError unless @store
@store[id]
end

def store(draft)
raise GOM::Storage::Adapter::NoSetupError unless @store
# store relations
draft.relations.each do |key, related_object_proxy|
related_object = related_object_proxy.object
Expand All @@ -60,10 +66,12 @@ def store(draft)
end

def remove(id)
raise GOM::Storage::Adapter::NoSetupError unless @store
@store.delete id
end

def collection(view_name)
raise GOM::Storage::Adapter::NoSetupError unless @store
case view_name.to_sym
when :test_object_class_view
GOM::Object::Collection.new ClassCollectionFetcher.new(@store, configuration.name, "GOM::Spec::Object")
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/gom/storage/adapter_spec.rb
Expand Up @@ -30,7 +30,7 @@

end

[ :setup, :fetch, :store, :remove, :collection ].each do |method_name|
[ :setup, :teardown, :fetch, :store, :remove, :collection ].each do |method_name|

describe "#{method_name}" do

Expand Down
22 changes: 22 additions & 0 deletions spec/lib/gom/storage/configuration_spec.rb
Expand Up @@ -20,6 +20,15 @@

end

describe "teardown" do

it "should call teardown on the adapter instance" do
@adapter.should_receive(:teardown)
@configuration.teardown
end

end

describe "name" do

it "should return the configuration's name" do
Expand Down Expand Up @@ -103,6 +112,19 @@

end

describe "teardown_all" do

before :each do
@configuration.stub(:teardown)
end

it "should call teardown on each configuration" do
@configuration.should_receive(:teardown)
described_class.teardown_all
end

end

describe "default" do

it "should select the first configuration" do
Expand Down
13 changes: 13 additions & 0 deletions spec/lib/gom/storage_spec.rb
Expand Up @@ -26,6 +26,19 @@

end

describe "teardown" do

before :each do
described_class::Configuration.stub(:teardown_all)
end

it "should teardown all storage configurations" do
described_class::Configuration.should_receive(:teardown_all)
described_class.teardown
end

end

describe "fetch" do

before :each do
Expand Down

0 comments on commit 0f50306

Please sign in to comment.