def test_filter_can_use_custom_queryset(factories): tracks = factories["music.Track"].create_batch(3) candidates = Track.objects.filter(pk=tracks[0].pk) qs = filters.run([{"type": "noop"}], candidates=candidates) assert qs.count() == 1 assert qs.first() == tracks[0]
def test_filter_on_tag(factories): tracks = factories["music.Track"].create_batch(3, tags=["metal"]) factories["music.Track"].create_batch(3, tags=["pop"]) expected = tracks f = [{"type": "tag", "names": ["metal"]}] candidates = filters.run(f) assert list(candidates.order_by("pk")) == expected
def test_filter_on_artist(factories): artist1 = factories["music.Artist"]() artist2 = factories["music.Artist"]() factories["music.Track"].create_batch(3, artist=artist1) factories["music.Track"].create_batch(3, artist=artist2) expected = list(artist1.tracks.order_by("pk")) f = [{"type": "artist", "ids": [artist1.pk]}] candidates = filters.run(f) assert list(candidates.order_by("pk")) == expected
def test_can_negate(factories): artist1 = factories["music.Artist"]() artist2 = factories["music.Artist"]() factories["music.Track"].create_batch(3, artist=artist1) factories["music.Track"].create_batch(3, artist=artist2) expected = artist2.tracks.order_by("pk") f = [{"type": "artist", "ids": [artist1.pk], "not": True}] candidates = filters.run(f) assert list(candidates.order_by("pk")) == list(expected)
def test_can_group(factories): artist1 = factories["music.Artist"]() artist2 = factories["music.Artist"]() factories["music.Track"].create_batch(2, artist=artist1) t1 = factories["music.Track"].create_batch(2, artist=artist1, tags=["metal"]) factories["music.Track"].create_batch(2, artist=artist2) t2 = factories["music.Track"].create_batch(2, artist=artist2, tags=["metal"]) factories["music.Track"].create_batch(2, tags=["metal"]) expected = t1 + t2 f = [ { "type": "tag", "names": ["metal"] }, { "type": "group", "operator": "and", "filters": [ { "type": "artist", "ids": [artist1.pk], "operator": "or" }, { "type": "artist", "ids": [artist2.pk], "operator": "or" }, ], }, ] candidates = filters.run(f) assert list(candidates.order_by("pk")) == list(expected)
def test_can_combine_with_or(factories): artist1 = factories["music.Artist"]() artist2 = factories["music.Artist"]() artist3 = factories["music.Artist"]() factories["music.Track"].create_batch(3, artist=artist1) factories["music.Track"].create_batch(3, artist=artist2) factories["music.Track"].create_batch(3, artist=artist3) expected = Track.objects.exclude(artist=artist3).order_by("pk") f = [ { "type": "artist", "ids": [artist1.pk] }, { "type": "artist", "ids": [artist2.pk], "operator": "or" }, ] candidates = filters.run(f) assert list(candidates.order_by("pk")) == list(expected)
def test_can_combine_with_and(factories): artist1 = factories["music.Artist"]() artist2 = factories["music.Artist"]() metal_tracks = factories["music.Track"].create_batch(2, artist=artist1, tags=["metal"]) factories["music.Track"].create_batch(2, artist=artist1, tags=["pop"]) factories["music.Track"].create_batch(3, artist=artist2) expected = metal_tracks f = [ { "type": "artist", "ids": [artist1.pk] }, { "type": "tag", "names": ["metal"], "operator": "and" }, ] candidates = filters.run(f) assert list(candidates.order_by("pk")) == list(expected)