def test_assignment(self): en_new_title = "New title" de_new_title = "Neuer titel" with set_field_language("en"): self.book.title = en_new_title with set_field_language("de"): self.book.title = "Neuer titel" self.book.save() self.book.refresh_from_db() with set_field_language("en"): self.assertEqual(self.book.title, en_new_title) self.assertNotEqual(self.book.title, de_new_title) with set_field_language("de"): self.assertEqual(self.book.title, de_new_title) self.assertNotEqual(self.book.title, en_new_title) self.assertEqual( self.book.translations.title, { "en": en_new_title, "de": de_new_title, }, )
def test_trigram(self): books = Book.objects.all() # Testing a regular field to make sure trigrams are enabled similar = similar_qs(books, "author", "I. R. Mice") self.assertTrue(similar.filter(similarity__gt=0.3).exists()) self.assertTrue(books.filter(author__trigram_similar="nice").exists()) # Testing a translation field to make sure trigrams work with set_field_language("en"): self.assertTrue( books.filter(title__trigram_similar="bood").exists()) self.assertFalse( books.filter(title__trigram_similar="buck").exists()) self.assertFalse( books.filter(title__trigram_similar="ein").exists()) self.assertTrue(lang_similar_qs(books, "title", "bood").exists()) # We can't get this to 0 because there is some overlap between German and English self.assertTrue(0.2 > lang_similar_qs( books, "title", "ein gut buck").first().similarity) with set_field_language("de"): self.assertFalse( books.filter(title__trigram_similar="bood").exists()) self.assertTrue(books.filter(title__icontains="buch").exists()) self.assertTrue( books.filter(title__trigram_similar="ein buck").exists()) self.assertTrue( books.filter(title__trigram_similar="eime buch").exists())
def test_max_length_validation(self): with set_field_language("en"): self.book.title = "A short value" with set_field_language("de"): self.book.title = "A long value " + ("=" * 350) with self.assertRaises(ValidationError): self.book.clean_fields()
def test_search(self): books = Book.objects.all() with set_field_language("en"): self.assertTrue(books.filter(title__search="book").exists()) self.assertFalse(books.filter(title__search="buch").exists()) with set_field_language("de"): self.assertFalse(books.filter(title__search="book").exists()) self.assertTrue(books.filter(title__search="buch").exists())
def test_iregex(self): books = Book.objects.all() with set_field_language("en"): self.assertTrue(books.filter(title__iregex="^A.+book$").exists()) self.assertTrue(books.filter(title__iregex="^A.+BOOK$").exists()) self.assertFalse(books.filter(title__iregex="^Ei.+buch$").exists()) with set_field_language("de"): self.assertFalse(books.filter(title__iregex="^A.+book$").exists()) self.assertTrue(books.filter(title__iregex="^Ei.+buch$").exists())
def test_iendswith(self): books = Book.objects.all() with set_field_language("en"): self.assertTrue(books.filter(title__iendswith="book").exists()) self.assertTrue(books.filter(title__iendswith="BOOK").exists()) self.assertFalse(books.filter(title__iendswith="BUCH").exists()) with set_field_language("de"): self.assertFalse(books.filter(title__iendswith="BOOK").exists()) self.assertTrue(books.filter(title__iendswith="BUCH").exists())
def test_istartswith(self): books = Book.objects.all() with set_field_language("en"): self.assertTrue(books.filter(title__istartswith="A go").exists()) self.assertTrue(books.filter(title__istartswith="a go").exists()) self.assertFalse(books.filter(title__istartswith="Eine").exists()) with set_field_language("de"): self.assertFalse(books.filter(title__istartswith="a go").exists()) self.assertTrue(books.filter(title__istartswith="eine g").exists())
def test_trigram_similarity(self): books = Book.objects.all() with set_field_language("en"): en_similarity = (lang_similar_qs( books, "title", "eine gut buck").first().similarity) with set_field_language("de"): de_similarity = (lang_similar_qs( books, "title", "eine gut buck").first().similarity) self.assertTrue(de_similarity > en_similarity)
def test_dict_assignment(self): data = {"en": "Stuff", "de": "Zeug"} with set_field_language("en"): self.book.title = data self.book.save() self.book.refresh_from_db() self.assertEqual(self.book.title, data["en"]) with set_field_language("de"): self.assertEqual(self.book.title, data["de"])
def test_icontains(self): books = Book.objects.all() with set_field_language("en"): self.assertTrue(books.filter(title__icontains="good").exists()) self.assertFalse(books.filter(title__icontains="gut").exists()) self.assertTrue(books.filter(title__icontains="GOOD").exists()) with set_field_language("de"): self.assertFalse(books.filter(title__icontains="good").exists()) self.assertTrue(books.filter(title__icontains="gut").exists()) self.assertTrue(books.filter(title__icontains="GUT").exists())
def test_reverse_fails_if_wrong_langauge(self): """Test that reversing migration in a different language fails""" with set_field_language("en"): self.nice.author = json.dumps({"en": self.nice.author}) self.nice.save() self.bad.author = json.dumps({"en": self.bad.author}) self.bad.save() migration = get_migration("library_app", {"book": ["author"]}) with set_field_language("de"): with self.assertRaises(KeyError): migration.reverse_code(apps, None)
def test_contains(self): books = Book.objects.all() with set_field_language("en"): self.assertTrue(books.filter(title__contains="good").exists()) self.assertFalse(books.filter(title__contains="gut").exists()) # An inexact match shouldn't be returned # But it is. This isn't a deal breaker right now :/ self.assertFalse(books.filter(title__contains="GOOD").exists()) self.assertFalse(books.filter(title__contains="GUT").exists()) with set_field_language("de"): self.assertFalse(books.filter(title__contains="good").exists()) self.assertTrue(books.filter(title__contains="gut").exists())
def test_values(self): books = Book.objects.all() with set_field_language("en"): self.assertEqual("A good book", books.values("title__en")[0]["title__en"]) self.assertEqual("A good book", books.values("title")[0]["title"]) with set_field_language("de"): self.assertEqual("Eine gut buch", books.values("title")[0]["title"]) self.assertEqual("Eine gut buch", books.values("title")[0]["title"]) self.assertEqual("A good book", books.values("title__en")[0]["title__en"])
def test_iexact(self): books = Book.objects.all() with set_field_language("en"): self.assertTrue( books.filter(title__iexact=self.book_data["title"] ["en"].upper()).exists()) self.assertFalse( books.filter(title__iexact=self.book_data["title"] ["de"].upper()).exists()) with set_field_language("de"): self.assertFalse( books.filter(title__iexact=self.book_data["title"] ["en"].upper()).exists()) self.assertTrue( books.filter(title__iexact=self.book_data["title"] ["de"].upper()).exists())
def test_validate_bad_code(self): """Test that validation prevents saving not selected code""" # Try to save in swedish with set_field_language("sv"): self.book.title = "Swedish title" with self.assertRaises(ValidationError): self.book.clean_fields()
def test_exact_mysql(self): books = Book.objects.all() with set_field_language("en"): self.assertTrue( books.filter(title=self.book_data["title"]["en"]).exists()) self.assertFalse( books.filter(title=self.book_data["title"]["de"]).exists()) # An inexact match shouldn't be returned as true, but is # We have this test so if this changes we will know. self.assertTrue( books.filter( title=self.book_data["title"]["en"].upper()).exists()) with set_field_language("de"): self.assertFalse( books.filter(title=self.book_data["title"]["en"]).exists()) self.assertTrue( books.filter(title=self.book_data["title"]["de"]).exists())
def test_qs_create_from_dict(self): book = Book.objects.create(**book_data) self.assertEqual(book.translations.title, book_data["title"]) self.assertNotEqual(book.translations.description, book_data["description"]) self.assertEqual( book.translations.description, {"en": book_data["description"]} ) with set_field_language("en"): self.assertEqual(book.title, book_data["title"]["en"]) self.assertEqual(book.description, book_data["description"])
def test_exact(self): books = Book.objects.all() with set_field_language("en"): self.assertTrue( books.filter(title__en=self.book_data["title"]["en"]).exists()) self.assertTrue( books.filter(title=self.book_data["title"]["en"]).exists()) self.assertFalse( books.filter(title=self.book_data["title"]["de"]).exists()) # An inexact match won't be returned as true self.assertFalse( books.filter( title=self.book_data["title"]["en"].upper()).exists()) self.assertFalse(books.filter(title="A GOoD bOoK").exists()) with set_field_language("de"): self.assertFalse( books.filter(title=self.book_data["title"]["en"]).exists()) self.assertTrue( books.filter(title=self.book_data["title"]["de"]).exists())
def setUp(self): with set_field_language("en"): self.book = Book.objects.create( title="book", author="Book guy", description="cool book", category={"data": { "is": "nested", }}, number_of_pages=1000, )
def test_update(self): """Test queryset .update on translated field with dict""" # Have to update with a dict instead of assigning a string with a language selected # as .update does a SQL UPDATE directly new_title = {"en": "New English Title", "de": "Eine Gut Buch"} Book.objects.filter(pk=self.book.pk).update(title=new_title) self.book.refresh_from_db() with set_field_language("en"): self.assertEqual(self.book.title, "New English Title") self.assertEqual(self.book.title_tsall, new_title)
def setUp(self): with set_field_language("en"): self.book = Book.objects.create( title={ "en": "A good book", "de": "Eine Gut Buch", }, author="Someone", description="Its ok i guess", category={"dewey": 123}, number_of_pages=100, )
def test_f_lookup(self): from garnett.expressions import LangF as F from django.db.models.functions import Upper self.book_data = dict( title={ "en": "Mr. Bob Bobbertson", "de": "Herr Bob Bobbertson", }, author="Mr. Bob Bobbertson", description= "Mr. Bob Bobbertson's amazing self-titled autobiography", category={ "dewey": 222, "subject": "Mr. Bob Bobbertson", }, number_of_pages=100, ) Book.objects.create(**self.book_data) books = Book.objects.all() self.assertTrue( # Author match books.filter(description__istartswith=F("author")).exists()) with set_field_language("en"): annotated = books.annotate(en_title=F("title"))[0] self.assertEqual(annotated.title, annotated.en_title) annotated = books.annotate(en_title=F("title__en"))[0] self.assertEqual(annotated.title, annotated.en_title) self.assertTrue( # Author=Title match books.filter(author=F("title")).exists()) self.assertTrue( # Title=Author match books.filter(title__en__iexact=F("author")).exists()) self.assertTrue( # Title=Author match books.filter(title__exact=F("author")).exists()) self.assertTrue( # Title=Author match books.filter(title=F("author")).exists()) self.assertFalse( # Title=Author match books.filter(title=Upper(F("author"))).exists()) self.assertTrue( # Title=Author match books.filter(title__en__iexact=F("author")).exists()) self.assertTrue( # Title=Author match books.filter(title__en__exact=F("author")).exists()) self.assertTrue( # Description matches Author books.filter(description__istartswith=F("author")).exists()) self.assertTrue( # Description en matches Author books.filter( description__en__istartswith=F("author")).exists()) self.assertTrue( # Description starts with Title books.filter(description__istartswith=F("title")).exists())
def test_nesting_context(self): with set_field_language("de"): book = Book.objects.create(**book_data.copy()) with set_field_language("en"): with set_field_language("de"): book.title = "de-title" with set_field_language("en"): book.description = "en-description" book.description = "de-description" book.title = "en-title" self.assertEqual(book.translations.title, {"en": "en-title", "de": "de-title"}) self.assertEqual( book.translations.description, {"en": "en-description", "de": "de-description"}, ) book.save() book.refresh_from_db() self.assertEqual(book.translations.title, {"en": "en-title", "de": "de-title"}) self.assertEqual( book.translations.description, {"en": "en-description", "de": "de-description"}, )
def setUp(self): with set_field_language("en"): self.book = Book.objects.create( title={ "en": "Testing for dummies", "de": "Testen auf Dummies", }, author="For dummies", description={ "en": "Testing but for dummies", "de": "Testen aber für Dummies", }, category={"cat": "book"}, number_of_pages=2, )
def setUp(self): with set_field_language("en"): self.nice = Book.objects.create( title="Nice book", author="Nice", description="Nice", category={}, number_of_pages=100, ) self.bad = Book.objects.create( title="Bad book", author="Bad", description="Bad", category={}, number_of_pages=100, )
def test_qs_create_from_strings(self): book_data = dict( title="A good book", author="I. M. Nice", description="A book on how to be good, and stuff", category={"dewey": 222}, number_of_pages=100, ) book = Book.objects.create(**book_data) self.assertNotEqual(book.translations.title, book_data["title"]) self.assertNotEqual(book.translations.description, book_data["description"]) self.assertEqual(book.translations.title, {"en": book_data["title"]}) self.assertEqual( book.translations.description, {"en": book_data["description"]} ) with set_field_language("en"): self.assertEqual(book.title, book_data["title"]) self.assertEqual(book.description, book_data["description"])
def test_bulk_update(self): """Test bulk_update on a translated field""" with set_field_language("en"): # Create second book book2 = Book.objects.create( title={ "en": "A bad book", "de": "Eine Gut Buch", }, author="Someone", description="Its not very good", category={"dewey": 123}, number_of_pages=100, ) self.assertEqual(Book.objects.count(), 2) # Bulk update title on the 2 books updated = [] for book in Book.objects.all(): book.title = "New English Title" updated.append(book) Book.objects.bulk_update(updated, ["title"]) # Check that just the english title was updated self.book.refresh_from_db() self.assertEqual( self.book.title_tsall, { "en": "New English Title", "de": "Eine Gut Buch" }, ) book2.refresh_from_db() self.assertEqual( book2.title_tsall, { "en": "New English Title", "de": "Eine Gut Buch" }, )
def test_language_default_function(self): """Test that dict is correct when inner default is function""" with set_field_language("fr"): book = DefaultBook.objects.create(number_of_pages=100) self.assertEqual(book.author, "John Jimson") self.assertEqual(book.author_tsall, {"fr": "John Jimson"})
def test_language_default(self): """Test that default creates dict using current language""" with set_field_language("fr"): book = DefaultBook.objects.create(number_of_pages=100) self.assertEqual(book.title, "DEFAULT TITLE") self.assertEqual(book.title_tsall, {"fr": "DEFAULT TITLE"})
def test_setter_validate_wrong_type(self): with set_field_language("en"): with self.assertRaises(TypeError): self.book.title = 700