ackbar 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,27 @@
1
+ print "Using native KirbyBase\n"
2
+ require_dependency 'fixtures/course'
3
+ require 'logger'
4
+ require 'create_dbs_for_ar_tests'
5
+
6
+ db1_path = File.expand_path File.join(File.dirname(__FILE__), 'db/activerecord_unittest')
7
+ db2_path = File.expand_path File.join(File.dirname(__FILE__), 'db/activerecord_unittest2')
8
+
9
+ [db1_path, db2_path].each_with_index do |path, idx|
10
+ FileUtils.rm_rf(path) rescue nil
11
+ FileUtils.mkdir(path) rescue Errno::EEXIST nil
12
+ end
13
+
14
+ db = KirbyBase.new :local, nil, nil, db1_path
15
+ create_and_init db
16
+ db2 = KirbyBase.new :local, nil, nil, db2_path
17
+ create_and_init_2 db2
18
+
19
+ ActiveRecord::Base.establish_connection(
20
+ :adapter => "kirbybase",
21
+ :database => db1_path
22
+ )
23
+
24
+ Course.establish_connection(
25
+ :adapter => "kirbybase",
26
+ :database => db2_path
27
+ )
@@ -0,0 +1,171 @@
1
+ require 'kirbybase'
2
+
3
+ def create_and_init db
4
+ db.create_table(:accounts,
5
+ :firm_id, { :DataType => :Integer, :Default => nil },
6
+ :credit_limit, { :DataType => :Integer, :Default => nil }
7
+ )
8
+
9
+ db.create_table(:companies,
10
+ :type, { :DataType => :String, :Default => nil },
11
+ :ruby_type, { :DataType => :String, :Default => nil },
12
+ :firm_id, { :DataType => :Integer, :Default => nil },
13
+ :name, { :DataType => :String, :Default => nil },
14
+ :client_of, { :DataType => :Integer, :Default => nil },
15
+ :rating, { :DataType => :Integer, :Default => 1 }
16
+ )
17
+
18
+
19
+ db.create_table(:topics,
20
+ :title, { :DataType => :String, :Default => nil },
21
+ :author_name, { :DataType => :String, :Default => nil },
22
+ :author_email_address, { :DataType => :String, :Default => nil },
23
+ :written_on, { :DataType => :Time, :Default => nil },
24
+ :bonus_time, { :DataType => :Time, :Default => nil },
25
+ :last_read, { :DataType => :Date, :Default => nil },
26
+ :content, { :DataType => :String },
27
+ :approved, { :DataType => :Boolean, :Default => true },
28
+ :replies_count, { :DataType => :Integer, :Default => 0 },
29
+ :parent_id, { :DataType => :Integer, :Default => nil },
30
+ :type, { :DataType => :String, :Default => nil }
31
+ )
32
+
33
+ db.create_table(:developers,
34
+ :name, { :DataType => :String, :Default => nil },
35
+ :salary, { :DataType => :Integer, :Default => 70000 },
36
+ :created_at, { :DataType => :Time, :Default => nil },
37
+ :updated_at, { :DataType => :Time, :Default => nil }
38
+ )
39
+
40
+ db.create_table(:projects,
41
+ :name, { :DataType => :String, :Default => nil },
42
+ :type, { :DataType => :String, :Default => nil }
43
+ )
44
+
45
+ db.create_table(:developers_projects,
46
+ :developer_id, { :DataType => :Integer, :Required => true },
47
+ :project_id, { :DataType => :Integer, :Required => true },
48
+ :joined_on, { :DataType => :Date, :Default => nil },
49
+ :access_level, { :DataType => :Integer, :Default => 1 }
50
+ )
51
+
52
+
53
+ db.create_table(:orders,
54
+ :name, { :DataType => :String, :Default => nil },
55
+ :billing_customer_id, { :DataType => :Integer, :Default => nil },
56
+ :shipping_customer_id, { :DataType => :Integer, :Default => nil }
57
+ )
58
+
59
+ db.create_table(:customers,
60
+ :name, { :DataType => :String, :Default => nil },
61
+ :balance, { :DataType => :Integer, :Default => 0 },
62
+ :address_street, { :DataType => :String, :Default => nil },
63
+ :address_city, { :DataType => :String, :Default => nil },
64
+ :address_country, { :DataType => :String, :Default => nil },
65
+ :gps_location, { :DataType => :String, :Default => nil }
66
+ )
67
+
68
+ db.create_table(:movies,
69
+ :movieid, { :DataType => :Integer, :Required => true },
70
+ :name, { :DataType => :String, :Default => nil }
71
+ )
72
+
73
+ db.create_table(:subscribers,
74
+ :nick, { :DataType => :String, :Required => true },
75
+ :name, { :DataType => :String, :Default => nil }
76
+ )
77
+
78
+ db.create_table(:booleantests,
79
+ :value, { :DataType => :Boolean, :Default => nil }
80
+ )
81
+
82
+ db.create_table(:auto_id_tests,
83
+ :auto_id, { :DataType => :Integer, :Calculated => "recno" }, # emulate the primary key in the adapter
84
+ :value, { :DataType => :Integer, :Default => nil }
85
+ )
86
+
87
+ db.create_table(:entrants,
88
+ :name, { :DataType => :String, :Required => true },
89
+ :course_id, { :DataType => :Integer, :Required => true }
90
+ )
91
+
92
+ db.create_table(:colnametests,
93
+ :references, { :DataType => :Integer, :Required => true }
94
+ )
95
+
96
+ db.create_table(:mixins,
97
+ :parent_id, { :DataType => :Integer, :Default => nil },
98
+ :type, { :DataType => :String, :Default => nil },
99
+ :pos, { :DataType => :Integer, :Default => nil },
100
+ :lft, { :DataType => :Integer, :Default => nil },
101
+ :rgt, { :DataType => :Integer, :Default => nil },
102
+ :root_id, { :DataType => :Integer, :Default => nil },
103
+ :created_at, { :DataType => :Time, :Default => nil },
104
+ :updated_at, { :DataType => :Time, :Default => nil }
105
+ )
106
+
107
+ db.create_table(:people,
108
+ :first_name, { :DataType => :String, :Default => nil },
109
+ :lock_version, { :DataType => :Integer, :Required => true, :Default => 0 }
110
+ )
111
+
112
+ db.create_table(:binaries,
113
+ :data, { :DataType => :String }
114
+ )
115
+
116
+ db.create_table(:computers,
117
+ :developer, { :DataType => :Integer, :Required => true },
118
+ :extendedWarranty, { :DataType => :Integer, :Required => true }
119
+ )
120
+
121
+ db.create_table(:posts,
122
+ :author_id, { :DataType => :Integer },
123
+ :title, { :DataType => :String, :Required => true },
124
+ :type, { :DataType => :String, :Required => true },
125
+ :body, { :DataType => :String, :Required => true }
126
+ )
127
+
128
+ db.create_table(:comments,
129
+ :post_id, { :DataType => :Integer, :Required => true },
130
+ :type, { :DataType => :String, :Required => true },
131
+ :body, { :DataType => :String, :Required => true }
132
+ )
133
+
134
+ db.create_table(:authors,
135
+ :name, { :DataType => :String, :Required => true }
136
+ )
137
+
138
+ db.create_table(:tasks,
139
+ :starting, { :DataType => :Time, :Default => nil },
140
+ :ending, { :DataType => :Time, :Default => nil }
141
+ )
142
+
143
+ db.create_table(:categories,
144
+ :name, { :DataType => :String, :Required => true },
145
+ :type, { :DataType => :String, :Default => nil }
146
+ )
147
+
148
+ db.create_table(:categories_posts,
149
+ :category_id, { :DataType => :Integer, :Required => true },
150
+ :post_id, { :DataType => :Integer, :Required => true }
151
+ )
152
+
153
+ db.create_table(:fk_test_has_pk
154
+ )
155
+
156
+ db.create_table(:fk_test_has_fk,
157
+ :fk_id, { :DataType => :Integer, :Required => true }
158
+ # FOREIGN KEY ('fk_id') REFERENCES 'fk_test_has_pk'('id')
159
+ )
160
+
161
+ db.create_table(:keyboards,
162
+ :key_number, { :DataType => :Integer, :Calculated => "recno" },
163
+ :name, { :DataType => :String, :Default => nil }
164
+ )
165
+ end
166
+
167
+ def create_and_init_2 db2
168
+ db2.create_table(:courses,
169
+ :name, { :DataType => :String, :Required => true }
170
+ )
171
+ end
@@ -0,0 +1,6 @@
1
+ --- !omap
2
+ - andy:
3
+ name: Andy Hunt
4
+
5
+ - dave:
6
+ name: Dave Thomas
@@ -0,0 +1,8 @@
1
+ --- !omap
2
+ - pr_andy:
3
+ author_id: 1
4
+ book_id: 1
5
+
6
+ - pr_dave:
7
+ author_id: 2
8
+ book_id: 1
@@ -0,0 +1,4 @@
1
+ pickaxe:
2
+ name: Programming Ruby (1st Edition)
3
+ published: 2000-01-01
4
+ publisher_id: 2
@@ -0,0 +1,98 @@
1
+ front:
2
+ content: front cover
3
+ book_id: 1
4
+ chap1:
5
+ content: chapter 1 text text text text text text text text text text text text text text text text text text
6
+ text text text text text text text text text text text text text text text text text text text text
7
+ text text text text text text text text text text text text text text text text text text text text
8
+ text text text text text text text text text text text text text text text text text text text text
9
+ text text text text text text text text text text text text text text text text text text text text
10
+ text text text text text text text text text text text text text text text text text text text text
11
+ text text text text text text text text text text text text text text text text text text text text
12
+ text text text text text text text text text text text text text text text text text text text text
13
+ text text text text text text text text text text text text text text text text text text text text
14
+ text text text text text text text text text text text text text text text text text text text text
15
+ text text text text text text text text text text text text text text text text text text text text
16
+ text text text text text text text text text text text text text text text text text text text text
17
+ text text text text text text text text text text text text text text text text text text text text
18
+ text text text text text text text text text text text text text text text text text text text text
19
+ text text text text text text text text text text text text text text text text text text text text
20
+ text text text text text text text text text text text text text text text text text text text text
21
+ text text text text text text text text text text text text text text text text text text text text
22
+ text text text text text text text text text text text text text text text text text text text text
23
+ text text text text text text text text text text text text text text text text text text text text
24
+ text text text text text text text text text text text text text text text text text text text text
25
+ text text text text text text text text text text text text text text text text text text text text
26
+ text text text text text text text text text text text text text text text text text text text text
27
+ text text text text text text text text text text text text text text text text text text text text
28
+ text text text text text text text text text text text text text text text text text text text text
29
+ text text text text text text text text text text text text text text text text text text text text
30
+ text text text text text text text text text text text text text text text text text text text text
31
+ text text text text text text text text text text text text text text text text text text text text
32
+ text text text text text text text text text text text text text text text text text text text text
33
+ text text text text text text text text text text text text text text text text text text text text
34
+ text text text text text text text text text text text text text text text text text text text text
35
+ text text text text text text text text text text text text text text text text text text text text
36
+ text text text text text text text text text text text text text text text text text text text text
37
+ text text text text text text text text text text text text text text text text text text text text
38
+ text text text text text text text text text text text text text text text text text text text text
39
+ text text text text text text text text text text text text text text text text text text text text
40
+ text text text text text text text text text text text text text text text text text text text text
41
+ text text text text text text text text text text text text text text text text text text text text
42
+ text text text text text text text text text text text text text text text text text text text text
43
+ text text text text text text text text text text text text text text text text text text text text
44
+ text text text text text text text text text text text text text text text text text text text text
45
+ text text text text text text text text text text text text text text text text text text text text
46
+ book_id: 1
47
+ chap2:
48
+ content: chapter 2 text text text text text text text text text text text text text text text text text text
49
+ text text text text text text text text text text text text text text text text text text text text
50
+ text text text text text text text text text text text text text text text text text text text text
51
+ text text text text text text text text text text text text text text text text text text text text
52
+ text text text text text text text text text text text text text text text text text text text text
53
+ text text text text text text text text text text text text text text text text text text text text
54
+ text text text text text text text text text text text text text text text text text text text text
55
+ text text text text text text text text text text text text text text text text text text text text
56
+ text text text text text text text text text text text text text text text text text text text text
57
+ text text text text text text text text text text text text text text text text text text text text
58
+ text text text text text text text text text text text text text text text text text text text text
59
+ text text text text text text text text text text text text text text text text text text text text
60
+ text text text text text text text text text text text text text text text text text text text text
61
+ text text text text text text text text text text text text text text text text text text text text
62
+ text text text text text text text text text text text text text text text text text text text text
63
+ text text text text text text text text text text text text text text text text text text text text
64
+ text text text text text text text text text text text text text text text text text text text text
65
+ text text text text text text text text text text text text text text text text text text text text
66
+ text text text text text text text text text text text text text text text text text text text text
67
+ text text text text text text text text text text text text text text text text text text text text
68
+ text text text text text text text text text text text text text text text text text text text text
69
+ text text text text text text text text text text text text text text text text text text text text
70
+ text text text text text text text text text text text text text text text text text text text text
71
+ text text text text text text text text text text text text text text text text text text text text
72
+ text text text text text text text text text text text text text text text text text text text text
73
+ text text text text text text text text text text text text text text text text text text text text
74
+ text text text text text text text text text text text text text text text text text text text text
75
+ text text text text text text text text text text text text text text text text text text text text
76
+ text text text text text text text text text text text text text text text text text text text text
77
+ text text text text text text text text text text text text text text text text text text text text
78
+ text text text text text text text text text text text text text text text text text text text text
79
+ text text text text text text text text text text text text text text text text text text text text
80
+ text text text text text text text text text text text text text text text text text text text text
81
+ text text text text text text text text text text text text text text text text text text text text
82
+ text text text text text text text text text text text text text text text text text text text text
83
+ text text text text text text text text text text text text text text text text text text text text
84
+ text text text text text text text text text text text text text text text text text text text text
85
+ text text text text text text text text text text text text text text text text text text text text
86
+ text text text text text text text text text text text text text text text text text text text text
87
+ text text text text text text text text text text text text text text text text text text text text
88
+ text text text text text text text text text text text text text text text text text text text text
89
+ text text text text text text text text text text text text text text text text text text text text
90
+ text text text text text text text text text text text text text text text text text text text text
91
+ book_id: 1
92
+ annex:
93
+ content: annex
94
+ book_id: 1
95
+ back:
96
+ content: back cover
97
+ book_id: 1
98
+
@@ -0,0 +1,7 @@
1
+ aw:
2
+ name: Addison Wesley
3
+ address:
4
+ pragprog:
5
+ name: "Pragmatic Programmers' Bookshelf"
6
+ address:
7
+
@@ -0,0 +1,107 @@
1
+ require 'test_helper'
2
+
3
+ class KBAssociationsTest < Test::Unit::TestCase
4
+
5
+ self.fixture_path = File.expand_path(File.dirname(__FILE__)) + '/fixtures'
6
+ use_transactional_fixtures = false
7
+ fixtures :publishers, :authors, :books, :pages, :authors_books
8
+
9
+ def setup
10
+ # recreate_kb_database
11
+ # load_kb_fixtures
12
+ end
13
+
14
+ def tear_down
15
+ # unload_kb_fixtures
16
+ end
17
+
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
30
+
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
52
+
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
59
+
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
85
+
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
107
+ end
@@ -0,0 +1,204 @@
1
+
2
+ require 'test_helper'
3
+
4
+ class KBBasicsTest < Test::Unit::TestCase
5
+
6
+ self.fixture_path = File.expand_path(File.dirname(__FILE__)) + '/fixtures'
7
+ use_transactional_fixtures = false
8
+ fixtures :publishers, :authors, :books, :pages, :authors_books
9
+
10
+ def setup
11
+ # recreate_kb_database
12
+ # load_kb_fixtures
13
+ end
14
+
15
+ def tear_down
16
+ # unload_kb_fixtures
17
+ end
18
+
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
56
+
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
73
+
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
+ end
82
+
83
+ def test_find_with_sql_fragments
84
+ # basic SQL strings should work:
85
+ assert_equal [@andy], Author.find(:all, :conditions => "name = 'Andy Hunt'")
86
+
87
+ # guess find with basic SQL fragment:
88
+ assert_nothing_raised() { Author.find(:all, :conditions => ['name =?', 'Andy Hunt']) }
89
+ assert_equal [@andy], Author.find(:all, :conditions => ['name =?', 'Andy Hunt'])
90
+ assert_equal @dave, Author.find(:first, :conditions => ['name =?', 'Dave Thomas'])
91
+
92
+ # test more complex SQL fragments:
93
+ assert_equal [@back], Page.find(:all, :conditions => ["book_id = ? AND content = ?", 1, 'back cover'])
94
+ end
95
+
96
+ def test_update
97
+ tbl = $db.get_table(:books)
98
+ assert_equal 'Programming Ruby (1st Edition)', tbl.select.first.name
99
+
100
+ pickaxe = Book.find :first
101
+ pickaxe.name = 'Programming Ruby (2nd Edition)'
102
+ pickaxe.publisher_id = 1
103
+ pickaxe.save
104
+
105
+ assert_equal 1, tbl.select.size
106
+ assert_equal 'Programming Ruby (2nd Edition)', tbl.select.first.name
107
+ end
108
+
109
+ def test_update_all
110
+ tbl = $db.get_table(:authors)
111
+ assert_equal ['Andy Hunt', 'Dave Thomas'], tbl.select.map{|r| r.name}
112
+
113
+ Author.update_all lambda{|rec| rec.name = rec.name.upcase}
114
+ assert_equal ['ANDY HUNT', 'DAVE THOMAS'], tbl.select.map{|r| r.name}
115
+
116
+ Author.update_all lambda{|rec| rec.name = rec.name.downcase}, lambda{|rec| rec.name =~ /Andy/i}
117
+ assert_equal ['andy hunt', 'DAVE THOMAS'], tbl.select.map{|r| r.name}
118
+
119
+ Author.update_all "name = 'Andy'", lambda{|rec| rec.name =~ /Andy/i}
120
+ assert_equal ['Andy', 'DAVE THOMAS'], tbl.select.map{|r| r.name}
121
+
122
+ Author.update_all ['name = ?', 'Mr. Hunt'], lambda{|rec| rec.name =~ /Andy/i}
123
+ assert_equal ['Mr. Hunt', 'DAVE THOMAS'], tbl.select.map{|r| r.name}
124
+
125
+ Author.update_all ['name = ?', 'Dave'], ['name = ?', 'DAVE THOMAS']
126
+ assert_equal ['Mr. Hunt', 'Dave'], tbl.select.map{|r| r.name}
127
+
128
+ # We can handle simple SQL fragments in conditions
129
+ Author.update_all ['num_books = ?', 1], "name = 'Dave'"
130
+ assert_equal ['Mr. Hunt', 'Dave'], tbl.select.map{|r| r.name}
131
+ assert_equal [0, 1], tbl.select.map{|r| r.num_books}
132
+
133
+ assert_nothing_raised { Author.update_all ['name = ?', 'Dave Thomas'], "name = 'Dave' and num_books = 1" }
134
+ assert_equal ['Mr. Hunt', 'Dave Thomas'], tbl.select.map{|r| r.name}
135
+
136
+ Author.update_all ['name = ?', 'Pragmatic']
137
+ assert_equal ['Pragmatic', 'Pragmatic'], tbl.select.map{|r| r.name}
138
+
139
+ # Check updates of non-strings
140
+ tbl.drop_column :num_books rescue nil
141
+ tbl.add_column :num_books, :Integer
142
+
143
+ Author.update_all ['num_books = ?', 1]
144
+ assert_equal [1, 1], tbl.select.map{|r| r.num_books}
145
+
146
+ Author.update_all "num_books = 2"
147
+ assert_equal [2, 2], tbl.select.map{|r| r.num_books}
148
+ end
149
+
150
+ def test_destroy
151
+ authors = $db.get_table(:authors)
152
+ assert_equal 2, authors.total_recs
153
+ assert_equal 2, Author.find(:all).size
154
+ # class level destroy
155
+ Author.destroy(1)
156
+ assert_equal 1, authors.total_recs
157
+ assert_equal 1, Author.find(:all).size
158
+ # object destroy
159
+ Author.find(:first).destroy
160
+ assert_equal 0, authors.total_recs
161
+ assert_equal 0, Author.find(:all).size
162
+ assert_raise(ActiveRecord::RecordNotFound) { Author.find(1) }
163
+
164
+ pages = $db.get_table(:pages)
165
+ assert_equal 5, pages.total_recs
166
+ Page.destroy [1,2,3]
167
+ assert_equal 2, pages.total_recs
168
+ Page.destroy [4,5]
169
+ assert_equal 0, pages.total_recs
170
+ assert_raise(ActiveRecord::RecordNotFound) { Page.destroy(1) }
171
+ end
172
+
173
+ def test_increment_counter
174
+ tbl = $db.get_table(:authors)
175
+ tbl.drop_column :num_books rescue nil
176
+ tbl.add_column :num_books, { :DataType => :Integer, :Default => 0 }
177
+ tbl.update_all :num_books => 1
178
+
179
+ assert_equal [1, 1], tbl.select.map{|a| a.num_books}
180
+ assert_equal [1, 1], Author.find(:all).map{|a| a.num_books}
181
+
182
+ Author.increment_counter 'num_books', 1
183
+ assert_equal [2, 1], tbl.select.map{|r| r.num_books}
184
+
185
+ Author.increment_counter 'num_books', [1, 2]
186
+ assert_equal [3, 2], tbl.select.map{|r| r.num_books}
187
+ end
188
+
189
+ def test_decrement_counter
190
+ tbl = $db.get_table(:authors)
191
+ tbl.drop_column :num_books rescue nil
192
+ tbl.add_column :num_books, { :DataType => :Integer, :Default => 2 }
193
+ tbl.update_all :num_books => 2
194
+
195
+ assert_equal [2, 2], tbl.select.map{|a| a.num_books}
196
+ assert_equal [2, 2], Author.find(:all).map{|a| a.num_books}
197
+
198
+ Author.decrement_counter 'num_books', 1
199
+ assert_equal [1, 2], tbl.select.map{|r| r.num_books}
200
+
201
+ Author.decrement_counter 'num_books', [1, 2]
202
+ assert_equal [0, 1], tbl.select.map{|r| r.num_books}
203
+ end
204
+ end