Beispiel #1
0
    def test_replacer_by_fixed_value01(self):
        civ = FakeCivility.objects.create(title='Kun')

        model_field = FakeContact._meta.get_field('civility')
        replacer1 = FixedValueReplacer(model_field=model_field, value=civ)
        self.assertEqual(model_field, replacer1.model_field)
        self.assertEqual(civ, replacer1._fixed_value)

        serialized = {
            'ctype': ContentType.objects.get_for_model(FakeContact).natural_key(),
            'field': 'civility',
            'pk':    civ.pk,
        }
        self.assertEqual(serialized, replacer1.as_dict())

        replacer2 = FixedValueReplacer.from_dict(serialized)
        self.assertIsInstance(replacer2, FixedValueReplacer)
        self.assertEqual(model_field,    replacer2.model_field)
        self.assertEqual(civ,            replacer2.get_value())

        self.assertEqual(
            _('In «{model} - {field}», replace by «{new}»').format(
                model='Test Contact',
                field=_('Civility'),
                new=civ.title,
            ),
            str(replacer1),
        )
Beispiel #2
0
    def test_replacer_by_fixed_value02(self):
        "<None> value + other ContentType."
        model_field = FakeOrganisation._meta.get_field('sector')
        replacer1 = FixedValueReplacer(model_field=model_field, value=None)

        serialized = {
            'ctype': ContentType.objects.get_for_model(FakeOrganisation).natural_key(),
            'field': 'sector',
        }
        self.assertEqual(serialized, replacer1.as_dict())

        replacer2 = FixedValueReplacer.from_dict(serialized)
        self.assertIsInstance(replacer2, FixedValueReplacer)
        self.assertEqual(model_field, replacer2.model_field)
        self.assertIsNone(replacer2.get_value())

        self.assertEqual(
            _('Empty «{model} - {field}»').format(
                model='Test Organisation',
                field=_('Sector'),
            ),
            str(replacer1),
        )