Skip to content

Commit 983e1ee

Browse files
committed
fixing tests for sqlite
1 parent cdff28a commit 983e1ee

File tree

8 files changed

+89
-16
lines changed

8 files changed

+89
-16
lines changed

piccolo/query/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ async def run(self, in_pool=True):
9898
else:
9999
responses = []
100100
# TODO - run in a transaction
101-
for querystring in self.querystring:
101+
for querystring in self.querystrings:
102102
results = await engine.run_querystring(
103103
querystring, in_pool=in_pool
104104
)

piccolo/query/methods/alter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def querystring(self) -> QueryString:
5151
class DropColumn(AlterColumnStatement):
5252
@property
5353
def querystring(self) -> QueryString:
54-
return QueryString(f"DROP {self.column_name}")
54+
return QueryString(f"DROP COLUMN {self.column_name}")
5555

5656

5757
@dataclasses.dataclass

piccolo/query/methods/select.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,16 @@ def querystrings(self) -> t.Sequence[QueryString]:
213213
query += " {}"
214214
args.append(self.order_by_delegate._order_by.querystring)
215215

216+
if (
217+
engine_type == "sqlite"
218+
and self.offset_delegate._offset
219+
and not self.limit_delegate._limit
220+
):
221+
raise ValueError(
222+
"A limit clause must be provided when doing an offset with "
223+
"SQLite."
224+
)
225+
216226
if self.limit_delegate._limit:
217227
query += " {}"
218228
args.append(self.limit_delegate._limit.querystring)

tests/table/test_alter.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from piccolo.columns import Integer
22

3-
from ..base import DBTestCase
3+
from ..base import DBTestCase, postgres_only
44
from ..example_project.tables import Band, Manager
55

66

@@ -26,8 +26,14 @@ def test_rename_column(self):
2626
self._test_rename("popularity")
2727

2828

29-
class TestDrop(DBTestCase):
30-
def _test_drop(self, column):
29+
@postgres_only
30+
class TestDropColumn(DBTestCase):
31+
"""
32+
Unfortunately this only works with Postgres at the moment.
33+
34+
SQLite has very limited support for ALTER statements.
35+
"""
36+
def _test_drop(self, column: str):
3137
self.insert_row()
3238

3339
Band.alter().drop_column(column).run_sync()
@@ -64,6 +70,7 @@ def test_add(self):
6470
self.assertEqual(response[0]["weight"], None)
6571

6672

73+
@postgres_only
6774
class TestUnique(DBTestCase):
6875
def test_unique(self):
6976
unique_query = Manager.alter().set_unique(Manager.name, True)
@@ -90,6 +97,8 @@ def test_unique(self):
9097
self.assertTrue(len(response), 2)
9198

9299

100+
# TODO - make it work for SQLite. Should work.
101+
@postgres_only
93102
class TestMultiple(DBTestCase):
94103
"""
95104
Make sure multiple alter statements work correctly.

tests/table/test_instance.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from ..base import DBTestCase
1+
from ..base import DBTestCase, sqlite_only, postgres_only
22
from ..example_project.tables import Band
33

44

@@ -7,12 +7,20 @@ class TestInstance(DBTestCase):
77
Test instantiating Table instances
88
"""
99

10-
def test_insert(self):
10+
@postgres_only
11+
def test_insert_postgres(self):
1112
Pythonistas = Band(name="Pythonistas")
1213
self.assertEqual(
1314
Pythonistas.__str__(), "(DEFAULT,null,'Pythonistas',0)"
1415
)
1516

17+
@sqlite_only
18+
def test_insert_sqlite(self):
19+
Pythonistas = Band(name="Pythonistas")
20+
self.assertEqual(
21+
Pythonistas.__str__(), "(null,null,'Pythonistas',0)"
22+
)
23+
1624
def test_non_existant_column(self):
1725
with self.assertRaises(ValueError):
1826
Band(name="Pythonistas", foo="bar")

tests/table/test_objects.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from ..base import DBTestCase
1+
from ..base import DBTestCase, postgres_only, sqlite_only
22
from ..example_project.tables import Band
33

44

@@ -25,13 +25,37 @@ def test_get_all(self):
2525
== "Rustaceans"
2626
)
2727

28-
def test_offset(self):
28+
@postgres_only
29+
def test_offset_postgres(self):
30+
"""
31+
Postgres can do an offset without a limit clause.
32+
"""
2933
self.insert_rows()
30-
3134
response = Band.objects().order_by(Band.name).offset(1).run_sync()
3235

3336
print(f"response = {response}")
3437

3538
self.assertEqual(
3639
[i.name for i in response], ["Pythonistas", "Rustaceans"]
3740
)
41+
42+
@sqlite_only
43+
def test_offset_sqlite(self):
44+
"""
45+
SQLite requires a limit clause for offset to work.
46+
"""
47+
self.insert_rows()
48+
query = Band.objects().order_by(Band.name).offset(1)
49+
50+
with self.assertRaises(ValueError):
51+
query.run_sync()
52+
53+
query = query.limit(5)
54+
55+
response = query.run_sync()
56+
57+
print(f"response = {response}")
58+
59+
self.assertEqual(
60+
[i.name for i in response], ["Pythonistas", "Rustaceans"]
61+
)

tests/table/test_select.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from ..base import DBTestCase
1+
from ..base import DBTestCase, postgres_only, sqlite_only
22
from ..example_project.tables import Band, Concert
33

44

@@ -218,7 +218,8 @@ def test_limit(self):
218218

219219
self.assertEqual(response, [{"name": "CSharps"}])
220220

221-
def test_offset(self):
221+
@postgres_only
222+
def test_offset_postgres(self):
222223
self.insert_rows()
223224

224225
response = (
@@ -235,6 +236,27 @@ def test_offset(self):
235236
response, [{"name": "Pythonistas"}, {"name": "Rustaceans"}]
236237
)
237238

239+
@sqlite_only
240+
def test_offset_sqlite(self):
241+
"""
242+
SQLite requires a limit clause for offset to work.
243+
"""
244+
self.insert_rows()
245+
246+
query = Band.select().columns(Band.name).order_by(Band.name).offset(1)
247+
248+
with self.assertRaises(ValueError):
249+
query.run_sync()
250+
251+
query = query.limit(5)
252+
response = query.run_sync()
253+
254+
print(f"response = {response}")
255+
256+
self.assertEqual(
257+
response, [{"name": "Pythonistas"}, {"name": "Rustaceans"}]
258+
)
259+
238260
def test_first(self):
239261
self.insert_rows()
240262

tests/test-requirements.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
coveralls==1.8.2
2-
flake8==3.5.0
3-
mypy==0.720
4-
pytest-cov==2.7.1
1+
coveralls==1.10.0
2+
flake8==3.7.9
3+
mypy==0.761
4+
pytest-cov==2.8.1
55
pytest==3.8.0

0 commit comments

Comments
 (0)