Skip to content

Commit 3ff33c3

Browse files
committed
Handling type row values
1 parent 0cda748 commit 3ff33c3

2 files changed

Lines changed: 38 additions & 3 deletions

File tree

jsonapi_query/database/sqlalchemy.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ def group_and_remove(items, models):
183183
response = [[] for model in models]
184184
for item in items:
185185
for member in item:
186+
if member is None:
187+
continue
186188
position = models.index(member.__class__)
187189
if member not in response[position]:
188190
response[position].append(member)

tests/unit/database/sqlalchemy_tests.py

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from sqlalchemy.orm import Query, sessionmaker
55

6-
from jsonapi_query.database.sqlalchemy import QueryMixin
6+
from jsonapi_query.database.sqlalchemy import group_and_remove, QueryMixin
77
from tests.sqlalchemy import (
88
BaseSQLAlchemyTestCase, Category, Person, Product, School, Student)
99

@@ -287,14 +287,47 @@ def test_include_ambiguous_join_conditions(self):
287287

288288
def test_include_does_not_restrict_primary_output(self):
289289
"""Test including a relationship does not restrict primary output."""
290-
p = Product(name='Tst')
290+
a = Category(name='Category A')
291+
self.session.add(a)
292+
b = Category(name='Category B', category_id=1)
293+
self.session.add(b)
294+
p = Product(primary_category_id=1, secondary_category_id=2, name='Tst')
295+
self.session.add(p)
296+
297+
p = Product(name='Tst 2')
291298
self.session.add(p)
292299

293300
models = self.session.query(Product).include(
294301
[Product.primary_category]).all()
295-
self.assertTrue(len(models) == 1)
302+
self.assertTrue(len(models) == 2)
296303

297304
def test_include_no_mappers(self):
298305
"""Test including an empty set of relationships."""
299306
models = self.session.query(Person).include([]).first()
300307
self.assertTrue(isinstance(models, Person))
308+
309+
310+
class UtilitySQLAlchemyTestCase(BaseDatabaseSQLAlchemyTests):
311+
"""Test handling a query's output."""
312+
313+
def test_group_and_remove(self):
314+
"""Test group and remove utility function."""
315+
a = Category(name='Category A')
316+
self.session.add(a)
317+
b = Category(name='Category B', category_id=1)
318+
self.session.add(b)
319+
p = Product(primary_category_id=1, secondary_category_id=2, name='Tst')
320+
self.session.add(p)
321+
322+
p = Product(name='Tst 2')
323+
self.session.add(p)
324+
325+
# Returns two products and one category.
326+
items = self.session.query(Product).include(
327+
[Product.primary_category]).all()
328+
self.assertTrue(len(items) == 2)
329+
330+
output = group_and_remove(items, [Product, Category])
331+
self.assertTrue(len(output) == 2)
332+
self.assertTrue(len(output[0]) == 2)
333+
self.assertTrue(len(output[1]) == 1)

0 commit comments

Comments
 (0)