rpbertp13-dm-core 0.9.11.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.autotest +26 -0
- data/.gitignore +18 -0
- data/CONTRIBUTING +51 -0
- data/FAQ +92 -0
- data/History.txt +52 -0
- data/MIT-LICENSE +22 -0
- data/Manifest.txt +130 -0
- data/QUICKLINKS +11 -0
- data/README.txt +143 -0
- data/Rakefile +32 -0
- data/SPECS +62 -0
- data/TODO +1 -0
- data/dm-core.gemspec +40 -0
- data/lib/dm-core.rb +217 -0
- data/lib/dm-core/adapters.rb +16 -0
- data/lib/dm-core/adapters/abstract_adapter.rb +209 -0
- data/lib/dm-core/adapters/data_objects_adapter.rb +716 -0
- data/lib/dm-core/adapters/in_memory_adapter.rb +87 -0
- data/lib/dm-core/adapters/mysql_adapter.rb +138 -0
- data/lib/dm-core/adapters/postgres_adapter.rb +189 -0
- data/lib/dm-core/adapters/sqlite3_adapter.rb +105 -0
- data/lib/dm-core/associations.rb +207 -0
- data/lib/dm-core/associations/many_to_many.rb +147 -0
- data/lib/dm-core/associations/many_to_one.rb +107 -0
- data/lib/dm-core/associations/one_to_many.rb +315 -0
- data/lib/dm-core/associations/one_to_one.rb +61 -0
- data/lib/dm-core/associations/relationship.rb +221 -0
- data/lib/dm-core/associations/relationship_chain.rb +81 -0
- data/lib/dm-core/auto_migrations.rb +105 -0
- data/lib/dm-core/collection.rb +670 -0
- data/lib/dm-core/dependency_queue.rb +32 -0
- data/lib/dm-core/hook.rb +11 -0
- data/lib/dm-core/identity_map.rb +42 -0
- data/lib/dm-core/is.rb +16 -0
- data/lib/dm-core/logger.rb +232 -0
- data/lib/dm-core/migrations/destructive_migrations.rb +17 -0
- data/lib/dm-core/migrator.rb +29 -0
- data/lib/dm-core/model.rb +526 -0
- data/lib/dm-core/naming_conventions.rb +84 -0
- data/lib/dm-core/property.rb +676 -0
- data/lib/dm-core/property_set.rb +169 -0
- data/lib/dm-core/query.rb +676 -0
- data/lib/dm-core/repository.rb +167 -0
- data/lib/dm-core/resource.rb +671 -0
- data/lib/dm-core/scope.rb +58 -0
- data/lib/dm-core/support.rb +7 -0
- data/lib/dm-core/support/array.rb +13 -0
- data/lib/dm-core/support/assertions.rb +8 -0
- data/lib/dm-core/support/errors.rb +23 -0
- data/lib/dm-core/support/kernel.rb +11 -0
- data/lib/dm-core/support/symbol.rb +41 -0
- data/lib/dm-core/transaction.rb +252 -0
- data/lib/dm-core/type.rb +160 -0
- data/lib/dm-core/type_map.rb +80 -0
- data/lib/dm-core/types.rb +19 -0
- data/lib/dm-core/types/boolean.rb +7 -0
- data/lib/dm-core/types/discriminator.rb +34 -0
- data/lib/dm-core/types/object.rb +24 -0
- data/lib/dm-core/types/paranoid_boolean.rb +34 -0
- data/lib/dm-core/types/paranoid_datetime.rb +33 -0
- data/lib/dm-core/types/serial.rb +9 -0
- data/lib/dm-core/types/text.rb +10 -0
- data/lib/dm-core/version.rb +3 -0
- data/script/all +4 -0
- data/script/performance.rb +282 -0
- data/script/profile.rb +87 -0
- data/spec/integration/association_spec.rb +1382 -0
- data/spec/integration/association_through_spec.rb +203 -0
- data/spec/integration/associations/many_to_many_spec.rb +449 -0
- data/spec/integration/associations/many_to_one_spec.rb +163 -0
- data/spec/integration/associations/one_to_many_spec.rb +188 -0
- data/spec/integration/auto_migrations_spec.rb +413 -0
- data/spec/integration/collection_spec.rb +1073 -0
- data/spec/integration/data_objects_adapter_spec.rb +32 -0
- data/spec/integration/dependency_queue_spec.rb +46 -0
- data/spec/integration/model_spec.rb +197 -0
- data/spec/integration/mysql_adapter_spec.rb +85 -0
- data/spec/integration/postgres_adapter_spec.rb +731 -0
- data/spec/integration/property_spec.rb +253 -0
- data/spec/integration/query_spec.rb +514 -0
- data/spec/integration/repository_spec.rb +61 -0
- data/spec/integration/resource_spec.rb +513 -0
- data/spec/integration/sqlite3_adapter_spec.rb +352 -0
- data/spec/integration/sti_spec.rb +273 -0
- data/spec/integration/strategic_eager_loading_spec.rb +156 -0
- data/spec/integration/transaction_spec.rb +60 -0
- data/spec/integration/type_spec.rb +275 -0
- data/spec/lib/logging_helper.rb +18 -0
- data/spec/lib/mock_adapter.rb +27 -0
- data/spec/lib/model_loader.rb +100 -0
- data/spec/lib/publicize_methods.rb +28 -0
- data/spec/models/content.rb +16 -0
- data/spec/models/vehicles.rb +34 -0
- data/spec/models/zoo.rb +48 -0
- data/spec/spec.opts +3 -0
- data/spec/spec_helper.rb +91 -0
- data/spec/unit/adapters/abstract_adapter_spec.rb +133 -0
- data/spec/unit/adapters/adapter_shared_spec.rb +15 -0
- data/spec/unit/adapters/data_objects_adapter_spec.rb +632 -0
- data/spec/unit/adapters/in_memory_adapter_spec.rb +98 -0
- data/spec/unit/adapters/postgres_adapter_spec.rb +133 -0
- data/spec/unit/associations/many_to_many_spec.rb +32 -0
- data/spec/unit/associations/many_to_one_spec.rb +159 -0
- data/spec/unit/associations/one_to_many_spec.rb +393 -0
- data/spec/unit/associations/one_to_one_spec.rb +7 -0
- data/spec/unit/associations/relationship_spec.rb +71 -0
- data/spec/unit/associations_spec.rb +242 -0
- data/spec/unit/auto_migrations_spec.rb +111 -0
- data/spec/unit/collection_spec.rb +182 -0
- data/spec/unit/data_mapper_spec.rb +35 -0
- data/spec/unit/identity_map_spec.rb +126 -0
- data/spec/unit/is_spec.rb +80 -0
- data/spec/unit/migrator_spec.rb +33 -0
- data/spec/unit/model_spec.rb +321 -0
- data/spec/unit/naming_conventions_spec.rb +36 -0
- data/spec/unit/property_set_spec.rb +90 -0
- data/spec/unit/property_spec.rb +753 -0
- data/spec/unit/query_spec.rb +571 -0
- data/spec/unit/repository_spec.rb +93 -0
- data/spec/unit/resource_spec.rb +649 -0
- data/spec/unit/scope_spec.rb +142 -0
- data/spec/unit/transaction_spec.rb +469 -0
- data/spec/unit/type_map_spec.rb +114 -0
- data/spec/unit/type_spec.rb +119 -0
- data/tasks/ci.rb +36 -0
- data/tasks/dm.rb +63 -0
- data/tasks/doc.rb +20 -0
- data/tasks/gemspec.rb +23 -0
- data/tasks/hoe.rb +46 -0
- data/tasks/install.rb +20 -0
- metadata +215 -0
@@ -0,0 +1,98 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', "..", 'spec_helper'))
|
2
|
+
|
3
|
+
describe DataMapper::Adapters::InMemoryAdapter do
|
4
|
+
before do
|
5
|
+
DataMapper.setup(:inmem, :adapter => 'in_memory')
|
6
|
+
|
7
|
+
class ::Heffalump
|
8
|
+
include DataMapper::Resource
|
9
|
+
|
10
|
+
def self.default_repository_name
|
11
|
+
:inmem
|
12
|
+
end
|
13
|
+
|
14
|
+
property :color, String, :key => true # TODO: Drop the 'must have a key' limitation
|
15
|
+
property :num_spots, Integer
|
16
|
+
property :striped, Boolean
|
17
|
+
end
|
18
|
+
|
19
|
+
@heff1 = Heffalump.create(:color => 'Black', :num_spots => 0, :striped => true)
|
20
|
+
@heff2 = Heffalump.create(:color => 'Brown', :num_spots => 25, :striped => false)
|
21
|
+
@heff3 = Heffalump.create(:color => 'Blue', :num_spots => nil, :striped => false)
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'should successfully save an object' do
|
25
|
+
@heff1.new_record?.should be_false
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'should be able to get the object' do
|
29
|
+
Heffalump.get('Black').should == @heff1
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should be able to get all the objects' do
|
33
|
+
Heffalump.all.should == [@heff1, @heff2, @heff3]
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'should be able to search for objects with equal value' do
|
37
|
+
Heffalump.all(:striped => true).should == [@heff1]
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'should be able to search for objects included in an array of values' do
|
41
|
+
Heffalump.all(:num_spots => [ 25, 50, 75, 100 ]).should == [@heff2]
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'should be able to search for objects included in a range of values' do
|
45
|
+
Heffalump.all(:num_spots => 25..100).should == [@heff2]
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'should be able to search for objects with nil value' do
|
49
|
+
Heffalump.all(:num_spots => nil).should == [@heff3]
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'should be able to search for objects with not equal value' do
|
53
|
+
Heffalump.all(:striped.not => true).should == [@heff2, @heff3]
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'should be able to search for objects not included in an array of values' do
|
57
|
+
Heffalump.all(:num_spots.not => [ 25, 50, 75, 100 ]).should == [@heff1, @heff3]
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'should be able to search for objects not included in a range of values' do
|
61
|
+
Heffalump.all(:num_spots.not => 25..100).should == [@heff1, @heff3]
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'should be able to search for objects with not nil value' do
|
65
|
+
Heffalump.all(:num_spots.not => nil).should == [@heff1, @heff2]
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'should be able to search for objects that match value' do
|
69
|
+
Heffalump.all(:color.like => 'Bl').should == [@heff1, @heff3]
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'should be able to search for objects with value greater than' do
|
73
|
+
Heffalump.all(:num_spots.gt => 0).should == [@heff2]
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'should be able to search for objects with value greater than or equal to' do
|
77
|
+
Heffalump.all(:num_spots.gte => 0).should == [@heff1, @heff2]
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'should be able to search for objects with value less than' do
|
81
|
+
Heffalump.all(:num_spots.lt => 1).should == [@heff1]
|
82
|
+
end
|
83
|
+
|
84
|
+
it 'should be able to search for objects with value less than or equal to' do
|
85
|
+
Heffalump.all(:num_spots.lte => 0).should == [@heff1]
|
86
|
+
end
|
87
|
+
|
88
|
+
it 'should be able to update an object' do
|
89
|
+
@heff1.num_spots = 10
|
90
|
+
@heff1.save
|
91
|
+
Heffalump.get('Black').num_spots.should == 10
|
92
|
+
end
|
93
|
+
|
94
|
+
it 'should be able to destroy an object' do
|
95
|
+
@heff1.destroy
|
96
|
+
Heffalump.all.size.should == 2
|
97
|
+
end
|
98
|
+
end
|
@@ -0,0 +1,133 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', "..", 'spec_helper'))
|
2
|
+
|
3
|
+
if HAS_POSTGRES
|
4
|
+
describe DataMapper::Adapters::PostgresAdapter do
|
5
|
+
before :all do
|
6
|
+
@adapter = repository(:postgres).adapter
|
7
|
+
end
|
8
|
+
|
9
|
+
describe '#upgrade_model_storage' do
|
10
|
+
before do
|
11
|
+
@repository = mock('repository', :kind_of? => true, :name => :postgres)
|
12
|
+
@model = mock('model', :kind_of? => true, :storage_name => 'models')
|
13
|
+
@property = mock('property', :kind_of? => true, :model => @model, :serial? => true, :field => 'property')
|
14
|
+
|
15
|
+
@model.should_receive(:properties).with(:postgres).any_number_of_times.and_return([@property])
|
16
|
+
|
17
|
+
@command = mock('command')
|
18
|
+
@connection = mock('connection', :create_command => @command, :close => true)
|
19
|
+
@result = mock('result', :to_i => 0)
|
20
|
+
|
21
|
+
DataObjects::Connection.stub!(:new).and_return(@connection)
|
22
|
+
|
23
|
+
@adapter.stub!(:execute).and_return(@result)
|
24
|
+
@adapter.stub!(:storage_exists?).and_return(true)
|
25
|
+
@adapter.stub!(:query).and_return([ 0 ])
|
26
|
+
|
27
|
+
@original_method = @adapter.class.superclass.instance_method(:upgrade_model_storage)
|
28
|
+
@adapter.class.superclass.send(:define_method, :upgrade_model_storage) { |repository, model| }
|
29
|
+
end
|
30
|
+
|
31
|
+
after do
|
32
|
+
method = @original_method
|
33
|
+
@adapter.class.superclass.send(:define_method, :upgrade_model_storage) do |*args|
|
34
|
+
method.bind(self).call(*args)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'should check to make sure the sequences exist' do
|
39
|
+
statement = %q[SELECT COUNT(*) FROM "information_schema"."sequences" WHERE "sequence_name" = ? AND "sequence_schema" = current_schema()]
|
40
|
+
@adapter.should_receive(:query).with(statement, 'models_property_seq').and_return([ 0 ])
|
41
|
+
@adapter.upgrade_model_storage(@repository, @model)
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'should add sequences' do
|
45
|
+
statement = %q[CREATE SEQUENCE "models_property_seq"]
|
46
|
+
@adapter.should_receive(:execute).with(statement)
|
47
|
+
@adapter.upgrade_model_storage(@repository, @model)
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'should execute the superclass upgrade_model_storage' do
|
51
|
+
rv = mock('inside super')
|
52
|
+
@adapter.class.superclass.send(:define_method, :upgrade_model_storage) { |repository, model| rv }
|
53
|
+
@adapter.upgrade_model_storage(@repository, @model).should == rv
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
describe '#create_model_storage' do
|
58
|
+
before do
|
59
|
+
@repository = mock('repository', :kind_of? => true, :name => :postgres)
|
60
|
+
@model = mock('model', :kind_of? => true, :storage_name => 'models')
|
61
|
+
@property = mock('property', :kind_of? => true, :model => @model, :serial? => true, :field => 'property')
|
62
|
+
|
63
|
+
@model.should_receive(:properties).with(:postgres).any_number_of_times.and_return([@property])
|
64
|
+
|
65
|
+
@adapter.stub!(:execute).and_return(@result)
|
66
|
+
@adapter.stub!(:storage_exists?).and_return(true)
|
67
|
+
@adapter.stub!(:query).and_return([ 0 ])
|
68
|
+
|
69
|
+
@original_method = @adapter.class.superclass.instance_method(:create_table_statement)
|
70
|
+
@adapter.class.superclass.send(:define_method, :create_table_statement) {}
|
71
|
+
end
|
72
|
+
|
73
|
+
after do
|
74
|
+
method = @original_method
|
75
|
+
@adapter.class.superclass.send(:define_method, :create_table_statement) do |*args|
|
76
|
+
method.bind(self).call(*args)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'should check to make sure the sequences exist' do
|
81
|
+
statement = %q[SELECT COUNT(*) FROM "information_schema"."sequences" WHERE "sequence_name" = ? AND "sequence_schema" = current_schema()]
|
82
|
+
@adapter.should_receive(:query).with(statement, 'models_property_seq').and_return([ 0 ])
|
83
|
+
@adapter.create_model_storage(@repository, @model)
|
84
|
+
end
|
85
|
+
|
86
|
+
it 'should add sequences' do
|
87
|
+
statement = %q[CREATE SEQUENCE "models_property_seq"]
|
88
|
+
@adapter.should_receive(:execute).with(statement)
|
89
|
+
@adapter.create_model_storage(@repository, @model)
|
90
|
+
end
|
91
|
+
|
92
|
+
it 'should execute the superclass upgrade_model_storage' do
|
93
|
+
rv = mock('inside super')
|
94
|
+
@adapter.class.superclass.send(:define_method, :create_table_statement) { |repository, model| rv }
|
95
|
+
@adapter.create_table_statement(@repository, @model).should == rv
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
describe '#destroy_model_storage' do
|
100
|
+
before do
|
101
|
+
@repository = mock('repository', :kind_of? => true, :name => :postgres)
|
102
|
+
@model = mock('model', :kind_of? => true, :storage_name => 'models')
|
103
|
+
@property = mock('property', :kind_of? => true, :model => @model, :serial? => true, :field => 'property')
|
104
|
+
|
105
|
+
@model.should_receive(:properties).with(:postgres).any_number_of_times.and_return([@property])
|
106
|
+
|
107
|
+
@original_method = @adapter.class.superclass.instance_method(:destroy_model_storage)
|
108
|
+
@adapter.class.superclass.send(:define_method, :destroy_model_storage) {}
|
109
|
+
end
|
110
|
+
|
111
|
+
after do
|
112
|
+
method = @original_method
|
113
|
+
@adapter.class.superclass.send(:define_method, :destroy_model_storage) do |*args|
|
114
|
+
method.bind(self).call(*args)
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
it 'should not execute the superclass destroy_model_storage if the storage does not exist' do
|
119
|
+
rv = mock('inside super')
|
120
|
+
@adapter.class.superclass.send(:define_method, :destroy_model_storage) { |repository, model| rv }
|
121
|
+
@adapter.destroy_model_storage(@repository, @model).should_not == rv
|
122
|
+
end
|
123
|
+
|
124
|
+
it 'should execute the superclass destroy_model_storage if the storage exists' do
|
125
|
+
rv = mock('inside super')
|
126
|
+
@adapter.class.superclass.send(:define_method, :destroy_model_storage) { |repository, model| rv }
|
127
|
+
@adapter.stub!(:storage_exists?).and_return(true)
|
128
|
+
|
129
|
+
@adapter.destroy_model_storage(@repository, @model).should == rv
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper'))
|
2
|
+
|
3
|
+
describe DataMapper::Associations::ManyToMany do
|
4
|
+
|
5
|
+
load_models_for_metaphor :vehicles, :content
|
6
|
+
|
7
|
+
it 'should allow a declaration' do
|
8
|
+
lambda do
|
9
|
+
class ::Supplier
|
10
|
+
has n, :manufacturers, :through => Resource
|
11
|
+
end
|
12
|
+
end.should_not raise_error
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'should handle models inside modules' do
|
16
|
+
lambda do
|
17
|
+
module ::Content
|
18
|
+
class Dialect
|
19
|
+
has n, :locales, :through => Resource, :class_name => "Language::Locale"
|
20
|
+
end
|
21
|
+
|
22
|
+
class Locale
|
23
|
+
has n, :dialects, :through => Resource, :class_name => "Language::Dialect"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end.should_not raise_error
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
describe DataMapper::Associations::ManyToMany::Proxy do
|
32
|
+
end
|
@@ -0,0 +1,159 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper'))
|
2
|
+
|
3
|
+
describe DataMapper::Associations::ManyToOne do
|
4
|
+
|
5
|
+
load_models_for_metaphor :vehicles
|
6
|
+
|
7
|
+
it 'should allow a declaration' do
|
8
|
+
lambda do
|
9
|
+
class ::Vehicle
|
10
|
+
belongs_to :manufacturer
|
11
|
+
end
|
12
|
+
end.should_not raise_error
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe DataMapper::Associations::ManyToOne::Proxy do
|
17
|
+
|
18
|
+
load_models_for_metaphor :vehicles
|
19
|
+
|
20
|
+
before do
|
21
|
+
@child = mock('child', :kind_of? => true)
|
22
|
+
@parent = mock('parent', :nil? => false, :new_record? => false)
|
23
|
+
@relationship = mock('relationship', :kind_of? => true, :get_parent => @parent, :attach_parent => nil)
|
24
|
+
@association = DataMapper::Associations::ManyToOne::Proxy.new(@relationship, @child)
|
25
|
+
|
26
|
+
@association.replace(@parent)
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'should provide #replace' do
|
30
|
+
@association.should respond_to(:replace)
|
31
|
+
end
|
32
|
+
|
33
|
+
describe '#class' do
|
34
|
+
it 'should be forwarded to parent' do
|
35
|
+
@parent.should_receive(:class).and_return(Manufacturer)
|
36
|
+
@association.class.should == Manufacturer
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe '#replace' do
|
41
|
+
before do
|
42
|
+
@other = mock('other parent')
|
43
|
+
end
|
44
|
+
|
45
|
+
before do
|
46
|
+
@relationship.should_receive(:attach_parent).with(@child, @other)
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'should remove the resource from the collection' do
|
50
|
+
@association.should == @parent
|
51
|
+
@association.replace(@other)
|
52
|
+
@association.should == @other
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'should not automatically save that the resource was removed from the association' do
|
56
|
+
@other.should_not_receive(:save)
|
57
|
+
@association.replace(@other)
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'should return the association' do
|
61
|
+
@association.replace(@other).object_id.should == @association.object_id
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'should provide #save' do
|
66
|
+
@association.should respond_to(:replace)
|
67
|
+
end
|
68
|
+
|
69
|
+
describe '#save' do
|
70
|
+
describe 'when the parent is nil' do
|
71
|
+
before do
|
72
|
+
@parent.stub!(:nil?).and_return(true)
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'should not save the parent' do
|
76
|
+
@association.save
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'should return false' do
|
80
|
+
@association.save.should == false
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
describe 'when the parent is not a new record' do
|
85
|
+
before do
|
86
|
+
@parent.should_receive(:new_record?).with(no_args).and_return(false)
|
87
|
+
end
|
88
|
+
|
89
|
+
it 'should not save the parent' do
|
90
|
+
@parent.should_not_receive(:save)
|
91
|
+
@association.save
|
92
|
+
end
|
93
|
+
|
94
|
+
it 'should return true' do
|
95
|
+
@association.save.should == true
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
describe 'when the parent is a new record' do
|
100
|
+
before do
|
101
|
+
@parent.should_receive(:new_record?).with(no_args).and_return(true)
|
102
|
+
end
|
103
|
+
|
104
|
+
it 'should save the parent' do
|
105
|
+
@relationship.should_receive(:with_repository).and_yield
|
106
|
+
@parent.should_receive(:save).with(no_args)
|
107
|
+
@association.save
|
108
|
+
end
|
109
|
+
|
110
|
+
it 'should return the result of the save' do
|
111
|
+
child_key = mock("child_key")
|
112
|
+
child_key.should_receive(:set).and_return(true)
|
113
|
+
parent_key = mock("parent_key")
|
114
|
+
parent_key.should_receive(:get).and_return(1)
|
115
|
+
@relationship.should_receive(:with_repository).and_yield
|
116
|
+
@relationship.should_receive(:child_key).and_return(child_key)
|
117
|
+
@relationship.should_receive(:parent_key).and_return(parent_key)
|
118
|
+
save_results = mock('save results')
|
119
|
+
@parent.should_receive(:save).with(no_args).and_return(save_results)
|
120
|
+
@association.save.object_id.should == save_results.object_id
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
it 'should provide #reload' do
|
126
|
+
@association.should respond_to(:reload)
|
127
|
+
end
|
128
|
+
|
129
|
+
describe '#reload' do
|
130
|
+
before(:each) do
|
131
|
+
@mock_parent = mock('#reload test parent')
|
132
|
+
@association.replace(@mock_parent)
|
133
|
+
end
|
134
|
+
|
135
|
+
it 'should set the @parent ivar to nil' do
|
136
|
+
@association.__send__(:parent).should == @mock_parent # Sanity check.
|
137
|
+
|
138
|
+
# We can't test the value of the instance variable since
|
139
|
+
# #instance_variable_get will be run on the @parent (thanks to
|
140
|
+
# Proxy#method_missing). Instead, test that Relationship#get_parent is
|
141
|
+
# run -- if @parent wasn't set to nil, this expectation should fail.
|
142
|
+
@relationship.should_receive(:get_parent).once.and_return(@mock_parent)
|
143
|
+
@association.reload
|
144
|
+
|
145
|
+
# Trigger #get_parent on the relationship.
|
146
|
+
@association.__send__(:parent)
|
147
|
+
end
|
148
|
+
|
149
|
+
it 'should not change the foreign key in the child' do
|
150
|
+
@relationship.should_not_receive(:attach_parent)
|
151
|
+
@association.reload
|
152
|
+
end
|
153
|
+
|
154
|
+
it 'should return self' do
|
155
|
+
@association.reload.should be_kind_of(DataMapper::Associations::ManyToOne::Proxy)
|
156
|
+
@association.reload.object_id.should == @association.object_id
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
@@ -0,0 +1,393 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper'))
|
2
|
+
|
3
|
+
describe DataMapper::Associations::OneToMany do
|
4
|
+
|
5
|
+
load_models_for_metaphor :vehicles
|
6
|
+
|
7
|
+
before do
|
8
|
+
@class = Class.new do
|
9
|
+
def self.name
|
10
|
+
'User'
|
11
|
+
end
|
12
|
+
|
13
|
+
include DataMapper::Resource
|
14
|
+
|
15
|
+
property :user_id, Integer, :key => true
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'should provide #has' do
|
20
|
+
@class.should respond_to(:has)
|
21
|
+
end
|
22
|
+
|
23
|
+
describe '#has' do
|
24
|
+
it 'should return a Relationship' do
|
25
|
+
@class.has(@class.n, :orders).should be_kind_of(DataMapper::Associations::Relationship)
|
26
|
+
end
|
27
|
+
|
28
|
+
describe 'relationship' do
|
29
|
+
before do
|
30
|
+
@relationship = mock('relationship')
|
31
|
+
DataMapper::Associations::Relationship.stub!(:new).and_return(@relationship)
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'should receive the name' do
|
35
|
+
DataMapper::Associations::Relationship.should_receive(:new) do |name,_,_,_,_|
|
36
|
+
name.should == :orders
|
37
|
+
end
|
38
|
+
@class.has(@class.n, :orders)
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'should receive the repository name' do
|
42
|
+
DataMapper::Associations::Relationship.should_receive(:new) do |_,repository_name,_,_,_|
|
43
|
+
repository_name.should == :mock
|
44
|
+
end
|
45
|
+
repository(:mock) do
|
46
|
+
@class.has(@class.n, :orders)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'should receive the child model name when passed in as class_name' do
|
51
|
+
DataMapper::Associations::Relationship.should_receive(:new) do |_,_,child_model_name,_,_|
|
52
|
+
child_model_name.should == 'Company::Order'
|
53
|
+
end
|
54
|
+
@class.has(@class.n, :orders, :class_name => 'Company::Order')
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'should receive the child model name when class_name not passed in' do
|
58
|
+
DataMapper::Associations::Relationship.should_receive(:new) do |_,_,child_model_name,_,_|
|
59
|
+
child_model_name.should == 'Order'
|
60
|
+
end
|
61
|
+
@class.has(@class.n, :orders)
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'should receive the parent model name' do
|
65
|
+
DataMapper::Associations::Relationship.should_receive(:new) do |_,_,_,parent_model_name,_|
|
66
|
+
parent_model_name.should == @class
|
67
|
+
end
|
68
|
+
@class.has(@class.n, :orders)
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'should receive the parent model name' do
|
72
|
+
options = { :min => 0, :max => 100 }
|
73
|
+
DataMapper::Associations::Relationship.should_receive(:new) do |_,_,_,parent_model_name,_|
|
74
|
+
options.object_id.should == options.object_id
|
75
|
+
end
|
76
|
+
@class.has(@class.n, :orders, options)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'should add an accessor for the proxy' do
|
81
|
+
@class.new.should_not respond_to(:orders)
|
82
|
+
@class.has(@class.n, :orders)
|
83
|
+
@class.new.should respond_to(:orders)
|
84
|
+
end
|
85
|
+
|
86
|
+
describe 'proxy accessor' do
|
87
|
+
before :all do
|
88
|
+
class ::User
|
89
|
+
include DataMapper::Resource
|
90
|
+
end
|
91
|
+
|
92
|
+
class ::Order
|
93
|
+
include DataMapper::Resource
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
it 'should return a OneToMany::Proxy' do
|
98
|
+
@class.has(@class.n, :orders)
|
99
|
+
@class.new.orders.should be_kind_of(DataMapper::Associations::OneToMany::Proxy)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
it 'should work with classes inside modules'
|
105
|
+
end
|
106
|
+
|
107
|
+
describe DataMapper::Associations::OneToMany::Proxy do
|
108
|
+
before do
|
109
|
+
@parent = mock('parent', :new_record? => true, :kind_of? => true)
|
110
|
+
@resource = mock('resource', :null_object => true)
|
111
|
+
@collection = []
|
112
|
+
@parent_key = mock('parent key', :get => [])
|
113
|
+
@repository = mock('repository', :save => nil, :kind_of? => true)
|
114
|
+
@relationship = mock('relationship', :get_children => @collection, :query => {}, :kind_of? => true, :child_key => [], :parent_key => @parent_key)
|
115
|
+
@association = DataMapper::Associations::OneToMany::Proxy.new(@relationship, @parent)
|
116
|
+
end
|
117
|
+
|
118
|
+
describe 'a method that relates the resource', :shared => true do
|
119
|
+
it 'should add the resource to the collection' do
|
120
|
+
@association.should_not include(@resource)
|
121
|
+
do_add.should == return_value
|
122
|
+
@association.should include(@resource)
|
123
|
+
end
|
124
|
+
|
125
|
+
it 'should not automatically save that the resource was added to the association' do
|
126
|
+
@relationship.should_not_receive(:attach_parent)
|
127
|
+
do_add.should == return_value
|
128
|
+
end
|
129
|
+
|
130
|
+
it 'should persist the addition after saving the association' do
|
131
|
+
@relationship.should_receive(:with_repository).with(@resource).and_yield(@repository)
|
132
|
+
do_add.should == return_value
|
133
|
+
@relationship.should_receive(:attach_parent).with(@resource, @parent)
|
134
|
+
@association.save
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
describe 'a method that orphans the resource', :shared => true do
|
139
|
+
before do
|
140
|
+
@association << @resource
|
141
|
+
end
|
142
|
+
|
143
|
+
it 'should remove the resource from the collection' do
|
144
|
+
@association.should include(@resource)
|
145
|
+
do_remove.should == return_value
|
146
|
+
@association.should_not include(@resource)
|
147
|
+
end
|
148
|
+
|
149
|
+
it 'should not automatically save that the resource was removed from the association' do
|
150
|
+
@relationship.should_not_receive(:attach_parent)
|
151
|
+
do_remove.should == return_value
|
152
|
+
end
|
153
|
+
|
154
|
+
it 'should persist the removal after saving the association' do
|
155
|
+
@relationship.should_receive(:with_repository).with(@resource).and_yield(@repository)
|
156
|
+
do_remove.should == return_value
|
157
|
+
@relationship.should_receive(:attach_parent).with(@resource, nil)
|
158
|
+
@association.save
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
it 'should provide #<<' do
|
163
|
+
@association.should respond_to(:<<)
|
164
|
+
end
|
165
|
+
|
166
|
+
describe '#<<' do
|
167
|
+
def do_add
|
168
|
+
@association << @resource
|
169
|
+
end
|
170
|
+
|
171
|
+
def return_value
|
172
|
+
@association
|
173
|
+
end
|
174
|
+
|
175
|
+
it_should_behave_like 'a method that relates the resource'
|
176
|
+
end
|
177
|
+
|
178
|
+
it 'should provide #push' do
|
179
|
+
@association.should respond_to(:push)
|
180
|
+
end
|
181
|
+
|
182
|
+
describe '#push' do
|
183
|
+
def do_add
|
184
|
+
@association.push(@resource)
|
185
|
+
end
|
186
|
+
|
187
|
+
def return_value
|
188
|
+
@association
|
189
|
+
end
|
190
|
+
|
191
|
+
it_should_behave_like 'a method that relates the resource'
|
192
|
+
end
|
193
|
+
|
194
|
+
it 'should provide #unshift' do
|
195
|
+
@association.should respond_to(:unshift)
|
196
|
+
end
|
197
|
+
|
198
|
+
describe '#unshift' do
|
199
|
+
def do_add
|
200
|
+
@association.unshift(@resource)
|
201
|
+
end
|
202
|
+
|
203
|
+
def return_value
|
204
|
+
@association
|
205
|
+
end
|
206
|
+
|
207
|
+
it_should_behave_like 'a method that relates the resource'
|
208
|
+
end
|
209
|
+
|
210
|
+
it 'should provide #replace' do
|
211
|
+
@association.should respond_to(:replace)
|
212
|
+
end
|
213
|
+
|
214
|
+
describe '#replace' do
|
215
|
+
before do
|
216
|
+
@children = [
|
217
|
+
mock('child 1', :save => true),
|
218
|
+
mock('child 2', :save => true),
|
219
|
+
]
|
220
|
+
@collection << @resource
|
221
|
+
@collection.stub!(:loaded?).and_return(true)
|
222
|
+
@relationship.stub!(:attach_parent)
|
223
|
+
end
|
224
|
+
|
225
|
+
def do_replace
|
226
|
+
@association.replace(@children)
|
227
|
+
end
|
228
|
+
|
229
|
+
def return_value
|
230
|
+
@association
|
231
|
+
end
|
232
|
+
|
233
|
+
it 'should remove the resource from the collection' do
|
234
|
+
@association.should include(@resource)
|
235
|
+
do_replace.should == return_value
|
236
|
+
@association.should_not include(@resource)
|
237
|
+
end
|
238
|
+
|
239
|
+
it 'should not automatically save that the resource was removed from the association' do
|
240
|
+
@relationship.should_not_receive(:attach_parent)
|
241
|
+
do_replace.should == return_value
|
242
|
+
end
|
243
|
+
|
244
|
+
it 'should persist the removal after saving the association' do
|
245
|
+
do_replace.should == return_value
|
246
|
+
@relationship.should_receive(:with_repository).exactly(3).times.and_yield(@repository)
|
247
|
+
@relationship.should_receive(:attach_parent).with(@resource, nil)
|
248
|
+
@association.save
|
249
|
+
end
|
250
|
+
|
251
|
+
it 'should not automatically save that the children were added to the association' do
|
252
|
+
@relationship.should_not_receive(:attach_parent)
|
253
|
+
do_replace.should == return_value
|
254
|
+
end
|
255
|
+
|
256
|
+
it 'should persist the addition after saving the association' do
|
257
|
+
do_replace.should == return_value
|
258
|
+
@relationship.should_receive(:with_repository).exactly(3).times.and_yield(@repository)
|
259
|
+
@relationship.should_receive(:attach_parent).with(@children[0], @parent)
|
260
|
+
@relationship.should_receive(:attach_parent).with(@children[1], @parent)
|
261
|
+
@association.save
|
262
|
+
end
|
263
|
+
end
|
264
|
+
|
265
|
+
it 'should provide #pop' do
|
266
|
+
@association.should respond_to(:pop)
|
267
|
+
end
|
268
|
+
|
269
|
+
describe '#pop' do
|
270
|
+
def do_remove
|
271
|
+
@association.pop
|
272
|
+
end
|
273
|
+
|
274
|
+
def return_value
|
275
|
+
@resource
|
276
|
+
end
|
277
|
+
|
278
|
+
it_should_behave_like 'a method that orphans the resource'
|
279
|
+
end
|
280
|
+
|
281
|
+
it 'should provide #shift' do
|
282
|
+
@association.should respond_to(:shift)
|
283
|
+
end
|
284
|
+
|
285
|
+
describe '#shift' do
|
286
|
+
def do_remove
|
287
|
+
@association.shift
|
288
|
+
end
|
289
|
+
|
290
|
+
def return_value
|
291
|
+
@resource
|
292
|
+
end
|
293
|
+
|
294
|
+
it_should_behave_like 'a method that orphans the resource'
|
295
|
+
end
|
296
|
+
|
297
|
+
it 'should provide #delete' do
|
298
|
+
@association.should respond_to(:delete)
|
299
|
+
end
|
300
|
+
|
301
|
+
describe '#delete' do
|
302
|
+
def do_remove
|
303
|
+
@association.delete(@resource)
|
304
|
+
end
|
305
|
+
|
306
|
+
def return_value
|
307
|
+
@resource
|
308
|
+
end
|
309
|
+
|
310
|
+
it_should_behave_like 'a method that orphans the resource'
|
311
|
+
end
|
312
|
+
|
313
|
+
it 'should provide #delete_at' do
|
314
|
+
@association.should respond_to(:delete_at)
|
315
|
+
end
|
316
|
+
|
317
|
+
describe '#delete_at' do
|
318
|
+
def do_remove
|
319
|
+
@association.delete_at(0)
|
320
|
+
end
|
321
|
+
|
322
|
+
def return_value
|
323
|
+
@resource
|
324
|
+
end
|
325
|
+
|
326
|
+
it_should_behave_like 'a method that orphans the resource'
|
327
|
+
end
|
328
|
+
|
329
|
+
it 'should provide #clear' do
|
330
|
+
@association.should respond_to(:clear)
|
331
|
+
end
|
332
|
+
|
333
|
+
describe '#clear' do
|
334
|
+
def do_remove
|
335
|
+
@association.clear
|
336
|
+
end
|
337
|
+
|
338
|
+
def return_value
|
339
|
+
@association
|
340
|
+
end
|
341
|
+
|
342
|
+
it_should_behave_like 'a method that orphans the resource'
|
343
|
+
|
344
|
+
it 'should empty the collection' do
|
345
|
+
@association << mock('other resource', :new_record? => false)
|
346
|
+
@association.should have(2).entries
|
347
|
+
do_remove
|
348
|
+
@association.should be_empty
|
349
|
+
end
|
350
|
+
end
|
351
|
+
|
352
|
+
it 'should provide #reload' do
|
353
|
+
@association.should respond_to(:reload)
|
354
|
+
end
|
355
|
+
|
356
|
+
describe '#reload' do
|
357
|
+
before do
|
358
|
+
@children = [ mock('child 1', :save => true), mock('child 2', :save => true) ]
|
359
|
+
@relationship.stub!(:get_children).and_return(@children)
|
360
|
+
end
|
361
|
+
|
362
|
+
it 'should set the @children ivar to nil' do
|
363
|
+
@association.__send__(:children).should == @children # Sanity check.
|
364
|
+
|
365
|
+
# We can't test the value of the @children instance variable since
|
366
|
+
# #instance_variable_get will be run on @children (thanks to
|
367
|
+
# Proxy#method_missing). Instead, test that Relationship#get_children is
|
368
|
+
# run -- if @children wasn't set to nil, this expectation should fail.
|
369
|
+
@relationship.should_receive(:get_children).once.and_return(@children)
|
370
|
+
@association.reload
|
371
|
+
|
372
|
+
# Trigger #get_children on the relationship.
|
373
|
+
@association.__send__(:children).should == @children
|
374
|
+
end
|
375
|
+
|
376
|
+
it 'should return self' do
|
377
|
+
@association.reload.should be_kind_of(DataMapper::Associations::OneToMany::Proxy)
|
378
|
+
@association.reload.object_id.should == @association.object_id
|
379
|
+
end
|
380
|
+
end
|
381
|
+
|
382
|
+
describe 'when deleting the parent' do
|
383
|
+
it 'should delete all the children without calling destroy if relationship :dependent is :delete_all'
|
384
|
+
|
385
|
+
it 'should destroy all the children if relationship :dependent is :destroy'
|
386
|
+
|
387
|
+
it 'should set the parent key for each child to nil if relationship :dependent is :nullify'
|
388
|
+
|
389
|
+
it 'should restrict the parent from being deleted if a child remains if relationship :dependent is restrict'
|
390
|
+
|
391
|
+
it 'should be restrict by default if relationship :dependent is not specified'
|
392
|
+
end
|
393
|
+
end
|