spec/storage_spec.rb in anemone-0.4.0 vs spec/storage_spec.rb in anemone-0.5.0

- old
+ new

@@ -1,7 +1,7 @@ require File.dirname(__FILE__) + '/spec_helper' -%w[pstore tokyo_cabinet].each { |file| require "anemone/storage/#{file}.rb" } +%w[pstore tokyo_cabinet mongodb redis].each { |file| require "anemone/storage/#{file}.rb" } module Anemone describe Storage do it "should have a class method to produce a Hash" do @@ -21,63 +21,87 @@ store = Anemone::Storage.TokyoCabinet(test_file) store.should be_an_instance_of(Anemone::Storage::TokyoCabinet) store.close end + it "should have a class method to produce a MongoDB" do + Anemone::Storage.should respond_to(:MongoDB) + store = Anemone::Storage.MongoDB + store.should be_an_instance_of(Anemone::Storage::MongoDB) + store.close + end + + it "should have a class method to produce a Redis" do + Anemone::Storage.should respond_to(:Redis) + store = Anemone::Storage.Redis + store.should be_an_instance_of(Anemone::Storage::Redis) + store.close + end + module Storage shared_examples_for "storage engine" do + + before(:each) do + @url = SPEC_DOMAIN + @page = Page.new(URI(@url)) + end + it "should implement [] and []=" do @store.should respond_to(:[]) @store.should respond_to(:[]=) - @store['index'] = 'test' - @store['index'].should == 'test' + @store[@url] = @page + @store[@url].url.should == URI(@url) end it "should implement has_key?" do @store.should respond_to(:has_key?) - @store['index'] = 'test' - @store.has_key?('index').should == true + @store[@url] = @page + @store.has_key?(@url).should == true @store.has_key?('missing').should == false end it "should implement delete" do @store.should respond_to(:delete) - @store['index'] = 'test' - @store.delete('index').should == 'test' - @store.has_key?('index').should == false + @store[@url] = @page + @store.delete(@url).url.should == @page.url + @store.has_key?(@url).should == false end it "should implement keys" do @store.should respond_to(:keys) - keys = ['a', 'b', 'c'] - keys.each { |key| @store[key] = key } + urls = [SPEC_DOMAIN, SPEC_DOMAIN + 'test', SPEC_DOMAIN + 'another'] + pages = urls.map { |url| Page.new(URI(url)) } + urls.zip(pages).each { |arr| @store[arr[0]] = arr[1] } - @store.keys.should == keys + (@store.keys - urls).should == [] end it "should implement each" do @store.should respond_to(:each) - keys = ['a', 'b', 'c'] - keys.each { |key| @store[key] = key } + urls = [SPEC_DOMAIN, SPEC_DOMAIN + 'test', SPEC_DOMAIN + 'another'] + pages = urls.map { |url| Page.new(URI(url)) } + urls.zip(pages).each { |arr| @store[arr[0]] = arr[1] } result = {} @store.each { |k, v| result[k] = v } - result.values.should == keys + (result.keys - urls).should == [] + (result.values.map { |page| page.url.to_s } - urls).should == [] end it "should implement merge!, and return self" do @store.should respond_to(:merge!) - hash = {'a' => 'a', 'b' => 'b', 'c' => 'c'} + hash = {SPEC_DOMAIN => Page.new(URI(SPEC_DOMAIN)), + SPEC_DOMAIN + 'test' => Page.new(URI(SPEC_DOMAIN + 'test'))} merged = @store.merge! hash - hash.each { |key, value| @store[key].should == value } + hash.each { |key, value| @store[key].url.to_s.should == key } merged.should === @store end end @@ -113,10 +137,33 @@ end it "should raise an error if supplied with a file extension other than .tch" do lambda { Anemone::Storage.TokyoCabinet('test.tmp') }.should raise_error(RuntimeError) end + end + describe Storage::MongoDB do + it_should_behave_like "storage engine" + + before(:each) do + @store = Storage.MongoDB + end + + after(:each) do + @store.close + end + end + + describe Storage::Redis do + it_should_behave_like "storage engine" + + before(:each) do + @store = Storage.Redis + end + + after(:each) do + @store.close + end end end end end