Skip to content

Commit

Permalink
Moved mock_adapter into spec/lib
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Kubb committed Apr 10, 2008
1 parent 81d2787 commit cef2804
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 72 deletions.
10 changes: 5 additions & 5 deletions environment.rb
Expand Up @@ -2,26 +2,26 @@
# Require the DataMapper, and a Mock Adapter. # Require the DataMapper, and a Mock Adapter.
require 'pathname' require 'pathname'
require Pathname(__FILE__).dirname.expand_path + 'lib/data_mapper' require Pathname(__FILE__).dirname.expand_path + 'lib/data_mapper'
require __DIR__ + 'spec/mock_adapter' require __DIR__ + 'spec/lib/mock_adapter'
require 'fileutils' require 'fileutils'


adapter = ENV["ADAPTER"] || "sqlite3" adapter = ENV["ADAPTER"] || "sqlite3"

repository_uri = URI.parse case ENV["ADAPTER"] repository_uri = URI.parse case ENV["ADAPTER"]
when 'mysql' then "mysql://localhost/data_mapper_1" when 'mysql' then "mysql://localhost/data_mapper_1"
when 'postgres' then "postgres://localhost/data_mapper_1" when 'postgres' then "postgres://localhost/data_mapper_1"
else "sqlite3://#{Dir.getwd}/data_mapper_1.db" else "sqlite3://#{Dir.getwd}/data_mapper_1.db"
end end


DataMapper.setup(:default, "mock://localhost") DataMapper.setup(:default, "mock://localhost")

# Determine log path. # Determine log path.
ENV['_'] =~ /(\w+)/ ENV['_'] =~ /(\w+)/
log_path = __DIR__ + "log/#{$1 == 'opt' ? 'spec' : $1}.log" log_path = __DIR__ + "log/#{$1 == 'opt' ? 'spec' : $1}.log"

FileUtils::mkdir_p(File.dirname(log_path)) FileUtils::mkdir_p(File.dirname(log_path))
# FileUtils::rm(log_path) if File.exists?(log_path) # FileUtils::rm(log_path) if File.exists?(log_path)

DataMapper::Logger.new(log_path, 0) DataMapper::Logger.new(log_path, 0)
at_exit { DataMapper.logger.close } at_exit { DataMapper.logger.close }


Expand Down
2 changes: 1 addition & 1 deletion spec/integration/association_spec.rb
@@ -1,7 +1,7 @@
require 'pathname' require 'pathname'
require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper' require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'


require __DIR__.parent.parent + 'lib/data_mapper' require ROOT_DIR + 'lib/data_mapper'


begin begin
require 'do_sqlite3' require 'do_sqlite3'
Expand Down
6 changes: 3 additions & 3 deletions spec/integration/mysql_adapter_spec.rb
@@ -1,17 +1,17 @@
require 'pathname' require 'pathname'
require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper' require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'


require __DIR__.parent.parent + 'lib/data_mapper' require ROOT_DIR + 'lib/data_mapper'


begin begin
require 'do_mysql' require 'do_mysql'


DataMapper.setup(:mysql, "mysql://localhost/dm_integration_test") DataMapper.setup(:mysql, "mysql://localhost/dm_integration_test")

rescue LoadError => e rescue LoadError => e
describe 'do_mysql' do describe 'do_mysql' do
it 'should be required' do it 'should be required' do
fail "MySQL integration specs not run! Could not load do_mysql: #{e}" fail "MySQL integration specs not run! Could not load do_mysql: #{e}"
end end
end end
end end
2 changes: 1 addition & 1 deletion spec/integration/postgres_adapter_spec.rb
@@ -1,7 +1,7 @@
require 'pathname' require 'pathname'
require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper' require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'


require __DIR__.parent.parent + 'lib/data_mapper' require ROOT_DIR + 'lib/data_mapper'


begin begin
require 'do_postgres' require 'do_postgres'
Expand Down
24 changes: 12 additions & 12 deletions spec/integration/property_spec.rb
Expand Up @@ -8,11 +8,11 @@


describe DataMapper::Property do describe DataMapper::Property do
describe" tracking strategies" do describe" tracking strategies" do

before do before do
class Actor class Actor
include DataMapper::Resource include DataMapper::Resource

property :id, Fixnum, :serial => true property :id, Fixnum, :serial => true
property :name, String, :lock => true property :name, String, :lock => true
property :notes, DataMapper::Types::Text, :track => false property :notes, DataMapper::Types::Text, :track => false
Expand All @@ -23,7 +23,7 @@ class Actor
property :agent, String, :track => :hash # :track only Object#hash value on :load. property :agent, String, :track => :hash # :track only Object#hash value on :load.
# Potentially faster, but less safe, so use judiciously, when the odds of a hash-collision are low. # Potentially faster, but less safe, so use judiciously, when the odds of a hash-collision are low.
end end

@adapter = DataMapper::Repository.adapters[:sqlite3] || DataMapper.setup(:sqlite3, "sqlite3://#{__DIR__}/integration_test.db") @adapter = DataMapper::Repository.adapters[:sqlite3] || DataMapper.setup(:sqlite3, "sqlite3://#{__DIR__}/integration_test.db")
@adapter.execute <<-EOS.compress_lines @adapter.execute <<-EOS.compress_lines
CREATE TABLE actors ( CREATE TABLE actors (
Expand All @@ -38,52 +38,52 @@ class Actor
) )
EOS EOS
end end

it "false" do it "false" do
pending("Implementation...") pending("Implementation...")
DataMapper::Resource::DIRTY.should_not be_nil DataMapper::Resource::DIRTY.should_not be_nil
bob = Actor.new(:name => 'bob') bob = Actor.new(:name => 'bob')
bob.original_attributes.should have_key(:name) bob.original_attributes.should have_key(:name)
bob.original_attributes[:name].should == DataMapper::Resource::DIRTY bob.original_attributes[:name].should == DataMapper::Resource::DIRTY
end end

it ":load" do it ":load" do
pending("Implementation...") pending("Implementation...")
DataMapper::Resource::DIRTY.should_not be_nil DataMapper::Resource::DIRTY.should_not be_nil
bob = Actor.new(:name => 'bob') bob = Actor.new(:name => 'bob')
bob.original_attributes.should have_key(:name) bob.original_attributes.should have_key(:name)
bob.original_attributes[:name].should == DataMapper::Resource::DIRTY bob.original_attributes[:name].should == DataMapper::Resource::DIRTY
end end

it ":hash" do it ":hash" do
pending("Implementation...") pending("Implementation...")
DataMapper::Resource::DIRTY.should_not be_nil DataMapper::Resource::DIRTY.should_not be_nil
bob = Actor.new(:name => 'bob') bob = Actor.new(:name => 'bob')
bob.original_attributes.should have_key(:name) bob.original_attributes.should have_key(:name)
bob.original_attributes[:name].should == DataMapper::Resource::DIRTY bob.original_attributes[:name].should == DataMapper::Resource::DIRTY
end end

it ":get" do it ":get" do
pending("Implementation...") pending("Implementation...")
DataMapper::Resource::DIRTY.should_not be_nil DataMapper::Resource::DIRTY.should_not be_nil
bob = Actor.new(:name => 'bob') bob = Actor.new(:name => 'bob')
bob.original_attributes.should have_key(:name) bob.original_attributes.should have_key(:name)
bob.original_attributes[:name].should == DataMapper::Resource::DIRTY bob.original_attributes[:name].should == DataMapper::Resource::DIRTY
end end

it ":set" do it ":set" do
pending("Implementation...") pending("Implementation...")
DataMapper::Resource::DIRTY.should_not be_nil DataMapper::Resource::DIRTY.should_not be_nil
bob = Actor.new(:name => 'bob') bob = Actor.new(:name => 'bob')
bob.original_attributes.should have_key(:name) bob.original_attributes.should have_key(:name)
bob.original_attributes[:name].should == DataMapper::Resource::DIRTY bob.original_attributes[:name].should == DataMapper::Resource::DIRTY
end end

after do after do
@adapter.execute("DROP TABLE actors") @adapter.execute("DROP TABLE actors")
end end
end end

describe "lazy loading" do describe "lazy loading" do
before do before do
@adapter = DataMapper::Repository.adapters[:sqlite3] || DataMapper.setup(:sqlite3, "sqlite3://#{__DIR__}/integration_test.db") @adapter = DataMapper::Repository.adapters[:sqlite3] || DataMapper.setup(:sqlite3, "sqlite3://#{__DIR__}/integration_test.db")
Expand Down Expand Up @@ -137,10 +137,10 @@ def property_by_name(name)
@adapter.execute('DROP TABLE "sail_boats"') @adapter.execute('DROP TABLE "sail_boats"')
end end


end end


end end

rescue LoadError rescue LoadError
warn "integration/property_spec not run! Could not load do_sqlite3." warn "integration/property_spec not run! Could not load do_sqlite3."
end end
26 changes: 13 additions & 13 deletions spec/integration/query_spec.rb
@@ -1,7 +1,7 @@
require 'pathname' require 'pathname'
require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper' require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'


require __DIR__.parent.parent + 'lib/data_mapper' require ROOT_DIR + 'lib/data_mapper'


begin begin
require 'do_sqlite3' require 'do_sqlite3'
Expand Down Expand Up @@ -261,29 +261,29 @@ class Vehicle
DataMapper::Query.new(Vehicle,:links=>[:sailing]) DataMapper::Query.new(Vehicle,:links=>[:sailing])
}.should raise_error(ArgumentError) }.should raise_error(ArgumentError)
end end

it 'should create an n-level query path' do it 'should create an n-level query path' do
Vehicle.factory.region.model.should == Region Vehicle.factory.region.model.should == Region
Vehicle.factory.region.name.property.should == Region.properties(Region.repository.name)[:name] Vehicle.factory.region.name.property.should == Region.properties(Region.repository.name)[:name]
end end

it 'should accept a DM::QueryPath as the key to a condition' do it 'should accept a DM::QueryPath as the key to a condition' do
repository(:sqlite3) do repository(:sqlite3) do
vehicle = Vehicle.first(Vehicle.factory.region.name => 'North West') vehicle = Vehicle.first(Vehicle.factory.region.name => 'North West')
vehicle.name.should == '10 ton delivery truck' vehicle.name.should == '10 ton delivery truck'
end end
end end


it 'should auto generate the link if a DM::Property from a different resource is in the :fields option' it 'should auto generate the link if a DM::Property from a different resource is in the :fields option'
it 'should create links with composite keys' it 'should create links with composite keys'


it 'should eager load associations' do it 'should eager load associations' do
repository(:sqlite3) do repository(:sqlite3) do
vehicle = Vehicle.first(:includes => [Vehicle.factory]) vehicle = Vehicle.first(:includes => [Vehicle.factory])
end end
end end


after do after do
@adapter.execute('DROP TABLE "regions"') @adapter.execute('DROP TABLE "regions"')
Expand Down
4 changes: 2 additions & 2 deletions spec/integration/repository_spec.rb
@@ -1,7 +1,7 @@
require 'pathname' require 'pathname'
require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper' require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'


require __DIR__.parent.parent + 'lib/data_mapper' require ROOT_DIR + 'lib/data_mapper'




begin begin
Expand Down Expand Up @@ -73,4 +73,4 @@ class SerialFinderSpec
end end
rescue LoadError rescue LoadError
warn "integration/repository_spec not run! Could not load do_sqlite3." warn "integration/repository_spec not run! Could not load do_sqlite3."
end end
4 changes: 2 additions & 2 deletions spec/integration/sqlite3_adapter_spec.rb
@@ -1,7 +1,7 @@
require 'pathname' require 'pathname'
require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper' require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'


require __DIR__.parent.parent + 'lib/data_mapper' require ROOT_DIR + 'lib/data_mapper'


begin begin
require 'do_sqlite3' require 'do_sqlite3'
Expand Down Expand Up @@ -221,7 +221,7 @@ class BankCustomer
after do after do
@adapter.execute('DROP TABLE "bank_customers"') @adapter.execute('DROP TABLE "bank_customers"')
end end
end end
end end
rescue LoadError => e rescue LoadError => e
describe 'do_sqlite3' do describe 'do_sqlite3' do
Expand Down
39 changes: 19 additions & 20 deletions spec/integration/type_spec.rb
Expand Up @@ -2,92 +2,91 @@
require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper' require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
require 'faster_csv' require 'faster_csv'



begin begin
require 'do_sqlite3' require 'do_sqlite3'

DataMapper.setup(:sqlite3, "sqlite3://#{__DIR__}/integration_test.db") unless DataMapper::Repository.adapters[:sqlite3] DataMapper.setup(:sqlite3, "sqlite3://#{__DIR__}/integration_test.db") unless DataMapper::Repository.adapters[:sqlite3]


describe DataMapper::Type do describe DataMapper::Type do


before do before do

@adapter = repository(:sqlite3).adapter @adapter = repository(:sqlite3).adapter
@adapter.execute("CREATE TABLE coconuts (id INTEGER PRIMARY KEY, faked TEXT, document TEXT, stuff TEXT)") @adapter.execute("CREATE TABLE coconuts (id INTEGER PRIMARY KEY, faked TEXT, document TEXT, stuff TEXT)")

module TypeTests module TypeTests
class Impostor < DataMapper::Type class Impostor < DataMapper::Type
primitive String primitive String
end end

class Coconut class Coconut
include DataMapper::Resource include DataMapper::Resource

resource_names[:sqlite3] = 'coconuts' resource_names[:sqlite3] = 'coconuts'

property :id, Fixnum, :serial => true property :id, Fixnum, :serial => true
property :faked, Impostor property :faked, Impostor
property :document, DM::Csv property :document, DM::Csv
property :stuff, DM::Yaml property :stuff, DM::Yaml
end end
end end

@document = <<-EOS.margin @document = <<-EOS.margin
NAME, RATING, CONVENIENCE NAME, RATING, CONVENIENCE
Freebird's, 3, 3 Freebird's, 3, 3
Whataburger, 1, 5 Whataburger, 1, 5
Jimmy John's, 3, 4 Jimmy John's, 3, 4
Mignon, 5, 2 Mignon, 5, 2
Fuzi Yao's, 5, 1 Fuzi Yao's, 5, 1
Blue Goose, 5, 1 Blue Goose, 5, 1
EOS EOS

@stuff = YAML::dump({ 'Happy Cow!' => true, 'Sad Cow!' => false }) @stuff = YAML::dump({ 'Happy Cow!' => true, 'Sad Cow!' => false })
end end

it "should instantiate an object with custom types" do it "should instantiate an object with custom types" do
coconut = TypeTests::Coconut.new(:faked => 'bob', :document => @document, :stuff => @stuff) coconut = TypeTests::Coconut.new(:faked => 'bob', :document => @document, :stuff => @stuff)
coconut.faked.should == 'bob' coconut.faked.should == 'bob'
coconut.document.should be_a_kind_of(Array) coconut.document.should be_a_kind_of(Array)
coconut.stuff.should be_a_kind_of(Hash) coconut.stuff.should be_a_kind_of(Hash)
end end

it "should CRUD an object with custom types" do it "should CRUD an object with custom types" do
repository(:sqlite3) do repository(:sqlite3) do
coconut = TypeTests::Coconut.new(:faked => 'bob', :document => @document, :stuff => @stuff) coconut = TypeTests::Coconut.new(:faked => 'bob', :document => @document, :stuff => @stuff)
coconut.save.should be_true coconut.save.should be_true
coconut.id.should_not be_nil coconut.id.should_not be_nil

fred = TypeTests::Coconut[coconut.id] fred = TypeTests::Coconut[coconut.id]
fred.faked.should == 'bob' fred.faked.should == 'bob'
fred.document.should be_a_kind_of(Array) fred.document.should be_a_kind_of(Array)
fred.stuff.should be_a_kind_of(Hash) fred.stuff.should be_a_kind_of(Hash)


texadelphia = ["Texadelphia", "5", "3"] texadelphia = ["Texadelphia", "5", "3"]

# Figure out how to track these... possibly proxies? :-p # Figure out how to track these... possibly proxies? :-p
document = fred.document.dup document = fred.document.dup
document << texadelphia document << texadelphia
fred.document = document fred.document = document

stuff = fred.stuff.dup stuff = fred.stuff.dup
stuff['Manic Cow!'] = :maybe stuff['Manic Cow!'] = :maybe
fred.stuff = stuff fred.stuff = stuff

fred.save.should be_true fred.save.should be_true

# Can't call coconut.reload! since coconut.loaded_set isn't setup. # Can't call coconut.reload! since coconut.loaded_set isn't setup.
mac = TypeTests::Coconut[fred.id] mac = TypeTests::Coconut[fred.id]
mac.document.last.should == texadelphia mac.document.last.should == texadelphia
mac.stuff['Manic Cow!'].should == :maybe mac.stuff['Manic Cow!'].should == :maybe
end end
end end

after do after do
@adapter = repository(:sqlite3).adapter @adapter = repository(:sqlite3).adapter
@adapter.execute("DROP TABLE coconuts") @adapter.execute("DROP TABLE coconuts")
end end
end end
rescue LoadError rescue LoadError
warn "integration/type_spec not run! Could not load do_sqlite3." warn "integration/type_spec not run! Could not load do_sqlite3."
end end
10 changes: 4 additions & 6 deletions spec/mock_adapter.rb → spec/lib/mock_adapter.rb
@@ -1,15 +1,13 @@
require 'pathname' require ROOT_DIR + 'lib/data_mapper/adapters/data_objects_adapter'
require Pathname(__FILE__).dirname.expand_path + 'spec_helper'
require __DIR__.parent + 'lib/data_mapper/adapters/data_objects_adapter'


module DataMapper module DataMapper
module Adapters module Adapters
class MockAdapter < DataMapper::Adapters::DataObjectsAdapter class MockAdapter < DataMapper::Adapters::DataObjectsAdapter

def create(repository, instance) def create(repository, instance)
instance instance
end end

end end
end end
end end

0 comments on commit cef2804

Please sign in to comment.