@@ -45,13 +45,13 @@ def test_run_runs(self):
45
45
self .db .run ("CREATE TABLE foo (bar text)" )
46
46
actual = self .db .all ("SELECT tablename FROM pg_tables "
47
47
"WHERE schemaname='public'" )
48
- assert actual == [{ "tablename" : " foo"} ]
48
+ assert actual == [" foo" ]
49
49
50
50
def test_run_inserts (self ):
51
51
self .db .run ("CREATE TABLE foo (bar text)" )
52
52
self .db .run ("INSERT INTO foo VALUES ('baz')" )
53
- actual = len ( self .db .one_or_zero ("SELECT * FROM foo ORDER BY bar" ) )
54
- assert actual == 1
53
+ actual = self .db .one_or_zero ("SELECT * FROM foo ORDER BY bar" )
54
+ assert actual == "baz"
55
55
56
56
57
57
# db.all
@@ -61,11 +61,11 @@ class TestRows(WithData):
61
61
62
62
def test_rows_fetches_all_rows (self ):
63
63
actual = self .db .all ("SELECT * FROM foo ORDER BY bar" )
64
- assert actual == [{ "bar" : " baz"}, { "bar" : " buz"} ]
64
+ assert actual == [" baz", " buz" ]
65
65
66
66
def test_rows_fetches_one_row (self ):
67
67
actual = self .db .all ("SELECT * FROM foo WHERE bar='baz'" )
68
- assert actual == [{ "bar" : " baz"} ]
68
+ assert actual == [" baz" ]
69
69
70
70
def test_rows_fetches_no_rows (self ):
71
71
actual = self .db .all ("SELECT * FROM foo WHERE bar='blam'" )
@@ -74,11 +74,11 @@ def test_rows_fetches_no_rows(self):
74
74
def test_bind_parameters_as_dict_work (self ):
75
75
params = {"bar" : "baz" }
76
76
actual = self .db .all ("SELECT * FROM foo WHERE bar=%(bar)s" , params )
77
- assert actual == [{ "bar" : " baz"} ]
77
+ assert actual == [" baz" ]
78
78
79
79
def test_bind_parameters_as_tuple_work (self ):
80
80
actual = self .db .all ("SELECT * FROM foo WHERE bar=%s" , ("baz" ,))
81
- assert actual == [{ "bar" : " baz"} ]
81
+ assert actual == [" baz" ]
82
82
83
83
84
84
# db.one_or_zero
@@ -88,28 +88,35 @@ class TestWrongNumberException(WithData):
88
88
89
89
def test_TooFew_message_is_helpful (self ):
90
90
try :
91
- exc = self .db .one_or_zero ("CREATE TABLE foux (baar text)" )
91
+ actual = self .db .one_or_zero ("CREATE TABLE foux (baar text)" )
92
92
except TooFew as exc :
93
- pass
94
- actual = str (exc )
93
+ actual = str (exc )
95
94
assert actual == "Got -1 rows; expecting 0 or 1."
96
95
97
96
def test_TooMany_message_is_helpful_for_two_options (self ):
98
97
try :
99
- exc = self .db ._some ("SELECT * FROM foo" , lo = 1 , hi = 1 )
98
+ actual = self .db ._some ( "SELECT * FROM foo"
99
+ , parameters = None
100
+ , lo = 1
101
+ , hi = 1
102
+ , record_type = None
103
+ )
100
104
except TooMany as exc :
101
- pass
102
- actual = str (exc )
105
+ actual = str (exc )
103
106
assert actual == "Got 2 rows; expecting exactly 1."
104
107
105
108
def test_TooMany_message_is_helpful_for_a_range (self ):
106
109
self .db .run ("INSERT INTO foo VALUES ('blam')" )
107
110
self .db .run ("INSERT INTO foo VALUES ('blim')" )
108
111
try :
109
- exc = self .db ._some ("SELECT * FROM foo" , lo = 1 , hi = 3 )
112
+ actual = self .db ._some ( "SELECT * FROM foo"
113
+ , parameters = None
114
+ , lo = 1
115
+ , hi = 3
116
+ , record_type = None
117
+ )
110
118
except TooMany as exc :
111
- pass
112
- actual = str (exc )
119
+ actual = str (exc )
113
120
assert actual == "Got 4 rows; expecting between 1 and 3 (inclusive)."
114
121
115
122
@@ -144,7 +151,7 @@ class WHEEEE: pass
144
151
145
152
def test_one_or_zero_returns_one (self ):
146
153
actual = self .db .one_or_zero ("SELECT * FROM foo WHERE bar='baz'" )
147
- assert actual == { "bar" : " baz"}
154
+ assert actual == " baz"
148
155
149
156
def test_with_strict_True_one_raises_TooMany (self ):
150
157
self .assertRaises (TooMany , self .db .one_or_zero , "SELECT * FROM foo" )
@@ -167,14 +174,14 @@ def test_transaction_is_isolated(self):
167
174
txn .execute ("INSERT INTO foo VALUES ('blam')" )
168
175
txn .execute ("SELECT * FROM foo ORDER BY bar" )
169
176
actual = self .db .all ("SELECT * FROM foo ORDER BY bar" )
170
- assert actual == [{ "bar" : " baz"}, { "bar" : " buz"} ]
177
+ assert actual == [" baz", " buz" ]
171
178
172
179
def test_transaction_commits_on_success (self ):
173
180
with self .db .get_transaction () as txn :
174
181
txn .execute ("INSERT INTO foo VALUES ('blam')" )
175
182
txn .execute ("SELECT * FROM foo ORDER BY bar" )
176
183
actual = self .db .all ("SELECT * FROM foo ORDER BY bar" )
177
- assert actual == [{ "bar" : " baz"}, { "bar" : " blam"}, { "bar" : " buz"} ]
184
+ assert actual == [" baz", " blam", " buz" ]
178
185
179
186
def test_transaction_rolls_back_on_failure (self ):
180
187
class Heck (Exception ): pass
@@ -186,7 +193,7 @@ class Heck(Exception): pass
186
193
except Heck :
187
194
pass
188
195
actual = self .db .all ("SELECT * FROM foo ORDER BY bar" )
189
- assert actual == [{ "bar" : " baz"}, { "bar" : " buz"} ]
196
+ assert actual == [" baz", " buz" ]
190
197
191
198
def test_we_close_the_cursor (self ):
192
199
with self .db .get_transaction () as txn :
@@ -218,12 +225,16 @@ def setUp(self): # override
218
225
self .db = Postgres (DATABASE_URL )
219
226
self .db .run ("DROP SCHEMA IF EXISTS public CASCADE" )
220
227
self .db .run ("CREATE SCHEMA public" )
221
- self .db .run ("CREATE TABLE foo (bar text)" )
222
- self .db .run ("INSERT INTO foo VALUES ('baz' )" )
223
- self .db .run ("INSERT INTO foo VALUES ('buz' )" )
228
+ self .db .run ("CREATE TABLE foo (bar text, baz int )" )
229
+ self .db .run ("INSERT INTO foo VALUES ('buz', 42 )" )
230
+ self .db .run ("INSERT INTO foo VALUES ('biz', 43 )" )
224
231
225
232
def test_NamedDictCursor_results_in_namedtuples (self ):
226
- Record = namedtuple ("Record" , ["bar" ])
227
- expected = [Record (bar = "baz" ), Record (bar = "buz" )]
233
+ Record = namedtuple ("Record" , ["bar" , "baz" ])
234
+ expected = [Record (bar = "biz" , baz = 43 ), Record (bar = "buz" , baz = 42 )]
228
235
actual = self .db .all ("SELECT * FROM foo ORDER BY bar" )
229
236
assert actual == expected
237
+
238
+ def test_namedtuples_can_be_unrolled (self ):
239
+ actual = self .db .all ("SELECT baz FROM foo ORDER BY bar" )
240
+ assert actual == [43 , 42 ]
0 commit comments