knjrbfw 0.0.32 → 0.0.33
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.
- data/Gemfile +2 -0
- data/Gemfile.lock +38 -0
- data/VERSION +1 -1
- data/knjrbfw.gemspec +11 -4
- data/lib/knj/autoload/wref.rb +6 -0
- data/lib/knj/autoload.rb +5 -3
- data/lib/knj/datarow.rb +74 -37
- data/lib/knj/errors.rb +5 -2
- data/lib/knj/http2.rb +8 -2
- data/lib/knj/knj.rb +0 -2
- data/lib/knj/knjdb/drivers/mysql/knjdb_mysql.rb +22 -4
- data/lib/knj/knjdb/drivers/mysql/knjdb_mysql_tables.rb +6 -8
- data/lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3.rb +1 -1
- data/lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3_tables.rb +6 -8
- data/lib/knj/knjdb/libknjdb.rb +27 -20
- data/lib/knj/knjdb/revision.rb +1 -1
- data/lib/knj/objects.rb +194 -109
- data/lib/knj/threadhandler.rb +2 -1
- data/lib/knj/unix_proc.rb +3 -3
- data/spec/datet_spec.rb +43 -0
- data/spec/knjrbfw_spec.rb +0 -290
- data/spec/objects_spec.rb +274 -0
- data/spec/php_spec.rb +28 -7
- data/spec/process_spec.rb +7 -1
- data/spec/web_spec.rb +61 -0
- metadata +40 -17
- data/lib/knj/wref.rb +0 -251
- data/spec/wref_spec.rb +0 -10
data/spec/knjrbfw_spec.rb
CHANGED
@@ -1,300 +1,11 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
3
|
describe "Knjrbfw" do
|
4
|
-
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
|
5
|
-
require "knjrbfw"
|
6
|
-
require "sqlite3" if RUBY_ENGINE != "jruby"
|
7
|
-
|
8
|
-
db_path = "#{Knj::Os.tmpdir}/knjrbfw_test_sqlite3.sqlite3"
|
9
|
-
|
10
|
-
begin
|
11
|
-
db = Knj::Db.new(
|
12
|
-
:type => "sqlite3",
|
13
|
-
:path => db_path,
|
14
|
-
:return_keys => "symbols",
|
15
|
-
:index_append_table_name => true
|
16
|
-
)
|
17
|
-
|
18
|
-
db.tables.create("Project", {
|
19
|
-
"columns" => [
|
20
|
-
{"name" => "id", "type" => "int", "autoincr" => true, "primarykey" => true},
|
21
|
-
{"name" => "category_id", "type" => "int"},
|
22
|
-
{"name" => "name", "type" => "varchar"}
|
23
|
-
],
|
24
|
-
"indexes" => [
|
25
|
-
{"name" => "category_id", "columns" => ["category_id"]}
|
26
|
-
]
|
27
|
-
})
|
28
|
-
|
29
|
-
db.tables.create("Task", {
|
30
|
-
"columns" => [
|
31
|
-
{"name" => "id", "type" => "int", "autoincr" => true, "primarykey" => true},
|
32
|
-
{"name" => "project_id", "type" => "int"},
|
33
|
-
{"name" => "user_id", "type" => "int"},
|
34
|
-
{"name" => "name", "type" => "varchar"}
|
35
|
-
],
|
36
|
-
"indexes" => [
|
37
|
-
{"name" => "project_id", "columns" => ["project_id"]}
|
38
|
-
]
|
39
|
-
})
|
40
|
-
|
41
|
-
db.tables.create("User", {
|
42
|
-
"columns" => [
|
43
|
-
{"name" => "id", "type" => "int", "autoincr" => true, "primarykey" => true},
|
44
|
-
{"name" => "name", "type" => "varchar"}
|
45
|
-
]
|
46
|
-
})
|
47
|
-
|
48
|
-
table = db.tables["Project"]
|
49
|
-
|
50
|
-
indexes = table.indexes
|
51
|
-
raise "Could not find the sample-index 'category_id' that should have been created." if !indexes["Project__category_id"]
|
52
|
-
|
53
|
-
|
54
|
-
#If we insert a row the ID should increase and the name should be the same as inserted (or something is very very wrong)...
|
55
|
-
db.insert("Project", {
|
56
|
-
"name" => "Test project"
|
57
|
-
})
|
58
|
-
|
59
|
-
db.q("SELECT * FROM Project") do |d|
|
60
|
-
raise "Somehow name was not 'Test project'" if d[:name] != "Test project"
|
61
|
-
raise "ID was not set?" if d[:id].to_i <= 0
|
62
|
-
end
|
63
|
-
|
64
|
-
$db = db
|
65
|
-
rescue => e
|
66
|
-
File.unlink(db_path) if File.exists?(db_path)
|
67
|
-
raise e
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
it "should be able to parse various date formats." do
|
72
|
-
date = Knj::Datet.in("2011-07-09 00:00:00 UTC")
|
73
|
-
date = Knj::Datet.in("1985-06-17 01:00:00")
|
74
|
-
date = Knj::Datet.in("1985-06-17")
|
75
|
-
date = Knj::Datet.in("17/06 1985")
|
76
|
-
|
77
|
-
raise "Couldnt register type 1 nullstamp." if !Knj::Datet.is_nullstamp?("0000-00-00")
|
78
|
-
raise "Couldnt register type 2 nullstamp." if !Knj::Datet.is_nullstamp?("0000-00-00 00:00:00")
|
79
|
-
raise "Registered nullstamp on valid date." if Knj::Datet.is_nullstamp?("1985-06-17")
|
80
|
-
raise "Registered nullstamp on valid date." if Knj::Datet.is_nullstamp?("1985-06-17 10:30:00")
|
81
|
-
|
82
|
-
date = Knj::Datet.in("2011-07-09 13:05:04 +0200")
|
83
|
-
ltime = date.localtime_str
|
84
|
-
|
85
|
-
#if RUBY_VERSION.slice(0, 3) == "1.9"
|
86
|
-
# if ltime != date.time.localtime
|
87
|
-
# raise "Calculated localtime (#{ltime}) was not the same as the real Time-localtime (#{date.time.localtime})."
|
88
|
-
# end
|
89
|
-
#end
|
90
|
-
|
91
|
-
if ltime != "2011-07-09 13:05:04 +0200"
|
92
|
-
raise "Datet didnt return expected result: '#{ltime}'."
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
|
-
it "should be able to compare dates" do
|
97
|
-
date1 = Knj::Datet.in("17/06 1985")
|
98
|
-
date2 = Knj::Datet.in("18/06 1985")
|
99
|
-
date3 = Knj::Datet.in("17/06 1985")
|
100
|
-
|
101
|
-
raise "Date1 was wrongly higher than date2." if date1 > date2
|
102
|
-
|
103
|
-
if date2 > date1
|
104
|
-
#do nothing.
|
105
|
-
else
|
106
|
-
raise "Date2 was wrongly not higher than date1."
|
107
|
-
end
|
108
|
-
|
109
|
-
raise "Date1 was wrongly not the same as date3." if date1 != date3
|
110
|
-
raise "Date1 was the same as date2?" if date1 == date2
|
111
|
-
end
|
112
|
-
|
113
|
-
it "should be able to automatic generate methods on datarow-classes (has_many, has_one)." do
|
114
|
-
class Project < Knj::Datarow
|
115
|
-
has_many [
|
116
|
-
{:class => :Task, :col => :project_id, :depends => true}
|
117
|
-
]
|
118
|
-
end
|
119
|
-
|
120
|
-
class Task < Knj::Datarow
|
121
|
-
has_one [
|
122
|
-
{:class => :User, :required => true},
|
123
|
-
:Project
|
124
|
-
]
|
125
|
-
end
|
126
|
-
|
127
|
-
class User < Knj::Datarow
|
128
|
-
has_one [:Project]
|
129
|
-
|
130
|
-
def html
|
131
|
-
return self[:name]
|
132
|
-
end
|
133
|
-
end
|
134
|
-
|
135
|
-
$ob = Knj::Objects.new(:db => $db, :datarow => true, :require => false)
|
136
|
-
|
137
|
-
$ob.add(:User, {
|
138
|
-
:name => "Kasper"
|
139
|
-
})
|
140
|
-
$ob.add(:Task, {
|
141
|
-
:name => "Test task",
|
142
|
-
:user_id => 1,
|
143
|
-
:project_id => 1
|
144
|
-
})
|
145
|
-
|
146
|
-
ret_proc = []
|
147
|
-
$ob.list(:Task) do |task|
|
148
|
-
ret_proc << task
|
149
|
-
end
|
150
|
-
|
151
|
-
raise "list with proc should return one task but didnt." if ret_proc.length != 1
|
152
|
-
|
153
|
-
|
154
|
-
project = $ob.get(:Project, 1)
|
155
|
-
|
156
|
-
tasks = project.tasks
|
157
|
-
raise "No tasks were found on project?" if tasks.empty?
|
158
|
-
|
159
|
-
|
160
|
-
ret_proc = []
|
161
|
-
ret_test = project.tasks do |task|
|
162
|
-
ret_proc << task
|
163
|
-
end
|
164
|
-
|
165
|
-
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
|
166
|
-
raise "list for project with proc should return one task but didnt (#{ret_proc.length})." if ret_proc.length != 1
|
167
|
-
|
168
|
-
user = tasks[0].user
|
169
|
-
project_second = tasks[0].project
|
170
|
-
|
171
|
-
raise "Returned object was not a user on task." if !user.is_a?(User)
|
172
|
-
raise "Returned object was not a project on task." if !project_second.is_a?(Project)
|
173
|
-
|
174
|
-
|
175
|
-
#Check that has_many-depending is actually working.
|
176
|
-
begin
|
177
|
-
$ob.delete(project)
|
178
|
-
raise "It was possible to delete project 1 even though task 1 depended on it!"
|
179
|
-
rescue
|
180
|
-
#this should happen - it should not possible to delete project 1 because task 1 depends on it."
|
181
|
-
end
|
182
|
-
end
|
183
|
-
|
184
|
-
it "should be able to connect to objects 'no-html' callback and test it." do
|
185
|
-
task = $ob.get(:Task, 1)
|
186
|
-
$ob.events.connect(:no_html) do |event, classname|
|
187
|
-
"[no #{classname.to_s.downcase}]"
|
188
|
-
end
|
189
|
-
|
190
|
-
raise "Unexpected user_html from task (should have been 'Kasper')." if task.user_html != "Kasper"
|
191
|
-
task.update(:user_id => 0)
|
192
|
-
raise "Unexpected user_html from task (should have been '[no user]')." if task.user_html != "[no user]"
|
193
|
-
end
|
194
|
-
|
195
|
-
it "should delete the temp database again." do
|
196
|
-
db_path = "#{Knj::Os.tmpdir}/knjrbfw_test_sqlite3.sqlite3"
|
197
|
-
File.unlink(db_path) if File.exists?(db_path)
|
198
|
-
end
|
199
|
-
|
200
|
-
it "should be able to execute various Knj::Php functions correctly." do
|
201
|
-
str = "Kasper Johansen"
|
202
|
-
|
203
|
-
#substr
|
204
|
-
teststr = Knj::Php.substr(str, 7, 8)
|
205
|
-
if teststr != "Johansen"
|
206
|
-
raise "substr did not return expected result: '#{teststr}'"
|
207
|
-
end
|
208
|
-
|
209
|
-
teststr = Knj::Php.substr(str, -8, 8)
|
210
|
-
if teststr != "Johansen"
|
211
|
-
raise "substr did not returned expected result when using negative positions: '#{teststr}'."
|
212
|
-
end
|
213
|
-
|
214
|
-
#strtoupper
|
215
|
-
teststr = Knj::Php.strtoupper(str)
|
216
|
-
if teststr != "KASPER JOHANSEN"
|
217
|
-
raise "strtoupper did not return expected result: '#{teststr}'."
|
218
|
-
end
|
219
|
-
|
220
|
-
#strtolower
|
221
|
-
teststr = Knj::Php.strtolower(str)
|
222
|
-
if teststr != "kasper johansen"
|
223
|
-
raise "strtolower did not return expected result: '#{teststr}'."
|
224
|
-
end
|
225
|
-
end
|
226
|
-
|
227
4
|
it "should be able to join arrays with callbacks." do
|
228
5
|
res = Knj::ArrayExt.join(:arr => [1, 2, 3], :sep => ",", :callback => proc{|value| "'#{value}'"})
|
229
6
|
raise "Unexpected result from ArrayExt." if res != "'1','2','3'"
|
230
7
|
end
|
231
8
|
|
232
|
-
it "should be able to execute various forms of Web.input methods." do
|
233
|
-
html = Knj::Web.inputs([{
|
234
|
-
:title => "Test 1",
|
235
|
-
:name => :textest1,
|
236
|
-
:type => :text,
|
237
|
-
:default => "hmm",
|
238
|
-
:value => "trala"
|
239
|
-
},{
|
240
|
-
:title => "Test 2",
|
241
|
-
:name => :chetest2,
|
242
|
-
:type => :checkbox,
|
243
|
-
:default => true
|
244
|
-
},{
|
245
|
-
:title => "Test 3",
|
246
|
-
:name => :seltest3,
|
247
|
-
:type => :select,
|
248
|
-
:default => 1,
|
249
|
-
:opts => $ob.list_optshash(:Task)
|
250
|
-
},{
|
251
|
-
:title => "Test 4",
|
252
|
-
:name => :textest4,
|
253
|
-
:type => :textarea,
|
254
|
-
:height => 300,
|
255
|
-
:default => "Hmm",
|
256
|
-
:value => "Trala"
|
257
|
-
},{
|
258
|
-
:title => "Test 5",
|
259
|
-
:name => :filetest5,
|
260
|
-
:type => :file
|
261
|
-
},{
|
262
|
-
:title => "Test 6",
|
263
|
-
:type => :info,
|
264
|
-
:value => "Argh"
|
265
|
-
}])
|
266
|
-
end
|
267
|
-
|
268
|
-
it "should be able to use alert and back." do
|
269
|
-
Knj::Web.alert("Trala")
|
270
|
-
|
271
|
-
begin
|
272
|
-
Knj::Web.back
|
273
|
-
raise "It should have called exit which it didnt."
|
274
|
-
rescue SystemExit
|
275
|
-
#ignore.
|
276
|
-
end
|
277
|
-
|
278
|
-
begin
|
279
|
-
Knj::Web.redirect("?show=test")
|
280
|
-
raise "It should have called exit which it didnt."
|
281
|
-
rescue SystemExit
|
282
|
-
#ignore.
|
283
|
-
end
|
284
|
-
end
|
285
|
-
|
286
|
-
it "should be able to properly parse 'Set-Cookie' headers." do
|
287
|
-
data = Knj::Web.parse_set_cookies("TestCookie=TestValue+; Expires=Fri, 05 Aug 2011 10:58:17 GMT; Path=\n")
|
288
|
-
|
289
|
-
raise "No data returned?" if !data or !data.respond_to?(:length)
|
290
|
-
raise "Wrong number of cookies returned: '#{data.length}'." if data.length != 1
|
291
|
-
|
292
|
-
raise "Unexpected name: '#{data[0]["name"]}'." if data[0]["name"] != "TestCookie"
|
293
|
-
raise "Unexpected value: '#{data[0]["value"]}'." if data[0]["value"] != "TestValue "
|
294
|
-
raise "Unexpected path: '#{data[0]["path"]}'." if data[0]["path"] != ""
|
295
|
-
raise "Unexpected expire:' #{data[0]["expire"]}'." if data[0]["expires"] != "Fri, 05 Aug 2011 10:58:17 GMT"
|
296
|
-
end
|
297
|
-
|
298
9
|
it "should be able to draw rounded transparent corners on images." do
|
299
10
|
require "rubygems"
|
300
11
|
require "RMagick"
|
@@ -313,7 +24,6 @@ describe "Knjrbfw" do
|
|
313
24
|
it "should be possible to use Strings.html_links-method." do
|
314
25
|
teststr = "This is a test. http://www.google.com This is a test."
|
315
26
|
|
316
|
-
|
317
27
|
#Test normal usage.
|
318
28
|
test1 = Knj::Strings.html_links(teststr)
|
319
29
|
raise "Unexpected string: '#{teststr}'" if test1 != "This is a test. <a href=\"http://www.google.com\">http://www.google.com</a> This is a test."
|
@@ -0,0 +1,274 @@
|
|
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
|
+
require "sqlite3" if RUBY_ENGINE != "jruby"
|
6
|
+
|
7
|
+
$db_path = "#{Knj::Os.tmpdir}/knjrbfw_objects_cache_test.sqlite3"
|
8
|
+
File.unlink($db_path) if File.exists?($db_path)
|
9
|
+
$db = Knj::Db.new(:type => :sqlite3, :path => $db_path, :return_keys => "symbols")
|
10
|
+
|
11
|
+
schema = {
|
12
|
+
"tables" => {
|
13
|
+
"Group" => {
|
14
|
+
"columns" => [
|
15
|
+
{"name" => "id", "type" => "int", "autoincr" => true, "primarykey" => true},
|
16
|
+
{"name" => "groupname", "type" => "varchar"}
|
17
|
+
]
|
18
|
+
},
|
19
|
+
"User" => {
|
20
|
+
"columns" => [
|
21
|
+
{"name" => "id", "type" => "int", "autoincr" => true, "primarykey" => true},
|
22
|
+
{"name" => "username", "type" => "varchar"}
|
23
|
+
]
|
24
|
+
}
|
25
|
+
}
|
26
|
+
}
|
27
|
+
Knj::Db::Revision.new.init_db("schema" => schema, "db" => $db)
|
28
|
+
|
29
|
+
class User < Knj::Datarow; end
|
30
|
+
|
31
|
+
$ob = Knj::Objects.new(
|
32
|
+
:db => $db,
|
33
|
+
:datarow => true,
|
34
|
+
:require => false,
|
35
|
+
:models => {
|
36
|
+
:User => {
|
37
|
+
:cache_ids => true
|
38
|
+
}
|
39
|
+
}
|
40
|
+
)
|
41
|
+
|
42
|
+
$ob.adds(:User, [
|
43
|
+
{:username => "User 1"},
|
44
|
+
{:username => "User 2"},
|
45
|
+
{:username => "User 3"},
|
46
|
+
{:username => "User 4"},
|
47
|
+
{:username => "User 5"}
|
48
|
+
])
|
49
|
+
|
50
|
+
raise "Expected user-ID-cache to be 5 but it wasnt: #{$ob.ids_cache[:User].length}" if $ob.ids_cache[:User].length != 5
|
51
|
+
|
52
|
+
user = $ob.get(:User, 4)
|
53
|
+
$ob.delete(user)
|
54
|
+
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
|
55
|
+
|
56
|
+
$ob.deletes([$ob.get(:User, 1), $ob.get(:User, 2)])
|
57
|
+
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
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should work even though stressed by threads (thread-safe)." do
|
61
|
+
userd = []
|
62
|
+
10.upto(25) do |i|
|
63
|
+
userd << {:username => "User #{i}"}
|
64
|
+
end
|
65
|
+
|
66
|
+
$ob.adds(:User, userd)
|
67
|
+
users = $ob.list(:User)
|
68
|
+
|
69
|
+
#Stress it to test threadsafety...
|
70
|
+
threads = []
|
71
|
+
0.upto(10) do |tc|
|
72
|
+
threads << Knj::Thread.new do
|
73
|
+
0.upto(10) do |ic|
|
74
|
+
user = $ob.add(:User, {:username => "User #{tc}-#{ic}"})
|
75
|
+
$ob.delete(user)
|
76
|
+
|
77
|
+
user1 = $ob.add(:User, {:username => "User #{tc}-#{ic}-1"})
|
78
|
+
user2 = $ob.add(:User, {:username => "User #{tc}-#{ic}-2"})
|
79
|
+
user3 = $ob.add(:User, {:username => "User #{tc}-#{ic}-3"})
|
80
|
+
$ob.deletes([user1, user2, user3])
|
81
|
+
|
82
|
+
users.each do |user|
|
83
|
+
user[:username] = "#{user[:username]}." if !user.deleted?
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
threads.each do |thread|
|
90
|
+
thread.join
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
it "should be able to skip queries when adding" do
|
95
|
+
class Group < Knj::Datarow; end
|
96
|
+
|
97
|
+
$ob2 = Knj::Objects.new(
|
98
|
+
:db => $db,
|
99
|
+
:datarow => true,
|
100
|
+
:require => false
|
101
|
+
)
|
102
|
+
|
103
|
+
threads = []
|
104
|
+
0.upto(10) do
|
105
|
+
threads << Knj::Thread.new do
|
106
|
+
0.upto(15) do
|
107
|
+
$ob2.add(:Group, {:groupname => "User 1"}, {:skip_ret => true})
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
threads.each do |thread|
|
113
|
+
thread.join
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
it "should delete the temporary database." do
|
118
|
+
File.unlink($db_path) if File.exists?($db_path)
|
119
|
+
end
|
120
|
+
|
121
|
+
#Moved from "knjrbfw_spec.rb"
|
122
|
+
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
|
123
|
+
$db_path = "#{Knj::Os.tmpdir}/knjrbfw_test_sqlite3.sqlite3"
|
124
|
+
$db = Knj::Db.new(
|
125
|
+
:type => "sqlite3",
|
126
|
+
:path => $db_path,
|
127
|
+
:return_keys => "symbols",
|
128
|
+
:index_append_table_name => true
|
129
|
+
)
|
130
|
+
|
131
|
+
$db.tables.create("Project", {
|
132
|
+
"columns" => [
|
133
|
+
{"name" => "id", "type" => "int", "autoincr" => true, "primarykey" => true},
|
134
|
+
{"name" => "category_id", "type" => "int"},
|
135
|
+
{"name" => "name", "type" => "varchar"}
|
136
|
+
],
|
137
|
+
"indexes" => [
|
138
|
+
{"name" => "category_id", "columns" => ["category_id"]}
|
139
|
+
]
|
140
|
+
})
|
141
|
+
|
142
|
+
$db.tables.create("Task", {
|
143
|
+
"columns" => [
|
144
|
+
{"name" => "id", "type" => "int", "autoincr" => true, "primarykey" => true},
|
145
|
+
{"name" => "project_id", "type" => "int"},
|
146
|
+
{"name" => "person_id", "type" => "int"},
|
147
|
+
{"name" => "name", "type" => "varchar"}
|
148
|
+
],
|
149
|
+
"indexes" => [
|
150
|
+
{"name" => "project_id", "columns" => ["project_id"]}
|
151
|
+
]
|
152
|
+
})
|
153
|
+
|
154
|
+
$db.tables.create("Person", {
|
155
|
+
"columns" => [
|
156
|
+
{"name" => "id", "type" => "int", "autoincr" => true, "primarykey" => true},
|
157
|
+
{"name" => "name", "type" => "varchar"}
|
158
|
+
]
|
159
|
+
})
|
160
|
+
|
161
|
+
table = $db.tables["Project"]
|
162
|
+
|
163
|
+
indexes = table.indexes
|
164
|
+
raise "Could not find the sample-index 'category_id' that should have been created." if !indexes["Project__category_id"]
|
165
|
+
|
166
|
+
|
167
|
+
#If we insert a row the ID should increase and the name should be the same as inserted (or something is very very wrong)...
|
168
|
+
$db.insert("Project", {
|
169
|
+
"name" => "Test project"
|
170
|
+
})
|
171
|
+
|
172
|
+
$db.q("SELECT * FROM Project") do |d|
|
173
|
+
raise "Somehow name was not 'Test project'" if d[:name] != "Test project"
|
174
|
+
raise "ID was not set?" if d[:id].to_i <= 0
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
it "should be able to automatic generate methods on datarow-classes (has_many, has_one)." do
|
179
|
+
class Project < Knj::Datarow
|
180
|
+
has_many [
|
181
|
+
{:class => :Task, :col => :project_id, :depends => true}
|
182
|
+
]
|
183
|
+
end
|
184
|
+
|
185
|
+
class Task < Knj::Datarow
|
186
|
+
has_one [
|
187
|
+
{:class => :Person, :required => true},
|
188
|
+
:Project
|
189
|
+
]
|
190
|
+
end
|
191
|
+
|
192
|
+
class Person < Knj::Datarow
|
193
|
+
has_one [:Project]
|
194
|
+
|
195
|
+
def html
|
196
|
+
return self[:name]
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
$ob = Knj::Objects.new(:db => $db, :datarow => true, :require => false)
|
201
|
+
|
202
|
+
$ob.add(:Person, {
|
203
|
+
:name => "Kasper"
|
204
|
+
})
|
205
|
+
$ob.add(:Task, {
|
206
|
+
:name => "Test task",
|
207
|
+
:person_id => 1,
|
208
|
+
:project_id => 1
|
209
|
+
})
|
210
|
+
|
211
|
+
ret_proc = []
|
212
|
+
$ob.list(:Task) do |task|
|
213
|
+
ret_proc << task
|
214
|
+
end
|
215
|
+
|
216
|
+
raise "list with proc should return one task but didnt." if ret_proc.length != 1
|
217
|
+
|
218
|
+
|
219
|
+
project = $ob.get(:Project, 1)
|
220
|
+
|
221
|
+
tasks = project.tasks
|
222
|
+
raise "No tasks were found on project?" if tasks.empty?
|
223
|
+
|
224
|
+
|
225
|
+
ret_proc = []
|
226
|
+
ret_test = project.tasks do |task|
|
227
|
+
ret_proc << task
|
228
|
+
end
|
229
|
+
|
230
|
+
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
|
231
|
+
raise "list for project with proc should return one task but didnt (#{ret_proc.length})." if ret_proc.length != 1
|
232
|
+
|
233
|
+
person = tasks.first.person
|
234
|
+
project_second = tasks.first.project
|
235
|
+
|
236
|
+
raise "Returned object was not a person on task." if !person.is_a?(Person)
|
237
|
+
raise "Returned object was not a project on task." if !project_second.is_a?(Project)
|
238
|
+
|
239
|
+
|
240
|
+
#Check that has_many-depending is actually working.
|
241
|
+
begin
|
242
|
+
$ob.delete(project)
|
243
|
+
raise "It was possible to delete project 1 even though task 1 depended on it!"
|
244
|
+
rescue
|
245
|
+
#this should happen - it should not possible to delete project 1 because task 1 depends on it."
|
246
|
+
end
|
247
|
+
end
|
248
|
+
|
249
|
+
it "should be able to generate lists for inputs" do
|
250
|
+
Knj::Web.inputs([{
|
251
|
+
:title => "Test 3",
|
252
|
+
:name => :seltest3,
|
253
|
+
:type => :select,
|
254
|
+
:default => 1,
|
255
|
+
:opts => $ob.list_optshash(:Task)
|
256
|
+
}])
|
257
|
+
end
|
258
|
+
|
259
|
+
it "should be able to connect to objects 'no-html' callback and test it." do
|
260
|
+
task = $ob.get(:Task, 1)
|
261
|
+
$ob.events.connect(:no_html) do |event, classname|
|
262
|
+
"[no #{classname.to_s.downcase}]"
|
263
|
+
end
|
264
|
+
|
265
|
+
raise "Unexpected person_html from task (should have been 'Kasper'): '#{task.person_html}'." if task.person_html != "Kasper"
|
266
|
+
task.update(:person_id => 0)
|
267
|
+
raise "Unexpected person_html from task (should have been '[no person]')." if task.person_html != "[no person]"
|
268
|
+
end
|
269
|
+
|
270
|
+
it "should delete the temp database again." do
|
271
|
+
db_path = "#{Knj::Os.tmpdir}/knjrbfw_test_sqlite3.sqlite3"
|
272
|
+
File.unlink(db_path) if File.exists?(db_path)
|
273
|
+
end
|
274
|
+
end
|
data/spec/php_spec.rb
CHANGED
@@ -2,8 +2,6 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
|
2
2
|
|
3
3
|
describe "Php" do
|
4
4
|
it "explode" do
|
5
|
-
require "knj/php"
|
6
|
-
|
7
5
|
teststr = "1;2;3"
|
8
6
|
arr = Knj::Php.explode(";", teststr)
|
9
7
|
|
@@ -14,8 +12,6 @@ describe "Php" do
|
|
14
12
|
end
|
15
13
|
|
16
14
|
it "is_numeric" do
|
17
|
-
require "knj/php"
|
18
|
-
|
19
15
|
raise "Failed." if !Knj::Php.is_numeric(123)
|
20
16
|
raise "Failed." if !Knj::Php.is_numeric("123")
|
21
17
|
raise "Failed." if Knj::Php.is_numeric("kasper123")
|
@@ -55,9 +51,6 @@ describe "Php" do
|
|
55
51
|
end
|
56
52
|
|
57
53
|
it "parse_str" do
|
58
|
-
require "knj/php"
|
59
|
-
require "knj/web"
|
60
|
-
|
61
54
|
teststr = "first=value&arr[]=foo+bar&arr[]=baz&hash[trala]=hmm&hash[trala2]=wtf"
|
62
55
|
|
63
56
|
hash = {}
|
@@ -69,4 +62,32 @@ describe "Php" do
|
|
69
62
|
raise "Invalid value for hash-trala." if hash["hash"]["trala"] != "hmm"
|
70
63
|
raise "Invalid value for hash-trala2." if hash["hash"]["trala2"] != "wtf"
|
71
64
|
end
|
65
|
+
|
66
|
+
#Moved from "knjrbfw_spec.rb".
|
67
|
+
it "should be able to execute various Knj::Php functions correctly." do
|
68
|
+
str = "Kasper Johansen"
|
69
|
+
|
70
|
+
#substr
|
71
|
+
teststr = Knj::Php.substr(str, 7, 8)
|
72
|
+
if teststr != "Johansen"
|
73
|
+
raise "substr did not return expected result: '#{teststr}'"
|
74
|
+
end
|
75
|
+
|
76
|
+
teststr = Knj::Php.substr(str, -8, 8)
|
77
|
+
if teststr != "Johansen"
|
78
|
+
raise "substr did not returned expected result when using negative positions: '#{teststr}'."
|
79
|
+
end
|
80
|
+
|
81
|
+
#strtoupper
|
82
|
+
teststr = Knj::Php.strtoupper(str)
|
83
|
+
if teststr != "KASPER JOHANSEN"
|
84
|
+
raise "strtoupper did not return expected result: '#{teststr}'."
|
85
|
+
end
|
86
|
+
|
87
|
+
#strtolower
|
88
|
+
teststr = Knj::Php.strtolower(str)
|
89
|
+
if teststr != "kasper johansen"
|
90
|
+
raise "strtolower did not return expected result: '#{teststr}'."
|
91
|
+
end
|
92
|
+
end
|
72
93
|
end
|
data/spec/process_spec.rb
CHANGED
@@ -59,7 +59,13 @@ describe "Process" do
|
|
59
59
|
end
|
60
60
|
|
61
61
|
#Stress it by doing 1000 requests.
|
62
|
-
|
62
|
+
if RUBY_ENGINE == "jruby"
|
63
|
+
tout = 7
|
64
|
+
else
|
65
|
+
tout = 2
|
66
|
+
end
|
67
|
+
|
68
|
+
Timeout.timeout(tout) do
|
63
69
|
0.upto(1000) do |count|
|
64
70
|
#$stderr.print "Testing #{count}\n"
|
65
71
|
answer = $process_client.send("test #{count}")
|