Skip to content

Commit abd4ed8

Browse files
committed
Catch up test suite
The test suite was a little behind recent changes: - automatic dereferencing - Python 3 exception assignment (only defined in the block)
1 parent 9bcd9b0 commit abd4ed8

File tree

1 file changed

+36
-25
lines changed

1 file changed

+36
-25
lines changed

tests.py

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ def test_run_runs(self):
4545
self.db.run("CREATE TABLE foo (bar text)")
4646
actual = self.db.all("SELECT tablename FROM pg_tables "
4747
"WHERE schemaname='public'")
48-
assert actual == [{"tablename": "foo"}]
48+
assert actual == ["foo"]
4949

5050
def test_run_inserts(self):
5151
self.db.run("CREATE TABLE foo (bar text)")
5252
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"
5555

5656

5757
# db.all
@@ -61,11 +61,11 @@ class TestRows(WithData):
6161

6262
def test_rows_fetches_all_rows(self):
6363
actual = self.db.all("SELECT * FROM foo ORDER BY bar")
64-
assert actual == [{"bar": "baz"}, {"bar": "buz"}]
64+
assert actual == ["baz", "buz"]
6565

6666
def test_rows_fetches_one_row(self):
6767
actual = self.db.all("SELECT * FROM foo WHERE bar='baz'")
68-
assert actual == [{"bar": "baz"}]
68+
assert actual == ["baz"]
6969

7070
def test_rows_fetches_no_rows(self):
7171
actual = self.db.all("SELECT * FROM foo WHERE bar='blam'")
@@ -74,11 +74,11 @@ def test_rows_fetches_no_rows(self):
7474
def test_bind_parameters_as_dict_work(self):
7575
params = {"bar": "baz"}
7676
actual = self.db.all("SELECT * FROM foo WHERE bar=%(bar)s", params)
77-
assert actual == [{"bar": "baz"}]
77+
assert actual == ["baz"]
7878

7979
def test_bind_parameters_as_tuple_work(self):
8080
actual = self.db.all("SELECT * FROM foo WHERE bar=%s", ("baz",))
81-
assert actual == [{"bar": "baz"}]
81+
assert actual == ["baz"]
8282

8383

8484
# db.one_or_zero
@@ -88,28 +88,35 @@ class TestWrongNumberException(WithData):
8888

8989
def test_TooFew_message_is_helpful(self):
9090
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)")
9292
except TooFew as exc:
93-
pass
94-
actual = str(exc)
93+
actual = str(exc)
9594
assert actual == "Got -1 rows; expecting 0 or 1."
9695

9796
def test_TooMany_message_is_helpful_for_two_options(self):
9897
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+
)
100104
except TooMany as exc:
101-
pass
102-
actual = str(exc)
105+
actual = str(exc)
103106
assert actual == "Got 2 rows; expecting exactly 1."
104107

105108
def test_TooMany_message_is_helpful_for_a_range(self):
106109
self.db.run("INSERT INTO foo VALUES ('blam')")
107110
self.db.run("INSERT INTO foo VALUES ('blim')")
108111
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+
)
110118
except TooMany as exc:
111-
pass
112-
actual = str(exc)
119+
actual = str(exc)
113120
assert actual == "Got 4 rows; expecting between 1 and 3 (inclusive)."
114121

115122

@@ -144,7 +151,7 @@ class WHEEEE: pass
144151

145152
def test_one_or_zero_returns_one(self):
146153
actual = self.db.one_or_zero("SELECT * FROM foo WHERE bar='baz'")
147-
assert actual == {"bar": "baz"}
154+
assert actual == "baz"
148155

149156
def test_with_strict_True_one_raises_TooMany(self):
150157
self.assertRaises(TooMany, self.db.one_or_zero, "SELECT * FROM foo")
@@ -167,14 +174,14 @@ def test_transaction_is_isolated(self):
167174
txn.execute("INSERT INTO foo VALUES ('blam')")
168175
txn.execute("SELECT * FROM foo ORDER BY bar")
169176
actual = self.db.all("SELECT * FROM foo ORDER BY bar")
170-
assert actual == [{"bar": "baz"}, {"bar": "buz"}]
177+
assert actual == ["baz", "buz"]
171178

172179
def test_transaction_commits_on_success(self):
173180
with self.db.get_transaction() as txn:
174181
txn.execute("INSERT INTO foo VALUES ('blam')")
175182
txn.execute("SELECT * FROM foo ORDER BY bar")
176183
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"]
178185

179186
def test_transaction_rolls_back_on_failure(self):
180187
class Heck(Exception): pass
@@ -186,7 +193,7 @@ class Heck(Exception): pass
186193
except Heck:
187194
pass
188195
actual = self.db.all("SELECT * FROM foo ORDER BY bar")
189-
assert actual == [{"bar": "baz"}, {"bar": "buz"}]
196+
assert actual == ["baz", "buz"]
190197

191198
def test_we_close_the_cursor(self):
192199
with self.db.get_transaction() as txn:
@@ -218,12 +225,16 @@ def setUp(self): # override
218225
self.db = Postgres(DATABASE_URL)
219226
self.db.run("DROP SCHEMA IF EXISTS public CASCADE")
220227
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)")
224231

225232
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)]
228235
actual = self.db.all("SELECT * FROM foo ORDER BY bar")
229236
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

Comments
 (0)