baza 0.0.13 → 0.0.14

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,4 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
- require "tmpdir"
1
+ require "spec_helper"
3
2
 
4
3
  describe "Objects" do
5
4
  it "should be able to cache rows" do
@@ -8,13 +7,14 @@ describe "Objects" do
8
7
  rescue LoadError
9
8
  require "array_enumerator"
10
9
  end
11
-
10
+
12
11
  require "sqlite3" if RUBY_ENGINE != "jruby"
13
-
12
+ require "tmpdir"
13
+
14
14
  $db_path = "#{Dir.tmpdir}/knjrbfw_objects_cache_test.sqlite3"
15
15
  File.unlink($db_path) if File.exists?($db_path)
16
16
  $db = Baza::Db.new(:type => :sqlite3, :path => $db_path, :return_keys => "symbols", :debug => false)
17
-
17
+
18
18
  schema = {
19
19
  :tables => {
20
20
  "Group" => {
@@ -32,9 +32,9 @@ describe "Objects" do
32
32
  }
33
33
  }
34
34
  Baza::Revision.new.init_db(:schema => schema, :db => $db)
35
-
35
+
36
36
  class User < Baza::Model; end
37
-
37
+
38
38
  $ob = Baza::ModelHandler.new(
39
39
  :db => $db,
40
40
  :datarow => true,
@@ -46,7 +46,7 @@ describe "Objects" do
46
46
  }
47
47
  }
48
48
  )
49
-
49
+
50
50
  $ob.adds(:User, [
51
51
  {:username => "User 1"},
52
52
  {:username => "User 2"},
@@ -54,32 +54,32 @@ describe "Objects" do
54
54
  {:username => "User 4"},
55
55
  {:username => "User 5"}
56
56
  ])
57
-
57
+
58
58
  raise "Expected user-ID-cache to be 5 but it wasnt: #{$ob.ids_cache[:User].length}" if $ob.ids_cache[:User].length != 5
59
-
59
+
60
60
  user = $ob.get(:User, 4)
61
61
  raise "No user returned." if !user
62
62
  $ob.delete(user)
63
63
  raise "Expected user-ID-cache to be 4 but it wasnt: #{$ob.ids_cache[:User].length} #{$ob.ids_cache}" if $ob.ids_cache[:User].length != 4
64
-
64
+
65
65
  $ob.deletes([$ob.get(:User, 1), $ob.get(:User, 2)])
66
66
  raise "Expected user-ID-cache to be 2 but it wasnt: #{$ob.ids_cache[:User].length} #{$ob.ids_cache}" if $ob.ids_cache[:User].length != 2
67
67
  end
68
-
68
+
69
69
  it "should be able to do 'select_col_as_array'" do
70
70
  res = $ob.list(:User, {"select_col_as_array" => "id"}).to_a
71
71
  raise "Expected length of 2 but got: #{res.length}" if res.length != 2
72
72
  end
73
-
73
+
74
74
  it "should work even though stressed by threads (thread-safe)." do
75
75
  userd = []
76
76
  10.upto(25) do |i|
77
77
  userd << {:username => "User #{i}"}
78
78
  end
79
-
79
+
80
80
  $ob.adds(:User, userd)
81
81
  users = $ob.list(:User).to_a
82
-
82
+
83
83
  #Stress it to test threadsafety...
84
84
  threads = []
85
85
  0.upto(5) do |tc|
@@ -88,39 +88,39 @@ describe "Objects" do
88
88
  user = $ob.add(:User, {:username => "User #{tc}-#{ic}"})
89
89
  raise "No user returned." if !user
90
90
  $ob.delete(user)
91
-
91
+
92
92
  user1 = $ob.add(:User, {:username => "User #{tc}-#{ic}-1"})
93
93
  user2 = $ob.add(:User, {:username => "User #{tc}-#{ic}-2"})
94
94
  user3 = $ob.add(:User, {:username => "User #{tc}-#{ic}-3"})
95
-
95
+
96
96
  raise "Missing user?" if !user1 or !user2 or !user3 or user1.deleted? or user2.deleted? or user3.deleted?
97
97
  $ob.deletes([user1, user2, user3])
98
-
98
+
99
99
  count = 0
100
100
  users.each do |user|
101
101
  count += 1
102
102
  user[:username] = "#{user[:username]}." if !user.deleted?
103
103
  end
104
-
104
+
105
105
  raise "Expected at least 15 users but got #{count}." if count != 18
106
106
  end
107
107
  end
108
108
  end
109
-
109
+
110
110
  threads.each do |thread|
111
111
  thread.join
112
112
  end
113
113
  end
114
-
114
+
115
115
  it "should be able to skip queries when adding" do
116
116
  class Group < Baza::Model; end
117
-
117
+
118
118
  $ob2 = Baza::ModelHandler.new(
119
119
  :db => $db,
120
120
  :datarow => true,
121
121
  :require => false
122
122
  )
123
-
123
+
124
124
  threads = []
125
125
  0.upto(5) do
126
126
  threads << Knj::Thread.new do
@@ -130,16 +130,16 @@ describe "Objects" do
130
130
  end
131
131
  end
132
132
  end
133
-
133
+
134
134
  threads.each do |thread|
135
135
  thread.join
136
136
  end
137
137
  end
138
-
138
+
139
139
  it "should delete the temporary database." do
140
140
  File.unlink($db_path) if File.exists?($db_path)
141
141
  end
142
-
142
+
143
143
  #Moved from "knjrbfw_spec.rb"
144
144
  it "should be able to generate a sample SQLite database and add a sample table, with sample columns and with a sample index to it" do
145
145
  $db_path = "#{Knj::Os.tmpdir}/knjrbfw_test_sqlite3.sqlite3"
@@ -148,7 +148,7 @@ describe "Objects" do
148
148
  :path => $db_path,
149
149
  :index_append_table_name => true
150
150
  )
151
-
151
+
152
152
  $db.tables.create("Project", {
153
153
  :columns => [
154
154
  {:name => :id, :type => :int, :autoincr => true, :primarykey => true},
@@ -159,7 +159,7 @@ describe "Objects" do
159
159
  {:name => :category_id, :columns => [:category_id]}
160
160
  ]
161
161
  })
162
-
162
+
163
163
  $db.tables.create("Task", {
164
164
  :columns => [
165
165
  {:name => :id, :type => :int, :autoincr => true, :primarykey => true},
@@ -171,14 +171,14 @@ describe "Objects" do
171
171
  {:name => :project_id, :columns => [:project_id]}
172
172
  ]
173
173
  })
174
-
174
+
175
175
  $db.tables.create("Person", {
176
176
  :columns => [
177
177
  {:name => :id, :type => :int, :autoincr => true, :primarykey => true},
178
178
  {:name => :name, :type => :varchar}
179
179
  ]
180
180
  })
181
-
181
+
182
182
  $db.tables.create("Timelog", {
183
183
  :columns => [
184
184
  {:name => :id, :type => :int, :autoincr => true, :primarykey => true},
@@ -188,60 +188,60 @@ describe "Objects" do
188
188
  :person_id
189
189
  ]
190
190
  })
191
-
191
+
192
192
  table = $db.tables[:Project]
193
-
193
+
194
194
  indexes = table.indexes
195
195
  raise "Could not find the sample-index 'category_id' that should have been created." if !indexes[:Project__category_id]
196
-
197
-
196
+
197
+
198
198
  #If we insert a row the ID should increase and the name should be the same as inserted (or something is very very wrong)...
199
199
  $db.insert("Project", {
200
200
  "name" => "Test project"
201
201
  })
202
-
202
+
203
203
  count = 0
204
204
  $db.q("SELECT * FROM Project") do |d|
205
205
  raise "Somehow name was not 'Test project'" if d[:name] != "Test project"
206
206
  raise "ID was not set?" if d[:id].to_i <= 0
207
207
  count += 1
208
208
  end
209
-
209
+
210
210
  raise "Expected count of 1 but it wasnt: #{count}" if count != 1
211
211
  end
212
-
212
+
213
213
  it "should be able to automatic generate methods on datarow-classes (has_many, has_one)." do
214
214
  class Project < Baza::Model
215
215
  has_many [
216
216
  {:class => :Task, :col => :project_id, :depends => true}
217
217
  ]
218
218
  end
219
-
219
+
220
220
  class Task < Baza::Model
221
221
  has_one [
222
222
  {:class => :Person, :required => true},
223
223
  :Project
224
224
  ]
225
225
  end
226
-
226
+
227
227
  class Person < Baza::Model
228
228
  has_one [:Project]
229
-
229
+
230
230
  has_many [
231
231
  {:class => :Timelog, :autozero => true}
232
232
  ]
233
-
233
+
234
234
  def html
235
235
  return self[:name]
236
236
  end
237
237
  end
238
-
238
+
239
239
  class Timelog < Baza::Model
240
-
240
+
241
241
  end
242
-
242
+
243
243
  $ob = Baza::ModelHandler.new(:db => $db, :datarow => true, :require => false)
244
-
244
+
245
245
  $ob.add(:Person, {
246
246
  :name => "Kasper"
247
247
  })
@@ -250,58 +250,58 @@ describe "Objects" do
250
250
  :person_id => 1,
251
251
  :project_id => 1
252
252
  })
253
-
253
+
254
254
  begin
255
255
  $obb.add(:Task, {:name => "Test task"})
256
256
  raise "Method should fail but didnt."
257
257
  rescue
258
258
  #ignore.
259
259
  end
260
-
261
-
260
+
261
+
262
262
  #Test 'list_invalid_required'.
263
263
  $db.insert(:Task, :name => "Invalid require")
264
264
  id = $db.last_id
265
265
  found = false
266
-
266
+
267
267
  $ob.list_invalid_required(:class => :Task) do |d|
268
268
  raise "Expected object ID to be #{id} but it wasnt: #{d[:obj].id}" if d[:obj].id.to_i != id.to_i
269
269
  $ob.delete(d[:obj])
270
270
  found = true
271
271
  end
272
-
272
+
273
273
  raise "Expected to find a task but didnt." if !found
274
-
275
-
274
+
275
+
276
276
  ret_proc = []
277
277
  $ob.list(:Task) do |task|
278
278
  ret_proc << task
279
279
  end
280
-
280
+
281
281
  raise "list with proc should return one task but didnt." if ret_proc.length != 1
282
-
283
-
282
+
283
+
284
284
  project = $ob.get(:Project, 1)
285
-
285
+
286
286
  tasks = project.tasks
287
287
  raise "No tasks were found on project?" if tasks.empty?
288
-
289
-
288
+
289
+
290
290
  ret_proc = []
291
291
  ret_test = project.tasks do |task|
292
292
  ret_proc << task
293
293
  end
294
-
294
+
295
295
  raise "When given a block the return should be nil so it doesnt hold weak-ref-objects in memory but it didnt return nil." if ret_test != nil
296
296
  raise "list for project with proc should return one task but didnt (#{ret_proc.length})." if ret_proc.length != 1
297
-
297
+
298
298
  person = tasks.first.person
299
299
  project_second = tasks.first.project
300
-
300
+
301
301
  raise "Returned object was not a person on task." if !person.is_a?(Person)
302
302
  raise "Returned object was not a project on task." if !project_second.is_a?(Project)
303
-
304
-
303
+
304
+
305
305
  #Check that has_many-depending is actually working.
306
306
  begin
307
307
  $ob.delete(project)
@@ -310,7 +310,7 @@ describe "Objects" do
310
310
  #this should happen - it should not possible to delete project 1 because task 1 depends on it."
311
311
  end
312
312
  end
313
-
313
+
314
314
  it "should be able to generate lists for inputs" do
315
315
  Knj::Web.inputs([{
316
316
  :title => "Test 3",
@@ -320,43 +320,43 @@ describe "Objects" do
320
320
  :opts => $ob.list_optshash(:Task)
321
321
  }])
322
322
  end
323
-
323
+
324
324
  it "should be able to connect to objects 'no-html' callback and test it." do
325
325
  task = $ob.get(:Task, 1)
326
326
  $ob.events.connect(:no_html) do |event, classname|
327
327
  "[no #{classname.to_s.downcase}]"
328
328
  end
329
-
329
+
330
330
  raise "Unexpected person_html from task (should have been 'Kasper'): '#{task.person_html}'." if task.person_html != "Kasper"
331
331
  task.update(:person_id => 0)
332
332
  raise "Unexpected person_html from task (should have been '[no person]')." if task.person_html != "[no person]"
333
333
  end
334
-
334
+
335
335
  it "should be able to to multiple additions and delete objects through a buffer" do
336
336
  objs = []
337
337
  0.upto(500) do
338
338
  objs << {:name => :Kasper}
339
339
  end
340
-
340
+
341
341
  $ob.adds(:Person, objs)
342
342
  pers_length = $ob.list(:Person, "count" => true)
343
-
343
+
344
344
  count = 0
345
345
  $db.q_buffer do |buffer|
346
346
  $ob.list(:Person) do |person|
347
347
  count += 1
348
348
  $ob.delete(person, :db_buffer => buffer)
349
349
  end
350
-
350
+
351
351
  buffer.flush
352
352
  end
353
-
353
+
354
354
  raise "Expected count to be #{pers_length} but it wasnt: #{count}" if count != pers_length
355
-
355
+
356
356
  persons = $ob.list(:Person).to_a
357
357
  raise "Expected persons count to be 0 but it wasnt: #{persons.map{|e| e.data} }" if persons.length > 0
358
358
  end
359
-
359
+
360
360
  it "should do autozero when deleting objects" do
361
361
  person1 = $ob.add(:Person, {
362
362
  :name => "Kasper"
@@ -364,49 +364,49 @@ describe "Objects" do
364
364
  person2 = $ob.add(:Person, {
365
365
  :name => "Charlotte"
366
366
  })
367
-
367
+
368
368
  timelog1 = $ob.add(:Timelog, {
369
369
  :person_id => person1.id
370
370
  })
371
371
  timelog2 = $ob.add(:Timelog, {
372
372
  :person_id => person2.id
373
373
  })
374
-
374
+
375
375
  $ob.delete(person1)
376
-
376
+
377
377
  raise "Expected timelog1's person-ID to be zero but it wasnt: '#{timelog1[:person_id]}'." if timelog1[:person_id].to_i != 0
378
378
  raise "Expected timelog2's person-ID to be #{person2.id} but it wasnt: '#{timelog2[:person_id]}'." if timelog2[:person_id].to_i != person2.id.to_i
379
379
  end
380
-
380
+
381
381
  it "should be able to do multiple deletes from ids" do
382
382
  ids = []
383
383
  1.upto(10) do |count|
384
384
  ids << $ob.add(:Person).id
385
385
  end
386
-
386
+
387
387
  $ob.delete_ids(:class => :Person, :ids => ids)
388
388
  end
389
-
389
+
390
390
  it "get_or_add" do
391
391
  person1 = $ob.add(:Person, {
392
392
  :name => "get_or_add"
393
393
  })
394
-
394
+
395
395
  person2 = $ob.get_or_add(:Person, {
396
396
  :name => "get_or_add"
397
397
  })
398
-
398
+
399
399
  person2.id.should eql(person1.id)
400
400
  person2[:name].should eql("get_or_add")
401
-
401
+
402
402
  person3 = $ob.get_or_add(:Person, {
403
403
  :name => "get_or_add3"
404
404
  })
405
-
405
+
406
406
  raise "Failure ID was the same" if person3.id == person2.id
407
407
  person3[:name].should eql("get_or_add3")
408
408
  end
409
-
409
+
410
410
  it "should delete the temp database again." do
411
411
  db_path = "#{Knj::Os.tmpdir}/knjrbfw_test_sqlite3.sqlite3"
412
412
  File.unlink(db_path) if File.exists?(db_path)
data/spec/spec_helper.rb CHANGED
@@ -1,12 +1,12 @@
1
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
- $LOAD_PATH.unshift(File.dirname(__FILE__))
3
- require 'rspec'
1
+ require "codeclimate-test-reporter"
2
+ CodeClimate::TestReporter.start
3
+
4
4
  require 'baza'
5
5
 
6
- # Requires supporting files with custom matchers and macros, etc,
7
- # in ./support/ and its subdirectories.
8
6
  Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
7
 
10
8
  RSpec.configure do |config|
11
-
9
+ config.expect_with :rspec do |c|
10
+ c.syntax = [:should, :expect]
11
+ end
12
12
  end