测试不会改变语言

时间:2019-12-19 23:58:16

标签: django translation django-testing django-tests

我想用多语言测试我的模型,但是代码忽略了我对语言的更改。

我的models.py

class NameType(models.Model):
    ...
    class Meta:
        if get_language().split('-')[0] == 'it':
            print(get_language())  # called once at begin of test
            ordering = ['name_it']
        elif get_language().split('-')[0] == 'en':
            print(get_language())  # never called
            ordering = ['name_en']

我的test_models.py

class NameTypeTest(TestCase):
    def test_nametype_ordered(self):
        name = NameType.objects.create(name_en='Name', name_it='Nome')
        surname = NameType.objects.create(name_en='Surname', name_it='Cognome')
        activate('it')
        nametype_list = NameType.objects.all()
        self.assertEqual(nametype_list[0], surname)
        self.assertEqual(nametype_list[1], name)
        print(get_language())  # it
        activate('en')
        nametype_list = NameType.objects.all()
        # nametype_list = sorted(NameType.objects.all())  # I tried even this
        print(get_language())  # en
        self.assertEqual(nametype_list[0], name)  # here the test fails
        self.assertEqual(nametype_list[1], surname)
        print(get_language())  # never called, the test fail before
即使激活了nametype_list[0]

surname仍为en,而应该为name,因为name_en=namename_en=surname之前(n get_language()提供了正确的语言)。该模型的元数据仅被调用一次。

谢谢

0 个答案:

没有答案