Methods
Public Instance methods
setup()
    # File test/kb_basics_test.rb, line 10
10:   def setup
11:     # recreate_kb_database
12:     # load_kb_fixtures
13:   end
tear_down()
    # File test/kb_basics_test.rb, line 15
15:   def tear_down
16:     # unload_kb_fixtures
17:   end
test_add_records()
    # File test/kb_basics_test.rb, line 19
19:   def test_add_records
20:     # Start with a clean slate - Fixture insert directly to DB; here we test
21:     # insertion through AR::Base.create
22:     recreate_kb_database
23: 
24:     # Plain Inserts:
25:     authors = $db.get_table :authors
26:     assert [], authors.select
27:     
28:     Author.create :name => 'Andy Hunt'
29:     Author.create :name => 'Dave Thomas'
30:     assert_equal ['Andy Hunt', 'Dave Thomas'], authors.select.map { |a| a.name }.sort
31:     
32:     publishers = $db.get_table :publishers
33:     assert [], publishers.select
34:     pragprog = Publisher.create :name => "Pragmatic Programmers' Bookshelf"
35:     assert_equal ["Pragmatic Programmers' Bookshelf"], publishers.select.map { |a| a.name }
36: 
37:     # Inserts with a belongs_to association:
38:     books = $db.get_table :books
39:     assert_equal [], books.select
40:     pickaxe = Book.create :name => 'Programming Ruby (1st Edition)', :published => Date.parse('2000-01-01'), 
41:                 :publisher => pragprog
42:     assert_equal [['Programming Ruby (1st Edition)', Date.parse('2000-01-01'), pragprog.id]],
43:                  books.select.map {|b| [b.name, b.published, b.publisher_id] }
44:     
45:     # Inserts with a belongs_to association:
46:     pages = $db.get_table :pages
47:     assert_equal 0, pages.select.size
48:     Page.create :book => pickaxe, :page_num => 0, :content => 'front cover'
49:     Page.create :book => pickaxe, :page_num => 1, :content => 'chapter 1'*256
50:     Page.create :book => pickaxe, :page_num => 2, :content => 'chapter 2'*512
51:     Page.create :book => pickaxe, :page_num => 3, :content => 'chapter 3'*513
52:     Page.create :book => pickaxe, :page_num => 4, :content => 'back cover'
53:     assert_equal 5, pages.select.size
54:     assert pages.select.all? { |page| page.book_id == pickaxe.id }
55:   end
test_count()
     # File test/kb_basics_test.rb, line 208
208:   def test_count
209:     assert_equal 2, Author.count
210:     assert_equal 1, Author.count {|rec| rec.name =~ /Andy/}
211:     assert_equal 1, Author.count("name = 'Dave Thomas'")
212:     assert_equal 1, Author.count(["name = ?", 'Dave Thomas'])
213:     assert_equal 2, Author.count(nil)
214:   end
test_decrement_counter()
     # File test/kb_basics_test.rb, line 192
192:   def test_decrement_counter
193:     tbl = $db.get_table(:authors)
194:     tbl.drop_column :num_books rescue nil
195:     tbl.add_column :num_books, { :DataType => :Integer, :Default => 2 }
196:     tbl.update_all :num_books => 2
197: 
198:     assert_equal [2, 2], tbl.select.map{|a| a.num_books}
199:     assert_equal [2, 2], Author.find(:all).map{|a| a.num_books}
200: 
201:     Author.decrement_counter 'num_books', 1
202:     assert_equal [1, 2], tbl.select.map{|r| r.num_books}
203: 
204:     Author.decrement_counter 'num_books', [1, 2]
205:     assert_equal [0, 1], tbl.select.map{|r| r.num_books}
206:   end
test_destroy()
     # File test/kb_basics_test.rb, line 153
153:   def test_destroy
154:     authors = $db.get_table(:authors)
155:     assert_equal 2, authors.total_recs
156:     assert_equal 2, Author.find(:all).size
157:     # class level destroy
158:     Author.destroy(1)
159:     assert_equal 1, authors.total_recs
160:     assert_equal 1, Author.find(:all).size
161:     # object destroy
162:     Author.find(:first).destroy
163:     assert_equal 0, authors.total_recs
164:     assert_equal 0, Author.find(:all).size
165:     assert_raise(ActiveRecord::RecordNotFound) { Author.find(1) }
166: 
167:     pages = $db.get_table(:pages)
168:     assert_equal 5, pages.total_recs
169:     Page.destroy [1,2,3]
170:     assert_equal 2, pages.total_recs
171:     Page.destroy [4,5]
172:     assert_equal 0, pages.total_recs
173:     assert_raise(ActiveRecord::RecordNotFound) { Page.destroy(1) }
174:   end
test_find()
    # File test/kb_basics_test.rb, line 57
57:   def test_find
58:     # test plain find :all
59:     assert_equal [@andy, @dave], Author.find(:all)
60: 
61:     # test plain find :first
62:     assert_equal @andy, Author.find(:first)
63: 
64:     # test find with block on :conditions parameter:
65:     assert_equal [@andy].map{|a| a.name}, Author.find(:all, :conditions => lambda{ |rec| rec.name =~ /andy/i }).map{|a| a.name}
66: 
67:     # test find with block:
68:     assert_equal [@dave].map{|a| a.name}, Author.find(:all) { |rec| rec.name =~ /Thomas/ }.map{|a| a.name}
69: 
70:     # test find_by_<property> methods:
71:     assert_equal @dave.name, Author.find_by_name('Dave Thomas').name
72:   end
test_find_by_ids()
    # File test/kb_basics_test.rb, line 74
74:   def test_find_by_ids
75:     # find by IDs
76:     assert_equal @andy, Author.find(1)
77:     assert_equal [@andy, @dave], Author.find(1,2)
78:     assert_equal [@andy, @dave], Author.find([1,2])
79:     pickaxe_id = $db.get_table(:books).select[0].recno
80:     assert_equal @pickaxe, Book.find_by_id(pickaxe_id)
81: 
82:     assert_equal @andy, Author.find('1')
83:     assert_equal [@andy, @dave], Author.find('1','2')
84:   end
test_find_with_sql_fragments()
    # File test/kb_basics_test.rb, line 86
86:   def test_find_with_sql_fragments
87:     # basic SQL strings should work:
88:     assert_equal [@andy], Author.find(:all, :conditions => "name = 'Andy Hunt'")
89: 
90:     # guess find with basic SQL fragment:
91:     assert_nothing_raised() { Author.find(:all, :conditions => ['name =?', 'Andy Hunt']) }
92:     assert_equal [@andy], Author.find(:all, :conditions => ['name =?', 'Andy Hunt'])
93:     assert_equal @dave, Author.find(:first, :conditions => ['name =?', 'Dave Thomas'])
94: 
95:     # test more complex SQL fragments:
96:     assert_equal [@back], Page.find(:all, :conditions => ["book_id = ? AND content = ?", 1, 'back cover'])
97:   end
test_increment_counter()
     # File test/kb_basics_test.rb, line 176
176:   def test_increment_counter
177:     tbl = $db.get_table(:authors)
178:     tbl.drop_column :num_books rescue nil
179:     tbl.add_column :num_books, { :DataType => :Integer, :Default => 0 }
180:     tbl.update_all :num_books => 1
181: 
182:     assert_equal [1, 1], tbl.select.map{|a| a.num_books}
183:     assert_equal [1, 1], Author.find(:all).map{|a| a.num_books}
184: 
185:     Author.increment_counter 'num_books', 1
186:     assert_equal [2, 1], tbl.select.map{|r| r.num_books}
187: 
188:     Author.increment_counter 'num_books', [1, 2]
189:     assert_equal [3, 2], tbl.select.map{|r| r.num_books}
190:   end
test_nil_values()
     # File test/kb_basics_test.rb, line 244
244:   def test_nil_values
245:     NilTest.table.insert(nil, 100)
246:     records = []
247: 
248:     assert_nothing_raised { records = NilTest.find :all, :conditions => lambda{|rec| rec.nil_value > 100} }
249:     assert_equal 0, records.length
250: 
251:     assert_nothing_raised { records = NilTest.find :all, :conditions => lambda{|rec| rec.nil_value > 100 and rec.conditional > 100} }
252:     assert_equal 0, records.length
253: 
254:     assert_nothing_raised { records = NilTest.find :all, :conditions => lambda{|rec| rec.nil_value > 100 or rec.conditional == 100} }
255:     assert_equal 1, records.length
256: 
257:     assert_nothing_raised { records = NilTest.find :all, :conditions => 'nil_value > 100' }
258:     assert_equal 0, records.length
259: 
260:     assert_nothing_raised { records = NilTest.find :all, :conditions => 'nil_value > 100 and conditional > 100' }
261:     assert_equal 0, records.length
262: 
263:     assert_nothing_raised { records = NilTest.find :all, :conditions => 'nil_value > 100 and conditional = 100' }
264:     assert_equal 0, records.length
265: 
266:     assert_nothing_raised { records = NilTest.find :all, :conditions => 'nil_value > 100 or conditional = 100' }
267:     assert_equal 1, records.length
268: 
269:     assert_nothing_raised { records = NilTest.find :all, :conditions => 'nil_value > 100 or conditional > 100' }
270:     assert_equal 0, records.length
271:   end
test_ruby_code_in_conditionals()
     # File test/kb_basics_test.rb, line 216
216:   def test_ruby_code_in_conditionals
217:     DateAndTimeTests.table.insert( Date.new(2005, 1, 1), 1.day.ago )
218:     records = []
219:     
220:     assert_nothing_raised { records = DateAndTimeTests.find :all, :conditions => 'date_value > Date.today' }
221:     assert_equal 0, records.length
222:     
223:     assert_nothing_raised { records = DateAndTimeTests.find :all, :conditions => 'date_value > Date.new(2004-1-1)' }
224:     assert_equal 1, records.length
225:     
226:     assert_nothing_raised { records = DateAndTimeTests.find :all, :conditions => 'time_value > Time.now' }
227:     assert_equal 0, records.length
228:     
229:     assert_nothing_raised { records = DateAndTimeTests.find :all, :conditions => 'time_value < Time.now' }
230:     assert_equal 1, records.length
231: 
232:     class << Object
233:       def now() Time.now end
234:     end
235: 
236:     assert_nothing_raised { records = DateAndTimeTests.find :all, :conditions => 'time_value > now' }
237:     assert_equal 0, records.length
238:     
239:     assert_nothing_raised { records = DateAndTimeTests.find :all, :conditions => 'time_value < now' }
240:     assert_equal 1, records.length
241: 
242:   end
test_update()
     # File test/kb_basics_test.rb, line 99
 99:   def test_update
100:     tbl = $db.get_table(:books)
101:     assert_equal 'Programming Ruby (1st Edition)', tbl.select.first.name
102:     
103:     pickaxe = Book.find :first
104:     pickaxe.name = 'Programming Ruby (2nd Edition)'
105:     pickaxe.publisher_id = 1
106:     pickaxe.save
107: 
108:     assert_equal 1, tbl.select.size
109:     assert_equal 'Programming Ruby (2nd Edition)', tbl.select.first.name
110:   end
test_update_all()
     # File test/kb_basics_test.rb, line 112
112:   def test_update_all
113:     tbl = $db.get_table(:authors)
114:     assert_equal ['Andy Hunt', 'Dave Thomas'], tbl.select.map{|r| r.name}
115:     
116:     Author.update_all lambda{|rec| rec.name = rec.name.upcase}
117:     assert_equal ['ANDY HUNT', 'DAVE THOMAS'], tbl.select.map{|r| r.name}
118:     
119:     Author.update_all lambda{|rec| rec.name = rec.name.downcase}, lambda{|rec| rec.name =~ /Andy/i}
120:     assert_equal ['andy hunt', 'DAVE THOMAS'], tbl.select.map{|r| r.name}
121: 
122:     Author.update_all "name = 'Andy'", lambda{|rec| rec.name =~ /Andy/i}
123:     assert_equal ['Andy', 'DAVE THOMAS'], tbl.select.map{|r| r.name}
124: 
125:     Author.update_all ['name = ?', 'Mr. Hunt'], lambda{|rec| rec.name =~ /Andy/i}
126:     assert_equal ['Mr. Hunt', 'DAVE THOMAS'], tbl.select.map{|r| r.name}
127: 
128:     Author.update_all ['name = ?', 'Dave'], ['name = ?', 'DAVE THOMAS']
129:     assert_equal ['Mr. Hunt', 'Dave'], tbl.select.map{|r| r.name}
130: 
131:     # We can handle simple SQL fragments in conditions
132:     Author.update_all ['num_books = ?', 1], "name = 'Dave'"
133:     assert_equal ['Mr. Hunt', 'Dave'], tbl.select.map{|r| r.name}
134:     assert_equal [0, 1], tbl.select.map{|r| r.num_books}
135:     
136:     assert_nothing_raised { Author.update_all ['name = ?', 'Dave Thomas'], "name = 'Dave' and num_books = 1" }
137:     assert_equal ['Mr. Hunt', 'Dave Thomas'], tbl.select.map{|r| r.name}
138:     
139:     Author.update_all ['name = ?', 'Pragmatic']
140:     assert_equal ['Pragmatic', 'Pragmatic'], tbl.select.map{|r| r.name}
141: 
142:     # Check updates of non-strings
143:     tbl.drop_column :num_books rescue nil
144:     tbl.add_column :num_books, :Integer
145: 
146:     Author.update_all ['num_books = ?', 1]
147:     assert_equal [1, 1], tbl.select.map{|r| r.num_books}
148: 
149:     Author.update_all "num_books = 2"
150:     assert_equal [2, 2], tbl.select.map{|r| r.num_books}
151:   end