Skip to content
This repository has been archived by the owner on Apr 24, 2020. It is now read-only.

Commit

Permalink
[Fun] Allowed to output a QuerySet as a string.
Browse files Browse the repository at this point in the history
  • Loading branch information
ducdetronquito committed Dec 21, 2016
1 parent ac72d54 commit dcf0333
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
7 changes: 6 additions & 1 deletion plume.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ def __init__(self, model):
self._clause = None
self._count = None
self._offset = None

def __str__(self):
return ''.join((
'(', SQLiteAPI.select(
self._tables, self._fields, self._clause, self._count, self._offset), ')'
))

def filter(self, *args):
"""
Expand Down Expand Up @@ -141,7 +147,6 @@ def slice(self, count, offset):

def __execute(self):
"""Query the database and returns the result as a list of model instances."""

query = SQLiteAPI.select(
self._tables, self._fields, self._clause, self._count, self._offset
)
Expand Down
30 changes: 26 additions & 4 deletions tests/queryset.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ class TrainerTestCase(TestCase):
}
}

class QuerySetAPI(TrainerTestCase):

def test_output_queryset_as_string(self):
expected = str(Trainer.objects.filter(Trainer.age > 18, Trainer.name != 'Giovanni'))

self.assertEqual(
"(SELECT * FROM trainer WHERE name != 'Giovanni' AND age > 18)",
expected,
)


class QuerySetSliceTests(TrainerTestCase):

Expand Down Expand Up @@ -92,8 +102,6 @@ def test_select_from_one_table_with_all_fields(self):
self.assertEqual(jessie.name, 'Jessie')
self.assertEqual(jessie.age, 17)



def test_select_from_one_table_with_one_criterion(self):
self.add_fixture(Trainer, ['Giovanni', 'James', 'Jessie'])

Expand All @@ -108,7 +116,6 @@ def test_select_from_one_table_with_one_criterion(self):
self.assertEqual(james.name, 'James')
self.assertEqual(james.age, 21)


def test_select_from_one_table_with_ANDs_criteria_operator(self):
self.add_fixture(Trainer, ['Giovanni', 'James', 'Jessie'])

Expand All @@ -120,7 +127,6 @@ def test_select_from_one_table_with_ANDs_criteria_operator(self):
self.assertEqual(james.name, 'James')
self.assertEqual(james.age, 21)


def test_select_from_one_table_with_ANDs_criteria_list(self):
self.add_fixture(Trainer, ['Giovanni', 'James', 'Jessie'])

Expand All @@ -145,4 +151,20 @@ def test_select_from_one_table_with_chained_filters(self):
self.assertEqual(james.name, 'James')
self.assertEqual(james.age, 21)

def test_select_from_one_table_with_inner_query(self):
"""
self.add_fixture(Trainer, ['Giovanni', 'James', 'Jessie'])
queryset = Trainer.objects.filter(Trainer.name != 'Giovanni')
queryset.filter(Trainer.age > Trainer.objects.select('age').filter(Trainer.name == 'Jessie'))
result = list(queryset)
self.assertEqual(len(result), 1)
james = result[0]
self.assertEqual(james.name, 'James')
self.assertEqual(james.age, 21)
"""
pass


6 changes: 6 additions & 0 deletions tests/sqlite_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,9 @@ def test_select_from_one_table_with_several_criteria(self):
SQLiteAPI.select(tables='pokemon', where='name = Pikachu AND level > 18'),
'SELECT * FROM pokemon WHERE name = Pikachu AND level > 18',
)

def test_select_from_one_table_with_count_and_offset(self):
self.assertEqual(
SQLiteAPI.select(tables='pokemon', count=10, offset=42),
'SELECT * FROM pokemon LIMIT 10 OFFSET 42',
)

0 comments on commit dcf0333

Please sign in to comment.