Skip to content

Commit

Permalink
Add a test for JSONField support
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobwegner committed Jun 27, 2024
1 parent d86afe1 commit a7bf905
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
10 changes: 10 additions & 0 deletions tests/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ def __str__(self):
return self.title


class TaggedBlog(models.Model):
title = models.CharField(max_length=255)
tags = JSONField(null=True, blank=True, default=list)

i18n = TranslationField(fields=("title", "tags"))

def __str__(self):
return self.title


class Person(models.Model):
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
Expand Down
18 changes: 18 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
ChallengeContent,
ChildArticle,
NullableTextModel,
TaggedBlog,
TextModel,
)
from .utils import CreateTestModel
Expand Down Expand Up @@ -119,6 +120,23 @@ def test_fallback_getting_TextField(self):
with override("fr"):
self.assertEqual(m.description_i18n, DESCRIPTION)

def test_fallback_getting_JSONField(self):
m = TaggedBlog.objects.create(title="Falcon", tags=["bird", "raptor"])
with override("de"):
# tags_de is not set, return fallback
self.assertEqual(m.tags_i18n, ["bird", "raptor"])

m = TaggedBlog.objects.create(title="Falcon", tags_fr=[])
with override("fr"):
# tags_fr is set, return the empty list
self.assertEqual(m.tags_i18n, [])

m = TaggedBlog.objects.create(title="Falcon", tags_fr=None)
with override("fr"):
# tags_fr is set to None, return field default (which is
# an empty list)
self.assertEqual(m.tags_i18n, [])

def test_creating_using_virtual_default_language_field(self):
m = Blog.objects.create(title_en="Falcon")

Expand Down
1 change: 1 addition & 0 deletions tests/test_translating.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def test_get_translated_models(self):
expected = {
app_models.Article,
app_models.Blog,
app_models.TaggedBlog,
app_models.ChildArticle,
app_models.Category,
app_models.Person,
Expand Down

0 comments on commit a7bf905

Please sign in to comment.