Methods
- setup
- tear_down
- test_belongs_to
- test_has_and_belongs_to_many
- test_has_many
- test_has_many_destroy_dependents
- test_has_one
Public Instance methods
[ show source ]
# File test/kb_associations_test.rb, line 9
9: def setup
10: # recreate_kb_database
11: # load_kb_fixtures
12: end
[ show source ]
# File test/kb_associations_test.rb, line 14
14: def tear_down
15: # unload_kb_fixtures
16: end
[ show source ]
# File test/kb_associations_test.rb, line 31
31: def test_belongs_to
32: @cvs = Book.create :publisher_id => @pragprog.id, :name => 'Pragmatic CVS', :published => Date.today
33: assert_equal @pragprog, @cvs.publisher
34: assert_equal @pragprog.id, $db.get_table(:books).select{|b|b.name =~ /CVS/}[0].publisher_id
35:
36: @svn = Book.create :publisher => @pragprog, :name => 'Pragmatic SVN', :published => Date.today
37: assert_equal @pragprog, @svn.publisher
38: assert_equal @pragprog.id, $db.get_table(:books).select{|b|b.name =~ /SVN/}[0].publisher_id
39:
40: assert_equal @pragprog.id, $db.get_table(:books).select{|b| b.name =~ /ruby/i}[0].publisher_id
41: book = Book.find(:first) {|b| b.name =~ /ruby/i}
42: assert_equal @pickaxe, book
43: assert_equal @pragprog, book.publisher
44:
45: book.publisher = nil
46: book.save
47:
48: assert_equal nil, $db.get_table(:books).select{|b|b.name =~ /ruby/i}[0].publisher_id
49: assert_equal nil, book.publisher
50: assert_equal nil, Book.find(:first).publisher
51: end
[ show source ]
# File test/kb_associations_test.rb, line 86
86: def test_has_and_belongs_to_many
87: assert_equal 2, $db.get_table(:authors_books).select.size
88: assert_equal [@andy, @dave], @pickaxe.author
89: assert_equal 2, @pickaxe.author.size
90:
91: assert_equal [@pickaxe], @andy.book
92: assert_equal [@pickaxe], @dave.book
93: assert_equal 1, @andy.book.size
94: assert_equal 1, @dave.book.size
95:
96: @cvs = Book.create :publisher_id => @pragprog.id, :name => 'Pragmatic CVS', :published => Date.today
97: @cvs.author << @andy
98: @svn = Book.create :publisher => @pragprog, :name => 'Pragmatic SVN', :published => Date.today
99: @svn.author << @dave
100:
101: assert_equal [@andy], @cvs.author
102: assert_equal [@dave], @svn.author
103: @andy.reload; @dave.reload
104: assert_equal [@pickaxe, @cvs], @andy.book
105: assert_equal [@pickaxe, @svn], @dave.book
106: end
[ show source ]
# File test/kb_associations_test.rb, line 53
53: def test_has_many
54: assert_equal 5, Book.find(:first).pages.size
55: assert_equal [@front, @chap1, @chap2, @annex, @back].sort_by(:id), @pickaxe.pages.sort_by(:id)
56:
57: assert_equal [@chap1, @chap2].sort_by(:id), @pickaxe.pages.find(:all, :conditions => lambda{|rec| rec.book_id == @pickaxe.id and rec.content =~ /text/}).sort_by(:id)
58: end
[ show source ]
# File test/kb_associations_test.rb, line 60
60: def test_has_many_destroy_dependents
61: book = Book.create :name => 'pulp fiction', :published => Date.today
62: page1 = Page.create :book => book, :page_num => 1, :content => 'text 1'
63: page2 = Page.create :book => book, :page_num => 2, :content => 'text 2'
64: page3 = Page.create :book => book, :page_num => 3, :content => 'text 3'
65:
66: book.pages << page1
67: book.pages << [page2, page3]
68: assert_equal 2, book.id
69:
70: assert_equal 2, Book.find(:all).size
71: assert_equal 8, Page.find(:all).size
72: assert_equal 5, Book.find(1).pages.size
73: assert_equal 3, Book.find(2).pages.size
74:
75: Book.destroy(2)
76: assert_raise(ActiveRecord::RecordNotFound) { Book.find(2) }
77: assert_raise(ActiveRecord::RecordNotFound) { Page.find(page1.id) }
78: assert_raise(ActiveRecord::RecordNotFound) { Page.find(page2.id) }
79: assert_raise(ActiveRecord::RecordNotFound) { Page.find(page3.id) }
80:
81: assert_equal 1, Book.find(:all).size
82: assert_equal 5, Page.find(:all).size
83: assert_equal 5, Book.find(1).pages.size
84: end
[ show source ]
# File test/kb_associations_test.rb, line 18
18: def test_has_one
19: @err = Errata.create :book_id => 1, :contents => 'page 1: no text'
20:
21: assert_equal @pickaxe, Errata.find(:first).book
22: assert_equal @err, Book.find(:first).errata
23:
24: @cvs = Book.create :name => 'Pragmatic CVS', :published => 1.year.ago.to_date, :publisher => @pragprogs
25: @err = Errata.create :book => @cvs, :contents => 'use SVN'
26: @cvs.reload; @err.reload
27: assert_equal @cvs, Errata.find(:first, :conditions => lambda{|r| r.book_id == @cvs.id}).book
28: assert_equal @err, Book.find(:first, :conditions => lambda{|r| r.name =~ /CVS/}).errata
29: end