Skip to content

Commit ad0c66a

Browse files
author
Daniel Townsend
committed
added test for objects
1 parent 126f8ca commit ad0c66a

File tree

4 files changed

+36
-16
lines changed

4 files changed

+36
-16
lines changed

aragorm/query/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ class Objects(
6464
table instances are returned, rather than just data.
6565
"""
6666

67+
_output = Output(as_objects=True)
68+
6769
def __str__(self):
6870
"""
6971
Need to do this without repeating select ...
@@ -73,11 +75,9 @@ def __str__(self):
7375
column_names=[]
7476
)
7577

76-
for attr in ('_limit', '_where', 'order_by'):
78+
for attr in ('_limit', '_where', '_output', 'order_by'):
7779
setattr(select, attr, getattr(self, attr))
7880

79-
select._output = Output(as_objects=True)
80-
8181
return select.__str__()
8282

8383

aragorm/query/base.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,22 @@ async def run(self, as_dict=True, credentials=None):
4141
raw = self.response_handler(raw)
4242

4343
output = getattr(self, '_output', None)
44-
if output and type(raw) is list:
45-
if output.as_list:
46-
if len(raw[0].keys()) != 1:
47-
raise ValueError(
48-
'Each row returned more than on value'
49-
)
50-
else:
51-
raw = list(
52-
itertools.chain(*[j.values() for j in raw])
53-
)
54-
if output.as_json:
55-
raw = json.dumps(raw)
44+
45+
if output:
46+
if output.as_objects:
47+
raw = [self.table(**columns) for columns in raw]
48+
elif type(raw) is list:
49+
if output.as_list:
50+
if len(raw[0].keys()) != 1:
51+
raise ValueError(
52+
'Each row returned more than on value'
53+
)
54+
else:
55+
raw = list(
56+
itertools.chain(*[j.values() for j in raw])
57+
)
58+
if output.as_json:
59+
raw = json.dumps(raw)
5660

5761
return raw
5862

aragorm/table.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,9 @@ def alter(cls) -> Alter:
282282

283283
@classmethod
284284
def objects(cls) -> Objects:
285-
return Objects()
285+
return Objects(
286+
table=cls
287+
)
286288

287289
@classmethod
288290
def exists(cls) -> Exists:

tests/table/test_objects.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from ..base import DBTestCase
2+
from ..example_project.tables import Pokemon
3+
4+
5+
class TestObjects(DBTestCase):
6+
7+
def test_get_all(self):
8+
self.insert_row()
9+
10+
response = Pokemon.objects().run_sync()
11+
12+
self.assertTrue(len(response) == 1)
13+
self.assertTrue(isinstance(response[0], Pokemon))
14+
self.assertTrue(response[0].name == 'pikachu')

0 commit comments

Comments
 (0)