def test_model_form_partially_translated(): activate("en") request = RequestFactory().get("/") test_name_en = "Test shop" payment_method = get_default_payment_method() payment_method.name = test_name_en payment_method.save() form = PaymentMethodForm(instance=payment_method, request=request, languages=settings.LANGUAGES) data = get_form_data(form, prepared=True) assert data.get("name__en") == test_name_en assert not data.get("name__fi") form = PaymentMethodForm(data=data, instance=payment_method, request=request, languages=settings.LANGUAGES) form.full_clean() assert form.is_valid() and not form.errors payment_method = form.save() # Add description for Finnish and and name in Finnish should be required data["description__fi"] = "Some description" form = PaymentMethodForm(data=data, instance=payment_method, request=request, languages=settings.LANGUAGES) form.full_clean() assert not form.is_valid() and form.errors test_name_fi = "Some method name in finnish" data["name__fi"] = test_name_fi form = PaymentMethodForm(data=data, instance=payment_method, request=request, languages=settings.LANGUAGES) form.full_clean() assert form.is_valid() and not form.errors payment_method = form.save() assert payment_method.name == test_name_en, "Object in English" activate("fi") payment_method.set_current_language("fi") assert payment_method.name == test_name_fi, "Object in Finnish" activate("ja") payment_method.set_current_language("ja") assert payment_method.name == test_name_en, "Should fallback to English" # Check that no sneaky translations is not created for Japan with pytest.raises(ObjectDoesNotExist): translation = payment_method.get_translation("ja") translation.refresh_from_db( ) # Just in case if the translation object comes from cache or something # Empty finnish translations and see if Finnish starts fallbacks too data["name__fi"] = data["description__fi"] = "" form = PaymentMethodForm(data=data, instance=payment_method, request=request, languages=settings.LANGUAGES) form.full_clean() assert form.is_valid() and not form.errors form.save() # Check that no sneaky translations is not created for Finnish with pytest.raises(ObjectDoesNotExist): translation = payment_method.get_translation("fi") translation.refresh_from_db( ) # Just in case if the translation object comes from cache or something
def test_model_form_partially_translated(rf, admin_user): with override_settings(**{"LANGUAGES": (("en", "en"), ("fi", "fi"), ("ja", "ja")), "PARLER_DEFAULT_LANGUAGE_CODE": "en"}): activate("en") get_default_shop() request = apply_request_middleware(rf.get("/"), user=admin_user) test_name_en = "Test shop" payment_method = get_default_payment_method() payment_method.name = test_name_en payment_method.save() form = PaymentMethodForm(instance=payment_method, request=request, languages=settings.LANGUAGES) data = get_form_data(form, prepared=True) assert data.get("name__en") == test_name_en assert not data.get("name__fi") form = PaymentMethodForm(data=data, instance=payment_method, request=request, languages=settings.LANGUAGES) form.full_clean() assert form.is_valid() and not form.errors payment_method = form.save() # Add description for Finnish and and name in Finnish should be required data["description__fi"] = "Some description" form = PaymentMethodForm(data=data, instance=payment_method, request=request, languages=settings.LANGUAGES) form.full_clean() assert not form.is_valid() and form.errors test_name_fi = "Some method name in finnish" data["name__fi"] = test_name_fi form = PaymentMethodForm(data=data, instance=payment_method, request=request, languages=settings.LANGUAGES) form.full_clean() assert form.is_valid() and not form.errors payment_method = form.save() assert payment_method.name == test_name_en, "Object in English" activate("fi") payment_method.set_current_language("fi") assert payment_method.name == test_name_fi, "Object in Finnish" activate("ja") payment_method.set_current_language("ja") assert payment_method.name == test_name_en, "Should fallback to English" # Check that no sneaky translations is not created for Japan with pytest.raises(ObjectDoesNotExist): translation = payment_method.get_translation("ja") translation.refresh_from_db() # Just in case if the translation object comes from cache or something # Empty finnish translations and see if Finnish starts fallbacks too data["name__fi"] = data["description__fi"] = "" form = PaymentMethodForm(data=data, instance=payment_method, request=request, languages=settings.LANGUAGES) form.full_clean() assert form.is_valid() and not form.errors form.save() # Check that no sneaky translations is not created for Finnish with pytest.raises(ObjectDoesNotExist): translation = payment_method.get_translation("fi") translation.refresh_from_db() # Just in case if the translation object comes from cache or something