def setUp(self):
        birds = Category.objects.create(name='Birds', name_nl='Vogels')
        amphibians = Category.objects.create(name='Amphibians',
                                             name_nl='Amfibiën')
        reptiles = Category.objects.create(name='Reptiles')

        Blog.objects.bulk_create([
            Blog(title='Falcon', title_nl='Valk', category=birds),
            Blog(title='Frog', title_nl='Kikker', category=amphibians),
            Blog(title='Gecko', category=reptiles),
        ])
    def test_get_has_no_translation(self):
        m = Blog(title='Falcon', i18n={'title_nl': 'Valk', 'title_de': 'Falk'})

        # Fallback to base langauge
        with override('fr'):
            self.assertEquals(m.title_i18n, 'Falcon')

        # other translations are still there.
        self.assertEquals(m.title_nl, 'Valk')
        self.assertEquals(m.title_de, 'Falk')
    def test_get_has_no_translation(self):
        m = Blog(title="Falcon", i18n={"title_nl": "Valk", "title_de": "Falk"})

        # Fallback to base langauge
        with override("fr"):
            self.assertEqual(m.title_i18n, "Falcon")

        # other translations are still there.
        self.assertEqual(m.title_nl, "Valk")
        self.assertEqual(m.title_de, "Falk")
    def test_get_active_language(self):
        m = Blog(title='Falcon', i18n={'title_nl': 'Valk', 'title_de': 'Falk'})

        with override('nl'):
            # value for the active language
            self.assertEquals(m.title_i18n, 'Valk')

            self.assertEquals(m.title_en, 'Falcon')
            self.assertEquals(m.title_de, 'Falk')

        with override('de'):
            self.assertEquals(m.title_i18n, 'Falk')
    def test_clean(self):
        m = Blog(title='Horse', body='Horses are nice')

        with self.assertRaises(ValidationError) as e:
            m.full_clean()

        self.assertEquals(list(e.exception),
                          [('title_nl', ['This field cannot be null.'])])

        # With an added `title_nl`, it should validate.
        m.title_nl = 'Paard'
        m.full_clean()
    def test_get_active_language(self):
        m = Blog(title="Falcon", i18n={"title_nl": "Valk", "title_de": "Falk"})

        with override("nl"):
            # value for the active language
            self.assertEqual(m.title_i18n, "Valk")

            self.assertEqual(m.title_en, "Falcon")
            self.assertEqual(m.title_de, "Falk")

        with override("de"):
            self.assertEqual(m.title_i18n, "Falk")
    def test_filtered_order_by(self):
        Blog.objects.bulk_create([
            Blog(title='Falcon', title_nl='Valk'),
            Blog(title='Frog', title_nl='Kikker'),
            Blog(title='Fox', title_nl='Vos'),
            Blog(title='Gecko'),
            Blog(title='Gerbil'),
            Blog(title='Vulture', title_nl='Gier')
        ])

        qs = Blog.objects.filter(title_en__contains='F').order_by('title_nl')
        self.assertEquals(key(qs, 'title_nl'), ['Kikker', 'Valk', 'Vos'])

        qs = Blog.objects.filter(title_en__contains='G').order_by('title_en')
        self.assertEquals(key(qs, 'title'), ['Gecko', 'Gerbil'])

        with override('nl'):
            qs = Blog.objects.filter(
                title_i18n__contains='G').order_by('title_i18n')
            self.assertEquals(key(qs, 'title_i18n'),
                              ['Gecko', 'Gerbil', 'Gier'])

        with override('en'):
            qs = Blog.objects.filter(
                title_i18n__contains='G').order_by('-title_i18n')

            self.assertEquals(key(qs, 'title_i18n'), ['Gerbil', 'Gecko'])
            self.assertTrue('annotation' not in str(qs.query))
    def test_clean(self):
        '''
        Blog has required_languages=('nl', ), so this should raise an error
        if `title_nl` is not set.
        '''
        m = Blog(title='Horse', body='Horses are nice')

        with self.assertRaises(ValidationError) as e:
            m.full_clean()

        self.assertEquals(
            {(field, tuple(errors)) for field, errors in e.exception},
            {
                ('title_nl', ('This field cannot be null.', )),
                ('body_nl', ('This field cannot be null.', ))
            }
        )

        # With an added `title_nl`, it should validate.
        m.title_nl = 'Paard'
        m.body_nl = 'foo'
        m.full_clean()
    def test_clean_required_languages_list(self):
        """
        Blog has required_languages=('nl', ), so this should raise an error
        if `title_nl` is not set.
        """
        m = Blog(title="Horse", body="Horses are nice")

        with self.assertRaises(ValidationError) as e:
            m.full_clean()

        self.assertEqual(
            {(field, tuple(errors))
             for field, errors in e.exception},
            {
                ("title_nl", ("This field cannot be null.", )),
                ("body_nl", ("This field cannot be null.", )),
            },
        )

        # With an added `title_nl`, it should validate.
        m.title_nl = "Paard"
        m.body_nl = "foo"
        m.full_clean()
    def test_get_non_translatable_field(self):
        m = Blog(title='Falcon')

        with self.assertRaisesMessage(AttributeError,
                                      "'Blog' object has no attribute 'foo'"):
            m.foo
    def test_model_constructor(self):
        b = Blog(title='Falcon', title_nl='Valk')
        b.save()

        self.assertEquals(b.title, 'Falcon')
        self.assertEquals(b.title_nl, 'Valk')
    def test_model_constructor(self):
        b = Blog(title="Falcon", title_nl="Valk")
        b.save()

        self.assertEqual(b.title, "Falcon")
        self.assertEqual(b.title_nl, "Valk")