Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dantownsend committed Jun 10, 2024
1 parent c807c58 commit a79b867
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions tests/query/test_querystring.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from unittest import TestCase

from piccolo.querystring import QueryString
from tests.base import postgres_only


# TODO - add more extensive tests (increased nesting and argument count).
Expand Down Expand Up @@ -28,3 +29,66 @@ def test_string(self):
def test_querystring_with_no_args(self):
qs = QueryString("SELECT name FROM band")
self.assertEqual(qs.compile_string(), ("SELECT name FROM band", []))


@postgres_only
class TestQueryStringOperators(TestCase):
"""
Make sure basic operations can be used on ``QueryString``.
"""

def test_add(self):
query = QueryString("SELECT price") + 1
self.assertIsInstance(query, QueryString)
self.assertEqual(
query.compile_string(),
("SELECT price + $1", [1]),
)

def test_divide(self):
query = QueryString("SELECT price") / 1
self.assertIsInstance(query, QueryString)
self.assertEqual(
query.compile_string(),
("SELECT price / $1", [1]),
)

def test_power(self):
query = QueryString("SELECT price") ** 2
self.assertIsInstance(query, QueryString)
self.assertEqual(
query.compile_string(),
("SELECT price ^ $1", [2]),
)

def test_subtract(self):
query = QueryString("SELECT price") - 1
self.assertIsInstance(query, QueryString)
self.assertEqual(
query.compile_string(),
("SELECT price - $1", [1]),
)

def test_modulus(self):
query = QueryString("SELECT price") % 1
self.assertIsInstance(query, QueryString)
self.assertEqual(
query.compile_string(),
("SELECT price % $1", [1]),
)

def test_like(self):
query = QueryString("strip(name)").like("Python%")
self.assertIsInstance(query, QueryString)
self.assertEqual(
query.compile_string(),
("strip(name) LIKE $1", ["Python%"]),
)

def test_ilike(self):
query = QueryString("strip(name)").ilike("Python%")
self.assertIsInstance(query, QueryString)
self.assertEqual(
query.compile_string(),
("strip(name) ILIKE $1", ["Python%"]),
)

0 comments on commit a79b867

Please sign in to comment.