knjrbfw 0.0.110 → 0.0.111
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.
- checksums.yaml +4 -4
- data/Gemfile +1 -1
- data/Gemfile.lock +2 -2
- data/VERSION +1 -1
- data/knjrbfw.gemspec +8 -36
- data/lib/knj/autoload.rb +1 -2
- data/lib/knj/gtk2_window.rb +7 -7
- data/lib/knj/unix_proc.rb +35 -35
- metadata +33 -62
- data/lib/knj/db.rb +0 -1
- data/lib/knj/knjdb/dbtime.rb +0 -35
- data/lib/knj/knjdb/drivers/mysql/knjdb_mysql.rb +0 -604
- data/lib/knj/knjdb/drivers/mysql/knjdb_mysql_columns.rb +0 -155
- data/lib/knj/knjdb/drivers/mysql/knjdb_mysql_indexes.rb +0 -69
- data/lib/knj/knjdb/drivers/mysql/knjdb_mysql_sqlspecs.rb +0 -5
- data/lib/knj/knjdb/drivers/mysql/knjdb_mysql_tables.rb +0 -443
- data/lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3.rb +0 -184
- data/lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3_columns.rb +0 -177
- data/lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3_indexes.rb +0 -29
- data/lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3_sqlspecs.rb +0 -5
- data/lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3_tables.rb +0 -449
- data/lib/knj/knjdb/dump.rb +0 -122
- data/lib/knj/knjdb/idquery.rb +0 -109
- data/lib/knj/knjdb/libknjdb.rb +0 -797
- data/lib/knj/knjdb/libknjdb_java_sqlite3.rb +0 -83
- data/lib/knj/knjdb/libknjdb_row.rb +0 -153
- data/lib/knj/knjdb/libknjdb_sqlite3_ironruby.rb +0 -69
- data/lib/knj/knjdb/query_buffer.rb +0 -87
- data/lib/knj/knjdb/revision.rb +0 -342
- data/lib/knj/knjdb/sqlspecs.rb +0 -5
- data/lib/knj/objects.rb +0 -957
- data/lib/knj/process.rb +0 -480
- data/lib/knj/process_meta.rb +0 -569
- data/spec/db_spec.rb +0 -282
- data/spec/db_spec_encoding_test_file.txt +0 -1
- data/spec/objects_spec.rb +0 -394
- data/spec/process_meta_spec.rb +0 -172
- data/spec/process_spec.rb +0 -115
data/spec/db_spec.rb
DELETED
@@ -1,282 +0,0 @@
|
|
1
|
-
#encoding: utf-8
|
2
|
-
|
3
|
-
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
4
|
-
|
5
|
-
describe "Db" do
|
6
|
-
it "should be able to handle various encodings" do
|
7
|
-
#I never got this test to actually fail... :-(
|
8
|
-
|
9
|
-
require "knj/db"
|
10
|
-
require "knj/os"
|
11
|
-
require "rubygems"
|
12
|
-
require "sqlite3" if !Kernel.const_defined?("SQLite3") and RUBY_ENGINE != "jruby"
|
13
|
-
|
14
|
-
db_path = "#{Knj::Os.tmpdir}/knjrbfw_test_sqlite3.sqlite3"
|
15
|
-
File.unlink(db_path) if File.exists?(db_path)
|
16
|
-
|
17
|
-
db = Knj::Db.new(
|
18
|
-
:type => "sqlite3",
|
19
|
-
:path => db_path,
|
20
|
-
:return_keys => "symbols",
|
21
|
-
:index_append_table_name => true
|
22
|
-
)
|
23
|
-
|
24
|
-
db.tables.create("test", {
|
25
|
-
"columns" => [
|
26
|
-
{"name" => "id", "type" => "int", "autoincr" => true, "primarykey" => true},
|
27
|
-
{"name" => "text", "type" => "varchar"}
|
28
|
-
]
|
29
|
-
})
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
#Get a list of tables and check the list for errors.
|
34
|
-
list = db.tables.list
|
35
|
-
raise "Table not found: 'test'." if !list.key?("test")
|
36
|
-
raise "Table-name expected to be 'test' but wasnt: '#{list["test"].name}'." if list["test"].name != "test"
|
37
|
-
|
38
|
-
|
39
|
-
#Test revision to create tables, indexes and insert rows.
|
40
|
-
schema = {
|
41
|
-
"tables" => {
|
42
|
-
"test_table" => {
|
43
|
-
"columns" => [
|
44
|
-
{"name" => "id", "type" => "int", "autoincr" => true, "primarykey" => true},
|
45
|
-
{"name" => "name", "type" => "varchar"},
|
46
|
-
{"name" => "age", "type" => "int"},
|
47
|
-
{"name" => "nickname", "type" => "varchar"}
|
48
|
-
],
|
49
|
-
"indexes" => [
|
50
|
-
"name"
|
51
|
-
],
|
52
|
-
"rows" => [
|
53
|
-
{
|
54
|
-
"find_by" => {"id" => 1},
|
55
|
-
"data" => {"id" => 1, "name" => "trala"}
|
56
|
-
}
|
57
|
-
]
|
58
|
-
}
|
59
|
-
}
|
60
|
-
}
|
61
|
-
|
62
|
-
rev = Knj::Db::Revision.new
|
63
|
-
rev.init_db("schema" => schema, "db" => db)
|
64
|
-
|
65
|
-
|
66
|
-
#Test wrong encoding.
|
67
|
-
cont = File.read("#{File.dirname(__FILE__)}/db_spec_encoding_test_file.txt")
|
68
|
-
cont.force_encoding("ASCII-8BIT")
|
69
|
-
|
70
|
-
db.insert("test", {
|
71
|
-
"text" => cont
|
72
|
-
})
|
73
|
-
|
74
|
-
|
75
|
-
#Throw out invalid encoding because it will make dumping fail.
|
76
|
-
db.tables[:test].truncate
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
#Test IDQueries.
|
81
|
-
rows_count = 1250
|
82
|
-
db.transaction do
|
83
|
-
0.upto(rows_count) do |count|
|
84
|
-
db.insert(:test_table, {:name => "User #{count}"})
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
block_ran = 0
|
89
|
-
idq = Knj::Db::Idquery.new(:db => db, :debug => false, :table => :test_table, :query => "SELECT id FROM test_table") do |data|
|
90
|
-
block_ran += 1
|
91
|
-
end
|
92
|
-
|
93
|
-
raise "Block with should have ran too little: #{block_ran}." if block_ran < rows_count
|
94
|
-
|
95
|
-
block_ran = 0
|
96
|
-
db.select(:test_table, {}, {:idquery => true}) 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
|
-
|
103
|
-
#Test upserting.
|
104
|
-
data = {:name => "Kasper Johansen"}
|
105
|
-
sel = {:nickname => "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
|
-
|
113
|
-
table.reload
|
114
|
-
table.rows_count.should eql(rows_count + 1)
|
115
|
-
|
116
|
-
db.upsert(:test_table, sel, data)
|
117
|
-
|
118
|
-
table.reload
|
119
|
-
table.rows_count.should eql(rows_count + 1)
|
120
|
-
|
121
|
-
|
122
|
-
#Test dumping.
|
123
|
-
dump = Knj::Db::Dump.new(:db => db, :debug => false)
|
124
|
-
str_io = StringIO.new
|
125
|
-
dump.dump(str_io)
|
126
|
-
str_io.rewind
|
127
|
-
|
128
|
-
|
129
|
-
#Remember some numbers for validation.
|
130
|
-
tables_count = db.tables.list.length
|
131
|
-
|
132
|
-
|
133
|
-
#Remove everything in the db.
|
134
|
-
db.tables.list do |table|
|
135
|
-
table.drop unless table.native?
|
136
|
-
end
|
137
|
-
|
138
|
-
|
139
|
-
#Run the exported SQL.
|
140
|
-
db.transaction do
|
141
|
-
str_io.each_line do |sql|
|
142
|
-
db.q(sql)
|
143
|
-
end
|
144
|
-
end
|
145
|
-
|
146
|
-
|
147
|
-
#Vaildate import.
|
148
|
-
raise "Not same amount of tables: #{tables_count}, #{db.tables.list.length}" if tables_count != db.tables.list.length
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
#Test revision table renaming.
|
153
|
-
Knj::Db::Revision.new.init_db("db" => db, "schema" => {
|
154
|
-
"tables" => {
|
155
|
-
"new_test_table" => {
|
156
|
-
"renames" => ["test_table"]
|
157
|
-
}
|
158
|
-
}
|
159
|
-
})
|
160
|
-
tables = db.tables.list
|
161
|
-
raise "Didnt expect table 'test_table' to exist but it did." if tables.key?("test_table")
|
162
|
-
raise "Expected 'new_test_table' to exist but it didnt." if !tables.key?("new_test_table")
|
163
|
-
|
164
|
-
|
165
|
-
#Test revision for column renaming.
|
166
|
-
Knj::Db::Revision.new.init_db("db" => db, "schema" => {
|
167
|
-
"tables" => {
|
168
|
-
"new_test_table" => {
|
169
|
-
"columns" => [
|
170
|
-
{"name" => "new_name", "type" => "varchar", "renames" => ["name"]}
|
171
|
-
]
|
172
|
-
}
|
173
|
-
}
|
174
|
-
})
|
175
|
-
columns = db.tables["new_test_table"].columns
|
176
|
-
raise "Didnt expect 'name' to exist but it did." if columns.key?("name")
|
177
|
-
raise "Expected 'new_name'-column to exist but it didnt." if !columns.key?("new_name")
|
178
|
-
|
179
|
-
|
180
|
-
#Delete test-database if everything went well.
|
181
|
-
File.unlink(db_path) if File.exists?(db_path)
|
182
|
-
end
|
183
|
-
|
184
|
-
it "should generate proper sql" do
|
185
|
-
require "knj/db"
|
186
|
-
require "knj/os"
|
187
|
-
require "rubygems"
|
188
|
-
require "sqlite3" if !Kernel.const_defined?("SQLite3") and RUBY_ENGINE != "jruby"
|
189
|
-
|
190
|
-
db_path = "#{Knj::Os.tmpdir}/knjrbfw_test_sqlite3.sqlite3"
|
191
|
-
File.unlink(db_path) if File.exists?(db_path)
|
192
|
-
|
193
|
-
db = Knj::Db.new(
|
194
|
-
:type => "sqlite3",
|
195
|
-
:path => db_path,
|
196
|
-
:return_keys => "symbols",
|
197
|
-
:index_append_table_name => true
|
198
|
-
)
|
199
|
-
|
200
|
-
time = Time.new(1985, 6, 17, 10, 30)
|
201
|
-
db.insert(:test, {:date => time}, :return_sql => true).should eql("INSERT INTO `test` (`date`) VALUES ('1985-06-17 10:30:00')")
|
202
|
-
|
203
|
-
date = Date.new(1985, 6, 17)
|
204
|
-
db.insert(:test, {:date => date}, :return_sql => true).should eql("INSERT INTO `test` (`date`) VALUES ('1985-06-17')")
|
205
|
-
end
|
206
|
-
|
207
|
-
it "should copy database structure and data" do
|
208
|
-
require "knj/db"
|
209
|
-
require "knj/os"
|
210
|
-
require "rubygems"
|
211
|
-
require "sqlite3" if !Kernel.const_defined?("SQLite3") and RUBY_ENGINE != "jruby"
|
212
|
-
|
213
|
-
db_path1 = "#{Knj::Os.tmpdir}/knjrbfw_test_sqlite3_db1.sqlite3"
|
214
|
-
File.unlink(db_path1) if File.exists?(db_path1)
|
215
|
-
|
216
|
-
db_path2 = "#{Knj::Os.tmpdir}/knjrbfw_test_sqlite3_db2.sqlite3"
|
217
|
-
File.unlink(db_path2) if File.exists?(db_path2)
|
218
|
-
|
219
|
-
db1 = Knj::Db.new(
|
220
|
-
:type => "sqlite3",
|
221
|
-
:path => db_path1,
|
222
|
-
:return_keys => "symbols",
|
223
|
-
:index_append_table_name => true
|
224
|
-
)
|
225
|
-
|
226
|
-
db1.tables.create(:test_table, {
|
227
|
-
"columns" => [
|
228
|
-
{"name" => "id", "type" => "int", "autoincr" => true, "primarykey" => true},
|
229
|
-
{"name" => "testname", "type" => "varchar"}
|
230
|
-
],
|
231
|
-
"indexes" => [
|
232
|
-
"testname"
|
233
|
-
]
|
234
|
-
})
|
235
|
-
|
236
|
-
table1 = db1.tables["test_table"]
|
237
|
-
cols1 = table1.columns
|
238
|
-
|
239
|
-
100.times do |count|
|
240
|
-
table1.insert(:testname => "TestRow#{count}")
|
241
|
-
end
|
242
|
-
|
243
|
-
db2 = Knj::Db.new(
|
244
|
-
:type => "sqlite3",
|
245
|
-
:path => db_path2,
|
246
|
-
:return_keys => "symbols",
|
247
|
-
:index_append_table_name => true
|
248
|
-
)
|
249
|
-
|
250
|
-
begin
|
251
|
-
table2 = db2.tables["test_table"]
|
252
|
-
raise "Expected not-found exception."
|
253
|
-
rescue Errno::ENOENT
|
254
|
-
#expected
|
255
|
-
end
|
256
|
-
|
257
|
-
db1.copy_to(db2)
|
258
|
-
|
259
|
-
table2 = db2.tables["test_table"]
|
260
|
-
|
261
|
-
cols2 = table2.columns
|
262
|
-
cols2.length.should eql(cols1.length)
|
263
|
-
|
264
|
-
table2.rows_count.should eql(table1.rows_count)
|
265
|
-
|
266
|
-
db1.select(:test_table) do |row1|
|
267
|
-
found = 0
|
268
|
-
db2.select(:test_table, row1) do |row2|
|
269
|
-
found += 1
|
270
|
-
|
271
|
-
row1.each do |key, val|
|
272
|
-
row2[key].should eql(val)
|
273
|
-
end
|
274
|
-
end
|
275
|
-
|
276
|
-
found.should eql(1)
|
277
|
-
end
|
278
|
-
|
279
|
-
table1.indexes.length.should eql(1)
|
280
|
-
table2.indexes.length.should eql(table1.indexes.length)
|
281
|
-
end
|
282
|
-
end
|
@@ -1 +0,0 @@
|
|
1
|
-
��pl��p���
|
data/spec/objects_spec.rb
DELETED
@@ -1,394 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
-
|
3
|
-
describe "Objects" do
|
4
|
-
it "should be able to cache rows" do
|
5
|
-
begin
|
6
|
-
require "#{File.dirname(__FILE__)}/../../array_enumerator/lib/array_enumerator"
|
7
|
-
rescue LoadError
|
8
|
-
require "array_enumerator"
|
9
|
-
end
|
10
|
-
|
11
|
-
require "sqlite3" if RUBY_ENGINE != "jruby"
|
12
|
-
|
13
|
-
$db_path = "#{Knj::Os.tmpdir}/knjrbfw_objects_cache_test.sqlite3"
|
14
|
-
File.unlink($db_path) if File.exists?($db_path)
|
15
|
-
$db = Knj::Db.new(:type => :sqlite3, :path => $db_path, :return_keys => "symbols", :debug => false)
|
16
|
-
|
17
|
-
schema = {
|
18
|
-
"tables" => {
|
19
|
-
"Group" => {
|
20
|
-
"columns" => [
|
21
|
-
{"name" => "id", "type" => "int", "autoincr" => true, "primarykey" => true},
|
22
|
-
{"name" => "groupname", "type" => "varchar"}
|
23
|
-
]
|
24
|
-
},
|
25
|
-
"User" => {
|
26
|
-
"columns" => [
|
27
|
-
{"name" => "id", "type" => "int", "autoincr" => true, "primarykey" => true},
|
28
|
-
{"name" => "username", "type" => "varchar"}
|
29
|
-
]
|
30
|
-
}
|
31
|
-
}
|
32
|
-
}
|
33
|
-
Knj::Db::Revision.new.init_db("schema" => schema, "db" => $db)
|
34
|
-
|
35
|
-
class User < Knj::Datarow; end
|
36
|
-
|
37
|
-
$ob = Knj::Objects.new(
|
38
|
-
:db => $db,
|
39
|
-
:datarow => true,
|
40
|
-
:require => false,
|
41
|
-
:array_enum => true,
|
42
|
-
:models => {
|
43
|
-
:User => {
|
44
|
-
:cache_ids => true
|
45
|
-
}
|
46
|
-
}
|
47
|
-
)
|
48
|
-
|
49
|
-
$ob.adds(:User, [
|
50
|
-
{:username => "User 1"},
|
51
|
-
{:username => "User 2"},
|
52
|
-
{:username => "User 3"},
|
53
|
-
{:username => "User 4"},
|
54
|
-
{:username => "User 5"}
|
55
|
-
])
|
56
|
-
|
57
|
-
raise "Expected user-ID-cache to be 5 but it wasnt: #{$ob.ids_cache[:User].length}" if $ob.ids_cache[:User].length != 5
|
58
|
-
|
59
|
-
user = $ob.get(:User, 4)
|
60
|
-
raise "No user returned." if !user
|
61
|
-
$ob.delete(user)
|
62
|
-
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
|
63
|
-
|
64
|
-
$ob.deletes([$ob.get(:User, 1), $ob.get(:User, 2)])
|
65
|
-
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
|
66
|
-
end
|
67
|
-
|
68
|
-
it "should be able to do 'select_col_as_array'" do
|
69
|
-
res = $ob.list(:User, {"select_col_as_array" => "id"}).to_a
|
70
|
-
raise "Expected length of 2 but got: #{res.length}" if res.length != 2
|
71
|
-
end
|
72
|
-
|
73
|
-
it "should work even though stressed by threads (thread-safe)." do
|
74
|
-
userd = []
|
75
|
-
10.upto(25) do |i|
|
76
|
-
userd << {:username => "User #{i}"}
|
77
|
-
end
|
78
|
-
|
79
|
-
$ob.adds(:User, userd)
|
80
|
-
users = $ob.list(:User).to_a
|
81
|
-
|
82
|
-
#Stress it to test threadsafety...
|
83
|
-
threads = []
|
84
|
-
0.upto(5) do |tc|
|
85
|
-
threads << Knj::Thread.new do
|
86
|
-
0.upto(5) do |ic|
|
87
|
-
user = $ob.add(:User, {:username => "User #{tc}-#{ic}"})
|
88
|
-
raise "No user returned." if !user
|
89
|
-
$ob.delete(user)
|
90
|
-
|
91
|
-
user1 = $ob.add(:User, {:username => "User #{tc}-#{ic}-1"})
|
92
|
-
user2 = $ob.add(:User, {:username => "User #{tc}-#{ic}-2"})
|
93
|
-
user3 = $ob.add(:User, {:username => "User #{tc}-#{ic}-3"})
|
94
|
-
|
95
|
-
raise "Missing user?" if !user1 or !user2 or !user3 or user1.deleted? or user2.deleted? or user3.deleted?
|
96
|
-
$ob.deletes([user1, user2, user3])
|
97
|
-
|
98
|
-
count = 0
|
99
|
-
users.each do |user|
|
100
|
-
count += 1
|
101
|
-
user[:username] = "#{user[:username]}." if !user.deleted?
|
102
|
-
end
|
103
|
-
|
104
|
-
raise "Expected at least 15 users but got #{count}." if count != 18
|
105
|
-
end
|
106
|
-
end
|
107
|
-
end
|
108
|
-
|
109
|
-
threads.each do |thread|
|
110
|
-
thread.join
|
111
|
-
end
|
112
|
-
end
|
113
|
-
|
114
|
-
it "should be able to skip queries when adding" do
|
115
|
-
class Group < Knj::Datarow; end
|
116
|
-
|
117
|
-
$ob2 = Knj::Objects.new(
|
118
|
-
:db => $db,
|
119
|
-
:datarow => true,
|
120
|
-
:require => false
|
121
|
-
)
|
122
|
-
|
123
|
-
threads = []
|
124
|
-
0.upto(5) do
|
125
|
-
threads << Knj::Thread.new do
|
126
|
-
0.upto(5) do
|
127
|
-
ret = $ob2.add(:Group, {:groupname => "User 1"}, {:skip_ret => true})
|
128
|
-
raise "Expected empty return but got something: #{ret}" if ret
|
129
|
-
end
|
130
|
-
end
|
131
|
-
end
|
132
|
-
|
133
|
-
threads.each do |thread|
|
134
|
-
thread.join
|
135
|
-
end
|
136
|
-
end
|
137
|
-
|
138
|
-
it "should delete the temporary database." do
|
139
|
-
File.unlink($db_path) if File.exists?($db_path)
|
140
|
-
end
|
141
|
-
|
142
|
-
#Moved from "knjrbfw_spec.rb"
|
143
|
-
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
|
144
|
-
$db_path = "#{Knj::Os.tmpdir}/knjrbfw_test_sqlite3.sqlite3"
|
145
|
-
$db = Knj::Db.new(
|
146
|
-
:type => "sqlite3",
|
147
|
-
:path => $db_path,
|
148
|
-
:return_keys => "symbols",
|
149
|
-
:index_append_table_name => true
|
150
|
-
)
|
151
|
-
|
152
|
-
$db.tables.create("Project", {
|
153
|
-
"columns" => [
|
154
|
-
{"name" => "id", "type" => "int", "autoincr" => true, "primarykey" => true},
|
155
|
-
{"name" => "category_id", "type" => "int"},
|
156
|
-
{"name" => "name", "type" => "varchar"}
|
157
|
-
],
|
158
|
-
"indexes" => [
|
159
|
-
{"name" => "category_id", "columns" => ["category_id"]}
|
160
|
-
]
|
161
|
-
})
|
162
|
-
|
163
|
-
$db.tables.create("Task", {
|
164
|
-
"columns" => [
|
165
|
-
{"name" => "id", "type" => "int", "autoincr" => true, "primarykey" => true},
|
166
|
-
{"name" => "project_id", "type" => "int"},
|
167
|
-
{"name" => "person_id", "type" => "int"},
|
168
|
-
{"name" => "name", "type" => "varchar"}
|
169
|
-
],
|
170
|
-
"indexes" => [
|
171
|
-
{"name" => "project_id", "columns" => ["project_id"]}
|
172
|
-
]
|
173
|
-
})
|
174
|
-
|
175
|
-
$db.tables.create("Person", {
|
176
|
-
"columns" => [
|
177
|
-
{"name" => "id", "type" => "int", "autoincr" => true, "primarykey" => true},
|
178
|
-
{"name" => "name", "type" => "varchar"}
|
179
|
-
]
|
180
|
-
})
|
181
|
-
|
182
|
-
$db.tables.create("Timelog", {
|
183
|
-
"columns" => [
|
184
|
-
{"name" => "id", "type" => "int", "autoincr" => true, "primarykey" => true},
|
185
|
-
{"name" => "person_id", "type" => "int"}
|
186
|
-
],
|
187
|
-
"indexes" => [
|
188
|
-
"person_id"
|
189
|
-
]
|
190
|
-
})
|
191
|
-
|
192
|
-
table = $db.tables["Project"]
|
193
|
-
|
194
|
-
indexes = table.indexes
|
195
|
-
raise "Could not find the sample-index 'category_id' that should have been created." if !indexes["Project__category_id"]
|
196
|
-
|
197
|
-
|
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
|
-
$db.insert("Project", {
|
200
|
-
"name" => "Test project"
|
201
|
-
})
|
202
|
-
|
203
|
-
count = 0
|
204
|
-
$db.q("SELECT * FROM Project") do |d|
|
205
|
-
raise "Somehow name was not 'Test project'" if d[:name] != "Test project"
|
206
|
-
raise "ID was not set?" if d[:id].to_i <= 0
|
207
|
-
count += 1
|
208
|
-
end
|
209
|
-
|
210
|
-
raise "Expected count of 1 but it wasnt: #{count}" if count != 1
|
211
|
-
end
|
212
|
-
|
213
|
-
it "should be able to automatic generate methods on datarow-classes (has_many, has_one)." do
|
214
|
-
class Project < Knj::Datarow
|
215
|
-
has_many [
|
216
|
-
{:class => :Task, :col => :project_id, :depends => true}
|
217
|
-
]
|
218
|
-
end
|
219
|
-
|
220
|
-
class Task < Knj::Datarow
|
221
|
-
has_one [
|
222
|
-
{:class => :Person, :required => true},
|
223
|
-
:Project
|
224
|
-
]
|
225
|
-
end
|
226
|
-
|
227
|
-
class Person < Knj::Datarow
|
228
|
-
has_one [:Project]
|
229
|
-
|
230
|
-
has_many [
|
231
|
-
{:class => :Timelog, :autozero => true}
|
232
|
-
]
|
233
|
-
|
234
|
-
def html
|
235
|
-
return self[:name]
|
236
|
-
end
|
237
|
-
end
|
238
|
-
|
239
|
-
class Timelog < Knj::Datarow
|
240
|
-
|
241
|
-
end
|
242
|
-
|
243
|
-
$ob = Knj::Objects.new(:db => $db, :datarow => true, :require => false)
|
244
|
-
|
245
|
-
$ob.add(:Person, {
|
246
|
-
:name => "Kasper"
|
247
|
-
})
|
248
|
-
$ob.add(:Task, {
|
249
|
-
:name => "Test task",
|
250
|
-
:person_id => 1,
|
251
|
-
:project_id => 1
|
252
|
-
})
|
253
|
-
|
254
|
-
begin
|
255
|
-
$obb.add(:Task, {:name => "Test task"})
|
256
|
-
raise "Method should fail but didnt."
|
257
|
-
rescue
|
258
|
-
#ignore.
|
259
|
-
end
|
260
|
-
|
261
|
-
|
262
|
-
#Test 'list_invalid_required'.
|
263
|
-
$db.insert(:Task, :name => "Invalid require")
|
264
|
-
id = $db.last_id
|
265
|
-
found = false
|
266
|
-
|
267
|
-
$ob.list_invalid_required(:class => :Task) do |d|
|
268
|
-
raise "Expected object ID to be #{id} but it wasnt: #{d[:obj].id}" if d[:obj].id.to_i != id.to_i
|
269
|
-
$ob.delete(d[:obj])
|
270
|
-
found = true
|
271
|
-
end
|
272
|
-
|
273
|
-
raise "Expected to find a task but didnt." if !found
|
274
|
-
|
275
|
-
|
276
|
-
ret_proc = []
|
277
|
-
$ob.list(:Task) do |task|
|
278
|
-
ret_proc << task
|
279
|
-
end
|
280
|
-
|
281
|
-
raise "list with proc should return one task but didnt." if ret_proc.length != 1
|
282
|
-
|
283
|
-
|
284
|
-
project = $ob.get(:Project, 1)
|
285
|
-
|
286
|
-
tasks = project.tasks
|
287
|
-
raise "No tasks were found on project?" if tasks.empty?
|
288
|
-
|
289
|
-
|
290
|
-
ret_proc = []
|
291
|
-
ret_test = project.tasks do |task|
|
292
|
-
ret_proc << task
|
293
|
-
end
|
294
|
-
|
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
|
-
raise "list for project with proc should return one task but didnt (#{ret_proc.length})." if ret_proc.length != 1
|
297
|
-
|
298
|
-
person = tasks.first.person
|
299
|
-
project_second = tasks.first.project
|
300
|
-
|
301
|
-
raise "Returned object was not a person on task." if !person.is_a?(Person)
|
302
|
-
raise "Returned object was not a project on task." if !project_second.is_a?(Project)
|
303
|
-
|
304
|
-
|
305
|
-
#Check that has_many-depending is actually working.
|
306
|
-
begin
|
307
|
-
$ob.delete(project)
|
308
|
-
raise "It was possible to delete project 1 even though task 1 depended on it!"
|
309
|
-
rescue
|
310
|
-
#this should happen - it should not possible to delete project 1 because task 1 depends on it."
|
311
|
-
end
|
312
|
-
end
|
313
|
-
|
314
|
-
it "should be able to generate lists for inputs" do
|
315
|
-
Knj::Web.inputs([{
|
316
|
-
:title => "Test 3",
|
317
|
-
:name => :seltest3,
|
318
|
-
:type => :select,
|
319
|
-
:default => 1,
|
320
|
-
:opts => $ob.list_optshash(:Task)
|
321
|
-
}])
|
322
|
-
end
|
323
|
-
|
324
|
-
it "should be able to connect to objects 'no-html' callback and test it." do
|
325
|
-
task = $ob.get(:Task, 1)
|
326
|
-
$ob.events.connect(:no_html) do |event, classname|
|
327
|
-
"[no #{classname.to_s.downcase}]"
|
328
|
-
end
|
329
|
-
|
330
|
-
raise "Unexpected person_html from task (should have been 'Kasper'): '#{task.person_html}'." if task.person_html != "Kasper"
|
331
|
-
task.update(:person_id => 0)
|
332
|
-
raise "Unexpected person_html from task (should have been '[no person]')." if task.person_html != "[no person]"
|
333
|
-
end
|
334
|
-
|
335
|
-
it "should be able to to multiple additions and delete objects through a buffer" do
|
336
|
-
objs = []
|
337
|
-
0.upto(500) do
|
338
|
-
objs << {:name => :Kasper}
|
339
|
-
end
|
340
|
-
|
341
|
-
$ob.adds(:Person, objs)
|
342
|
-
pers_length = $ob.list(:Person, "count" => true)
|
343
|
-
|
344
|
-
count = 0
|
345
|
-
$db.q_buffer do |buffer|
|
346
|
-
$ob.list(:Person) do |person|
|
347
|
-
count += 1
|
348
|
-
$ob.delete(person, :db_buffer => buffer)
|
349
|
-
end
|
350
|
-
|
351
|
-
buffer.flush
|
352
|
-
end
|
353
|
-
|
354
|
-
raise "Expected count to be #{pers_length} but it wasnt: #{count}" if count != pers_length
|
355
|
-
|
356
|
-
persons = $ob.list(:Person).to_a
|
357
|
-
raise "Expected persons count to be 0 but it wasnt: #{persons.map{|e| e.data} }" if persons.length > 0
|
358
|
-
end
|
359
|
-
|
360
|
-
it "should do autozero when deleting objects" do
|
361
|
-
person1 = $ob.add(:Person, {
|
362
|
-
:name => "Kasper"
|
363
|
-
})
|
364
|
-
person2 = $ob.add(:Person, {
|
365
|
-
:name => "Charlotte"
|
366
|
-
})
|
367
|
-
|
368
|
-
timelog1 = $ob.add(:Timelog, {
|
369
|
-
:person_id => person1.id
|
370
|
-
})
|
371
|
-
timelog2 = $ob.add(:Timelog, {
|
372
|
-
:person_id => person2.id
|
373
|
-
})
|
374
|
-
|
375
|
-
$ob.delete(person1)
|
376
|
-
|
377
|
-
raise "Expected timelog1's person-ID to be zero but it wasnt: '#{timelog1[:person_id]}'." if timelog1[:person_id].to_i != 0
|
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
|
-
end
|
380
|
-
|
381
|
-
it "should be able to do multiple deletes from ids" do
|
382
|
-
ids = []
|
383
|
-
1.upto(10) do |count|
|
384
|
-
ids << $ob.add(:Person).id
|
385
|
-
end
|
386
|
-
|
387
|
-
$ob.delete_ids(:class => :Person, :ids => ids)
|
388
|
-
end
|
389
|
-
|
390
|
-
it "should delete the temp database again." do
|
391
|
-
db_path = "#{Knj::Os.tmpdir}/knjrbfw_test_sqlite3.sqlite3"
|
392
|
-
File.unlink(db_path) if File.exists?(db_path)
|
393
|
-
end
|
394
|
-
end
|