forked from crystal-lang/crystal-db
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_spec.cr
134 lines (114 loc) · 3.75 KB
/
db_spec.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
require "./spec_helper"
private def connections
DummyDriver::DummyConnection.connections
end
describe DB do
it "should get driver class by name" do
DB.driver_class("dummy").should eq(DummyDriver)
end
it "should instantiate driver with connection uri" do
db = DB.open "dummy://localhost:1027"
db.driver.should be_a(DummyDriver)
db.uri.scheme.should eq("dummy")
db.uri.host.should eq("localhost")
db.uri.port.should eq(1027)
end
it "should create a connection and close it" do
DummyDriver::DummyConnection.clear_connections
DB.open "dummy://localhost" do |db|
end
connections.size.should eq(1)
connections.first.closed?.should be_true
end
it "should create a connection and close it" do
DummyDriver::DummyConnection.clear_connections
DB.connect "dummy://localhost" do |cnn|
cnn.should be_a(DummyDriver::DummyConnection)
end
connections.size.should eq(1)
connections.first.closed?.should be_true
end
it "should create a connection and wait for explicit closing" do
DummyDriver::DummyConnection.clear_connections
cnn = DB.connect "dummy://localhost"
cnn.should be_a(DummyDriver::DummyConnection)
connections.size.should eq(1)
connections.first.closed?.should be_false
cnn.close
connections.first.closed?.should be_true
end
it "query should close result_set" do
with_witness do |w|
with_dummy do |db|
db.query "1,2" do
break
end
w.check
DummyDriver::DummyResultSet.last_result_set.closed?.should be_true
end
end
end
it "scalar should close statement" do
with_dummy do |db|
db.scalar "1"
DummyDriver::DummyResultSet.last_result_set.closed?.should be_true
end
end
it "initially a single connection should be created" do
with_dummy do |db|
connections.size.should eq(1)
end
end
it "the connection should be closed after db usage" do
with_dummy do |db|
connections.first.closed?.should be_false
end
connections.first.closed?.should be_true
end
it "should raise if the sole connection is been used" do
with_dummy "dummy://host?max_pool_size=1&checkout_timeout=0.5" do |db|
db.query "1" do |rs|
expect_raises DB::PoolTimeout do
db.scalar "2"
end
end
end
end
it "should use 'unlimited' connections by default" do
with_dummy "dummy://host?checkout_timeout=0.5" do |db|
rs = [] of DB::ResultSet
500.times do
rs << db.query "1"
end
DummyDriver::DummyConnection.connections.size.should eq(500)
end
end
it "exec should return to pool" do
with_dummy do |db|
db.exec "foo"
db.exec "bar"
end
end
it "scalar should return to pool" do
with_dummy do |db|
db.scalar "foo"
db.scalar "bar"
end
end
it "gives nice error message when no driver is registered for schema (#21)" do
expect_raises(ArgumentError, %(no driver was registered for the schema "foobar", did you maybe forget to require the database driver?)) do
DB.open "foobar://baz"
end
end
it "should parse boolean query string params" do
DB.fetch_bool(HTTP::Params.parse("foo=true"), "foo", false).should be_true
DB.fetch_bool(HTTP::Params.parse("foo=True"), "foo", false).should be_true
DB.fetch_bool(HTTP::Params.parse("foo=false"), "foo", true).should be_false
DB.fetch_bool(HTTP::Params.parse("foo=False"), "foo", true).should be_false
DB.fetch_bool(HTTP::Params.parse("bar=true"), "foo", false).should be_false
DB.fetch_bool(HTTP::Params.parse("bar=true"), "foo", true).should be_true
expect_raises(ArgumentError, %(invalid "other" value for option "foo")) do
DB.fetch_bool(HTTP::Params.parse("foo=other"), "foo", true)
end
end
end