baza 0.0.0 → 0.0.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/spec/baza_spec.rb CHANGED
@@ -1,286 +1,335 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  describe "Baza" do
4
- it "should be able to handle various encodings" do
5
- #I never got this test to actually fail... :-(
6
-
7
- require "baza"
8
- require "knjrbfw"
9
- require "knj/os"
10
- require "rubygems"
11
- require "sqlite3" if !Kernel.const_defined?("SQLite3") and RUBY_ENGINE != "jruby"
12
-
13
- db_path = "#{Knj::Os.tmpdir}/knjrbfw_test_sqlite3.sqlite3"
14
- File.unlink(db_path) if File.exists?(db_path)
15
-
16
- db = Baza::Db.new(
17
- :type => "sqlite3",
18
- :path => db_path,
19
- :return_keys => "symbols",
20
- :index_append_table_name => true
21
- )
22
-
23
- db.tables.create("test", {
24
- "columns" => [
25
- {"name" => "id", "type" => "int", "autoincr" => true, "primarykey" => true},
26
- {"name" => "text", "type" => "varchar"}
27
- ]
28
- })
29
-
30
-
31
-
32
- #Get a list of tables and check the list for errors.
33
- list = db.tables.list
34
- raise "Table not found: 'test'." if !list.key?("test")
35
- raise "Table-name expected to be 'test' but wasnt: '#{list["test"].name}'." if list["test"].name != "test"
36
-
37
-
38
- #Test revision to create tables, indexes and insert rows.
39
- schema = {
40
- "tables" => {
41
- "test_table" => {
42
- "columns" => [
43
- {"name" => "id", "type" => "int", "autoincr" => true, "primarykey" => true},
44
- {"name" => "name", "type" => "varchar"},
45
- {"name" => "age", "type" => "int"},
46
- {"name" => "nickname", "type" => "varchar"}
47
- ],
48
- "indexes" => [
49
- "name"
50
- ],
51
- "rows" => [
52
- {
53
- "find_by" => {"id" => 1},
54
- "data" => {"id" => 1, "name" => "trala"}
55
- }
4
+ require "baza"
5
+ require "knjrbfw"
6
+ require "knj/os"
7
+ require "rubygems"
8
+ require "tmpdir"
9
+
10
+ drivers = []
11
+ Baza::Db.drivers.each do |driver_data|
12
+ name = driver_data[:name].to_s
13
+ const_name = "Info#{name.slice(0, 1).upcase}#{name.slice(1, name.length)}"
14
+ require "#{File.dirname(__FILE__)}/info_#{driver_data[:name]}.rb"
15
+ raise "Constant was not defined: '#{const_name}'." if !Baza.const_defined?(const_name)
16
+
17
+ drivers << {
18
+ :const => Baza.const_get(const_name),
19
+ }
20
+ end
21
+
22
+ drivers.each do |driver|
23
+ it "should be able to handle various encodings" do
24
+ #I never got this test to actually fail... :-(
25
+ debug = false
26
+
27
+ driver[:const].sample_db do |db|
28
+ db.tables.create("test", {
29
+ :columns => [
30
+ {:name => "id", :type => :int, :autoincr => true, :primarykey => true},
31
+ {:name => "text", :type => :varchar}
56
32
  ]
33
+ })
34
+
35
+
36
+
37
+ #Get a list of tables and check the list for errors.
38
+ list = db.tables.list
39
+ raise "Table not found: 'test'." if !list.key?(:test)
40
+
41
+ list[:test].name.should eql(:test)
42
+
43
+
44
+ #Test revision to create tables, indexes and insert rows.
45
+ schema = {
46
+ :tables => {
47
+ :test_table => {
48
+ :columns => [
49
+ {:name => "id", :type => :int, :autoincr => true, :primarykey => true},
50
+ {:name => "name", :type => :varchar},
51
+ {:name => "age", :type => :int},
52
+ {:name => "nickname", :type => :varchar}
53
+ ],
54
+ :indexes => [
55
+ "name"
56
+ ],
57
+ :rows => [
58
+ {
59
+ :find_by => {"id" => 1},
60
+ :data => {"id" => 1, "name" => "trala"}
61
+ }
62
+ ]
63
+ }
64
+ }
57
65
  }
58
- }
59
- }
60
-
61
- rev = Baza::Revision.new
62
- rev.init_db("schema" => schema, "db" => db)
63
-
64
-
65
- #Test wrong encoding.
66
- cont = File.read("#{File.dirname(__FILE__)}/db_spec_encoding_test_file.txt")
67
- cont.force_encoding("ASCII-8BIT")
68
-
69
- db.insert("test", {
70
- "text" => cont
71
- })
72
-
73
-
74
- #Throw out invalid encoding because it will make dumping fail.
75
- db.tables[:test].truncate
76
-
77
-
78
-
79
- #Test IDQueries.
80
- rows_count = 1250
81
- db.transaction do
82
- 0.upto(rows_count) do |count|
83
- db.insert(:test_table, {:name => "User #{count}"})
66
+
67
+ rev = Baza::Revision.new
68
+ rev.init_db(:schema => schema, :debug => debug, :db => db)
69
+
70
+ test_table = db.tables[:test_table]
71
+
72
+
73
+ #Test wrong encoding.
74
+ cont = File.read("#{File.dirname(__FILE__)}/db_spec_encoding_test_file.txt")
75
+ cont.force_encoding("ASCII-8BIT")
76
+
77
+ db.insert("test", {
78
+ "text" => cont
79
+ })
80
+
81
+
82
+ #Throw out invalid encoding because it will make dumping fail.
83
+ db.tables[:test].truncate
84
+
85
+
86
+
87
+ #Test IDQueries.
88
+ rows_count = 1250
89
+ db.transaction do
90
+ 0.upto(rows_count) do |count|
91
+ db.insert(:test_table, {:name => "User #{count}"})
92
+ end
93
+ end
94
+
95
+ block_ran = 0
96
+ idq = Baza::Idquery.new(:db => db, :debug => debug, :table => :test_table, :query => "SELECT id FROM test_table") do |data|
97
+ block_ran += 1
98
+ end
99
+
100
+ raise "Block with should have ran too little: #{block_ran}." if block_ran < rows_count
101
+
102
+ block_ran = 0
103
+ db.select(:test_table, {}, {:idquery => true}) do |data|
104
+ block_ran += 1
105
+ end
106
+
107
+ raise "Block with should have ran too little: #{block_ran}." if block_ran < rows_count
108
+
109
+
110
+ #Test upserting.
111
+ data = {:name => "upsert - Kasper Johansen"}
112
+ data2 = {:name => "upsert - Kasper Nielsen Johansen"}
113
+ sel = {:nickname => "upsert - kaspernj"}
114
+
115
+ table = db.tables[:test_table]
116
+ table.reload
117
+ rows_count = table.rows_count
118
+
119
+ db.upsert(:test_table, sel, data)
120
+ row = db.select(:test_table, sel).fetch
121
+ row[:name].should eql("upsert - Kasper Johansen")
122
+
123
+ table.reload
124
+ table.rows_count.should eql(rows_count + 1)
125
+
126
+ db.upsert(:test_table, sel, data2)
127
+ row = db.select(:test_table, sel).fetch
128
+ row[:name].should eql("upsert - Kasper Nielsen Johansen")
129
+
130
+ table.reload
131
+ table.rows_count.should eql(rows_count + 1)
132
+
133
+
134
+ #Test dumping.
135
+ dump = Baza::Dump.new(:db => db, :debug => false)
136
+ str_io = StringIO.new
137
+ dump.dump(str_io)
138
+ str_io.rewind
139
+
140
+
141
+ #Remember some numbers for validation.
142
+ tables_count = db.tables.list.length
143
+
144
+
145
+ #Remove everything in the db.
146
+ db.tables.list do |table|
147
+ table.drop unless table.native?
148
+ end
149
+
150
+
151
+ #Run the exported SQL.
152
+ db.transaction do
153
+ str_io.each_line do |sql|
154
+ db.q(sql)
155
+ end
156
+ end
157
+
158
+
159
+ #Vaildate import.
160
+ raise "Not same amount of tables: #{tables_count}, #{db.tables.list.length}" if tables_count != db.tables.list.length
161
+
162
+
163
+
164
+ #Test revision table renaming.
165
+ Baza::Revision.new.init_db(:db => db, :debug => debug, :schema => {
166
+ :tables => {
167
+ :new_test_table => {
168
+ :renames => [:test_table]
169
+ }
170
+ }
171
+ })
172
+ tables = db.tables.list
173
+ raise "Didnt expect table 'test_table' to exist but it did." if tables.key?(:test_table)
174
+ raise "Expected 'new_test_table' to exist but it didnt." if !tables.key?(:new_test_table)
175
+
176
+
177
+ #Test revision for column renaming.
178
+ Baza::Revision.new.init_db(:db => db, :debug => debug, :schema => {
179
+ :tables => {
180
+ :new_test_table => {
181
+ :columns => [
182
+ {:name => :new_name, :type => :varchar, :renames => [:name]}
183
+ ]
184
+ }
185
+ }
186
+ })
187
+ columns = db.tables[:new_test_table].columns
188
+ raise "Didnt expect 'name' to exist but it did." if columns.key?(:name)
189
+ raise "Expected 'new_name'-column to exist but it didnt." if !columns.key?(:new_name)
84
190
  end
85
191
  end
86
192
 
87
- block_ran = 0
88
- idq = Baza::Idquery.new(:db => db, :debug => false, :table => :test_table, :query => "SELECT id FROM test_table") do |data|
89
- block_ran += 1
90
- end
91
-
92
- raise "Block with should have ran too little: #{block_ran}." if block_ran < rows_count
93
-
94
- block_ran = 0
95
- db.select(:test_table, {}, {:idquery => true}) do |data|
96
- block_ran += 1
193
+ it "should generate proper sql" do
194
+ driver[:const].sample_db do |db|
195
+ time = Time.new(1985, 6, 17, 10, 30)
196
+ db.insert(:test, {:date => time}, :return_sql => true).should eql("INSERT INTO `test` (`date`) VALUES ('1985-06-17 10:30:00')")
197
+
198
+ date = Date.new(1985, 6, 17)
199
+ db.insert(:test, {:date => date}, :return_sql => true).should eql("INSERT INTO `test` (`date`) VALUES ('1985-06-17')")
200
+ end
97
201
  end
98
202
 
99
- raise "Block with should have ran too little: #{block_ran}." if block_ran < rows_count
100
-
101
-
102
- #Test upserting.
103
- data = {:name => "upsert - Kasper Johansen"}
104
- data2 = {:name => "upsert - Kasper Nielsen Johansen"}
105
- sel = {:nickname => "upsert - kaspernj"}
106
-
107
- table = db.tables[:test_table]
108
- table.reload
109
- rows_count = table.rows_count
110
-
111
- db.upsert(:test_table, sel, data)
112
- row = db.select(:test_table, sel).fetch
113
- row[:name].should eql("upsert - Kasper Johansen")
114
-
115
- table.reload
116
- table.rows_count.should eql(rows_count + 1)
117
-
118
- db.upsert(:test_table, sel, data2)
119
- row = db.select(:test_table, sel).fetch
120
- row[:name].should eql("upsert - Kasper Nielsen Johansen")
121
-
122
- table.reload
123
- table.rows_count.should eql(rows_count + 1)
124
-
125
-
126
- #Test dumping.
127
- dump = Baza::Dump.new(:db => db, :debug => false)
128
- str_io = StringIO.new
129
- dump.dump(str_io)
130
- str_io.rewind
131
-
132
-
133
- #Remember some numbers for validation.
134
- tables_count = db.tables.list.length
135
-
136
-
137
- #Remove everything in the db.
138
- db.tables.list do |table|
139
- table.drop unless table.native?
203
+ it "should copy database structure and data" do
204
+ driver[:const].sample_db do |db1|
205
+ driver[:const].sample_db do |db2|
206
+ db1.tables.create(:test_table, {
207
+ :columns => [
208
+ {:name => "id", :type => :int, :autoincr => true, :primarykey => true},
209
+ {:name => "testname", :type => :varchar}
210
+ ],
211
+ :indexes => [
212
+ "testname"
213
+ ]
214
+ })
215
+
216
+ table1 = db1.tables["test_table"]
217
+ cols1 = table1.columns
218
+
219
+ 100.times do |count|
220
+ table1.insert(:testname => "TestRow#{count}")
221
+ end
222
+
223
+ begin
224
+ table2 = db2.tables[:test_table]
225
+ raise "Expected not-found exception."
226
+ rescue Errno::ENOENT
227
+ #expected
228
+ end
229
+
230
+ db1.copy_to(db2)
231
+
232
+ table2 = db2.tables[:test_table]
233
+
234
+ cols2 = table2.columns
235
+ cols2.length.should eql(cols1.length)
236
+
237
+ table2.rows_count.should eql(table1.rows_count)
238
+
239
+ db1.select(:test_table) do |row1|
240
+ found = 0
241
+ db2.select(:test_table, row1) do |row2|
242
+ found += 1
243
+
244
+ row1.each do |key, val|
245
+ row2[key].should eql(val)
246
+ end
247
+ end
248
+
249
+ found.should eql(1)
250
+ end
251
+
252
+ table1.indexes.length.should eql(1)
253
+ table2.indexes.length.should eql(table1.indexes.length)
254
+ end
255
+ end
140
256
  end
141
257
 
142
-
143
- #Run the exported SQL.
144
- db.transaction do
145
- str_io.each_line do |sql|
146
- db.q(sql)
258
+ it "should be able to make new connections based on given objects" do
259
+ driver[:const].sample_db do |db|
260
+ db2 = Baza::Db.from_object(:object => db.conn.conn)
147
261
  end
148
262
  end
149
263
 
150
-
151
- #Vaildate import.
152
- raise "Not same amount of tables: #{tables_count}, #{db.tables.list.length}" if tables_count != db.tables.list.length
153
-
154
-
155
-
156
- #Test revision table renaming.
157
- Baza::Revision.new.init_db("db" => db, "schema" => {
158
- "tables" => {
159
- "new_test_table" => {
160
- "renames" => ["test_table"]
161
- }
162
- }
163
- })
164
- tables = db.tables.list
165
- raise "Didnt expect table 'test_table' to exist but it did." if tables.key?("test_table")
166
- raise "Expected 'new_test_table' to exist but it didnt." if !tables.key?("new_test_table")
167
-
168
-
169
- #Test revision for column renaming.
170
- Baza::Revision.new.init_db("db" => db, "schema" => {
171
- "tables" => {
172
- "new_test_table" => {
173
- "columns" => [
174
- {"name" => "new_name", "type" => "varchar", "renames" => ["name"]}
264
+ it "should be able to do ID-queries through the select-method" do
265
+ driver[:const].sample_db do |db|
266
+ db.tables.create(:test_table, {
267
+ :columns => [
268
+ {:name => :idrow, :type => :int, :autoincr => true, :primarykey => true},
269
+ {:name => :name, :type => :varchar}
175
270
  ]
176
- }
177
- }
178
- })
179
- columns = db.tables["new_test_table"].columns
180
- raise "Didnt expect 'name' to exist but it did." if columns.key?("name")
181
- raise "Expected 'new_name'-column to exist but it didnt." if !columns.key?("new_name")
182
-
183
-
184
- #Delete test-database if everything went well.
185
- File.unlink(db_path) if File.exists?(db_path)
186
- end
187
-
188
- it "should generate proper sql" do
189
- require "knj/db"
190
- require "knj/os"
191
- require "rubygems"
192
- require "sqlite3" if !Kernel.const_defined?("SQLite3") and RUBY_ENGINE != "jruby"
193
-
194
- db_path = "#{Knj::Os.tmpdir}/knjrbfw_test_sqlite3.sqlite3"
195
- File.unlink(db_path) if File.exists?(db_path)
196
-
197
- db = Baza::Db.new(
198
- :type => "sqlite3",
199
- :path => db_path,
200
- :return_keys => "symbols",
201
- :index_append_table_name => true
202
- )
203
-
204
- time = Time.new(1985, 6, 17, 10, 30)
205
- db.insert(:test, {:date => time}, :return_sql => true).should eql("INSERT INTO `test` (`date`) VALUES ('1985-06-17 10:30:00')")
206
-
207
- date = Date.new(1985, 6, 17)
208
- db.insert(:test, {:date => date}, :return_sql => true).should eql("INSERT INTO `test` (`date`) VALUES ('1985-06-17')")
271
+ })
272
+
273
+ count = 0
274
+ 100.times do
275
+ arr = []
276
+ 100.times do
277
+ count += 1
278
+ arr << {:name => "Kasper #{count}"}
279
+ end
280
+
281
+ db.insert_multi(:test_table, arr)
282
+ end
283
+
284
+ count_found = 0
285
+ db.select(:test_table, nil, :idquery => :idrow) do |row|
286
+ count_found += 1
287
+
288
+ row[:name].should eql("Kasper #{count_found}")
289
+ end
290
+
291
+ count_found.should eql(10000)
292
+ end
293
+ end
209
294
  end
210
295
 
211
- it "should copy database structure and data" do
212
- require "knj/db"
213
- require "knj/os"
214
- require "rubygems"
215
- require "sqlite3" if !Kernel.const_defined?("SQLite3") and RUBY_ENGINE != "jruby"
216
-
217
- db_path1 = "#{Knj::Os.tmpdir}/knjrbfw_test_sqlite3_db1.sqlite3"
218
- File.unlink(db_path1) if File.exists?(db_path1)
219
-
220
- db_path2 = "#{Knj::Os.tmpdir}/knjrbfw_test_sqlite3_db2.sqlite3"
221
- File.unlink(db_path2) if File.exists?(db_path2)
222
-
223
- db1 = Baza::Db.new(
224
- :type => "sqlite3",
225
- :path => db_path1,
226
- :return_keys => "symbols",
227
- :index_append_table_name => true
228
- )
229
-
230
- db1.tables.create(:test_table, {
231
- "columns" => [
232
- {"name" => "id", "type" => "int", "autoincr" => true, "primarykey" => true},
233
- {"name" => "testname", "type" => "varchar"}
234
- ],
235
- "indexes" => [
236
- "testname"
237
- ]
238
- })
239
-
240
- table1 = db1.tables["test_table"]
241
- cols1 = table1.columns
242
-
243
- 100.times do |count|
244
- table1.insert(:testname => "TestRow#{count}")
245
- end
246
-
247
- db2 = Baza::Db.new(
248
- :type => "sqlite3",
249
- :path => db_path2,
250
- :return_keys => "symbols",
251
- :index_append_table_name => true
252
- )
253
-
254
- begin
255
- table2 = db2.tables["test_table"]
256
- raise "Expected not-found exception."
257
- rescue Errno::ENOENT
258
- #expected
259
- end
260
-
261
- db1.copy_to(db2)
262
-
263
- table2 = db2.tables["test_table"]
264
-
265
- cols2 = table2.columns
266
- cols2.length.should eql(cols1.length)
267
-
268
- table2.rows_count.should eql(table1.rows_count)
269
-
270
- db1.select(:test_table) do |row1|
271
- found = 0
272
- db2.select(:test_table, row1) do |row2|
273
- found += 1
296
+ it "should be able to connect to mysql and do various stuff" do
297
+ Baza::InfoSqlite3.sample_db do |db3|
298
+ Baza::InfoMysql.sample_db do |db1|
299
+ #create table.
300
+ db1.tables.create(:test_table, {
301
+ :columns => [
302
+ {:name => :id, :type => :int, :autoincr => true, :primarykey => true},
303
+ {:name => :name, :type => :varchar, :maxlength => 100}
304
+ ]
305
+ })
274
306
 
275
- row1.each do |key, val|
276
- row2[key].should eql(val)
277
- end
307
+ test_table = db1.tables[:test_table]
308
+
309
+ col_id = test_table.column(:id)
310
+ col_name = test_table.column(:name)
311
+
312
+ #Test various operations actually work.
313
+ test_table.optimize
314
+
315
+ #object_from test
316
+ db2 = Baza::Db.from_object(db1.conn.conn)
317
+
318
+ #Test dumping to SQLite.
319
+ db2.copy_to(db3)
320
+
321
+ table_sqlite = db3.tables[:test_table]
322
+ table_sqlite.columns.length.should eql(test_table.columns.length)
323
+
324
+ col_id_sqlite = table_sqlite.column(:id)
325
+ col_id_sqlite.type.should eql(:int)
326
+ col_id_sqlite.autoincr?.should eql(true)
327
+ col_id_sqlite.primarykey?.should eql(true)
328
+
329
+ col_name_sqlite = table_sqlite.column(:name)
330
+ col_name_sqlite.type.should eql(:varchar)
331
+ col_name_sqlite.maxlength.to_i.should eql(100)
278
332
  end
279
-
280
- found.should eql(1)
281
333
  end
282
-
283
- table1.indexes.length.should eql(1)
284
- table2.indexes.length.should eql(table1.indexes.length)
285
334
  end
286
335
  end
@@ -0,0 +1,6 @@
1
+ $mysql_info = {
2
+ :host => "localhost",
3
+ :user => "baza_spec",
4
+ :pass => "123",
5
+ :db => "baza_spec"
6
+ }
@@ -0,0 +1,20 @@
1
+ class Baza::InfoSqlite3
2
+ def self.sample_db
3
+ require "sqlite3"
4
+ path = "#{Dir.tmpdir}/baza_sqlite3_test.sqlite3_#{Time.now.to_f.to_s.hash}"
5
+ File.unlink(path) if File.exists?(path)
6
+ db = Baza::Db.new(
7
+ :type => :sqlite3,
8
+ :path => path,
9
+ :index_append_table_name => true,
10
+ :sql_to_error => true
11
+ )
12
+
13
+ begin
14
+ yield db
15
+ ensure
16
+ db.close
17
+ File.unlink(path)
18
+ end
19
+ end
20
+ end