Skip to content

Commit

Permalink
add a test
Browse files Browse the repository at this point in the history
  • Loading branch information
dantownsend committed Oct 1, 2024
1 parent c6f3934 commit 363aaae
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
10 changes: 7 additions & 3 deletions piccolo/columns/m2m.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,9 +384,13 @@ async def run(self):
.output(as_list=True)
)

results = await secondary_table.objects().where(
secondary_table._meta.primary_key.is_in(ids)
) if len(ids) > 0 else []
results = (
await secondary_table.objects().where(
secondary_table._meta.primary_key.is_in(ids)
)
if len(ids) > 0
else []
)

return results

Expand Down
16 changes: 16 additions & 0 deletions tests/columns/m2m/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,22 @@ def test_get_m2m(self):

self.assertEqual([i.name for i in genres], ["Rock", "Folk"])

def test_get_m2m_no_rows(self):
"""
If there are no matching objects, then an empty list should be
returned.
https://github.com/piccolo-orm/piccolo/issues/1090
"""
band = Band.objects().get(Band.name == "Pythonistas").run_sync()
assert band is not None

Genre.delete(force=True).run_sync()

genres = band.get_m2m(Band.genres).run_sync()
self.assertEqual(genres, [])

def test_remove_m2m(self):
"""
Make sure we can remove related items via the joining table.
Expand Down

0 comments on commit 363aaae

Please sign in to comment.