2021-06-26 19:33:15 +00:00
|
|
|
""" django configuration of postgres """
|
2021-06-26 23:48:57 +00:00
|
|
|
from unittest.mock import patch
|
2021-06-26 19:33:15 +00:00
|
|
|
from django.test import TestCase
|
|
|
|
|
|
|
|
from bookwyrm import models
|
|
|
|
|
2021-06-26 20:20:23 +00:00
|
|
|
|
2021-11-12 17:17:00 +00:00
|
|
|
@patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async")
|
2021-06-26 19:33:15 +00:00
|
|
|
class PostgresTriggers(TestCase):
|
|
|
|
"""special migrations, fancy stuff ya know"""
|
|
|
|
|
2021-08-02 23:05:40 +00:00
|
|
|
def test_search_vector_on_create(self, _):
|
2021-06-26 19:33:15 +00:00
|
|
|
"""make sure that search_vector is being set correctly on create"""
|
|
|
|
book = models.Edition.objects.create(title="The Long Goodbye")
|
|
|
|
book.refresh_from_db()
|
|
|
|
self.assertEqual(book.search_vector, "'goodby':3A 'long':2A")
|
|
|
|
|
2021-08-02 23:05:40 +00:00
|
|
|
def test_search_vector_on_update(self, _):
|
2021-06-26 19:33:15 +00:00
|
|
|
"""make sure that search_vector is being set correctly on edit"""
|
|
|
|
book = models.Edition.objects.create(title="The Long Goodbye")
|
|
|
|
book.title = "The Even Longer Goodbye"
|
|
|
|
book.save(broadcast=False)
|
|
|
|
book.refresh_from_db()
|
|
|
|
self.assertEqual(book.search_vector, "'even':2A 'goodby':4A 'longer':3A")
|
|
|
|
|
2021-08-02 23:05:40 +00:00
|
|
|
def test_search_vector_fields(self, _):
|
2021-06-26 19:33:15 +00:00
|
|
|
"""use multiple fields to create search vector"""
|
|
|
|
author = models.Author.objects.create(name="The Rays")
|
|
|
|
book = models.Edition.objects.create(
|
|
|
|
title="The Long Goodbye",
|
|
|
|
subtitle="wow cool",
|
|
|
|
series="series name",
|
|
|
|
languages=["irrelevent"],
|
|
|
|
)
|
|
|
|
book.authors.add(author)
|
|
|
|
book.refresh_from_db()
|
|
|
|
self.assertEqual(
|
|
|
|
book.search_vector,
|
2021-06-27 02:07:09 +00:00
|
|
|
"'cool':5B 'goodby':3A 'long':2A 'name':9 'rays':7C 'seri':8 'the':6C 'wow':4B",
|
2021-06-26 19:33:15 +00:00
|
|
|
)
|
|
|
|
|
2021-08-02 23:05:40 +00:00
|
|
|
def test_seach_vector_on_author_update(self, _):
|
2021-06-26 19:33:15 +00:00
|
|
|
"""update search when an author name changes"""
|
|
|
|
author = models.Author.objects.create(name="The Rays")
|
|
|
|
book = models.Edition.objects.create(
|
|
|
|
title="The Long Goodbye",
|
|
|
|
)
|
|
|
|
book.authors.add(author)
|
|
|
|
author.name = "Jeremy"
|
|
|
|
author.save(broadcast=False)
|
|
|
|
book.refresh_from_db()
|
|
|
|
|
2021-06-27 02:07:09 +00:00
|
|
|
self.assertEqual(book.search_vector, "'goodby':3A 'jeremy':4C 'long':2A")
|
2021-06-26 19:33:15 +00:00
|
|
|
|
2021-08-02 23:05:40 +00:00
|
|
|
def test_seach_vector_on_author_delete(self, _):
|
2021-06-26 19:33:15 +00:00
|
|
|
"""update search when an author name changes"""
|
|
|
|
author = models.Author.objects.create(name="Jeremy")
|
|
|
|
book = models.Edition.objects.create(
|
|
|
|
title="The Long Goodbye",
|
|
|
|
)
|
|
|
|
|
|
|
|
book.authors.add(author)
|
|
|
|
book.refresh_from_db()
|
2021-06-27 02:07:09 +00:00
|
|
|
self.assertEqual(book.search_vector, "'goodby':3A 'jeremy':4C 'long':2A")
|
2021-06-26 19:33:15 +00:00
|
|
|
|
|
|
|
book.authors.remove(author)
|
|
|
|
book.refresh_from_db()
|
|
|
|
self.assertEqual(book.search_vector, "'goodby':3A 'long':2A")
|
2021-06-26 19:42:36 +00:00
|
|
|
|
2021-08-02 23:05:40 +00:00
|
|
|
def test_search_vector_stop_word_fallback(self, _):
|
2021-06-26 19:42:36 +00:00
|
|
|
"""use a fallback when removing stop words leads to an empty vector"""
|
|
|
|
book = models.Edition.objects.create(
|
|
|
|
title="there there",
|
|
|
|
)
|
|
|
|
book.refresh_from_db()
|
|
|
|
self.assertEqual(book.search_vector, "'there':1A,2A")
|