django-parlor

Django model translations with even less nasty hacks.
git clone https://git.ce9e.org/django-parlor.git

commit
6421ce00c6cd47b9062906cf5966fcc9b4a4ba5a
parent
f19ab778d10a267c6dd105264d38d8cb4d5a78a4
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2025-08-07 06:10
setup tests

Diffstat

M .github/workflows/main.yml 21 ++++++++++++++++++++-
A tests/__init__.py 0
A tests/models.py 19 +++++++++++++++++++
A tests/settings.py 14 ++++++++++++++
A tests/tests.py 35 +++++++++++++++++++++++++++++++++++

5 files changed, 88 insertions, 1 deletions


diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml

@@ -9,8 +9,27 @@ jobs:
    9     9     - name: linters
   10    10       run: |
   11    11         ruff check parlor
   -1    12   test:
   -1    13     runs-on: ubuntu-latest
   -1    14     strategy:
   -1    15       matrix:
   -1    16         include:
   -1    17           - python: '3.10'
   -1    18             django: '4.2'
   -1    19           - python: '3.13'
   -1    20             django: '5.2'
   -1    21     steps:
   -1    22     - uses: actions/checkout@v4
   -1    23     - uses: actions/setup-python@v5
   -1    24       with:
   -1    25         python-version: ${{ matrix.python }}
   -1    26     - run: pip install . coverage "django==${{ matrix.django }}"
   -1    27     - name: tests
   -1    28       run: |
   -1    29         coverage run -m django test --settings tests.settings
   -1    30         coverage report
   12    31   publish:
   13    -1     needs: [lint]
   -1    32     needs: [lint, test]
   14    33     if: startsWith(github.ref, 'refs/tags')
   15    34     runs-on: ubuntu-latest
   16    35     permissions:

diff --git a/tests/__init__.py b/tests/__init__.py

diff --git a/tests/models.py b/tests/models.py

@@ -0,0 +1,19 @@
   -1     1 from django.contrib import admin
   -1     2 from django.db import models
   -1     3 
   -1     4 from parlor.admin import TranslatableAdmin
   -1     5 from parlor.models import TranslatableModel
   -1     6 
   -1     7 
   -1     8 class MyModel(TranslatableModel):
   -1     9     pass
   -1    10 
   -1    11 
   -1    12 class MyModelTranslation(models.Model):
   -1    13     parent = MyModel.get_parent_field()
   -1    14     language_code = MyModel.get_language_field()
   -1    15 
   -1    16     label = models.CharField(max_length=32)
   -1    17 
   -1    18     class Meta:
   -1    19         unique_together = [('parent', 'language_code')]

diff --git a/tests/settings.py b/tests/settings.py

@@ -0,0 +1,14 @@
   -1     1 SECRET_KEY = "fake-key"
   -1     2 
   -1     3 DATABASES = {
   -1     4     'default': {
   -1     5         'ENGINE': 'django.db.backends.sqlite3',
   -1     6         'NAME': ':memory:',
   -1     7     }
   -1     8 }
   -1     9 
   -1    10 DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
   -1    11 
   -1    12 INSTALLED_APPS = [
   -1    13     'tests',
   -1    14 ]

diff --git a/tests/tests.py b/tests/tests.py

@@ -0,0 +1,35 @@
   -1     1 from django.test import TestCase
   -1     2 from django.utils import translation
   -1     3 
   -1     4 from .models import MyModel
   -1     5 
   -1     6 
   -1     7 class TranslatableModelTests(TestCase):
   -1     8     def test_translation(self):
   -1     9         animal = MyModel.objects.create()
   -1    10         animal.translations.create(language_code='en', label='Frog')
   -1    11 
   -1    12         with translation.override('en'):
   -1    13             self.assertEqual(animal.label, 'Frog')
   -1    14 
   -1    15     def test_override(self):
   -1    16         animal = MyModel.objects.create()
   -1    17         animal.translations.create(language_code='en', label='Frog')
   -1    18         animal.translations.create(language_code='de', label='Frosch')
   -1    19 
   -1    20         with translation.override('en'):
   -1    21             self.assertEqual(animal.label, 'Frog')
   -1    22         with translation.override('de'):
   -1    23             self.assertEqual(animal.label, 'Frosch')
   -1    24 
   -1    25     def test_fallback(self):
   -1    26         animal = MyModel.objects.create()
   -1    27 
   -1    28         with translation.override('en'):
   -1    29             self.assertEqual(animal.label, 'not translated')
   -1    30 
   -1    31     def test_non_field(self):
   -1    32         animal = MyModel.objects.create()
   -1    33 
   -1    34         with self.assertRaises(AttributeError):
   -1    35             animal.does_not_exist