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_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_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_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"