public
Description: DataMapper - Core
Homepage: http://datamapper.org
Clone URL: git://github.com/sam/dm-core.git
dm-core / spec / integration / associations / many_to_many_spec.rb
100644 450 lines (343 sloc) 11.68 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_helper"))
 
describe DataMapper::Associations::ManyToMany::Proxy do
  before :all do
    class Editor
      include DataMapper::Resource
 
      def self.default_repository_name; ADAPTER end
 
      property :id, Serial
      property :name, String
 
      has n, :books, :through => Resource
    end
 
    Object.send(:remove_const, :Book) if defined?(Book)
 
    class Book
      include DataMapper::Resource
 
      def self.default_repository_name; ADAPTER end
 
      property :id, Serial
      property :title, String
 
      has n, :editors, :through => Resource
    end
  end
 
  before do
    [ Book, Editor, BookEditor ].each { |k| k.auto_migrate! }
 
    repository(ADAPTER) do
      book_1 = Book.create(:title => "Dubliners")
      book_2 = Book.create(:title => "Portrait of the Artist as a Young Man")
      book_3 = Book.create(:title => "Ulysses")
 
      editor_1 = Editor.create(:name => "Jon Doe")
      editor_2 = Editor.create(:name => "Jane Doe")
 
      BookEditor.create(:book => book_1, :editor => editor_1)
      BookEditor.create(:book => book_2, :editor => editor_1)
      BookEditor.create(:book => book_1, :editor => editor_2)
 
      @parent = book_3
      @association = @parent.editors
      @other = [ editor_1 ]
    end
  end
 
  it "should provide #replace" do
    @association.should respond_to(:replace)
  end
 
  describe "#replace" do
    it "should remove the resource from the collection" do
      @association.should have(0).entries
      @association.replace(@other)
      @association.should == @other
    end
 
    it "should not automatically save that the resource was removed from the association" do
      @association.replace(@other)
      @parent.reload.should have(0).editors
    end
 
    it "should return the association" do
      @association.replace(@other).object_id.should == @association.object_id
    end
 
    it "should add the new resources so they will be saved when saving the parent" do
      @association.replace(@other)
      @association.should == @other
      @parent.save
      @association.reload.should == @other
    end
 
    it "should instantiate the remote model if passed an array of hashes" do
      @association.replace([ { :name => "Jim Smith" } ])
      other = [ Editor.first(:name => "Jim Smith") ]
      other.first.should_not be_nil
      @association.should == other
      @parent.save
      @association.reload.should == other
    end
  end
 
  it "should correctly link records" do
    Book.get(1).should have(2).editors
    Book.get(2).should have(1).editors
    Book.get(3).should have(0).editors
    Editor.get(1).should have(2).books
    Editor.get(2).should have(1).books
  end
 
  it "should be able to have associated objects manually added" do
    book = Book.get(3)
    book.should have(0).editors
 
    be = BookEditor.new(:book_id => book.id, :editor_id => 2)
    book.book_editors << be
    book.save
 
    book.reload.should have(1).editors
  end
 
  it "should automatically added necessary through class" do
    book = Book.get(3)
    book.should have(0).editors
 
    book.editors << Editor.get(1)
    book.editors << Editor.new(:name => "Jimmy John")
    book.save
 
    book.reload.should have(2).editors
  end
 
  it "should react correctly to a new record" do
    book = Book.new(:title => "Finnegan's Wake")
    editor = Editor.get(2)
    book.should have(0).editors
    editor.should have(1).books
 
    book.editors << editor
    book.save
 
    book.reload.should have(1).editors
    editor.reload.should have(2).books
  end
 
  it "should be able to delete intermediate model" do
    book = Book.get(1)
    book.should have(2).book_editors
    book.should have(2).editors
 
    be = BookEditor.get(1,1)
    book.book_editors.delete(be)
    book.save
 
    book.reload
    book.should have(1).book_editors
    book.should have(1).editors
  end
 
  it "should be clearable" do
    repository(ADAPTER) do
      book = Book.get(2)
      book.should have(1).book_editors
      book.should have(1).editors
 
      book.editors.clear
      book.save
 
      book.reload
      book.should have(0).book_editors
      book.should have(0).editors
    end
    repository(ADAPTER) do
      Book.get(2).should have(0).editors
    end
  end
 
  it "should be able to delete one object" do
    book = Book.get(1)
    book.should have(2).book_editors
    book.should have(2).editors
 
    editor = book.editors.first
    book.editors.delete(editor)
    book.save
 
    book.reload
    book.should have(1).book_editors
    book.should have(1).editors
    editor.reload.books.should_not include(book)
  end
 
  it "should be destroyable" do
    pending "cannot destroy a collection yet" do
      book = Book.get(2)
      book.should have(1).editors
 
      book.editors.destroy
      book.save
 
      book.reload
      book.should have(0).editors
    end
  end
 
  describe "with natural keys" do
    before :all do
      class Author
        include DataMapper::Resource
 
        def self.default_repository_name; ADAPTER end
 
        property :name, String, :key => true
 
        has n, :books, :through => Resource
      end
 
      class Book
        has n, :authors, :through => Resource
      end
    end
 
    before do
      [ Author, AuthorBook ].each { |k| k.auto_migrate! }
 
      @author = Author.create(:name => "James Joyce")
 
      @book_1 = Book.get!(1)
      @book_2 = Book.get!(2)
      @book_3 = Book.get!(3)
 
      AuthorBook.create(:book => @book_1, :author => @author)
      AuthorBook.create(:book => @book_2, :author => @author)
      AuthorBook.create(:book => @book_3, :author => @author)
    end
 
    it "should have a join resource where the natural key is a property" do
      AuthorBook.properties[:author_name].primitive.should == String
    end
 
    it "should have a join resource where every property is part of the key" do
      AuthorBook.key.should == AuthorBook.properties.to_a
    end
 
    it "should correctly link records" do
      @author.should have(3).books
      @book_1.should have(1).authors
      @book_2.should have(1).authors
      @book_3.should have(1).authors
    end
  end
 
  describe "When join model has non-serial (integer) natural keys." do
    before :all do
      class Tag
        include DataMapper::Resource
 
        def self.default_repository_name; ADAPTER end
 
        property :id, Serial
        property :name, String, :size => 128
 
        has n, :book_taggings
        has n, :books, :through => :book_taggings
      end
 
      class BookTagging
        include DataMapper::Resource
 
        def self.default_repository_name; ADAPTER end
 
        property :book_id, Integer, :key => true
        property :tag_id, Integer, :key => true
 
        belongs_to :book
        belongs_to :tag
      end
 
      class Book
        has n, :book_taggings
        has n, :tags, :through => :book_taggings
      end
    end
 
    before do
      [ Tag, BookTagging ].each { |k| k.auto_migrate! }
 
      @tag_1 = Tag.create(:name => "good")
      @tag_2 = Tag.create(:name => "long")
 
      @book_1 = Book.get!(1)
      @book_2 = Book.get!(2)
      @book_3 = Book.get!(3)
 
      BookTagging.create(:book => @book_2, :tag => @tag_1)
      BookTagging.create(:book => @book_2, :tag => @tag_2)
      BookTagging.create(:book => @book_3, :tag => @tag_2)
    end
 
    it "should fetch all tags for a book" do
      @book_1.tags.should have(0).tags
      @book_2.tags.should have(2).tags
      @book_3.tags.should have(1).tags
    end
 
    it "should allow for adding an association using the << operator" do
      @book_1.book_taggings << @tag_1
      @book_1.tags.should have(0).tags
    end
  end
 
  describe "with renamed associations" do
    before :all do
      class Singer
        include DataMapper::Resource
 
        def self.default_repository_name; ADAPTER end
 
        property :id, Serial
        property :name, String
 
        has n, :tunes, :through => Resource, :class_name => 'Song'
      end
 
      class Song
        include DataMapper::Resource
 
        def self.default_repository_name; ADAPTER end
 
        property :id, Serial
        property :title, String
 
        has n, :performers, :through => Resource, :class_name => 'Singer'
      end
    end
 
    before do
      [ Singer, Song, SingerSong ].each { |k| k.auto_migrate! }
 
      song_1 = Song.create(:title => "Dubliners")
      song_2 = Song.create(:title => "Portrait of the Artist as a Young Man")
      song_3 = Song.create(:title => "Ulysses")
 
      singer_1 = Singer.create(:name => "Jon Doe")
      singer_2 = Singer.create(:name => "Jane Doe")
 
      SingerSong.create(:song => song_1, :singer => singer_1)
      SingerSong.create(:song => song_2, :singer => singer_1)
      SingerSong.create(:song => song_1, :singer => singer_2)
 
      @parent = song_3
      @association = @parent.performers
      @other = [ singer_1 ]
    end
 
    it "should provide #replace" do
      @association.should respond_to(:replace)
    end
 
    it "should correctly link records" do
      Song.get(1).should have(2).performers
      Song.get(2).should have(1).performers
      Song.get(3).should have(0).performers
      Singer.get(1).should have(2).tunes
      Singer.get(2).should have(1).tunes
    end
 
    it "should be able to have associated objects manually added" do
      song = Song.get(3)
      song.should have(0).performers
 
      be = SingerSong.new(:song_id => song.id, :singer_id => 2)
      song.singer_songs << be
      song.save
 
      song.reload.should have(1).performers
    end
 
    it "should automatically added necessary through class" do
      song = Song.get(3)
      song.should have(0).performers
 
      song.performers << Singer.get(1)
      song.performers << Singer.new(:name => "Jimmy John")
      song.save
 
      song.reload.should have(2).performers
    end
 
    it "should react correctly to a new record" do
      song = Song.new(:title => "Finnegan's Wake")
      singer = Singer.get(2)
      song.should have(0).performers
      singer.should have(1).tunes
 
      song.performers << singer
      song.save
 
      song.reload.should have(1).performers
      singer.reload.should have(2).tunes
    end
 
    it "should be able to delete intermediate model" do
      song = Song.get(1)
      song.should have(2).singer_songs
      song.should have(2).performers
 
      be = SingerSong.get(1,1)
      song.singer_songs.delete(be)
      song.save
 
      song.reload
      song.should have(1).singer_songs
      song.should have(1).performers
    end
 
    it "should be clearable" do
      repository(ADAPTER) do
        song = Song.get(2)
        song.should have(1).singer_songs
        song.should have(1).performers
 
        song.performers.clear
        song.save
 
        song.reload
        song.should have(0).singer_songs
        song.should have(0).performers
      end
      repository(ADAPTER) do
        Song.get(2).should have(0).performers
      end
    end
 
    it "should be able to delete one object" do
      song = Song.get(1)
      song.should have(2).singer_songs
      song.should have(2).performers
 
      editor = song.performers.first
      song.performers.delete(editor)
      song.save
 
      song.reload
      song.should have(1).singer_songs
      song.should have(1).performers
      editor.reload.tunes.should_not include(song)
    end
 
    it "should be destroyable" do
      pending "cannot destroy a collection yet" do
        song = Song.get(2)
        song.should have(1).performers
 
        song.performers.destroy
        song.save
 
        song.reload
        song.should have(0).performers
      end
    end
  end
 
end