baza 0.0.20 → 0.0.21

Sign up to get free protection for your applications and to get access to all the features.
Files changed (167) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +84 -0
  3. data/.rubocop_todo.yml +17 -135
  4. data/.travis.yml +21 -0
  5. data/Gemfile +10 -7
  6. data/Gemfile.lock +39 -44
  7. data/README.md +61 -3
  8. data/VERSION +1 -1
  9. data/baza.gemspec +146 -98
  10. data/config/best_project_practice_rubocop.yml +8 -0
  11. data/config/best_project_practice_rubocop_todo.yml +6 -0
  12. data/lib/baza.rb +8 -12
  13. data/lib/baza/base_sql_driver.rb +198 -52
  14. data/lib/baza/cloner.rb +1 -0
  15. data/lib/baza/column.rb +26 -0
  16. data/lib/baza/database.rb +19 -3
  17. data/lib/baza/db.rb +69 -271
  18. data/lib/baza/driver.rb +1 -6
  19. data/lib/baza/{drivers → driver}/active_record.rb +65 -21
  20. data/lib/baza/{drivers → driver}/active_record/columns.rb +0 -0
  21. data/lib/baza/driver/active_record/commands.rb +10 -0
  22. data/lib/baza/driver/active_record/databases.rb +10 -0
  23. data/lib/baza/{drivers → driver}/active_record/indexes.rb +0 -0
  24. data/lib/baza/{drivers → driver}/active_record/result.rb +3 -1
  25. data/lib/baza/{drivers → driver}/active_record/tables.rb +0 -0
  26. data/lib/baza/driver/active_record/users.rb +12 -0
  27. data/lib/baza/{drivers → driver}/mysql.rb +9 -26
  28. data/lib/baza/{drivers → driver}/mysql/column.rb +14 -35
  29. data/lib/baza/{drivers → driver}/mysql/columns.rb +9 -12
  30. data/lib/baza/driver/mysql/commands.rb +39 -0
  31. data/lib/baza/driver/mysql/database.rb +64 -0
  32. data/lib/baza/driver/mysql/databases.rb +63 -0
  33. data/lib/baza/{drivers → driver}/mysql/index.rb +0 -0
  34. data/lib/baza/{drivers → driver}/mysql/indexes.rb +0 -0
  35. data/lib/baza/{drivers → driver}/mysql/result.rb +15 -7
  36. data/lib/baza/{drivers → driver}/mysql/sqlspecs.rb +0 -0
  37. data/lib/baza/{drivers → driver}/mysql/table.rb +27 -43
  38. data/lib/baza/{drivers → driver}/mysql/tables.rb +5 -34
  39. data/lib/baza/{drivers → driver}/mysql/unbuffered_result.rb +8 -2
  40. data/lib/baza/driver/mysql/user.rb +22 -0
  41. data/lib/baza/driver/mysql/users.rb +39 -0
  42. data/lib/baza/{drivers → driver}/mysql2.rb +19 -49
  43. data/lib/baza/{drivers → driver}/mysql2/column.rb +0 -0
  44. data/lib/baza/{drivers → driver}/mysql2/columns.rb +0 -0
  45. data/lib/baza/driver/mysql2/commands.rb +2 -0
  46. data/lib/baza/{drivers → driver}/mysql2/database.rb +0 -0
  47. data/lib/baza/{drivers → driver}/mysql2/databases.rb +0 -0
  48. data/lib/baza/{drivers → driver}/mysql2/index.rb +0 -0
  49. data/lib/baza/{drivers → driver}/mysql2/indexes.rb +0 -0
  50. data/lib/baza/{drivers → driver}/mysql2/result.rb +3 -1
  51. data/lib/baza/{drivers → driver}/mysql2/table.rb +0 -0
  52. data/lib/baza/{drivers → driver}/mysql2/tables.rb +0 -0
  53. data/lib/baza/driver/mysql2/user.rb +2 -0
  54. data/lib/baza/driver/mysql2/users.rb +2 -0
  55. data/lib/baza/{drivers → driver}/mysql_java.rb +60 -38
  56. data/lib/baza/{drivers → driver}/mysql_java/column.rb +0 -0
  57. data/lib/baza/{drivers → driver}/mysql_java/columns.rb +0 -0
  58. data/lib/baza/driver/mysql_java/commands.rb +2 -0
  59. data/lib/baza/driver/mysql_java/database.rb +2 -0
  60. data/lib/baza/driver/mysql_java/databases.rb +2 -0
  61. data/lib/baza/{drivers → driver}/mysql_java/index.rb +0 -0
  62. data/lib/baza/{drivers → driver}/mysql_java/indexes.rb +0 -0
  63. data/lib/baza/{drivers → driver}/mysql_java/table.rb +0 -0
  64. data/lib/baza/{drivers → driver}/mysql_java/tables.rb +0 -0
  65. data/lib/baza/driver/mysql_java/user.rb +2 -0
  66. data/lib/baza/driver/mysql_java/users.rb +2 -0
  67. data/lib/baza/driver/pg.rb +80 -0
  68. data/lib/baza/driver/pg/column.rb +125 -0
  69. data/lib/baza/driver/pg/columns.rb +37 -0
  70. data/lib/baza/driver/pg/commands.rb +35 -0
  71. data/lib/baza/driver/pg/create_index_sql_creator.rb +51 -0
  72. data/lib/baza/driver/pg/database.rb +89 -0
  73. data/lib/baza/driver/pg/databases.rb +79 -0
  74. data/lib/baza/driver/pg/index.rb +35 -0
  75. data/lib/baza/driver/pg/indexes.rb +5 -0
  76. data/lib/baza/driver/pg/result.rb +139 -0
  77. data/lib/baza/driver/pg/table.rb +184 -0
  78. data/lib/baza/driver/pg/tables.rb +45 -0
  79. data/lib/baza/{drivers → driver}/sqlite3.rb +6 -24
  80. data/lib/baza/{drivers → driver}/sqlite3/column.rb +22 -24
  81. data/lib/baza/{drivers → driver}/sqlite3/columns.rb +6 -6
  82. data/lib/baza/driver/sqlite3/commands.rb +28 -0
  83. data/lib/baza/{drivers → driver}/sqlite3/database.rb +0 -0
  84. data/lib/baza/{drivers → driver}/sqlite3/databases.rb +0 -1
  85. data/lib/baza/{drivers → driver}/sqlite3/index.rb +0 -0
  86. data/lib/baza/{drivers → driver}/sqlite3/indexes.rb +0 -0
  87. data/lib/baza/{drivers → driver}/sqlite3/result.rb +14 -6
  88. data/lib/baza/{drivers → driver}/sqlite3/sqlspecs.rb +0 -0
  89. data/lib/baza/{drivers → driver}/sqlite3/table.rb +25 -16
  90. data/lib/baza/{drivers → driver}/sqlite3/tables.rb +5 -6
  91. data/lib/baza/{drivers → driver}/sqlite3/unbuffered_result.rb +8 -2
  92. data/lib/baza/{drivers → driver}/sqlite3_java.rb +13 -23
  93. data/lib/baza/{drivers → driver}/sqlite3_java/column.rb +0 -0
  94. data/lib/baza/{drivers → driver}/sqlite3_java/columns.rb +0 -0
  95. data/lib/baza/driver/sqlite3_java/commands.rb +2 -0
  96. data/lib/baza/{drivers → driver}/sqlite3_java/database.rb +0 -0
  97. data/lib/baza/{drivers → driver}/sqlite3_java/index.rb +0 -0
  98. data/lib/baza/{drivers → driver}/sqlite3_java/indexes.rb +0 -0
  99. data/lib/baza/{drivers → driver}/sqlite3_java/table.rb +0 -0
  100. data/lib/baza/{drivers → driver}/sqlite3_java/tables.rb +0 -0
  101. data/lib/baza/{drivers → driver}/sqlite3_java/unbuffered_result.rb +14 -9
  102. data/lib/baza/{drivers → driver}/sqlite3_rhodes.rb +6 -24
  103. data/lib/baza/errors.rb +2 -0
  104. data/lib/baza/idquery.rb +15 -8
  105. data/lib/baza/index.rb +7 -0
  106. data/lib/baza/jdbc_driver.rb +4 -16
  107. data/lib/baza/jdbc_result.rb +20 -12
  108. data/lib/baza/mysql_base_driver.rb +7 -7
  109. data/lib/baza/query_buffer.rb +20 -19
  110. data/lib/baza/row.rb +16 -16
  111. data/lib/baza/sql_queries.rb +3 -0
  112. data/lib/baza/sql_queries/generic_insert.rb +81 -0
  113. data/lib/baza/sql_queries/generic_update.rb +31 -0
  114. data/lib/baza/sql_queries/mysql_upsert.rb +52 -0
  115. data/lib/baza/sql_queries/mysql_upsert_duplicate_key.rb +57 -0
  116. data/lib/baza/sql_queries/non_atomic_upsert.rb +25 -0
  117. data/lib/baza/sql_queries/postgres_upsert_duplicate_key.rb +118 -0
  118. data/lib/baza/sql_queries/select.rb +170 -0
  119. data/lib/baza/sql_queries/sqlite_upsert_duplicate_key.rb +99 -0
  120. data/lib/baza/table.rb +35 -8
  121. data/spec/active_record/models/user.rb +3 -0
  122. data/spec/{cloner_spec.rb → baza/cloner_spec.rb} +0 -0
  123. data/spec/drivers/active_record_mysql2_spec.rb +5 -3
  124. data/spec/drivers/active_record_mysql_spec.rb +2 -1
  125. data/spec/drivers/active_record_pg_spec.rb +20 -0
  126. data/spec/drivers/active_record_sqlite3_spec.rb +2 -1
  127. data/spec/drivers/mysql2_spec.rb +1 -1
  128. data/spec/drivers/mysql_spec.rb +10 -10
  129. data/spec/drivers/pg_spec.rb +18 -0
  130. data/spec/drivers/sqlite3_spec.rb +7 -8
  131. data/spec/info_active_record_example.rb +1 -1
  132. data/spec/{info_active_record_mysql2.rb → info_active_record_mysql2_example.rb} +3 -2
  133. data/spec/info_active_record_mysql2_travis.rb +35 -0
  134. data/spec/{info_active_record_mysql.rb → info_active_record_mysql_example.rb} +5 -4
  135. data/spec/info_active_record_mysql_travis.rb +36 -0
  136. data/spec/info_active_record_pg_example.rb +36 -0
  137. data/spec/info_active_record_pg_travis.rb +34 -0
  138. data/spec/info_active_record_sqlite3.rb +1 -1
  139. data/spec/info_mysql2_example.rb +1 -3
  140. data/spec/{info_mysql2_shippable.rb → info_mysql2_travis.rb} +2 -4
  141. data/spec/info_mysql_example.rb +1 -3
  142. data/spec/{info_mysql_shippable.rb → info_mysql_travis.rb} +2 -4
  143. data/spec/info_pg_example.rb +22 -0
  144. data/spec/info_pg_travis.rb +20 -0
  145. data/spec/info_sqlite3.rb +1 -3
  146. data/spec/spec_helper.rb +1 -1
  147. data/spec/support/driver_active_record_collection.rb +62 -0
  148. data/spec/support/driver_collection.rb +136 -121
  149. data/spec/support/driver_columns_collection.rb +19 -10
  150. data/spec/support/driver_databases_collection.rb +23 -1
  151. data/spec/support/driver_indexes_collection.rb +2 -2
  152. data/spec/support/driver_tables_collection.rb +24 -4
  153. data/spec/support/driver_users_collection.rb +53 -0
  154. metadata +185 -104
  155. data/lib/baza/drivers/mysql/database.rb +0 -28
  156. data/lib/baza/drivers/mysql/databases.rb +0 -35
  157. data/lib/baza/drivers/mysql_java/database.rb +0 -2
  158. data/lib/baza/drivers/mysql_java/databases.rb +0 -2
  159. data/lib/baza/model.rb +0 -875
  160. data/lib/baza/model_custom.rb +0 -155
  161. data/lib/baza/model_handler.rb +0 -910
  162. data/lib/baza/model_handler_sqlhelper.rb +0 -484
  163. data/lib/baza/revision.rb +0 -383
  164. data/shippable.yml +0 -17
  165. data/spec/info_active_record_mysql2_shippable.rb +0 -34
  166. data/spec/info_active_record_mysql_shippable.rb +0 -34
  167. data/spec/model_handler_spec.rb +0 -431
@@ -9,12 +9,16 @@ shared_examples_for "a baza driver" do
9
9
  db.select(:test, text: "Kasper").fetch
10
10
  end
11
11
  let(:test_table) do
12
- db.tables.create("test", columns: [
13
- {name: "id", type: :int, autoincr: true, primarykey: true},
14
- {name: "text", type: :varchar},
15
- {name: "number", type: :int, default: 0},
16
- {name: "float", type: :float, default: 0.0}
17
- ])
12
+ db.tables.create(
13
+ "test",
14
+ columns: [
15
+ {name: "id", type: :int, autoincr: true, primarykey: true},
16
+ {name: "text", type: :varchar},
17
+ {name: "number", type: :int, default: 0},
18
+ {name: "float", type: :float, default: 0.0},
19
+ {name: "created_at", type: :datetime}
20
+ ]
21
+ )
18
22
  db.tables[:test]
19
23
  end
20
24
 
@@ -28,62 +32,29 @@ shared_examples_for "a baza driver" do
28
32
  driver2.after
29
33
  end
30
34
 
31
- it "should do revisions" do
32
- test_table
33
-
34
- schema = {
35
- tables: {
36
- test_table: {
37
- columns: [
38
- {name: "id", type: :int, autoincr: true, primarykey: true},
39
- {name: "name", type: :varchar},
40
- {name: "age", type: :int, default: 0},
41
- {name: "nickname", type: :varchar, default: ""}
42
- ],
43
- indexes: [
44
- "name"
45
- ],
46
- rows: [
47
- {
48
- find_by: {"id" => 1},
49
- data: {"id" => 1, "name" => "trala"}
50
- }
51
- ]
52
- }
53
- }
54
- }
55
-
56
- rev = Baza::Revision.new
57
- rev.init_db(schema: schema, debug: false, db: db)
58
-
59
- test_table = db.tables[:test_table]
60
- expect(test_table.columns.map(&:name)).to include "age"
61
- expect(test_table.columns.map(&:name)).to include "nickname"
62
- end
63
-
64
35
  it "does id-queries" do
65
36
  test_table
66
37
 
67
38
  rows_count = 1250
68
39
  db.transaction do
69
- 0.upto(rows_count) do |count|
40
+ rows_count.times do |count|
70
41
  db.insert(:test, text: "User #{count}")
71
42
  end
72
43
  end
73
44
 
74
45
  block_ran = 0
75
- idq = Baza::Idquery.new(db: db, debug: false, table: :test, query: "SELECT id FROM test") do |_data|
46
+ Baza::Idquery.new(db: db, debug: false, table: :test, query: "SELECT id FROM test") do
76
47
  block_ran += 1
77
48
  end
78
49
 
79
- raise "Block with should have ran too little: #{block_ran}." if block_ran < rows_count
50
+ expect(block_ran).to eq rows_count
80
51
 
81
52
  block_ran = 0
82
- db.select(:test, {}, idquery: true) do |_data|
53
+ db.select(:test, {}, idquery: true) do
83
54
  block_ran += 1
84
55
  end
85
56
 
86
- raise "Block with should have ran too little: #{block_ran}." if block_ran < rows_count
57
+ expect(block_ran).to eq rows_count
87
58
  end
88
59
 
89
60
  it "does unbuffered queries" do
@@ -94,7 +65,7 @@ shared_examples_for "a baza driver" do
94
65
  end
95
66
 
96
67
  count_results = 0
97
- db.q("SELECT * FROM test", type: :unbuffered) do |row|
68
+ db.query_ubuf("SELECT * FROM test") do |row|
98
69
  expect(row[:text]).to eq "Test #{count_results}"
99
70
  count_results += 1
100
71
  end
@@ -102,7 +73,7 @@ shared_examples_for "a baza driver" do
102
73
  expect(count_results).to eq 10
103
74
  end
104
75
 
105
- it "does upserting" do
76
+ it "#upsert" do
106
77
  test_table.create_columns([{name: "nickname", type: :varchar}])
107
78
 
108
79
  # Test upserting.
@@ -116,17 +87,53 @@ shared_examples_for "a baza driver" do
116
87
 
117
88
  db.upsert(:test, data, sel)
118
89
  row = db.select(:test, sel).fetch
119
- row[:text].should eq "upsert - Kasper Johansen"
90
+ expect(row[:text]).to eq "upsert - Kasper Johansen"
120
91
 
121
92
  table.reload
122
- table.rows_count.should eql(rows_count + 1)
93
+ expect(table.rows_count).to eq rows_count + 1
123
94
 
124
95
  db.upsert(:test, data2, sel)
125
96
  row = db.select(:test, sel).fetch
126
- row[:text].should eq "upsert - Kasper Nielsen Johansen"
97
+ expect(row[:text]).to eq "upsert - Kasper Nielsen Johansen"
127
98
 
128
99
  table.reload
129
- table.rows_count.should eq rows_count + 1
100
+ expect(table.rows_count).to eq rows_count + 1
101
+ end
102
+
103
+ describe "#upsert_duplicate_key" do
104
+ before do
105
+ test_table.create_indexes([name: "unique_text", columns: ["text"], unique: true])
106
+ end
107
+
108
+ it "inserts records with terms" do
109
+ expect(test_table.rows_count).to eq 0
110
+ test_table.upsert_duplicate_key({number: 2}, text: "test1")
111
+ expect(test_table.rows_count).to eq 1
112
+ end
113
+
114
+ it "updates existing records with terms" do
115
+ test_table.insert(text: "test1", number: 1)
116
+ test_table.upsert_duplicate_key({number: 2}, text: "test1")
117
+ expect(test_table.rows_count).to eq 1
118
+
119
+ rows = test_table.rows.to_a.map(&:to_hash)
120
+ rows[0][:float] = "0.0" if rows[0][:float] == "0"
121
+ expect(rows).to eq [{id: "1", text: "test1", number: "2", float: "0.0", created_at: ""}]
122
+ end
123
+
124
+ it "inserts with empty terms" do
125
+ expect(test_table.rows_count).to eq 0
126
+ id = test_table.upsert_duplicate_key({text: "test2"}, {}, return_id: true)
127
+ expect(test_table.rows_count).to eq 1
128
+ expect(id).to eq 1
129
+ end
130
+
131
+ it "updates existing records with empty terms" do
132
+ test_table.insert(text: "test1", number: 1)
133
+ id = test_table.upsert_duplicate_key({number: 2, text: "test1"}, {}, return_id: true)
134
+ expect(test_table.rows_count).to eq 1
135
+ expect(id).to eq 1
136
+ end
130
137
  end
131
138
 
132
139
  it "dumps as SQL" do
@@ -139,9 +146,7 @@ shared_examples_for "a baza driver" do
139
146
  tables_count = db.tables.list.length
140
147
 
141
148
  # Remove everything in the db.
142
- db.tables.list do |table|
143
- table.drop unless table.native?
144
- end
149
+ db.tables.list.select(&:native?).each(&:drop)
145
150
 
146
151
  # Run the exported SQL.
147
152
  db.transaction do
@@ -154,69 +159,29 @@ shared_examples_for "a baza driver" do
154
159
  raise "Not same amount of tables: #{tables_count}, #{db.tables.list.length}" if tables_count != db.tables.list.length
155
160
  end
156
161
 
157
- it "renames tables in revisions" do
158
- test_table
159
-
160
- Baza::Revision.new.init_db(
161
- db: db,
162
- debug: false,
163
- schema: {
164
- tables: {
165
- new_test_table: {
166
- renames: [:test]
167
- }
168
- }
169
- }
170
- )
171
-
172
- tables = db.tables.list.map(&:name)
173
-
174
- expect(tables).to_not include "test"
175
- expect(tables).to include "new_test_table"
176
- end
177
-
178
- it "renames columns in revisions" do
179
- test_table
180
-
181
- Baza::Revision.new.init_db(
182
- db: db,
183
- debug: false,
184
- schema: {
185
- tables: {
186
- new_test_table: {
187
- columns: [
188
- {name: :new_name, type: :varchar, renames: [:text]}
189
- ]
190
- }
191
- }
192
- }
193
- )
194
-
195
- columns = db.tables[:new_test_table].columns.map(&:name)
196
- expect(columns).to_not include "text"
197
- expect(columns).to include "new_name"
198
- end
199
-
200
162
  it "generates proper sql" do
201
163
  time = Time.new(1985, 6, 17, 10, 30)
202
- db.insert(:test, {date: time}, return_sql: true).should eql("INSERT INTO `test` (`date`) VALUES ('1985-06-17 10:30:00')")
164
+ expect(db.insert(:test, {date: time}, return_sql: true)).to eq "INSERT INTO #{db.sep_table}test#{db.sep_table} (#{db.sep_col}date#{db.sep_col}) VALUES (#{db.sep_val}1985-06-17 10:30:00#{db.sep_val})"
203
165
 
204
166
  date = Date.new(1985, 6, 17)
205
- db.insert(:test, {date: date}, return_sql: true).should eql("INSERT INTO `test` (`date`) VALUES ('1985-06-17')")
167
+ expect(db.insert(:test, {date: date}, return_sql: true)).to eq "INSERT INTO #{db.sep_table}test#{db.sep_table} (#{db.sep_col}date#{db.sep_col}) VALUES (#{db.sep_val}1985-06-17#{db.sep_val})"
206
168
  end
207
169
 
208
170
  it "is able to make new connections based on given objects" do
209
171
  # Mysql doesn't support it...
210
172
  unless db.opts.fetch(:type) == :mysql
211
- new_db = Baza::Db.from_object(object: db.driver.conn)
173
+ Baza::Db.from_object(object: db.driver.conn)
212
174
  end
213
175
  end
214
176
 
215
177
  it "is able to do ID-queries through the select-method" do
216
- db.tables.create(:test_table, columns: [
217
- {name: :idrow, type: :int, autoincr: true, primarykey: true},
218
- {name: :name, type: :varchar}
219
- ])
178
+ db.tables.create(
179
+ :test_table,
180
+ columns: [
181
+ {name: :idrow, type: :int, autoincr: true, primarykey: true},
182
+ {name: :name, type: :varchar}
183
+ ]
184
+ )
220
185
 
221
186
  count = 0
222
187
  100.times do
@@ -233,17 +198,20 @@ shared_examples_for "a baza driver" do
233
198
  db.select(:test_table, nil, idquery: :idrow) do |row|
234
199
  count_found += 1
235
200
 
236
- row[:name].should eq "Kasper #{count_found}"
201
+ expect(row[:name]).to eq "Kasper #{count_found}"
237
202
  end
238
203
 
239
204
  expect(count_found).to eq 10_000
240
205
  end
241
206
 
242
- it "should be able to use query buffers" do
243
- db.tables.create(:test_table, columns: [
244
- {name: :id, type: :int, autoincr: true, primarykey: true},
245
- {name: :name, type: :varchar}
246
- ])
207
+ it "uses query buffers" do
208
+ db.tables.create(
209
+ :test_table,
210
+ columns: [
211
+ {name: :id, type: :int, autoincr: true, primarykey: true},
212
+ {name: :name, type: :varchar}
213
+ ]
214
+ )
247
215
 
248
216
  upsert = false
249
217
  count_inserts = 0
@@ -300,16 +268,15 @@ shared_examples_for "a baza driver" do
300
268
  count = 0
301
269
  db.select(:test_table) do |row|
302
270
  count += 1
303
-
304
271
  time_start = Time.now.to_f if count == 1000
305
272
 
306
- buffer.delete(:test_table, id: row[:id])
273
+ buffer.delete(:test_table, id: row.fetch(:id))
307
274
 
308
275
  next unless count == 1000
309
276
  time_end = Time.now.to_f
310
277
 
311
278
  time_spent = time_end - time_start
312
- raise "Too much time spent: '#{time_spent}'." if time_spent > 0.01
279
+ raise "Too much time spent: '#{time_spent}'." if time_spent > 0.015
313
280
  end
314
281
  end
315
282
 
@@ -356,14 +323,17 @@ shared_examples_for "a baza driver" do
356
323
  end
357
324
 
358
325
  it "does type translation" do
359
- db_with_type_translation.tables.create(:test, columns: [
360
- {name: "id", type: :int, autoincr: true, primarykey: true},
361
- {name: "text", type: :varchar},
362
- {name: "number", type: :int},
363
- {name: "float", type: :float},
364
- {name: "created_at", type: :datetime},
365
- {name: "date", type: :date}
366
- ])
326
+ db_with_type_translation.tables.create(
327
+ :test,
328
+ columns: [
329
+ {name: "id", type: :int, autoincr: true, primarykey: true},
330
+ {name: "text", type: :varchar},
331
+ {name: "number", type: :int},
332
+ {name: "float", type: :float},
333
+ {name: "created_at", type: :datetime},
334
+ {name: "date", type: :date}
335
+ ]
336
+ )
367
337
 
368
338
  db_with_type_translation.insert(:test, text: "Kasper", number: 30, float: 4.5, created_at: Time.now, date: Date.new(2015, 06, 17))
369
339
 
@@ -373,9 +343,54 @@ shared_examples_for "a baza driver" do
373
343
  expect(row.fetch(:number).class).to eq Fixnum
374
344
  expect(row.fetch(:float).class).to eq Float
375
345
 
376
- unless db.driver.conn.class.name == "ActiveRecord::ConnectionAdapters::SQLite3Adapter"
346
+ if db.driver.conn.class.name == "ActiveRecord::ConnectionAdapters::SQLite3Adapter"
347
+ check_time_and_date = false
348
+ elsif db.driver.class.name == "Baza::Driver::ActiveRecord" && RUBY_PLATFORM == "java"
349
+ check_time_and_date = false
350
+ else
351
+ check_time_and_date = true
352
+ end
353
+
354
+ if check_time_and_date
377
355
  expect(row.fetch(:created_at).class).to eq Time
378
356
  expect(row.fetch(:date).class).to eq Date
379
357
  end
380
358
  end
359
+
360
+ it "returns arguments used to connect" do
361
+ require_relative "../../lib/baza/driver/active_record"
362
+
363
+ unless db.driver.is_a?(Baza::Driver::ActiveRecord)
364
+ args = db.driver.class.args
365
+ expect(args).to be_a Array
366
+ end
367
+ end
368
+
369
+ it "#new_query" do
370
+ test_table
371
+ test_table.insert(text: "Kasper")
372
+
373
+ query = db.new_query.from(:test).where(text: "Kasper").to_a
374
+ query[0][:float] = query[0][:float].to_f.to_s if query[0][:float] == "0"
375
+ expect(query.to_a).to eq [{id: "1", text: "Kasper", number: "0", float: "0.0", created_at: ""}]
376
+ end
377
+
378
+ it "#last_id" do
379
+ test_table.insert(text: "Kasper")
380
+ expect(db.last_id).to eq 1
381
+ end
382
+
383
+ it "handels null values for datetimes" do
384
+ id = test_table.insert({text: "Kasper"}, return_id: true)
385
+ row = test_table.row(id)
386
+ expect(row[:created_at]).to eq ""
387
+ end
388
+
389
+ describe "#insert" do
390
+ it "returns id" do
391
+ test_table
392
+ id = db.insert("test", {text: "Kasper"}, return_id: true)
393
+ expect(id).to eq 1
394
+ end
395
+ end
381
396
  end
@@ -2,10 +2,13 @@ shared_examples_for "a baza columns driver" do
2
2
  let(:driver) { constant.new }
3
3
  let(:db) { driver.db }
4
4
  let(:test_table) do
5
- db.tables.create("test", columns: [
6
- {name: "id", type: :int, autoincr: true, primarykey: true},
7
- {name: "text", type: :varchar}
8
- ])
5
+ db.tables.create(
6
+ "test",
7
+ columns: [
8
+ {name: "id", type: :int, autoincr: true, primarykey: true},
9
+ {name: "text", type: :varchar}
10
+ ]
11
+ )
9
12
  db.tables[:test]
10
13
  end
11
14
 
@@ -25,24 +28,30 @@ shared_examples_for "a baza columns driver" do
25
28
 
26
29
  table = db.tables[:test2]
27
30
  column = table.column(:text2)
28
- column.table.name.should eq "test2"
31
+ expect(column.table.name).to eq "test2"
29
32
  end
30
33
 
31
34
  it "creates columns right" do
32
35
  col_id = test_table.column(:id)
33
- col_id.type.should eq :int
36
+ expect(col_id.type).to eq :int
37
+ expect(col_id.primarykey?).to eq true
38
+ expect(col_id.autoincr?).to eq true
39
+ expect(col_id.default).to eq nil
34
40
 
35
41
  col_text = test_table.column(:text)
36
- col_text.type.should eq :varchar
42
+ expect(col_text.type).to eq :varchar
43
+ expect(col_text.primarykey?).to eq false
44
+ expect(col_text.autoincr?).to eq false
45
+ expect(col_text.default).to eq nil
37
46
  end
38
47
 
39
48
  it "is able to change columns" do
40
49
  col_text = test_table.column(:text)
41
50
  col_text.change(name: "text2", type: :int, default: 5)
42
51
 
43
- col_text.type.should eq :int
44
- col_text.default.should eq "5"
45
- col_text.name.should eq "text2"
52
+ expect(col_text.type).to eq :int
53
+ expect(col_text.default).to eq "5"
54
+ expect(col_text.name).to eq "text2"
46
55
  end
47
56
 
48
57
  it "is able to drop a column" do
@@ -11,7 +11,9 @@ shared_examples_for "a baza databases driver" do
11
11
  it "renames database" do
12
12
  begin
13
13
  db.databases["renamed-db"].drop
14
+ # rubocop:disable Lint/HandleExceptions
14
15
  rescue Baza::Errors::DatabaseNotFound
16
+ # rubocop:enable Lint/HandleExceptions
15
17
  # Ignore - it shouldn't exist
16
18
  end
17
19
 
@@ -23,7 +25,27 @@ shared_examples_for "a baza databases driver" do
23
25
 
24
26
  it "drops databases" do
25
27
  test_database.drop
26
-
27
28
  expect { db.databases["baza-test-create"] }.to raise_error(Baza::Errors::DatabaseNotFound)
28
29
  end
30
+
31
+ it "creates tables" do
32
+ if test_database.table_exists?("test")
33
+ puts "DROPPING TEST TABLE"
34
+ test_database.table("test").drop
35
+ end
36
+
37
+ test_database.create_table(
38
+ "test",
39
+ columns: [
40
+ {name: :id, type: :int, autoincr: true, primarykey: true},
41
+ {name: :name, type: :varchar}
42
+ ]
43
+ )
44
+
45
+ tables = test_database.tables.map(&:name).to_a
46
+ expect(tables).to eq ["test"]
47
+
48
+ table = test_database.table("test")
49
+ expect(table.name).to eq "test"
50
+ end
29
51
  end