33import os
44from unittest import TestCase
55
6- from postgres import Postgres
6+ from postgres import Postgres , TooFew , TooMany
77
88
99DATABASE_URL = os .environ ['DATABASE_URL' ]
1010
1111
12+ # harnesses
13+ # =========
14+
1215class WithSchema (TestCase ):
1316
1417 def setUp (self ):
@@ -18,6 +21,7 @@ def setUp(self):
1821
1922 def tearDown (self ):
2023 self .db .run ("DROP SCHEMA IF EXISTS public CASCADE" )
24+ del self .db
2125
2226
2327class WithData (WithSchema ):
@@ -29,6 +33,9 @@ def setUp(self):
2933 self .db .run ("INSERT INTO foo VALUES ('buz')" )
3034
3135
36+ # db.run
37+ # ======
38+
3239class TestRun (WithSchema ):
3340
3441 def test_run_runs (self ):
@@ -44,16 +51,126 @@ def test_run_inserts(self):
4451 assert actual == 1
4552
4653
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.
4862
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'" )
51124 assert actual == {"bar" : "baz" }
52125
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'" )
55138 assert actual is None
56139
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+
57174 def test_rows_fetches_all_rows (self ):
58175 actual = self .db .rows ("SELECT * FROM foo ORDER BY bar" )
59176 assert actual == [{"bar" : "baz" }, {"bar" : "buz" }]
@@ -68,6 +185,9 @@ def test_bind_parameters_as_tuple_work(self):
68185 assert actual == [{"bar" : "baz" }]
69186
70187
188+ # db.get_cursor
189+ # =============
190+
71191class TestCursor (WithData ):
72192
73193 def test_get_cursor_gets_a_cursor (self ):
@@ -89,6 +209,9 @@ def test_we_can_use_cursor_closed(self):
89209 assert not actual
90210
91211
212+ # db.get_transaction
213+ # ==================
214+
92215class TestTransaction (WithData ):
93216
94217 def test_get_transaction_gets_a_transaction (self ):
@@ -125,6 +248,9 @@ class Heck(Exception): pass
125248 assert actual == [{"bar" : "baz" }, {"bar" : "buz" }]
126249
127250
251+ # db.get_connection
252+ # =================
253+
128254class TestConnection (WithData ):
129255
130256 def test_get_connection_gets_a_connection (self ):
0 commit comments