def test_str_fallback(): """Tests whether the :see:LocalizedValue class's __str__'s fallback functionality works properly.""" test_value = 'myvalue' localized_value = LocalizedValue({settings.LANGUAGE_CODE: test_value}) other_language = settings.LANGUAGES[-1][0] # make sure that, by default it returns # the value in the default language assert str(localized_value) == test_value # make sure that it falls back to the # primary language when there's no value # available in the current language translation.activate(other_language) assert str(localized_value) == test_value # make sure that it's just __str__ falling # back and that for the other language # there's no actual value assert localized_value.get(other_language) != test_value
def test_get_explicit(): """Tests whether the the :see:LocalizedValue class's :see:get function works properly when specifying an explicit value.""" keys = get_init_values() localized_value = LocalizedValue(keys) for language, value in keys.items(): assert localized_value.get(language) == value
def test_set(): """Tests whether the :see:LocalizedValue class's see:set function works properly.""" localized_value = LocalizedValue() for language, value in get_init_values(): localized_value.set(language, value) assert localized_value.get(language) == value assert getattr(localized_value, language) == value
def test_get_default_language(): """Tests whether the :see:LocalizedValue class's see:get function properly gets the value in the default language.""" keys = get_init_values() localized_value = LocalizedValue(keys) for language, _ in keys.items(): translation.activate(language) assert localized_value.get() == keys[settings.LANGUAGE_CODE]
def test_init_array(): """Tests whether the __init__ function of :see:LocalizedValue properly handles an array. Arrays can be passed to LocalizedValue as a result of a ArrayAgg operation. """ value = LocalizedValue(["my value"]) assert value.get(settings.LANGUAGE_CODE) == "my value"
def test_decompress(): """Tests whether a :see:LocalizedValue instance can correctly be "decompressed" over the available widgets.""" localized_value = LocalizedValue() for lang_code, lang_name in settings.LANGUAGES: localized_value.set(lang_code, lang_name) widget = LocalizedFieldWidget() decompressed_values = widget.decompress(localized_value) for (lang_code, _), value in zip(settings.LANGUAGES, decompressed_values): assert localized_value.get(lang_code) == value
def test_construct_string(): """Tests whether the :see:LocalizedValue's constructor assumes the primary language when passing a single string.""" value = LocalizedValue("beer") assert value.get(settings.LANGUAGE_CODE) == "beer"