Skip to content

Commit

Permalink
Add failing tests for FileField support
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobwegner committed May 13, 2024
1 parent ca4e797 commit f17a9bb
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
15 changes: 15 additions & 0 deletions tests/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,18 @@ class Comment(models.Model):

def __str__(self):
return self.text


class Attachment(models.Model):
post = models.ForeignKey(
Post,
on_delete=models.CASCADE,
limit_choices_to={"is_published": True},
related_name="attachments",
)
file = models.FileField()

i18n = TranslationField(fields=("file",))

def __str__(self):
return self.file.name
33 changes: 33 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django import VERSION as DJANGO_VERSION
from django.core.exceptions import ValidationError
from django.core.files.base import ContentFile
from django.db import DataError, models, transaction
from django.test import TestCase, override_settings
from django.utils.translation import override
Expand All @@ -8,11 +9,13 @@

from .app.models import (
Article,
Attachment,
Blog,
Challenge,
ChallengeContent,
ChildArticle,
NullableTextModel,
Post,
TextModel,
)
from .utils import CreateTestModel
Expand Down Expand Up @@ -120,6 +123,36 @@ def test_fallback_getting_TextField(self):
with override("fr"):
self.assertEqual(m.description_i18n, DESCRIPTION)

def test_fallback_getting_FileField(self):
post = Post.objects.create(title="Test Post", is_published=True)
sample_file = ContentFile("sample content", name="sample-en.txt")
attachment = Attachment.objects.create(post=post, file=sample_file)
default_file_name = attachment.file.name
assert default_file_name

with override("fr"):
self.assertIsInstance(attachment.file_i18n, models.fields.files.FieldFile)
self.assertEqual(attachment.file_i18n.name, default_file_name)

def test_set_FileField(self):
post = Post.objects.create(title="Test Post", is_published=True)

en_content = "sample content"
fr_content = "exemple de contenu"
sample_file_en = ContentFile(en_content, name="sample-en.txt")
sample_file_fr = ContentFile(fr_content, name="sample-fr.txt")

attachment = Attachment.objects.create(
post=post, file=sample_file_en, file_fr=sample_file_fr
)

saved_fr_content = attachment.file_fr.read().decode("utf-8")
self.assertEqual(saved_fr_content, fr_content)
self.assertIsInstance(attachment.file_fr, models.fields.files.FieldFile)

with override("fr"):
self.assertEqual(attachment.file_i18n, attachment.file_fr)

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 @@ -45,6 +45,7 @@ def test_get_translated_models(self):
app_models.Challenge,
app_models.ChallengeContent,
app_models.Post,
app_models.Attachment,
app_models.Comment,
}
self.assertEqual(set(get_translated_models("app")), expected)
Expand Down

0 comments on commit f17a9bb

Please sign in to comment.