baza 0.0.13 → 0.0.14

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.
@@ -0,0 +1,288 @@
1
+ shared_examples_for "a baza driver" do
2
+ let(:constant){
3
+ name = described_class.name.split("::").last
4
+ const_name = "Info#{name.slice(0, 1).upcase}#{name.slice(1, name.length)}"
5
+ require "#{File.dirname(__FILE__)}/../info_#{StringCases.camel_to_snake(name)}"
6
+ raise "Constant was not defined: '#{const_name}'." unless Baza.const_defined?(const_name)
7
+ Baza.const_get(const_name)
8
+ }
9
+ let(:driver){ constant.new }
10
+ let(:driver2){ constant.new }
11
+ let(:db){ driver.db }
12
+ let(:db2){ driver2.db }
13
+ let(:test_table){
14
+ db.tables.create("test", {
15
+ columns: [
16
+ {name: "id", type: :int, autoincr: true, primarykey: true},
17
+ {name: "text", type: :varchar}
18
+ ]
19
+ })
20
+ db.tables[:test]
21
+ }
22
+
23
+ before do
24
+ driver.before
25
+ driver2.before
26
+ end
27
+
28
+ after do
29
+ driver.after
30
+ driver2.after
31
+ end
32
+
33
+ it "should do revisions" do
34
+ test_table
35
+
36
+ schema = {
37
+ tables: {
38
+ test_table: {
39
+ columns: [
40
+ {name: "id", type: :int, autoincr: true, primarykey: true},
41
+ {name: "name", type: :varchar},
42
+ {name: "age", type: :int, default: 0},
43
+ {name: "nickname", type: :varchar, default: ""}
44
+ ],
45
+ indexes: [
46
+ "name"
47
+ ],
48
+ rows: [
49
+ {
50
+ find_by: {"id" => 1},
51
+ data: {"id" => 1, "name" => "trala"}
52
+ }
53
+ ]
54
+ }
55
+ }
56
+ }
57
+
58
+ rev = Baza::Revision.new
59
+ rev.init_db(schema: schema, debug: false, db: db)
60
+
61
+ test_table = db.tables[:test_table]
62
+ test_table.columns.keys.should include :age
63
+ test_table.columns.keys.should include :nickname
64
+ end
65
+
66
+ it "should do id-queries" do
67
+ test_table
68
+
69
+ rows_count = 1250
70
+ db.transaction do
71
+ 0.upto(rows_count) do |count|
72
+ db.insert(:test, text: "User #{count}")
73
+ end
74
+ end
75
+
76
+ block_ran = 0
77
+ idq = Baza::Idquery.new(db: db, debug: false, table: :test, query: "SELECT id FROM test") do |data|
78
+ block_ran += 1
79
+ end
80
+
81
+ raise "Block with should have ran too little: #{block_ran}." if block_ran < rows_count
82
+
83
+ block_ran = 0
84
+ db.select(:test, {}, idquery: true) do |data|
85
+ block_ran += 1
86
+ end
87
+
88
+ raise "Block with should have ran too little: #{block_ran}." if block_ran < rows_count
89
+ end
90
+
91
+ it "should do upserting" do
92
+ test_table.create_columns([{name: "nickname", type: :varchar}])
93
+
94
+ #Test upserting.
95
+ data = {text: "upsert - Kasper Johansen"}
96
+ data2 = {text: "upsert - Kasper Nielsen Johansen"}
97
+ sel = {nickname: "upsert - kaspernj"}
98
+
99
+ table = db.tables[:test]
100
+ table.reload
101
+ rows_count = table.rows_count
102
+
103
+ db.upsert(:test, data, sel)
104
+ row = db.select(:test, sel).fetch
105
+ row[:text].should eq "upsert - Kasper Johansen"
106
+
107
+ table.reload
108
+ table.rows_count.should eql(rows_count + 1)
109
+
110
+ db.upsert(:test, data2, sel)
111
+ row = db.select(:test, sel).fetch
112
+ row[:text].should eq "upsert - Kasper Nielsen Johansen"
113
+
114
+ table.reload
115
+ table.rows_count.should eq rows_count + 1
116
+ end
117
+
118
+ it "should dump as SQL" do
119
+ dump = Baza::Dump.new(db: db, debug: false)
120
+ str_io = StringIO.new
121
+ dump.dump(str_io)
122
+ str_io.rewind
123
+
124
+ #Remember some numbers for validation.
125
+ tables_count = db.tables.list.length
126
+
127
+ #Remove everything in the db.
128
+ db.tables.list do |table|
129
+ table.drop unless table.native?
130
+ end
131
+
132
+ #Run the exported SQL.
133
+ db.transaction do
134
+ str_io.each_line do |sql|
135
+ db.q(sql)
136
+ end
137
+ end
138
+
139
+ #Vaildate import.
140
+ raise "Not same amount of tables: #{tables_count}, #{db.tables.list.length}" if tables_count != db.tables.list.length
141
+ end
142
+
143
+ it "should rename tables in revisions" do
144
+ test_table
145
+
146
+ Baza::Revision.new.init_db(db: db, debug: false, schema: {
147
+ tables: {
148
+ new_test_table: {
149
+ renames: [:test]
150
+ }
151
+ }
152
+ })
153
+ tables = db.tables.list
154
+ raise "Didnt expect table 'test' to exist but it did." if tables.key?(:test)
155
+ raise "Expected 'new_test_table' to exist but it didnt." if !tables.key?(:new_test_table)
156
+ end
157
+
158
+ it "should rename columns in revisions" do
159
+ test_table
160
+
161
+ Baza::Revision.new.init_db(db: db, debug: false, schema: {
162
+ tables: {
163
+ new_test_table: {
164
+ columns: [
165
+ {name: :new_name, type: :varchar, renames: [:text]}
166
+ ]
167
+ }
168
+ }
169
+ })
170
+ columns = db.tables[:new_test_table].columns
171
+ raise "Didnt expect 'text' to exist but it did." if columns.key?(:text)
172
+ raise "Expected 'new_name'-column to exist but it didnt." unless columns.key?(:new_name)
173
+ end
174
+
175
+ it "should generate proper sql" do
176
+ time = Time.new(1985, 6, 17, 10, 30)
177
+ db.insert(:test, {:date => time}, :return_sql => true).should eql("INSERT INTO `test` (`date`) VALUES ('1985-06-17 10:30:00')")
178
+
179
+ date = Date.new(1985, 6, 17)
180
+ db.insert(:test, {:date => date}, :return_sql => true).should eql("INSERT INTO `test` (`date`) VALUES ('1985-06-17')")
181
+ end
182
+
183
+ it "should be able to make new connections based on given objects" do
184
+ new_db = Baza::Db.from_object(:object => db.conn.conn)
185
+ end
186
+
187
+ it "should be able to do ID-queries through the select-method" do
188
+ db.tables.create(:test_table, {
189
+ :columns => [
190
+ {:name => :idrow, :type => :int, :autoincr => true, :primarykey => true},
191
+ {:name => :name, :type => :varchar}
192
+ ]
193
+ })
194
+
195
+ count = 0
196
+ 100.times do
197
+ arr = []
198
+ 100.times do
199
+ count += 1
200
+ arr << {name: "Kasper #{count}"}
201
+ end
202
+
203
+ db.insert_multi(:test_table, arr)
204
+ end
205
+
206
+ count_found = 0
207
+ db.select(:test_table, nil, :idquery => :idrow) do |row|
208
+ count_found += 1
209
+
210
+ row[:name].should eq "Kasper #{count_found}"
211
+ end
212
+
213
+ count_found.should eq 10000
214
+ end
215
+
216
+ it "should be able to use query buffers" do
217
+ db.tables.create(:test_table, {
218
+ :columns => [
219
+ {:name => :id, :type => :int, :autoincr => true, :primarykey => true},
220
+ {:name => :name, :type => :varchar}
221
+ ]
222
+ })
223
+
224
+ upsert = false
225
+ db.q_buffer do |buffer|
226
+ 2500.times do |count|
227
+ if upsert
228
+ buffer.upsert(:test_table, {:name => "Kasper #{count}"}, {:name => "Kasper #{count}"})
229
+ upsert = false
230
+ else
231
+ buffer.insert(:test_table, {:name => "Kasper #{count}"})
232
+ upsert = true
233
+ end
234
+ end
235
+ end
236
+
237
+ test_table = db.tables[:test_table]
238
+ test_table.rows_count.should eql(2500)
239
+
240
+ db.q_buffer do |buffer|
241
+ count = 0
242
+ upsert = false
243
+
244
+ db.select(:test_table, {}, :orderby => :id) do |row|
245
+ row[:name].should eql("Kasper #{count}")
246
+
247
+ if upsert
248
+ buffer.upsert(:test_table, {name: "Kasper #{count}-#{count}"}, {id: row[:id]})
249
+ upsert = false
250
+ else
251
+ buffer.update(:test_table, {name: "Kasper #{count}-#{count}"}, {id: row[:id]})
252
+ upsert = true
253
+ end
254
+
255
+ count += 1
256
+ end
257
+ end
258
+
259
+ count = 0
260
+ db.select(:test_table, {}, :orderby => :id) do |row|
261
+ row[:name].should eq "Kasper #{count}-#{count}"
262
+ count += 1
263
+ end
264
+
265
+ #Test the flush-async which flushes transactions in a thread asyncronous.
266
+ db.q_buffer(:flush_async => true) do |buffer|
267
+ count = 0
268
+ db.select(:test_table) do |row|
269
+ count += 1
270
+
271
+ if count == 1000
272
+ time_start = Time.now.to_f
273
+ end
274
+
275
+ buffer.delete(:test_table, {:id => row[:id]})
276
+
277
+ if count == 1000
278
+ time_end = Time.now.to_f
279
+
280
+ time_spent = time_end - time_start
281
+ raise "Too much time spent: '#{time_spent}'." if time_spent > 0.01
282
+ end
283
+ end
284
+ end
285
+
286
+ test_table.rows_count.should eql(0)
287
+ end
288
+ end
@@ -0,0 +1,49 @@
1
+ shared_examples_for "a baza columns driver" do
2
+ let(:constant){
3
+ name = described_class.name.split("::").last
4
+ const_name = "Info#{name.slice(0, 1).upcase}#{name.slice(1, name.length)}"
5
+ require "#{File.dirname(__FILE__)}/../info_#{StringCases.camel_to_snake(name)}"
6
+ raise "Constant was not defined: '#{const_name}'." unless Baza.const_defined?(const_name)
7
+ Baza.const_get(const_name)
8
+ }
9
+ let(:driver){ constant.new }
10
+ let(:db){ driver.db }
11
+ let(:test_table){
12
+ db.tables.create("test", {
13
+ columns: [
14
+ {name: "id", type: :int, autoincr: true, primarykey: true},
15
+ {name: "text", type: :varchar}
16
+ ]
17
+ })
18
+ db.tables[:test]
19
+ }
20
+
21
+ before do
22
+ driver.before
23
+ end
24
+
25
+ after do
26
+ driver.after
27
+ end
28
+
29
+ it "renames columns for renamed tables" do
30
+ # Load up columns to make them set table-name.
31
+ test_table.columns.each do |name, column|
32
+ end
33
+
34
+ test_table.rename("test2")
35
+ test_table.columns[:text].change(name: "text2")
36
+
37
+ table = db.tables[:test2]
38
+ column = table.columns[:text2]
39
+ column.table.name.should eq :test2
40
+ end
41
+
42
+ it "should create columns right" do
43
+ col_id = test_table.column(:id)
44
+ col_id.type.should eq :int
45
+
46
+ col_text = test_table.column(:text)
47
+ col_text.type.should eq :varchar
48
+ end
49
+ end
@@ -0,0 +1,49 @@
1
+ shared_examples_for "a baza indexes driver" do
2
+ let(:constant){
3
+ name = described_class.name.split("::").last
4
+ const_name = "Info#{name.slice(0, 1).upcase}#{name.slice(1, name.length)}"
5
+ require "#{File.dirname(__FILE__)}/../info_#{StringCases.camel_to_snake(name)}"
6
+ raise "Constant was not defined: '#{const_name}'." unless Baza.const_defined?(const_name)
7
+ Baza.const_get(const_name)
8
+ }
9
+ let(:driver){ constant.new }
10
+ let(:db){ driver.db }
11
+ let(:test_table){
12
+ db.tables.create("test", {
13
+ columns: [
14
+ {name: "id", type: :int, autoincr: true, primarykey: true},
15
+ {name: "text", type: :varchar}
16
+ ]
17
+ })
18
+ db.tables[:test]
19
+ }
20
+
21
+ before do
22
+ driver.before
23
+ end
24
+
25
+ after do
26
+ driver.after
27
+ end
28
+
29
+ it "renames indexes for renamed tables" do
30
+ # Load up columns to make them set table-name.
31
+ test_table.indexes.each do |name, index|
32
+ end
33
+
34
+ test_table.create_indexes([{name: "index_on_text", columns: [:text]}])
35
+ test_table.rename("test2")
36
+
37
+ test_table.index("index_on_text").rename("index_on_text2")
38
+
39
+ table = db.tables[:test2]
40
+ index = table.index(:index_on_text2)
41
+ index.table.name.should eq :test2
42
+ end
43
+
44
+ it "should raise an error when index is not found" do
45
+ expect {
46
+ test_table.index("index_that_doesnt_exist")
47
+ }.to raise_error(Errno::ENOENT)
48
+ end
49
+ end
@@ -0,0 +1,73 @@
1
+ shared_examples_for "a baza tables driver" do
2
+ let(:constant){
3
+ name = described_class.name.split("::").last
4
+ const_name = "Info#{name.slice(0, 1).upcase}#{name.slice(1, name.length)}"
5
+ require "#{File.dirname(__FILE__)}/../info_#{StringCases.camel_to_snake(name)}"
6
+ raise "Constant was not defined: '#{const_name}'." unless Baza.const_defined?(const_name)
7
+ Baza.const_get(const_name)
8
+ }
9
+ let(:driver){ constant.new }
10
+ let(:driver2){ constant.new }
11
+ let(:db){ driver.db }
12
+ let(:db2){ driver2.db }
13
+ let(:test_table){
14
+ db.tables.create("test", {
15
+ columns: [
16
+ {name: "id", type: :int, autoincr: true, primarykey: true},
17
+ {name: "text", type: :varchar}
18
+ ]
19
+ })
20
+ db.tables[:test]
21
+ }
22
+
23
+ before do
24
+ driver.before
25
+ end
26
+
27
+ after do
28
+ driver.after
29
+ end
30
+
31
+ it "should create tables" do
32
+ test_table.name.should eq :test
33
+ db.tables[:test].should_not eq nil
34
+ end
35
+
36
+ it "should list tables" do
37
+ test_table
38
+ db.tables.list.values.should include test_table
39
+ end
40
+
41
+ it "should optimize tables" do
42
+ test_table.optimize
43
+ # FIXME: How to validate?
44
+ end
45
+
46
+ it "should truncate tables" do
47
+ test_table
48
+
49
+ db.insert(:test, text: "test")
50
+ test_table.rows_count.should eq 1
51
+
52
+ # Throw out invalid encoding because it will make dumping fail.
53
+ db.tables[:test].truncate
54
+ test_table.rows_count.should eq 0
55
+ end
56
+
57
+ it "#clone" do
58
+ test_table
59
+ test_table.create_indexes([{name: "index_on_text", columns: ["text"]}])
60
+ test_table.indexes.length.should eq 1
61
+
62
+ test_table.insert(text: "test1")
63
+ test_table.insert(text: "test2")
64
+
65
+ test_table.clone("test2")
66
+ test_table2 = db.tables[:test2]
67
+
68
+ test_table2.columns.length.should eq test_table.columns.length
69
+ test_table2.indexes.length.should eq test_table.indexes.length
70
+ test_table2.rows_count.should eq test_table.rows_count
71
+ test_table2.rows_count.should eq 2
72
+ end
73
+ end