3
3
import os
4
4
from unittest import TestCase
5
5
6
- from postgres import Postgres
6
+ from postgres import Postgres , TooFew , TooMany
7
7
8
8
9
9
DATABASE_URL = os .environ ['DATABASE_URL' ]
10
10
11
11
12
+ # harnesses
13
+ # =========
14
+
12
15
class WithSchema (TestCase ):
13
16
14
17
def setUp (self ):
@@ -18,6 +21,7 @@ def setUp(self):
18
21
19
22
def tearDown (self ):
20
23
self .db .run ("DROP SCHEMA IF EXISTS public CASCADE" )
24
+ del self .db
21
25
22
26
23
27
class WithData (WithSchema ):
@@ -29,6 +33,9 @@ def setUp(self):
29
33
self .db .run ("INSERT INTO foo VALUES ('buz')" )
30
34
31
35
36
+ # db.run
37
+ # ======
38
+
32
39
class TestRun (WithSchema ):
33
40
34
41
def test_run_runs (self ):
@@ -44,16 +51,126 @@ def test_run_inserts(self):
44
51
assert actual == 1
45
52
46
53
47
- class TestOneAndRows (WithData ):
54
+ # db.one
55
+ # ======
56
+ # With all the combinations of strict_one and strict, we end up with a number
57
+ # of tests here. Since the behavior of the one method with a strict parameter
58
+ # of True or False is expected to be the same regardless of what strict_one is
59
+ # set to, we can write those once and then use the TestOne TestCase as the base
60
+ # class for other TestCases that vary the strict_one attribute. The TestOne
61
+ # tests will be re-run in each new context.
48
62
49
- def test_one_fetches_the_first_one (self ):
50
- actual = self .db .one ("SELECT * FROM foo ORDER BY bar" )
63
+ class TestNotOneException (WithData ):
64
+
65
+ def test_TooFew_message_is_helpful (self ):
66
+ try :
67
+ self .db .one ("SELECT * FROM foo WHERE bar='blah'" , strict = True )
68
+ except TooFew , exc :
69
+ actual = str (exc )
70
+ assert actual == "Got 0 rows instead of 1."
71
+
72
+ def test_TooMany_message_is_helpful (self ):
73
+ try :
74
+ self .db .one ("SELECT * FROM foo" , strict = True )
75
+ except TooMany , exc :
76
+ actual = str (exc )
77
+ assert actual == "Got 2 rows instead of 1."
78
+
79
+
80
+ class TestOne (WithData ):
81
+
82
+ def test_with_strict_True_one_raises_TooFew (self ):
83
+ self .assertRaises ( TooFew
84
+ , self .db .one
85
+ , "SELECT * FROM foo WHERE bar='blah'"
86
+ , strict = True
87
+ )
88
+
89
+ def test_with_strict_True_one_fetches_the_one (self ):
90
+ actual = self .db .one ("SELECT * FROM foo WHERE bar='baz'" , strict = True )
91
+ assert actual == {"bar" : "baz" }
92
+
93
+ def test_with_strict_True_one_raises_TooMany (self ):
94
+ self .assertRaises ( TooMany
95
+ , self .db .one
96
+ , "SELECT * FROM foo"
97
+ , strict = True
98
+ )
99
+
100
+
101
+ def test_with_strict_False_one_returns_None_if_theres_none (self ):
102
+ actual = self .db .one ("SELECT * FROM foo WHERE bar='nun'" , strict = False )
103
+ assert actual is None
104
+
105
+ def test_with_strict_False_one_fetches_the_first_one (self ):
106
+ actual = self .db .one ("SELECT * FROM foo ORDER BY bar" , strict = False )
107
+ assert actual == {"bar" : "baz" }
108
+
109
+
110
+ class TestOne_StrictOneNone (TestOne ):
111
+
112
+ def setUp (self ):
113
+ WithData .setUp (self )
114
+ self .db .strict_one = None
115
+
116
+ def test_one_raises_TooFew (self ):
117
+ self .assertRaises ( TooFew
118
+ , self .db .one
119
+ , "SELECT * FROM foo WHERE bar='nun'"
120
+ )
121
+
122
+ def test_one_returns_one (self ):
123
+ actual = self .db .one ("SELECT * FROM foo WHERE bar='baz'" )
51
124
assert actual == {"bar" : "baz" }
52
125
53
- def test_one_returns_None_if_theres_none (self ):
54
- actual = self .db .one ("SELECT * FROM foo WHERE bar='blam'" )
126
+ def test_one_raises_TooMany (self ):
127
+ self .assertRaises (TooMany , self .db .one , "SELECT * FROM foo" )
128
+
129
+
130
+ class TestOne_StrictOneFalse (TestOne ):
131
+
132
+ def setUp (self ):
133
+ WithData .setUp (self )
134
+ self .db .strict_one = False
135
+
136
+ def test_one_returns_None (self ):
137
+ actual = self .db .one ("SELECT * FROM foo WHERE bar='nun'" )
55
138
assert actual is None
56
139
140
+ def test_one_returns_one (self ):
141
+ actual = self .db .one ("SELECT * FROM foo WHERE bar='baz'" )
142
+ assert actual == {"bar" : "baz" }
143
+
144
+ def test_one_returns_first_one (self ):
145
+ actual = self .db .one ("SELECT * FROM foo ORDER BY bar" )
146
+ assert actual == {"bar" : "baz" }
147
+
148
+
149
+ class TestOne_StrictOneTrue (TestOne ):
150
+
151
+ def setUp (self ):
152
+ WithData .setUp (self )
153
+ self .db .strict_one = True
154
+
155
+ def test_one_raises_TooFew (self ):
156
+ self .assertRaises ( TooFew
157
+ , self .db .one
158
+ , "SELECT * FROM foo WHERE bar='nun'"
159
+ )
160
+
161
+ def test_one_returns_one (self ):
162
+ actual = self .db .one ("SELECT * FROM foo WHERE bar='baz'" )
163
+ assert actual == {"bar" : "baz" }
164
+
165
+ def test_one_raises_TooMany (self ):
166
+ self .assertRaises (TooMany , self .db .one , "SELECT * FROM foo" )
167
+
168
+
169
+ # db.rows
170
+ # =======
171
+
172
+ class TestRows (WithData ):
173
+
57
174
def test_rows_fetches_all_rows (self ):
58
175
actual = self .db .rows ("SELECT * FROM foo ORDER BY bar" )
59
176
assert actual == [{"bar" : "baz" }, {"bar" : "buz" }]
@@ -68,6 +185,9 @@ def test_bind_parameters_as_tuple_work(self):
68
185
assert actual == [{"bar" : "baz" }]
69
186
70
187
188
+ # db.get_cursor
189
+ # =============
190
+
71
191
class TestCursor (WithData ):
72
192
73
193
def test_get_cursor_gets_a_cursor (self ):
@@ -89,6 +209,9 @@ def test_we_can_use_cursor_closed(self):
89
209
assert not actual
90
210
91
211
212
+ # db.get_transaction
213
+ # ==================
214
+
92
215
class TestTransaction (WithData ):
93
216
94
217
def test_get_transaction_gets_a_transaction (self ):
@@ -125,6 +248,9 @@ class Heck(Exception): pass
125
248
assert actual == [{"bar" : "baz" }, {"bar" : "buz" }]
126
249
127
250
251
+ # db.get_connection
252
+ # =================
253
+
128
254
class TestConnection (WithData ):
129
255
130
256
def test_get_connection_gets_a_connection (self ):
0 commit comments