Skip to content

Instantly share code, notes, and snippets.

@smerritt
Created February 12, 2010 23:49
Show Gist options
  • Save smerritt/303115 to your computer and use it in GitHub Desktop.
Save smerritt/303115 to your computer and use it in GitHub Desktop.
dm_version = '=0.10.3'
gem 'dm-core', dm_version, :git => 'git://github.com/datamapper/dm-core'
gem "data_objects", '=0.10'
gem "do_sqlite3", '=0.10'
only :test do
gem 'rspec', :require_as => 'spec'
end
require 'vendor/gems/environment'
Bundler.require_env
DataMapper.setup(:default, 'sqlite3:///tmp/stives.db')
class Man
include DataMapper::Resource
property :id, Serial
property :name, String
has n, :wives # n should probably be 7, but oh well...
has n, :sacks, :through => :wives
has n, :cats, :through => :sacks
# kits aren't really needed to illustrate these bugs;
# we apologize to all the nursery-rhyme purists
end
class Wife
include DataMapper::Resource
property :id, Serial
property :name, String
belongs_to :man
has n, :sacks
end
class Sack
include DataMapper::Resource
property :id, Serial
belongs_to :wife
has n, :cats
end
class Cat
include DataMapper::Resource
property :id, Serial
property :name, String
belongs_to :sack
end
# DataObjects::Sqlite3.logger = DataObjects::Logger.new(STDOUT, 0)
describe "chained many-to-one relationships" do
def create_man
man = Man.create(
:name => "Albert",
:wives => [
Wife.new(
:name => "Brenda",
:sacks => [
Sack.new(
:cats => [
Cat.new(:name => 'Fluffy'),
Cat.new(:name => 'Hairball'),
]),
Sack.new(
:cats => [
Cat.new(:name => 'Underfoot'),
Cat.new(:name => 'Death of Mice'),
])
]),
Wife.new(
:name => 'Carol',
:sacks => [
Sack.new(
:cats => [
Cat.new(:name => 'Felix'),
Cat.new(:name => 'Tiger'),
]),
Sack.new(
:cats => [
Cat.new(:name => 'Muffin'),
Cat.new(:name => 'Princess')
])
]),
])
raise "invalid test setup" if man.new?
man
end
describe "finding objects" do
before(:all) do
DataMapper.auto_migrate!
@man = create_man
end
it "finds things 1 layer deep" do
@man.wives.size.should == 2
end
it "finds things 2 layers deep" do
@man.sacks.size.should == 4
end
it "finds things 3 layers deep" do
@man.cats.size.should == 8
end
end
describe "destroying objects" do
before(:each) do
DataMapper.auto_migrate!
@man = create_man
end
it "destroys only the specified things (simple many-to-one)" do
lambda do
lambda do
lambda do
lambda do
@man.wives.destroy!
end.should_not change { Man.all.size }
end .should change { Wife.all.size }.by(-2)
end .should_not change { Sack.all.size }
end .should_not change { Cat.all.size }
end
# fails: it deletes the sacks, but also the wives
it "destroys only the specified things (two-level many-to-one)" do
lambda do
lambda do
lambda do
lambda do
@man.sacks.destroy!
end.should_not change { Man.all.size }
end .should_not change { Wife.all.size }
end .should change { Sack.all.size }.by(-4)
end .should_not change { Cat.all.size }
end
# fail: it deletes the cats, but also the sacks and wives
it "destroys only the specified things (three-level many-to-one)" do
lambda do
lambda do
lambda do
lambda do
@man.cats.destroy!
end.should_not change { Man.all.size }
end .should_not change { Wife.all.size }
end .should_not change { Sack.all.size }
end .should change { Cat.all.size }.by(-8)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment