def test_simple_children_formset(): FormSet = formset_factory(SimpleVariationChildForm, SimpleVariationChildFormSet, extra=5, can_delete=True) parent = create_product(printable_gibberish()) child = create_product(printable_gibberish()) # No links yet formset = FormSet(parent_product=parent) assert formset.initial_form_count() == 0 # No children yet # Save a link data = dict(get_form_data(formset, True), **{"form-0-child": child.pk}) formset = FormSet(parent_product=parent, data=data) formset.save() assert parent.variation_children.filter( pk=child.pk).exists() # Got link'd! # Remove the link formset = FormSet(parent_product=parent) assert formset.initial_form_count() == 1 # Got the child here data = dict(get_form_data(formset, True), **{"form-0-DELETE": "1"}) formset = FormSet(parent_product=parent, data=data) formset.save() assert not parent.variation_children.exists() # Got unlinked
def test_product_options_attributes_form(): """ Test whether we can select attributes of type options """ product_type = ProductType.objects.create(name="Default Product Type") options_attribute = create_attribute_with_options("attribute1", ["A", "B", "C"], 1, 3) product_type.attributes.add(options_attribute) product = create_product("TestProduct", shop=get_default_shop(), type=product_type) choices = list( options_attribute.choices.all().order_by("translations__name")) option_a, option_b, option_c = choices product.set_attribute_value("attribute1", [option_c.pk]) paf = ProductAttributesForm(product=product, languages=("en"), default_language="en") assert compare_partial_dicts(paf.initial, {"attribute1": [option_c.pk]}) form_data = get_form_data(paf) form_data.update({"attribute1": [option_b.pk]}) paf = ProductAttributesForm(product=product, languages=("en"), default_language="en", data=form_data) paf.full_clean() assert not paf.errors paf.save() assert product.get_attribute_value("attribute1") == [option_b]
def test_product_attributes_form(): # Come up with a decent product... product = create_product("TestProduct", shop=get_default_shop()) assert product.type.attributes.count() product.set_attribute_value("bogomips", 6400) product.set_attribute_value("genre", "Kauhu", "fi") product.set_attribute_value("genre", "Horror", "en") product.set_attribute_value("time_to_finish", 12.05) product.set_attribute_value("release_date", VOGONY_DATE) assert product.get_attribute_value("bogomips") == 6400 # English is missing on purpose from the languages list; it'll still be available # for `genre` as it has an extant value. paf = ProductAttributesForm(product=product, languages=("fi", "sv"), default_language="sv") assert paf.languages[0] == "sv" assert compare_partial_dicts( paf.initial, { # Check that things get loaded. "bogomips": 6400, "genre__fi": "Kauhu", "genre__en": "Horror", "release_date": VOGONY_DATE, "time_to_finish": Decimal("12.05"), }, ) form_data = get_form_data(paf) form_data.update( { # Change, clear and add fields "genre__sv": "Skräck", "genre__en": "Terror", "bogomips": "", "release_date": "", "awesome": "True", "important_datetime": make_aware(datetime.datetime(2000, 1, 1, 1, 2, 3), utc), } ) paf = ProductAttributesForm(product=product, languages=("fi", "sv"), default_language="sv", data=form_data) paf.full_clean() assert not paf.errors paf.save() for identifier in ("bogomips", "release_date"): # Value should be gone assert not product.get_attribute_value(identifier) # ... and so should the actual product attribute object assert not ProductAttribute.objects.filter( attribute__identifier=identifier, product=product).exists() # Those other values got updated, right? assert product.get_attribute_value("genre", "en") == form_data["genre__en"] assert product.get_attribute_value("genre", "sv") == form_data["genre__sv"] assert product.get_attribute_value("awesome") is True assert str(product.get_attribute_value( "important_datetime")) == "2000-01-01 01:02:03+00:00"
def test_default_language_finnish(): activate("en") test_name_en = "Test shop" test_name_fi = "Testi kauppa" shop = get_default_shop() shop.name = test_name_en shop.public_name = test_name_en shop.save() shop_form = ShopBaseForm(instance=shop, languages=settings.LANGUAGES) assert shop_form.languages[0] == "fi" data = get_form_data(shop_form, prepared=True) assert data.get("name__en") == test_name_en assert not data.get("name__fi") shop_form = ShopBaseForm(data=data, instance=shop, languages=settings.LANGUAGES) shop_form.full_clean() assert not shop_form.is_valid() and shop_form.errors data["name__fi"] = test_name_fi data["public_name__fi"] = test_name_fi shop_form = ShopBaseForm(data=data, instance=shop, languages=settings.LANGUAGES) shop_form.full_clean() assert shop_form.is_valid() and not shop_form.errors shop_form.save() shop.set_current_language("en") assert shop.name == test_name_en, "English activated" shop.set_current_language("fi") assert shop.name == test_name_fi, "Finnish activated"
def test_editor_save(rf): layout, svc = get_test_layout_and_svc() with initialize_editor_view(svc.view_name, layout.placeholder_name) as view_obj: view_obj.request.GET.update({"x": 0, "y": 0}) view_obj.dispatch(view_obj.request) assert view_obj.current_cell assert view_obj.form assert "general" in view_obj.form.forms assert "plugin" in view_obj.form.forms form_data = get_form_data(view_obj.form, prepared=True) new_text = printable_gibberish() form_data["plugin-text_%s" % FALLBACK_LANGUAGE_CODE] = new_text form_data["save"] = "1" request = rf.post("/pepe/", data=form_data) # sort of rare pepe request.GET = dict(request.GET, x=0, y=0) with initialize_editor_view(svc.view_name, layout.placeholder_name, request) as view_obj: view_obj.dispatch(request) assert view_obj.form assert not view_obj.form.errors assert view_obj.current_cell.config["text"] == { FALLBACK_LANGUAGE_CODE: new_text }
def test_default_language_finnish(): activate("en") test_name_en = "Test shop" test_name_fi = "Testi kauppa" shop = get_default_shop() shop.name = test_name_en shop.public_name = test_name_en shop.save() shop_form = ShopBaseForm(instance=shop, languages=settings.LANGUAGES) data = get_form_data(shop_form, prepared=True) assert data.get("name__en") == test_name_en assert not data.get("name__fi") shop_form = ShopBaseForm(data=data, instance=shop, languages=settings.LANGUAGES) shop_form.full_clean() assert not shop_form.is_valid() and shop_form.errors data["name__fi"] = test_name_fi data["public_name__fi"] = test_name_fi shop_form = ShopBaseForm(data=data, instance=shop, languages=settings.LANGUAGES) shop_form.full_clean() assert shop_form.is_valid() and not shop_form.errors shop_form.save() shop.set_current_language("en") assert shop.name == test_name_en, "English activated" shop.set_current_language("fi") assert shop.name == test_name_fi, "Finnish activated"
def test_protected_fields(): activate("en") shop = Shop.objects.create( name="testshop", identifier="testshop", status=ShopStatus.ENABLED, public_name="test shop", domain="derp", currency="EUR" ) get_currency("EUR") get_currency("USD") assert shop.name == "testshop" assert shop.currency == "EUR" assert not ConfigurationItem.objects.filter(shop=shop, key="languages").exists() shop_form = ShopBaseForm(instance=shop, languages=settings.LANGUAGES) assert not shop_form._get_protected_fields() # No protected fields just yet, right? data = get_form_data(shop_form, prepared=True) shop_form = ShopBaseForm(data=data, instance=shop, languages=settings.LANGUAGES) _test_cleanliness(shop_form) shop_form.save() # Now let's make it protected! create_product(printable_gibberish(), shop=shop, supplier=get_default_supplier()) order = create_random_order(customer=create_random_person(), shop=shop) assert order.shop == shop # And try again... data["currency"] = "USD" shop_form = ShopBaseForm(data=data, instance=shop, languages=settings.LANGUAGES) assert shop_form._get_protected_fields() # So protected! _test_cleanliness(shop_form) shop = shop_form.save() assert shop.currency == "EUR" # But the shop form ignored the change . . .
def test_change_shop_price(): product = _get_test_product() shop = get_default_shop() group = get_default_customer_group() price = shop.create_price form_field = "s_%d_g_%d" % (shop.id, group.id) frm = CustomerGroupPricingForm(product=product, empty_permitted=True) form_data = get_form_data(frm, prepared=True) # Price hike time! form_data[form_field] = "4000" frm = CustomerGroupPricingForm(product=product, data=form_data, empty_permitted=True) frm.full_clean() frm.save() assert CgpPrice.objects.get(product=product, shop=shop, group=group).price == price(4000) # Never mind actually, same price for all shops form_data[form_field] = "" frm = CustomerGroupPricingForm(product=product, data=form_data, empty_permitted=True) frm.full_clean() frm.save() assert not CgpPrice.objects.filter(product=product, shop=shop, group=group).exists()
def test_change_shop_price(form): product = _get_test_product() shop = get_default_shop() group = get_default_customer_group() price = shop.create_price form_field = "s_%d_g_%d" % (shop.id, group.id) frm = form(product=product, shop=shop, empty_permitted=True) form_data = get_form_data(frm, prepared=True) if form == CustomerGroupPricingForm: form_data[form_field] = "4000" else: form_data[form_field] = "50" frm = form(product=product, shop=shop, data=form_data, empty_permitted=True) frm.full_clean() frm.save() if form == CustomerGroupPricingForm: assert CgpPrice.objects.get(product=product, shop=shop, group=group).price == price(4000) else: assert CgpDiscount.objects.get(product=product, shop=shop, group=group).discount_amount == price(50) # Never mind actually, same price for all shops form_data[form_field] = "" frm = form(product=product, shop=shop, data=form_data, empty_permitted=True) frm.full_clean() frm.save() if form == CustomerGroupPricingForm: assert not CgpPrice.objects.filter(product=product, shop=shop, group=group).exists() else: assert not CgpDiscount.objects.filter(product=product, shop=shop, group=group).exists()
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
def test_no_changes_into_form(): product = _get_test_product() shop = get_default_shop() frm = CustomerGroupPricingForm(product=product, empty_permitted=True) # No changes made, right? form_data = get_form_data(frm, prepared=True) frm = CustomerGroupPricingForm(product=product, data=form_data, empty_permitted=True) frm.full_clean() frm.save() assert CgpPrice.objects.get(product=product, shop=shop).price.value == 250
def test_simple_children_formset(): FormSet = formset_factory(SimpleVariationChildForm, SimpleVariationChildFormSet, extra=5, can_delete=True) parent = create_product(printable_gibberish()) child = create_product(printable_gibberish()) # No links yet formset = FormSet(parent_product=parent) assert formset.initial_form_count() == 0 # No children yet # Save a link data = dict(get_form_data(formset, True), **{"form-0-child": child.pk}) formset = FormSet(parent_product=parent, data=data) formset.save() assert parent.variation_children.filter(pk=child.pk).exists() # Got link'd! # Remove the link formset = FormSet(parent_product=parent) assert formset.initial_form_count() == 1 # Got the child here data = dict(get_form_data(formset, True), **{"form-0-DELETE": "1"}) formset = FormSet(parent_product=parent, data=data) formset.save() assert not parent.variation_children.exists() # Got unlinked
def test_contact_group_members_formset(rf): FormSet = formset_factory(ContactGroupMembersForm, ContactGroupMembersFormSet, extra=1, can_delete=True) contact_group = get_default_customer_group() person = create_random_person() # No members formset = FormSet(contact_group=contact_group) assert formset.initial_form_count() == 0 # Add a member data = dict(get_form_data(formset, True), **{"form-0-member": person.pk}) formset = FormSet(contact_group=contact_group, data=data) formset.save() assert contact_group.members.filter(pk=person.pk).exists()
def test_category_form_with_parent(rf, admin_user): with transaction.atomic(): shop = get_default_shop() category1 = CategoryFactory() category2 = CategoryFactory() category2.shops.clear() assert shop not in category2.shops.all() category3 = CategoryFactory() category3.shops.clear() assert shop not in category3.shops.all() request = apply_request_middleware(rf.get("/"), user=admin_user, shop=shop) form_kwargs = dict(instance=category1, request=request, languages=("sw", ), default_language="sw") form = CategoryBaseForm(**form_kwargs) assert isinstance(form, CategoryBaseForm) form_data = get_form_data(form, prepared=True) for lang, field_map in form.trans_name_map.items(): for dst_field in field_map.values(): form_data[form.add_prefix(dst_field)] = "IJWEHGWOHKSL" # Make sure we have right parent options parent_bound_field = [ field for field in form.visible_fields() if field.name == "parent" ][0] assert len(parent_bound_field.field.choices) == 1 assert parent_bound_field.field.choices[0][0] is None category2.shops.add(shop) form_kwargs["data"] = form_data form = CategoryBaseForm(**form_kwargs) assert isinstance(form, CategoryBaseForm) # Make sure category 2 is now in parent options parent_bound_field = [ field for field in form.visible_fields() if field.name == "parent" ][0] assert len(parent_bound_field.field.choices) == 2 assert parent_bound_field.field.choices[1][0] == category2.id # Make sure saving the form still works form.full_clean() form.save() category = form.instance category.set_current_language("sw") assert category.name == "IJWEHGWOHKSL"
def test_default_language_english(): activate("en") test_name_en = "Test shop" shop = get_default_shop() shop.name = test_name_en shop.public_name = test_name_en shop.save() shop_form = ShopBaseForm(instance=shop, languages=settings.LANGUAGES) data = get_form_data(shop_form, prepared=True) assert data.get("name__en") == test_name_en assert not data.get("name__fi") shop_form = ShopBaseForm(data=data, instance=shop, languages=settings.LANGUAGES) shop_form.full_clean() assert shop_form.is_valid() and not shop_form.errors
def test_cannot_add_product_to_own_package(rf): FormSet = formset_factory(PackageChildForm, PackageChildFormSet, extra=5, can_delete=True) parent = create_product(printable_gibberish()) # No products in the package formset = FormSet(parent_product=parent) assert formset.initial_form_count() == 0 # No children yet assert not parent.get_all_package_children() # Try to add a product to its own package data = dict(get_form_data(formset, True), **{"form-0-child": parent.pk, "form-0-quantity": 2}) formset = FormSet(parent_product=parent, data=data) formset.save() assert not parent.get_all_package_children()
def test_product_attributes_form(): # Come up with a decent product... product = create_product("TestProduct", shop=get_default_shop()) assert product.type.attributes.count() product.set_attribute_value("bogomips", 6400) product.set_attribute_value("genre", "Kauhu", "fi") product.set_attribute_value("genre", "Horror", "en") product.set_attribute_value("time_to_finish", 12.05) product.set_attribute_value("release_date", VOGONY_DATE) assert product.get_attribute_value("bogomips") == 6400 # English is missing on purpose from the languages list; it'll still be available # for `genre` as it has an extant value. paf = ProductAttributesForm(product=product, languages=("fi", "sv"), default_language="sv") assert paf.languages[0] == "sv" assert compare_partial_dicts(paf.initial, { # Check that things get loaded. "bogomips": 6400, "genre__fi": "Kauhu", "genre__en": "Horror", "release_date": VOGONY_DATE, "time_to_finish": Decimal("12.05") }) form_data = get_form_data(paf) form_data.update({ # Change, clear and add fields "genre__sv": "Skräck", "genre__en": "Terror", "bogomips": "", "release_date": "", "awesome": "True", "important_datetime": make_aware(datetime.datetime(2000, 1, 1, 1, 2, 3), utc) }) paf = ProductAttributesForm(product=product, languages=("fi", "sv"), default_language="sv", data=form_data) paf.full_clean() assert not paf.errors paf.save() for identifier in ("bogomips", "release_date"): # Value should be gone assert not product.get_attribute_value(identifier) # ... and so should the actual product attribute object assert not ProductAttribute.objects.filter(attribute__identifier=identifier, product=product).exists() # Those other values got updated, right? assert product.get_attribute_value("genre", "en") == form_data["genre__en"] assert product.get_attribute_value("genre", "sv") == form_data["genre__sv"] assert product.get_attribute_value("awesome") is True assert str(product.get_attribute_value("important_datetime")) == "2000-01-01 01:02:03+00:00"
def test_product_not_in_normal_mode(): FormSet = formset_factory(PackageChildForm, PackageChildFormSet, extra=5, can_delete=True) parent = create_product(printable_gibberish()) child_1 = create_product(printable_gibberish()) child_1.link_to_parent(parent) child_2 = create_product(printable_gibberish()) parent.verify_mode() assert parent.mode == ProductMode.SIMPLE_VARIATION_PARENT # Trying to create a package from a non-normal mode product with pytest.raises(Problem): formset = FormSet(parent_product=parent) data = dict(get_form_data(formset, True), **{"form-0-child": child_2.pk, "form-0-quantity": 2}) formset = FormSet(parent_product=parent, data=data) formset.save()
def test_category_form_saving(rf): with transaction.atomic(): category = CategoryFactory() form_kwargs = dict(instance=category, languages=("sw",), default_language="sw") form = CategoryBaseForm(**form_kwargs) assert isinstance(form, CategoryBaseForm) form_data = get_form_data(form, prepared=True) for lang, field_map in form.trans_name_map.items(): for dst_field in field_map.values(): form_data[form.add_prefix(dst_field)] = "IJWEHGWOHKSL" form_kwargs["data"] = form_data form = CategoryBaseForm(**form_kwargs) form.full_clean() form.save() category = form.instance category.set_current_language("sw") assert category.name == "IJWEHGWOHKSL"
def test_no_changes_into_form(form): product = _get_test_product() shop = get_default_shop() frm = form(product=product, shop=shop) # No changes made, right? form_data = get_form_data(frm, prepared=True) frm = form(product=product, shop=shop, data=form_data) frm.full_clean() frm.save() if form == CustomerGroupPricingForm: assert CgpPrice.objects.get(product=product, shop=shop).price.value == 250 else: assert CgpDiscount.objects.get(product=product, shop=shop).discount_amount.value == 100
def test_package_child_formset(): FormSet = formset_factory(PackageChildForm, PackageChildFormSet, extra=5, can_delete=True) parent = create_product(printable_gibberish()) child = create_product(printable_gibberish()) # No products in the package formset = FormSet(parent_product=parent) assert formset.initial_form_count() == 0 # No children yet assert not parent.get_all_package_children() data = dict(get_form_data(formset, True), **{"form-0-child": child.pk, "form-0-quantity": 2}) formset = FormSet(parent_product=parent, data=data) formset.save() assert parent.get_all_package_children() clear_existing_package(parent) assert not parent.get_all_package_children()
def test_address_assigned_to_multiple_times_for_one_contact(): person = create_random_person() address = create_random_address() person.default_billing_address = person.default_shipping_address = address person.save() assert person.default_billing_address_id == address.id assert person.default_shipping_address_id == address.id form = MutableAddressForm(instance=address) data = get_form_data(form, prepared=True) form = MutableAddressForm(data=data, instance=address) form.full_clean() assert not form.errors assert form.cleaned_data form.save() address.refresh_from_db() assert person.default_billing_address_id != address.id assert person.default_shipping_address_id != address.id
def test_impossible_simple_variation(): FormSet = formset_factory(SimpleVariationChildForm, SimpleVariationChildFormSet, extra=5, can_delete=True) parent = create_product(printable_gibberish()) child = create_product(printable_gibberish()) grandchild = create_product(printable_gibberish()) grandchild.link_to_parent(child) assert child.variation_children.exists() formset = FormSet(parent_product=parent) data = dict(get_form_data(formset, True), **{"form-0-child": child.pk}) formset = FormSet(parent_product=parent, data=data) assert formset.is_valid() # It's technically valid, but... with pytest.raises(Problem) as ei: formset.save() if six.PY3: # Can only test inner exceptions on Py3. Ah well. inner_exc = ei.value.__context__ assert isinstance(inner_exc, ImpossibleProductModeException) assert inner_exc.code == "multilevel"
def test_category_form_with_parent(rf, admin_user): with transaction.atomic(): shop = get_default_shop() category1 = CategoryFactory() category2 = CategoryFactory() category2.shops.clear() assert shop not in category2.shops.all() category3 = CategoryFactory() category3.shops.clear() assert shop not in category3.shops.all() request = apply_request_middleware(rf.get("/"), user=admin_user, shop=shop) form_kwargs = dict(instance=category1, request=request, languages=("sw",), default_language="sw") form = CategoryBaseForm(**form_kwargs) assert isinstance(form, CategoryBaseForm) form_data = get_form_data(form, prepared=True) for lang, field_map in form.trans_name_map.items(): for dst_field in field_map.values(): form_data[form.add_prefix(dst_field)] = "IJWEHGWOHKSL" # Make sure we have right parent options parent_bound_field = [field for field in form.visible_fields() if field.name == "parent"][0] assert len(parent_bound_field.field.choices) == 1 assert parent_bound_field.field.choices[0][0] is None category2.shops.add(shop) form_kwargs["data"] = form_data form = CategoryBaseForm(**form_kwargs) assert isinstance(form, CategoryBaseForm) # Make sure category 2 is now in parent options parent_bound_field = [field for field in form.visible_fields() if field.name == "parent"][0] assert len(parent_bound_field.field.choices) == 2 assert parent_bound_field.field.choices[1][0] == category2.id # Make sure saving the form still works form.full_clean() form.save() category = form.instance category.set_current_language("sw") assert category.name == "IJWEHGWOHKSL"
def test_editor_save(rf): layout, svc = get_test_layout_and_svc() with initialize_editor_view(svc.view_name, layout.placeholder_name) as view_obj: view_obj.request.GET.update({"x": 0, "y": 0}) view_obj.dispatch(view_obj.request) assert view_obj.current_cell assert view_obj.form assert "general" in view_obj.form.forms assert "plugin" in view_obj.form.forms form_data = get_form_data(view_obj.form, prepared=True) new_text = printable_gibberish() form_data["plugin-text_%s" % FALLBACK_LANGUAGE_CODE] = new_text form_data["save"] = "1" request = rf.post("/pepe/", data=form_data) # sort of rare pepe request.GET = dict(request.GET, x=0, y=0) with initialize_editor_view(svc.view_name, layout.placeholder_name, request) as view_obj: view_obj.dispatch(request) assert view_obj.form assert not view_obj.form.errors assert view_obj.current_cell.config["text"] == {FALLBACK_LANGUAGE_CODE: new_text}
def test_change_shop_price(form): product = _get_test_product() shop = get_default_shop() group = get_default_customer_group() price = shop.create_price form_field = "s_%d_g_%d" % (shop.id, group.id) frm = form(product=product, shop=shop) form_data = get_form_data(frm, prepared=True) if form == CustomerGroupPricingForm: form_data[form_field] = "4000" else: form_data[form_field] = "50" frm = form(product=product, shop=shop, data=form_data) frm.full_clean() frm.save() if form == CustomerGroupPricingForm: assert CgpPrice.objects.get(product=product, shop=shop, group=group).price == price(4000) else: assert CgpDiscount.objects.get( product=product, shop=shop, group=group).discount_amount == price(50) # Never mind actually, same price for all shops form_data[form_field] = "" frm = form(product=product, shop=shop, data=form_data) frm.full_clean() frm.save() if form == CustomerGroupPricingForm: assert not CgpPrice.objects.filter( product=product, shop=shop, group=group).exists() else: assert not CgpDiscount.objects.filter( product=product, shop=shop, group=group).exists()
def test_rule_admin(rf, admin_user): shop = get_default_shop() tax = create_tax("test-1", rate=Decimal("0.12")) tax.save() tax_class = TaxClass.objects.create(name="test") view = TaxRuleEditView( request=apply_request_middleware(rf.get("/"), user=admin_user)) form_class = view.get_form_class() form_kwargs = view.get_form_kwargs() form = form_class(**form_kwargs) data = get_form_data(form) data.update({ "shop": shop.pk, "postal_codes_pattern": "99501-99511,99513-99524,99529-99530,99540,99590,99550,99567,99573,99577,99586-99588", "country_codes_pattern": "FI,CA,US,SE", "region_codes_pattern": "CA,AR,IA,AK,WY,TN", "tax": tax.id, "tax_classes": [tax_class.id] }) form = form_class(**dict(form_kwargs, data=data)) form.full_clean() assert not form.errors rule = form.save() assert rule._postal_codes_min == "99501" assert rule._postal_codes_max == "99590" postal_code = "99510" assert TaxRule.objects.may_match_postal_code(postal_code).count() == 1 postal_code = "99500" assert not TaxRule.objects.may_match_postal_code(postal_code).count()
def test_rule_admin(rf, admin_user): shop = get_default_shop() tax = create_tax("test-1", rate=Decimal("0.12")) tax.save() tax_class = TaxClass.objects.create(name="test") view = TaxRuleEditView(request=apply_request_middleware(rf.get("/"), user=admin_user)) form_class = view.get_form_class() form_kwargs = view.get_form_kwargs() form = form_class(**form_kwargs) data = get_form_data(form) data.update({ "shop": shop.pk, "postal_codes_pattern": "99501-99511,99513-99524,99529-99530,99540,99590,99550,99567,99573,99577,99586-99588", "country_codes_pattern": "FI,CA,US,SE", "region_codes_pattern": "CA,AR,IA,AK,WY,TN", "tax": tax.id, "tax_classes": [tax_class.id] }) form = form_class(**dict(form_kwargs, data=data)) form.full_clean() assert not form.errors rule = form.save() assert rule._postal_codes_min == "99501" assert rule._postal_codes_max == "99590" postal_code = "99510" assert TaxRule.objects.may_match_postal_code(postal_code).count() == 1 postal_code = "99500" assert not TaxRule.objects.may_match_postal_code(postal_code).count()
def test_page_form(rf, admin_user): shop = get_default_shop() request = rf.get("/") request.user = admin_user request.shop = shop view = PageEditView(request=request) form_class = PageForm form = form_class(**dict(request=request, languages=settings.LANGUAGES)) assert not form.is_bound data = get_form_data(form) data.update({ "available_from": "", "available_to": "", "content__en": "", "content__fi": "suomi", "content__ja": "", "identifier": "", "title__en": "", "title__fi": "", "title__ja": "", "url__en": "", "url__fi": "suomi", "url__ja": "", }) form = form_class( **dict(request=request, languages=settings.LANGUAGES, data=data)) form.full_clean() assert ( "title__fi" in form.errors ) # We get an error because all of a given language's fields must be filled if any are data["title__fi"] = "suomi" form = form_class( **dict(request=request, languages=settings.LANGUAGES, data=data)) form.full_clean() assert not form.errors page = form.save() assert set(page.get_available_languages()) == { "fi" } # The page should be only in Finnish # Let's edit that page original_url = "errrnglish" data.update({ "title__en": "englaish", "url__en": original_url, "content__en": "ennnn ennnn ennnnnnn-nn-n-n" }) form = form_class(**dict(request=request, languages=settings.LANGUAGES, data=data, instance=page)) form.full_clean() assert not form.errors page = form.save() assert set(page.get_available_languages()) == {"fi", "en"} # English GET # Try to make page a child of itself data.update({"parent": page.pk}) form = form_class(**dict(request=request, languages=settings.LANGUAGES, data=data, instance=page)) form.full_clean() assert form.errors del data["parent"] # add dummy page with simple url, page is in english dummy = create_page(url="test", shop=get_default_shop()) # edit page again and try to set duplicate url data.update({ "title__en": "englaish", "url__en": "test", "content__en": "ennnn ennnn ennnnnnn-nn-n-n" }) form = form_class(**dict(request=request, languages=settings.LANGUAGES, data=data, instance=page)) form.full_clean() assert len(form.errors) == 1 assert "url__en" in form.errors assert form.errors["url__en"].as_data()[0].code == "invalid_url" # it should be possible to change back to the original url data.update({ "title__en": "englaish", "url__en": original_url, "content__en": "ennnn ennnn ennnnnnn-nn-n-n" }) form = form_class(**dict(request=request, languages=settings.LANGUAGES, data=data, instance=page)) form.full_clean() assert not form.errors page = form.save() # add finnish urls, it should not be possible to enter original url data.update({ "title__fi": "englaish", "url__fi": original_url, "content__fi": "ennnn ennnn ennnnnnn-nn-n-n" }) assert data["url__fi"] == data[ "url__en"] # both urls are same, should raise two errors form = form_class(**dict(request=request, languages=settings.LANGUAGES, data=data, instance=page)) form.full_clean() assert len(form.errors) == 1 assert "url__fi" in form.errors error_data = form.errors["url__fi"].as_data() assert error_data[0].code == "invalid_url" assert error_data[1].code == "invalid_unique_url" page.soft_delete() data["url__fi"] = "suomi" form = form_class( **dict(request=request, languages=settings.LANGUAGES, data=data)) form.full_clean() assert len(form.errors) == 0 page = form.save()
def test_page_form(rf): view = PageEditView(request=rf.get("/")) form_class = view.get_form_class() form_kwargs = view.get_form_kwargs() form = form_class(**form_kwargs) assert not form.is_bound data = get_form_data(form) data.update({ "available_from": "", "available_to": "", "content__en": "", "content__fi": "suomi", "content__ja": "", "identifier": "", "title__en": "", "title__fi": "", "title__ja": "", "url__en": "", "url__fi": "suomi", "url__ja": "", }) form = form_class(**dict(form_kwargs, data=data)) form.full_clean() assert "title__fi" in form.errors # We get an error because all of a given language's fields must be filled if any are data["title__fi"] = "suomi" form = form_class(**dict(form_kwargs, data=data)) form.full_clean() assert not form.errors page = form.save() assert set(page.get_available_languages()) == {"fi"} # The page should be only in Finnish # Let's edit that page original_url = "errrnglish" data.update({"title__en": "englaish", "url__en": original_url, "content__en": "ennnn ennnn ennnnnnn-nn-n-n"}) form = form_class(**dict(form_kwargs, data=data, instance=page)) form.full_clean() assert not form.errors page = form.save() assert set(page.get_available_languages()) == {"fi", "en"} # English GET # Try to make page a child of itself data.update({"parent": page.pk}) form = form_class(**dict(form_kwargs, data=data, instance=page)) form.full_clean() assert form.errors del data["parent"] # add dummy page with simple url, page is in english dummy = create_page(url="test") # edit page again and try to set duplicate url data.update({"title__en": "englaish", "url__en": "test", "content__en": "ennnn ennnn ennnnnnn-nn-n-n"}) form = form_class(**dict(form_kwargs, data=data, instance=page)) form.full_clean() assert len(form.errors) == 1 assert "url__en" in form.errors assert form.errors["url__en"].as_data()[0].code == "invalid_url" # it should be possible to change back to the original url data.update({"title__en": "englaish", "url__en": original_url, "content__en": "ennnn ennnn ennnnnnn-nn-n-n"}) form = form_class(**dict(form_kwargs, data=data, instance=page)) form.full_clean() assert not form.errors page = form.save() # add finnish urls, it should not be possible to enter original url data.update({"title__fi": "englaish", "url__fi": original_url, "content__fi": "ennnn ennnn ennnnnnn-nn-n-n"}) form = form_class(**dict(form_kwargs, data=data, instance=page)) form.full_clean() assert len(form.errors) == 1 assert "url__fi" in form.errors assert form.errors["url__fi"].as_data()[0].code == "invalid_url"
def test_page_form(rf): view = PageEditView(request=rf.get("/")) form_class = view.get_form_class() form_kwargs = view.get_form_kwargs() form = form_class(**form_kwargs) assert not form.is_bound data = get_form_data(form) data.update({ "available_from": "", "available_to": "", "content__en": "", "content__fi": "suomi", "content__ja": "", "identifier": "", "title__en": "", "title__fi": "", "title__ja": "", "url__en": "", "url__fi": "suomi", "url__ja": "", }) form = form_class(**dict(form_kwargs, data=data)) form.full_clean() assert "title__fi" in form.errors # We get an error because all of a given language's fields must be filled if any are data["title__fi"] = "suomi" form = form_class(**dict(form_kwargs, data=data)) form.full_clean() assert not form.errors page = form.save() assert set(page.get_available_languages()) == {"fi"} # The page should be only in Finnish # Let's edit that page original_url = "errrnglish" data.update({"title__en": "englaish", "url__en": original_url, "content__en": "ennnn ennnn ennnnnnn-nn-n-n"}) form = form_class(**dict(form_kwargs, data=data, instance=page)) form.full_clean() assert not form.errors page = form.save() assert set(page.get_available_languages()) == {"fi", "en"} # English GET # add dummy page with simple url, page is in english dummy = create_page(url="test") # edit page again and try to set duplicate url data.update({"title__en": "englaish", "url__en": "test", "content__en": "ennnn ennnn ennnnnnn-nn-n-n"}) form = form_class(**dict(form_kwargs, data=data, instance=page)) form.full_clean() assert len(form.errors) == 1 assert "url__en" in form.errors assert form.errors["url__en"].as_data()[0].code == "invalid_url" # it should be possible to change back to the original url data.update({"title__en": "englaish", "url__en": original_url, "content__en": "ennnn ennnn ennnnnnn-nn-n-n"}) form = form_class(**dict(form_kwargs, data=data, instance=page)) form.full_clean() assert not form.errors page = form.save() # add finnish urls, it should not be possible to enter original url data.update({"title__fi": "englaish", "url__fi": original_url, "content__fi": "ennnn ennnn ennnnnnn-nn-n-n"}) form = form_class(**dict(form_kwargs, data=data, instance=page)) form.full_clean() assert len(form.errors) == 1 assert "url__fi" in form.errors assert form.errors["url__fi"].as_data()[0].code == "invalid_url"