baza 0.0.17 → 0.0.18

Sign up to get free protection for your applications and to get access to all the features.
Files changed (116) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +9 -11
  3. data/Gemfile.lock +76 -50
  4. data/README.md +3 -2
  5. data/VERSION +1 -1
  6. data/baza.gemspec +107 -54
  7. data/lib/baza.rb +1 -1
  8. data/lib/baza/base_sql_driver.rb +64 -0
  9. data/lib/baza/cloner.rb +31 -0
  10. data/{include → lib/baza}/column.rb +0 -0
  11. data/{include → lib/baza}/db.rb +61 -49
  12. data/{include → lib/baza}/dbtime.rb +0 -0
  13. data/{include → lib/baza}/driver.rb +1 -1
  14. data/lib/baza/drivers/active_record.rb +107 -0
  15. data/lib/baza/drivers/active_record/columns.rb +10 -0
  16. data/lib/baza/drivers/active_record/indexes.rb +10 -0
  17. data/lib/baza/drivers/active_record/result.rb +23 -0
  18. data/lib/baza/drivers/active_record/tables.rb +12 -0
  19. data/lib/baza/drivers/mysql.rb +220 -0
  20. data/{include/drivers/mysql/mysql_column.rb → lib/baza/drivers/mysql/column.rb} +0 -0
  21. data/{include/drivers/mysql/mysql_columns.rb → lib/baza/drivers/mysql/columns.rb} +0 -0
  22. data/{include/drivers/mysql/mysql_index.rb → lib/baza/drivers/mysql/index.rb} +0 -0
  23. data/{include/drivers/mysql/mysql_indexes.rb → lib/baza/drivers/mysql/indexes.rb} +0 -0
  24. data/lib/baza/drivers/mysql/result.rb +81 -0
  25. data/{include/drivers/mysql/mysql_sqlspecs.rb → lib/baza/drivers/mysql/sqlspecs.rb} +0 -0
  26. data/{include/drivers/mysql/mysql_table.rb → lib/baza/drivers/mysql/table.rb} +2 -2
  27. data/{include/drivers/mysql/mysql_tables.rb → lib/baza/drivers/mysql/tables.rb} +0 -1
  28. data/{include/drivers/mysql/mysql_result_unbuffered.rb → lib/baza/drivers/mysql/unbuffered_result.rb} +19 -23
  29. data/lib/baza/drivers/mysql2.rb +259 -0
  30. data/lib/baza/drivers/mysql2/column.rb +2 -0
  31. data/lib/baza/drivers/mysql2/columns.rb +2 -0
  32. data/lib/baza/drivers/mysql2/index.rb +2 -0
  33. data/lib/baza/drivers/mysql2/indexes.rb +2 -0
  34. data/lib/baza/drivers/mysql2/result.rb +26 -0
  35. data/lib/baza/drivers/mysql2/table.rb +2 -0
  36. data/lib/baza/drivers/mysql2/tables.rb +2 -0
  37. data/lib/baza/drivers/mysql_java.rb +178 -0
  38. data/lib/baza/drivers/mysql_java/column.rb +2 -0
  39. data/lib/baza/drivers/mysql_java/columns.rb +2 -0
  40. data/lib/baza/drivers/mysql_java/index.rb +2 -0
  41. data/lib/baza/drivers/mysql_java/indexes.rb +2 -0
  42. data/lib/baza/drivers/mysql_java/table.rb +2 -0
  43. data/lib/baza/drivers/mysql_java/tables.rb +2 -0
  44. data/lib/baza/drivers/sqlite3.rb +81 -0
  45. data/{include/drivers/sqlite3/sqlite3_column.rb → lib/baza/drivers/sqlite3/column.rb} +0 -0
  46. data/{include/drivers/sqlite3/sqlite3_columns.rb → lib/baza/drivers/sqlite3/columns.rb} +0 -0
  47. data/{include/drivers/sqlite3/sqlite3_index.rb → lib/baza/drivers/sqlite3/index.rb} +0 -0
  48. data/{include/drivers/sqlite3/sqlite3_indexes.rb → lib/baza/drivers/sqlite3/indexes.rb} +0 -0
  49. data/lib/baza/drivers/sqlite3/result.rb +64 -0
  50. data/{include/drivers/sqlite3/sqlite3_sqlspecs.rb → lib/baza/drivers/sqlite3/sqlspecs.rb} +1 -1
  51. data/{include/drivers/sqlite3/sqlite3_table.rb → lib/baza/drivers/sqlite3/table.rb} +0 -0
  52. data/{include/drivers/sqlite3/sqlite3_tables.rb → lib/baza/drivers/sqlite3/tables.rb} +0 -0
  53. data/lib/baza/drivers/sqlite3/unbuffered_result.rb +33 -0
  54. data/lib/baza/drivers/sqlite3_java.rb +75 -0
  55. data/lib/baza/drivers/sqlite3_java/column.rb +2 -0
  56. data/lib/baza/drivers/sqlite3_java/columns.rb +2 -0
  57. data/lib/baza/drivers/sqlite3_java/index.rb +2 -0
  58. data/lib/baza/drivers/sqlite3_java/indexes.rb +2 -0
  59. data/lib/baza/drivers/sqlite3_java/table.rb +2 -0
  60. data/lib/baza/drivers/sqlite3_java/tables.rb +2 -0
  61. data/lib/baza/drivers/sqlite3_java/unbuffered_result.rb +33 -0
  62. data/lib/baza/drivers/sqlite3_rhodes.rb +78 -0
  63. data/{include → lib/baza}/dump.rb +0 -0
  64. data/{include → lib/baza}/errors.rb +0 -0
  65. data/{include → lib/baza}/idquery.rb +0 -0
  66. data/{include → lib/baza}/index.rb +0 -0
  67. data/lib/baza/jdbc_driver.rb +102 -0
  68. data/lib/baza/jdbc_result.rb +125 -0
  69. data/{include → lib/baza}/model.rb +2 -2
  70. data/{include → lib/baza}/model_custom.rb +0 -0
  71. data/{include → lib/baza}/model_handler.rb +1 -1
  72. data/{include → lib/baza}/model_handler_sqlhelper.rb +0 -0
  73. data/{include → lib/baza}/query_buffer.rb +2 -2
  74. data/lib/baza/result_base.rb +25 -0
  75. data/{include → lib/baza}/revision.rb +1 -1
  76. data/{include → lib/baza}/row.rb +0 -0
  77. data/{include → lib/baza}/sqlspecs.rb +0 -0
  78. data/{include → lib/baza}/table.rb +0 -0
  79. data/shippable.yml +3 -1
  80. data/spec/cloner_spec.rb +10 -0
  81. data/spec/drivers/active_record_mysql2_spec.rb +18 -0
  82. data/spec/drivers/active_record_mysql_spec.rb +16 -0
  83. data/spec/drivers/active_record_sqlite3_spec.rb +16 -0
  84. data/spec/drivers/mysql2_spec.rb +16 -0
  85. data/spec/{include/drivers → drivers}/mysql_spec.rb +9 -1
  86. data/spec/{include/drivers → drivers}/sqlite3_spec.rb +8 -0
  87. data/spec/info_active_record_mysql.rb +37 -0
  88. data/spec/info_active_record_mysql2.rb +37 -0
  89. data/spec/info_active_record_mysql2_shippable.rb +36 -0
  90. data/spec/info_active_record_mysql_shippable.rb +36 -0
  91. data/spec/info_active_record_sqlite3.rb +37 -0
  92. data/spec/info_mysql2_example.rb +23 -0
  93. data/spec/info_mysql2_shippable.rb +22 -0
  94. data/spec/info_mysql_example.rb +3 -4
  95. data/spec/info_mysql_shippable.rb +7 -17
  96. data/spec/info_sqlite3.rb +5 -4
  97. data/spec/model_handler_spec.rb +137 -105
  98. data/spec/spec_helper.rb +8 -0
  99. data/spec/support/driver_collection.rb +124 -34
  100. data/spec/support/driver_columns_collection.rb +0 -7
  101. data/spec/support/driver_indexes_collection.rb +4 -11
  102. data/spec/support/driver_tables_collection.rb +0 -7
  103. metadata +160 -52
  104. data/include/cloner.rb +0 -18
  105. data/include/drivers/active_record/active_record.rb +0 -159
  106. data/include/drivers/mysql/mysql.rb +0 -443
  107. data/include/drivers/mysql/mysql_result.rb +0 -42
  108. data/include/drivers/mysql/mysql_result_java.rb +0 -61
  109. data/include/drivers/mysql/mysql_result_mysql2.rb +0 -26
  110. data/include/drivers/sqlite3/sqlite3.rb +0 -159
  111. data/include/drivers/sqlite3/sqlite3_result.rb +0 -35
  112. data/include/drivers/sqlite3/sqlite3_result_java.rb +0 -39
  113. data/spec/include/cloner_spec.rb +0 -10
  114. data/spec/include/drivers/active_record_spec.rb +0 -8
  115. data/spec/info_active_record.rb +0 -49
  116. data/spec/info_active_record_shippable.rb +0 -47
@@ -0,0 +1,37 @@
1
+ class Baza::InfoActiveRecordMysql2
2
+ attr_reader :db
3
+
4
+ def self.connection
5
+ require "active_record"
6
+
7
+ @conn_pool ||= ::ActiveRecord::Base.establish_connection(
8
+ adapter: "mysql2",
9
+ host: "localhost",
10
+ database: "baza-test",
11
+ username: "baza-test",
12
+ password: "BBH7djRUKzL5nmG3"
13
+ )
14
+ @conn ||= @conn_pool.connection
15
+
16
+ return {pool: @conn_pool, conn: @conn}
17
+ end
18
+
19
+ def initialize(args = {})
20
+ data = Baza::InfoActiveRecordMysql2.connection
21
+
22
+ @db = Baza::Db.new({
23
+ type: :active_record,
24
+ conn: data[:conn]
25
+ }.merge(args))
26
+ end
27
+
28
+ def before
29
+ @db.tables.list.each do |name, table|
30
+ table.drop
31
+ end
32
+ end
33
+
34
+ def after
35
+ @db.close
36
+ end
37
+ end
@@ -0,0 +1,36 @@
1
+ class Baza::InfoActiveRecordMysql2
2
+ attr_reader :db
3
+
4
+ def self.connection
5
+ require "active_record"
6
+
7
+ conn_pool ||= ::ActiveRecord::Base.establish_connection(
8
+ adapter: "mysql2",
9
+ host: "localhost",
10
+ database: "baza",
11
+ username: "shippa"
12
+ )
13
+ conn = conn_pool.connection
14
+
15
+ return {pool: conn_pool, conn: conn}
16
+ end
17
+
18
+ def initialize
19
+ data = Baza::InfoActiveRecord.connection
20
+
21
+ @db = Baza::Db.new(
22
+ type: :active_record,
23
+ conn: data[:conn]
24
+ )
25
+ end
26
+
27
+ def before
28
+ @db.tables.list.each do |name, table|
29
+ table.drop
30
+ end
31
+ end
32
+
33
+ def after
34
+ @db.close
35
+ end
36
+ end
@@ -0,0 +1,36 @@
1
+ class Baza::InfoActiveRecordMysql
2
+ attr_reader :db
3
+
4
+ def self.connection
5
+ require "active_record"
6
+
7
+ conn_pool ||= ::ActiveRecord::Base.establish_connection(
8
+ adapter: "mysql",
9
+ host: "localhost",
10
+ database: "baza",
11
+ username: "shippa"
12
+ )
13
+ conn = conn_pool.connection
14
+
15
+ return {pool: conn_pool, conn: conn}
16
+ end
17
+
18
+ def initialize
19
+ data = Baza::InfoActiveRecord.connection
20
+
21
+ @db = Baza::Db.new(
22
+ type: :active_record,
23
+ conn: data[:conn]
24
+ )
25
+ end
26
+
27
+ def before
28
+ @db.tables.list.each do |name, table|
29
+ table.drop
30
+ end
31
+ end
32
+
33
+ def after
34
+ @db.close
35
+ end
36
+ end
@@ -0,0 +1,37 @@
1
+ class Baza::InfoActiveRecordSqlite3
2
+ attr_reader :db
3
+
4
+ def self.connection
5
+ require "active_record"
6
+
7
+ path = "#{Dir.tmpdir}/baza_sqlite3_test_#{Time.now.to_f.to_s.hash}_#{Random.rand}.sqlite3"
8
+ File.unlink(path) if File.exists?(path)
9
+
10
+ @conn_pool ||= ::ActiveRecord::Base.establish_connection(
11
+ adapter: "sqlite3",
12
+ database: path
13
+ )
14
+ @conn ||= @conn_pool.connection
15
+
16
+ return {pool: @conn_pool, conn: @conn}
17
+ end
18
+
19
+ def initialize(args = {})
20
+ data = Baza::InfoActiveRecordSqlite3.connection
21
+
22
+ @db = Baza::Db.new({
23
+ type: :active_record,
24
+ conn: data[:conn]
25
+ }.merge(args))
26
+ end
27
+
28
+ def before
29
+ @db.tables.list.each do |name, table|
30
+ table.drop
31
+ end
32
+ end
33
+
34
+ def after
35
+ @db.close
36
+ end
37
+ end
@@ -0,0 +1,23 @@
1
+ class Baza::InfoMysql
2
+ attr_reader :db
3
+
4
+ def initialize(args = {})
5
+ @db = Baza::Db.new({
6
+ type: :mysql2,
7
+ host: "localhost",
8
+ user: "baza-test",
9
+ pass: "password",
10
+ db: "baza-test"
11
+ }.merge(args))
12
+ end
13
+
14
+ def before
15
+ @db.tables.list.each do |name, table|
16
+ table.drop
17
+ end
18
+ end
19
+
20
+ def after
21
+ @db.close
22
+ end
23
+ end
@@ -0,0 +1,22 @@
1
+ class Baza::InfoMysql2
2
+ attr_reader :db
3
+
4
+ def initialize(args = {})
5
+ @db = Baza::Db.new({
6
+ type: :mysql2,
7
+ host: "localhost",
8
+ user: "shippa",
9
+ db: "baza"
10
+ }.merge(args))
11
+ end
12
+
13
+ def before
14
+ @db.tables.list.each do |name, table|
15
+ table.drop
16
+ end
17
+ end
18
+
19
+ def after
20
+ @db.close
21
+ end
22
+ end
@@ -1,15 +1,14 @@
1
1
  class Baza::InfoMysql
2
2
  attr_reader :db
3
3
 
4
- def initialize
5
- @db = Baza::Db.new(
4
+ def initialize(args = {})
5
+ @db = Baza::Db.new({
6
6
  type: :mysql,
7
- subtype: :mysql2,
8
7
  host: "localhost",
9
8
  user: "baza-test",
10
9
  pass: "password",
11
10
  db: "baza-test"
12
- )
11
+ }.merge(args))
13
12
  end
14
13
 
15
14
  def before
@@ -1,23 +1,13 @@
1
1
  class Baza::InfoMysql
2
2
  attr_reader :db
3
3
 
4
- def initialize
5
- if RUBY_ENGINE == "jruby"
6
- @db = Baza::Db.new(
7
- type: :mysql,
8
- host: "localhost",
9
- user: "shippa",
10
- db: "baza"
11
- )
12
- else
13
- @db = Baza::Db.new(
14
- type: :mysql,
15
- subtype: :mysql2,
16
- host: "localhost",
17
- user: "shippa",
18
- db: "baza"
19
- )
20
- end
4
+ def initialize(args = {})
5
+ @db = Baza::Db.new({
6
+ type: :mysql,
7
+ host: "localhost",
8
+ user: "shippa",
9
+ db: "baza"
10
+ }.merge(args))
21
11
  end
22
12
 
23
13
  def before
@@ -1,18 +1,19 @@
1
1
  class Baza::InfoSqlite3
2
2
  attr_reader :db
3
3
 
4
- def initialize
4
+ def initialize(args = {})
5
5
  require "sqlite3" unless RUBY_ENGINE == "jruby"
6
6
  require "tmpdir"
7
7
 
8
- @path = "#{Dir.tmpdir}/baza_sqlite3_test_#{Time.now.to_f.to_s.hash}.sqlite3"
8
+ @path = "#{Dir.tmpdir}/baza_sqlite3_test_#{Time.now.to_f.to_s.hash}_#{Random.rand}.sqlite3"
9
9
  File.unlink(path) if File.exists?(@path)
10
- @db = Baza::Db.new(
10
+
11
+ @db = Baza::Db.new({
11
12
  type: :sqlite3,
12
13
  path: @path,
13
14
  index_append_table_name: true,
14
15
  sql_to_error: true
15
- )
16
+ }.merge(args))
16
17
  end
17
18
 
18
19
  def before
@@ -1,63 +1,100 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe "Objects" do
4
- before(:all) do
4
+ let(:db_path) { "#{Dir.tmpdir}/baza_model_handler_test_#{Time.now.to_f}_#{Random.rand}.sqlite3" }
5
+ let(:db) do
5
6
  require "sqlite3" unless RUBY_ENGINE == "jruby"
6
7
  require "tmpdir"
7
8
 
8
- $db_path = "#{Dir.tmpdir}/knjrbfw_objects_cache_test.sqlite3"
9
- File.unlink($db_path) if File.exists?($db_path)
10
- $db = Baza::Db.new(:type => :sqlite3, :path => $db_path, :return_keys => "symbols", :debug => false)
9
+ File.unlink(db_path) if File.exists?(db_path)
10
+ db = Baza::Db.new(type: :sqlite3, path: db_path, debug: false)
11
11
 
12
12
  schema = {
13
- :tables => {
13
+ tables: {
14
14
  "Group" => {
15
- :columns => [
16
- {:name => :id, :type => :int, :autoincr => true, :primarykey => true},
17
- {:name => :groupname, :type => :varchar}
15
+ columns: [
16
+ {name: :id, type: :int, autoincr: true, primarykey: true},
17
+ {name: :groupname, type: :varchar}
18
+ ]
19
+ },
20
+ "Person" => {
21
+ columns: [
22
+ {name: :id, type: :int, autoincr: true, primarykey: true},
23
+ {name: :name, type: :varchar}
24
+ ]
25
+ },
26
+ "Project" => {
27
+ columns: [
28
+ {name: :id, type: :int, autoincr: true, primarykey: true}
29
+ ]
30
+ },
31
+ "Task" => {
32
+ columns: [
33
+ {name: :id, type: :int, autoincr: true, primarykey: true},
34
+ {name: :name, type: :varchar},
35
+ {name: :person_id, type: :int},
36
+ {name: :project_id, type: :int}
37
+ ]
38
+ },
39
+ "Timelog" => {
40
+ columns: [
41
+ {name: :id, type: :int, autoincr: true, primarykey: true},
42
+ {name: :person_id, type: :int}
18
43
  ]
19
44
  },
20
45
  "User" => {
21
- :columns => [
22
- {:name => :id, :type => :int, :autoincr => true, :primarykey => true},
23
- {:name => :username, :type => :varchar}
46
+ columns: [
47
+ {name: :id, type: :int, autoincr: true, primarykey: true},
48
+ {name: :username, type: :varchar}
24
49
  ]
25
50
  }
26
51
  }
27
52
  }
28
- Baza::Revision.new.init_db(:schema => schema, :db => $db)
53
+ Baza::Revision.new.init_db(schema: schema, db: db)
29
54
 
30
- class User < Baza::Model; end
55
+ db
56
+ end
31
57
 
32
- $ob = Baza::ModelHandler.new(
33
- :db => $db,
34
- :datarow => true,
35
- :require => false,
36
- :array_enum => true,
37
- :models => {
38
- :User => {
39
- :cache_ids => true
58
+ let(:ob) do
59
+ ob = Baza::ModelHandler.new(
60
+ db: db,
61
+ datarow: true,
62
+ require: false,
63
+ array_enum: true,
64
+ models: {
65
+ User: {
66
+ cache_ids: true
40
67
  }
41
68
  }
42
69
  )
43
70
 
44
- $ob.adds(:User, [
45
- {:username => "User 1"},
46
- {:username => "User 2"},
47
- {:username => "User 3"},
48
- {:username => "User 4"},
49
- {:username => "User 5"}
71
+ ob.adds(:User, [
72
+ {username: "User 1"},
73
+ {username: "User 2"},
74
+ {username: "User 3"},
75
+ {username: "User 4"},
76
+ {username: "User 5"}
50
77
  ])
51
78
 
79
+ ob
80
+ end
81
+
82
+ let(:task) { ob.add(:Task, name: 'Test task', person_id: person.id) }
83
+ let(:person) { ob.add(:Person, name: 'Kasper') }
84
+ let(:project) { ob.add(:Project) }
85
+
86
+ before(:all) do
87
+ class User < Baza::Model; end
88
+
52
89
  class Project < Baza::Model
53
90
  has_many [
54
- {:class => :Task, :col => :project_id, :depends => true}
91
+ {class: :Task, col: :project_id, depends: true}
55
92
  ]
56
93
  end
57
94
 
58
95
  class Task < Baza::Model
59
96
  has_one [
60
- {:class => :Person, :required => true},
97
+ {class: :Person, required: true},
61
98
  :Project
62
99
  ]
63
100
  end
@@ -66,7 +103,7 @@ describe "Objects" do
66
103
  has_one [:Project]
67
104
 
68
105
  has_many [
69
- {:class => :Timelog, :autozero => true}
106
+ {class: :Timelog, autozero: true}
70
107
  ]
71
108
 
72
109
  def html
@@ -74,75 +111,71 @@ describe "Objects" do
74
111
  end
75
112
  end
76
113
 
77
- class Timelog < Baza::Model
78
- end
114
+ class Timelog < Baza::Model; end
79
115
  end
80
116
 
81
117
  it "should be able to cache rows" do
82
- raise "Expected user-ID-cache to be 5 but it wasnt: #{$ob.ids_cache[:User].length}" if $ob.ids_cache[:User].length != 5
118
+ expect(ob.ids_cache[:User].length).to eq 5
83
119
 
84
- user = $ob.get(:User, 4)
120
+ user = ob.get(:User, 4)
85
121
  raise "No user returned." if !user
86
- $ob.delete(user)
87
- 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
122
+ ob.delete(user)
123
+
124
+ expect(ob.ids_cache[:User].length).to eq 4
88
125
 
89
- $ob.deletes([$ob.get(:User, 1), $ob.get(:User, 2)])
90
- 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
126
+ ob.deletes([ob.get(:User, 1), ob.get(:User, 2)])
127
+ expect(ob.ids_cache[:User].length).to eq 2
91
128
  end
92
129
 
93
130
  it "should be able to do 'select_col_as_array'" do
94
- res = $ob.list(:User, {"select_col_as_array" => "id"}).to_a
95
- raise "Expected length of 2 but got: #{res.length}" if res.length != 2
131
+ res = ob.list(:User, "select_col_as_array" => "id").to_a
132
+ expect(res.length).to eq 5
96
133
  end
97
134
 
98
135
  it "should work even though stressed by threads (thread-safe)." do
99
136
  userd = []
100
137
  10.upto(25) do |i|
101
- userd << {:username => "User #{i}"}
138
+ userd << {username: "User #{i}"}
102
139
  end
103
140
 
104
- $ob.adds(:User, userd)
105
- users = $ob.list(:User).to_a
141
+ ob.adds(:User, userd)
142
+ users = ob.list(:User).to_a
106
143
 
107
144
  #Stress it to test threadsafety...
108
145
  threads = []
109
146
  0.upto(5) do |tc|
110
147
  threads << Thread.new do
111
- Thread.current.abort_on_exception = true
112
-
113
148
  0.upto(5) do |ic|
114
- user = $ob.add(:User, {:username => "User #{tc}-#{ic}"})
149
+ user = ob.add(:User, {:username => "User #{tc}-#{ic}"})
115
150
  raise "No user returned." if !user
116
- $ob.delete(user)
151
+ ob.delete(user)
117
152
 
118
- user1 = $ob.add(:User, {:username => "User #{tc}-#{ic}-1"})
119
- user2 = $ob.add(:User, {:username => "User #{tc}-#{ic}-2"})
120
- user3 = $ob.add(:User, {:username => "User #{tc}-#{ic}-3"})
153
+ user1 = ob.add(:User, {:username => "User #{tc}-#{ic}-1"})
154
+ user2 = ob.add(:User, {:username => "User #{tc}-#{ic}-2"})
155
+ user3 = ob.add(:User, {:username => "User #{tc}-#{ic}-3"})
121
156
 
122
157
  raise "Missing user?" if !user1 or !user2 or !user3 or user1.deleted? or user2.deleted? or user3.deleted?
123
- $ob.deletes([user1, user2, user3])
158
+ ob.deletes([user1, user2, user3])
124
159
 
125
160
  count = 0
126
161
  users.each do |user|
127
162
  count += 1
128
- user[:username] = "#{user[:username]}." if !user.deleted?
163
+ user[:username] = "#{user[:username]}." unless user.deleted?
129
164
  end
130
165
 
131
- raise "Expected at least 15 users but got #{count}." if count != 18
166
+ expect(count).to eq 21
132
167
  end
133
168
  end
134
169
  end
135
170
 
136
- threads.each do |thread|
137
- thread.join
138
- end
171
+ threads.each(&:join)
139
172
  end
140
173
 
141
174
  it "should be able to skip queries when adding" do
142
175
  class Group < Baza::Model; end
143
176
 
144
- $ob2 = Baza::ModelHandler.new(
145
- :db => $db,
177
+ ob2 = Baza::ModelHandler.new(
178
+ :db => db,
146
179
  :datarow => true,
147
180
  :require => false
148
181
  )
@@ -153,7 +186,7 @@ describe "Objects" do
153
186
  Thread.current.abort_on_exception = true
154
187
 
155
188
  0.upto(5) do
156
- ret = $ob2.add(:Group, {:groupname => "User 1"}, {:skip_ret => true})
189
+ ret = ob2.add(:Group, {groupname: "User 1"}, {skip_ret: true})
157
190
  raise "Expected empty return but got something: #{ret}" if ret
158
191
  end
159
192
  end
@@ -165,21 +198,21 @@ describe "Objects" do
165
198
  end
166
199
 
167
200
  it "should delete the temporary database." do
168
- File.unlink($db_path) if File.exists?($db_path)
201
+ File.unlink(db_path) if File.exists?(db_path)
169
202
  end
170
203
 
171
204
  #Moved from "knjrbfw_spec.rb"
172
205
  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
173
206
  require 'tmpdir'
174
207
 
175
- $db_path = "#{Dir.tmpdir}/knjrbfw_test_sqlite3.sqlite3"
176
- $db = Baza::Db.new(
208
+ db_path = "#{Dir.tmpdir}/knjrbfw_test_sqlite3.sqlite3"
209
+ db = Baza::Db.new(
177
210
  :type => :sqlite3,
178
- :path => $db_path,
211
+ :path => db_path,
179
212
  :index_append_table_name => true
180
213
  )
181
214
 
182
- $db.tables.create("Project", {
215
+ db.tables.create("Project", {
183
216
  :columns => [
184
217
  {:name => :id, :type => :int, :autoincr => true, :primarykey => true},
185
218
  {:name => :category_id, :type => :int},
@@ -190,7 +223,7 @@ describe "Objects" do
190
223
  ]
191
224
  })
192
225
 
193
- $db.tables.create("Task", {
226
+ db.tables.create("Task", {
194
227
  :columns => [
195
228
  {:name => :id, :type => :int, :autoincr => true, :primarykey => true},
196
229
  {:name => :project_id, :type => :int},
@@ -202,14 +235,14 @@ describe "Objects" do
202
235
  ]
203
236
  })
204
237
 
205
- $db.tables.create("Person", {
238
+ db.tables.create("Person", {
206
239
  :columns => [
207
240
  {:name => :id, :type => :int, :autoincr => true, :primarykey => true},
208
241
  {:name => :name, :type => :varchar}
209
242
  ]
210
243
  })
211
244
 
212
- $db.tables.create("Timelog", {
245
+ db.tables.create("Timelog", {
213
246
  :columns => [
214
247
  {:name => :id, :type => :int, :autoincr => true, :primarykey => true},
215
248
  {:name => :person_id, :type => :int}
@@ -219,19 +252,19 @@ describe "Objects" do
219
252
  ]
220
253
  })
221
254
 
222
- table = $db.tables[:Project]
255
+ table = db.tables[:Project]
223
256
 
224
257
  indexes = table.indexes
225
258
  raise "Could not find the sample-index 'category_id' that should have been created." unless indexes[:Project__category_id]
226
259
 
227
260
 
228
261
  #If we insert a row the ID should increase and the name should be the same as inserted (or something is very very wrong)...
229
- $db.insert("Project", {
262
+ db.insert("Project", {
230
263
  "name" => "Test project"
231
264
  })
232
265
 
233
266
  count = 0
234
- $db.q("SELECT * FROM Project") do |d|
267
+ db.q("SELECT * FROM Project") do |d|
235
268
  raise "Somehow name was not 'Test project'" if d[:name] != "Test project"
236
269
  raise "ID was not set?" if d[:id].to_i <= 0
237
270
  count += 1
@@ -241,19 +274,19 @@ describe "Objects" do
241
274
  end
242
275
 
243
276
  it "should be able to automatic generate methods on datarow-classes (has_many, has_one)." do
244
- $ob = Baza::ModelHandler.new(:db => $db, :datarow => true, :require => false)
277
+ ob = Baza::ModelHandler.new(:db => db, :datarow => true, :require => false)
245
278
 
246
- $ob.add(:Person, {
279
+ ob.add(:Person, {
247
280
  :name => "Kasper"
248
281
  })
249
- $ob.add(:Task, {
282
+ ob.add(:Task, {
250
283
  :name => "Test task",
251
- :person_id => 1,
252
- :project_id => 1
284
+ :person_id => person.id,
285
+ :project_id => project.id
253
286
  })
254
287
 
255
288
  begin
256
- $obb.add(:Task, {:name => "Test task"})
289
+ obb.add(:Task, {:name => "Test task"})
257
290
  raise "Method should fail but didnt."
258
291
  rescue
259
292
  #ignore.
@@ -261,13 +294,13 @@ describe "Objects" do
261
294
 
262
295
 
263
296
  #Test 'list_invalid_required'.
264
- $db.insert(:Task, :name => "Invalid require")
265
- id = $db.last_id
297
+ db.insert(:Task, :name => "Invalid require")
298
+ id = db.last_id
266
299
  found = false
267
300
 
268
- $ob.list_invalid_required(:class => :Task) do |d|
301
+ ob.list_invalid_required(:class => :Task) do |d|
269
302
  raise "Expected object ID to be #{id} but it wasnt: #{d[:obj].id}" if d[:obj].id.to_i != id.to_i
270
- $ob.delete(d[:obj])
303
+ ob.delete(d[:obj])
271
304
  found = true
272
305
  end
273
306
 
@@ -275,15 +308,13 @@ describe "Objects" do
275
308
 
276
309
 
277
310
  ret_proc = []
278
- $ob.list(:Task) do |task|
311
+ ob.list(:Task) do |task|
279
312
  ret_proc << task
280
313
  end
281
314
 
282
315
  raise "list with proc should return one task but didnt." if ret_proc.length != 1
283
316
 
284
317
 
285
- project = $ob.get(:Project, 1)
286
-
287
318
  tasks = project.tasks
288
319
  raise "No tasks were found on project?" if tasks.empty?
289
320
 
@@ -305,7 +336,7 @@ describe "Objects" do
305
336
 
306
337
  #Check that has_many-depending is actually working.
307
338
  begin
308
- $ob.delete(project)
339
+ ob.delete(project)
309
340
  raise "It was possible to delete project 1 even though task 1 depended on it!"
310
341
  rescue
311
342
  #this should happen - it should not possible to delete project 1 because task 1 depends on it."
@@ -313,20 +344,21 @@ describe "Objects" do
313
344
  end
314
345
 
315
346
  it "should be able to generate lists for inputs" do
316
- list = $ob.list_optshash(:Task)
347
+ task
348
+ list = ob.list_optshash(:Task)
317
349
  list.length.should eq 1
318
350
  list[1].should eq 'Test task'
319
351
  end
320
352
 
321
353
  it "should be able to connect to objects 'no-html' callback and test it." do
322
- task = $ob.get(:Task, 1)
323
- $ob.events.connect(:no_html) do |event, classname|
354
+ task
355
+ ob.events.connect(:no_html) do |event, classname|
324
356
  "[no #{classname.to_s.downcase}]"
325
357
  end
326
358
 
327
- raise "Unexpected person_html from task (should have been 'Kasper'): '#{task.person_html}'." if task.person_html != "Kasper"
359
+ expect(task.person_html).to eq "Kasper"
328
360
  task.update(:person_id => 0)
329
- raise "Unexpected person_html from task (should have been '[no person]')." if task.person_html != "[no person]"
361
+ expect(task.person_html).to eq "[no person]"
330
362
  end
331
363
 
332
364
  it "should be able to to multiple additions and delete objects through a buffer" do
@@ -335,14 +367,14 @@ describe "Objects" do
335
367
  objs << {:name => :Kasper}
336
368
  end
337
369
 
338
- $ob.adds(:Person, objs)
339
- pers_length = $ob.list(:Person, "count" => true)
370
+ ob.adds(:Person, objs)
371
+ pers_length = ob.list(:Person, "count" => true)
340
372
 
341
373
  count = 0
342
- $db.q_buffer do |buffer|
343
- $ob.list(:Person) do |person|
374
+ db.q_buffer do |buffer|
375
+ ob.list(:Person) do |person|
344
376
  count += 1
345
- $ob.delete(person, :db_buffer => buffer)
377
+ ob.delete(person, :db_buffer => buffer)
346
378
  end
347
379
 
348
380
  buffer.flush
@@ -350,26 +382,26 @@ describe "Objects" do
350
382
 
351
383
  raise "Expected count to be #{pers_length} but it wasnt: #{count}" if count != pers_length
352
384
 
353
- persons = $ob.list(:Person).to_a
385
+ persons = ob.list(:Person).to_a
354
386
  raise "Expected persons count to be 0 but it wasnt: #{persons.map{|e| e.data} }" if persons.length > 0
355
387
  end
356
388
 
357
389
  it "should do autozero when deleting objects" do
358
- person1 = $ob.add(:Person, {
390
+ person1 = ob.add(:Person, {
359
391
  :name => "Kasper"
360
392
  })
361
- person2 = $ob.add(:Person, {
393
+ person2 = ob.add(:Person, {
362
394
  :name => "Charlotte"
363
395
  })
364
396
 
365
- timelog1 = $ob.add(:Timelog, {
397
+ timelog1 = ob.add(:Timelog, {
366
398
  :person_id => person1.id
367
399
  })
368
- timelog2 = $ob.add(:Timelog, {
400
+ timelog2 = ob.add(:Timelog, {
369
401
  :person_id => person2.id
370
402
  })
371
403
 
372
- $ob.delete(person1)
404
+ ob.delete(person1)
373
405
 
374
406
  raise "Expected timelog1's person-ID to be zero but it wasnt: '#{timelog1[:person_id]}'." if timelog1[:person_id].to_i != 0
375
407
  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
@@ -378,25 +410,25 @@ describe "Objects" do
378
410
  it "should be able to do multiple deletes from ids" do
379
411
  ids = []
380
412
  1.upto(10) do |count|
381
- ids << $ob.add(:Person).id
413
+ ids << ob.add(:Person).id
382
414
  end
383
415
 
384
- $ob.delete_ids(:class => :Person, :ids => ids)
416
+ ob.delete_ids(:class => :Person, :ids => ids)
385
417
  end
386
418
 
387
419
  it "get_or_add" do
388
- person1 = $ob.add(:Person, {
420
+ person1 = ob.add(:Person, {
389
421
  :name => "get_or_add"
390
422
  })
391
423
 
392
- person2 = $ob.get_or_add(:Person, {
424
+ person2 = ob.get_or_add(:Person, {
393
425
  :name => "get_or_add"
394
426
  })
395
427
 
396
428
  person2.id.should eql(person1.id)
397
429
  person2[:name].should eql("get_or_add")
398
430
 
399
- person3 = $ob.get_or_add(:Person, {
431
+ person3 = ob.get_or_add(:Person, {
400
432
  :name => "get_or_add3"
401
433
  })
402
434