Revert "Revert "Fix #994: use PostgreSQL full-text-search""
This reverts commit 7b0db234e2.
This commit is contained in:
parent
c2cb510eb9
commit
57949c02c1
14 changed files with 368 additions and 38 deletions
50
api/tests/music/test_triggers.py
Normal file
50
api/tests/music/test_triggers.py
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
import pytest
|
||||
|
||||
from django.db import connection
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"factory_name,fields",
|
||||
[
|
||||
("music.Artist", ["name"]),
|
||||
("music.Album", ["title"]),
|
||||
("music.Track", ["title"]),
|
||||
],
|
||||
)
|
||||
def test_body_text_trigger_creation(factory_name, fields, factories):
|
||||
obj = factories[factory_name]()
|
||||
obj.refresh_from_db()
|
||||
cursor = connection.cursor()
|
||||
sql = """
|
||||
SELECT to_tsvector('{indexed_text}')
|
||||
""".format(
|
||||
indexed_text=" ".join([getattr(obj, f) for f in fields if getattr(obj, f)]),
|
||||
)
|
||||
cursor.execute(sql)
|
||||
|
||||
assert cursor.fetchone()[0] == obj.body_text
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"factory_name,fields",
|
||||
[
|
||||
("music.Artist", ["name"]),
|
||||
("music.Album", ["title"]),
|
||||
("music.Track", ["title"]),
|
||||
],
|
||||
)
|
||||
def test_body_text_trigger_updaten(factory_name, fields, factories, faker):
|
||||
obj = factories[factory_name]()
|
||||
for field in fields:
|
||||
setattr(obj, field, faker.sentence())
|
||||
obj.save()
|
||||
obj.refresh_from_db()
|
||||
cursor = connection.cursor()
|
||||
sql = """
|
||||
SELECT to_tsvector('{indexed_text}')
|
||||
""".format(
|
||||
indexed_text=" ".join([getattr(obj, f) for f in fields if getattr(obj, f)]),
|
||||
)
|
||||
cursor.execute(sql)
|
||||
|
||||
assert cursor.fetchone()[0] == obj.body_text
|
||||
|
|
@ -1193,3 +1193,48 @@ def test_get_upload_audio_metadata(logged_in_api_client, factories):
|
|||
assert response.status_code == 200
|
||||
assert serializer.is_valid(raise_exception=True) is True
|
||||
assert response.data == serializer.validated_data
|
||||
|
||||
|
||||
@pytest.mark.parametrize("use_fts", [True, False])
|
||||
def test_search_get(use_fts, settings, logged_in_api_client, factories):
|
||||
settings.USE_FULL_TEXT_SEARCH = use_fts
|
||||
artist = factories["music.Artist"](name="Foo Fighters")
|
||||
album = factories["music.Album"](title="Foo Bar")
|
||||
track = factories["music.Track"](title="Foo Baz")
|
||||
tag = factories["tags.Tag"](name="Foo")
|
||||
|
||||
factories["music.Track"]()
|
||||
factories["tags.Tag"]()
|
||||
|
||||
url = reverse("api:v1:search")
|
||||
expected = {
|
||||
"artists": [serializers.ArtistWithAlbumsSerializer(artist).data],
|
||||
"albums": [serializers.AlbumSerializer(album).data],
|
||||
"tracks": [serializers.TrackSerializer(track).data],
|
||||
"tags": [views.TagSerializer(tag).data],
|
||||
}
|
||||
response = logged_in_api_client.get(url, {"q": "foo"})
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.data == expected
|
||||
|
||||
|
||||
def test_search_get_fts_advanced(settings, logged_in_api_client, factories):
|
||||
settings.USE_FULL_TEXT_SEARCH = True
|
||||
artist1 = factories["music.Artist"](name="Foo Bighters")
|
||||
artist2 = factories["music.Artist"](name="Bar Fighter")
|
||||
factories["music.Artist"]()
|
||||
|
||||
url = reverse("api:v1:search")
|
||||
expected = {
|
||||
"artists": serializers.ArtistWithAlbumsSerializer(
|
||||
[artist2, artist1], many=True
|
||||
).data,
|
||||
"albums": [],
|
||||
"tracks": [],
|
||||
"tags": [],
|
||||
}
|
||||
response = logged_in_api_client.get(url, {"q": '"foo | bar"'})
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.data == expected
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue