def test_order_by(self):
        """Tests whether ordering by a :see:LocalizedIntegerField key works
        expected."""

        # using key transforms (score__en) in order_by(..) is only
        # supported since Django 2.1
        # https://github.com/django/django/commit/2162f0983de0dfe2178531638ce7ea56f54dd4e7#diff-0edd853580d56db07e4020728d59e193

        if django.VERSION < (2, 1):
            return

        model = get_fake_model({
            "score":
            LocalizedIntegerField(default={settings.LANGUAGE_CODE: 1337},
                                  null=True)
        })

        model.objects.create(score=dict(en=982))
        model.objects.create(score=dict(en=382))
        model.objects.create(score=dict(en=1331))

        res = list(
            model.objects.values_list("score__en",
                                      flat=True).order_by("-score__en"))
        assert res == [1331, 982, 382]
    def test_callable_default_value(self):
        output = {"en": 5}

        def func():
            return output

        model = get_fake_model({"test": LocalizedIntegerField(default=func)})
        obj = model.objects.create()

        assert obj.test["en"] == output["en"]
    def test_completely_optional(self):
        """Tests whether having all languages optional works properly."""

        model = get_fake_model({
            "score":
            LocalizedIntegerField(null=True, required=[], blank=True)
        })

        obj = model()
        obj.save()

        for lang_code, _ in settings.LANGUAGES:
            assert getattr(obj.score, lang_code) is None
    def test_default_value_update(self):
        """Tests whether a default is properly set when specified during
        updates."""

        model = get_fake_model({
            "score":
            LocalizedIntegerField(default={settings.LANGUAGE_CODE: 75},
                                  null=True)
        })

        obj = model.objects.create(
            score=LocalizedIntegerValue({settings.LANGUAGE_CODE: 35}))
        assert obj.score.get(settings.LANGUAGE_CODE) == 35

        model.objects.update(
            score=LocalizedIntegerValue({settings.LANGUAGE_CODE: None}))
        obj.refresh_from_db()
        assert obj.score.get(settings.LANGUAGE_CODE) == 75
    def test_default_value(self):
        """Tests whether a default is properly set when specified."""

        model = get_fake_model({
            "score":
            LocalizedIntegerField(default={settings.LANGUAGE_CODE: 75})
        })

        obj = model.objects.create()
        assert obj.score.get(settings.LANGUAGE_CODE) == 75

        obj = model()
        for lang_code, _ in settings.LANGUAGES:
            obj.score.set(lang_code, None)
        obj.save()

        for lang_code, _ in settings.LANGUAGES:
            if lang_code == settings.LANGUAGE_CODE:
                assert obj.score.get(lang_code) == 75
            else:
                assert obj.score.get(lang_code) is None
    def setUpClass(cls):
        super().setUpClass()

        cls.TestModel = get_fake_model({"score": LocalizedIntegerField()})