def test_materials_listed(self): materials = MaterialFactory.create_batch(5) self.client.force_login(UserFactory()) response = self.client.get(reverse("catalog:catalog")) self.assertTemplateUsed(response, "catalog/material_list.html") for material in materials: self.assertContainsMaterialOnce(response, material)
def test_json_format(self): materials = MaterialFactory.create_batch(10) response = self.client.get("/booking/api/material?format=json") content = str(response.content, encoding="utf8") json_response = json.loads(content) for item in json_response: material = Material.objects.get(pk=int(item["id"])) self.assertEqual(item["name"], material.name) self.assertEqual(item["gm"], material.gm) for category in material.categories.all(): self.assertTrue(category.name in item["categories"]) self.assertEqual( material.images.count(), len(item["images"]), "not all images are included", ) for image_url in item["images"]: mimetype, encoding = mimetypes.guess_type(image_url) self.assertTrue( mimetype and mimetype.startswith("image"), "url does not seem image" ) self.assertTrue( "testserver" in image_url, "url does not contain domain" ) self.assertEqual(item["catalogUrl"], f"/catalog/{material.pk}/modal")
def test_materials_filter_category(self): category = CategoryFactory() other_category = CategoryFactory() materials_in_category = MaterialFactory.create_batch( 5, categories=[category]) materials_other_category = MaterialFactory.create_batch( 5, categories=[other_category]) self.client.force_login(UserFactory()) response = self.client.get(category.get_absolute_url()) self.assertTemplateUsed(response, "catalog/material_list.html") for material in materials_in_category: self.assertContainsMaterialOnce(response, material) for material in materials_other_category: self.assertNotContains(response, str(material)) # Check that the category filter buttons are in the response self.assertContains(response, str(category)) self.assertContains(response, str(other_category))
def test_typeahead_thumbprint_not_none(self): materials = MaterialFactory.create_batch(3) last_material = MaterialFactory() self.assertNotEqual(materials[0].last_modified, last_material.last_modified) event = EventFactory() self.client.force_login(UserFactory()) response = self.client.get(event.get_absolute_url()) self.assertEqual( response.context["typeahead_thumbprint"], last_material.last_modified.isoformat(), "typeahead thumbprint is not correct", )
def test_edit_rate_class_remove_material(self): rate_class = RateClassFactory() materials = MaterialFactory.create_batch(5, rate_class=rate_class) # Only keep the first 4 materials associated to the rate class self.run_form(rate_class, materials[:4]) for material in materials: material.refresh_from_db() # Verify that the first 4 materials are still associated for material in materials[:4]: self.assertEqual(material.rate_class, rate_class) # Verify that the 5th is no longer associated self.assertEqual(materials[4].rate_class, None)
def test_create_rate_class_with_materials(self): rate_class = RateClassFactory.build() # do not save to DB self.assertFalse( RateClass.objects.all()) # verify that it is not in DB materials = MaterialFactory.create_batch(3, rate_class=None) self.run_form(rate_class, materials) # Verify that the rate class has been saved self.assertEqual(list(RateClass.objects.all()), [rate_class]) for material in materials: material.refresh_from_db() # Verify that all materials were associated for material in materials: self.assertEqual(material.rate_class, rate_class)
def test_csv_format(self): materials = MaterialFactory.create_batch(10) response = self.client.get("/booking/api/material?format=woocommerce") self.assertEqual( response.get("Content-Disposition"), 'attachment; filename="materials.csv"' ) self.assertEqual(response.status_code, 200) content = response.content.decode("utf-8") # Test if all materials are included in CSV + a header self.assertEqual( len(content.splitlines()), len(materials) + 1, "not all materials are part of csv", ) csv_reader = csv.DictReader(io.StringIO(content)) # Verify material attributes for row in csv_reader: material = Material.objects.get(pk=int(row["sku"]) - 2000) self.assertEqual(row["post_title"], material.name) self.assertEqual(row["post_name"], material.name) if material.lendable: self.assertEqual(row["tax:product_visibility"], "visible") self.assertEqual(row["post_status"], "publish") else: self.assertEqual( row["tax:product_visibility"], "exclude-from-catalog|exclude-from-search", ) self.assertEqual(row["post_status"], "private") if material.stock_value is not None: self.assertAlmostEqual(float(row["stock"]), material.stock_value) else: self.assertEqual(row["stock"], "") self.check_post_content(row["post_content"], material) if material.rate_class is not None: self.assertEqual( row["regular_price"], str(material.rate_class.rate.amount) ) else: self.assertEqual(row["regular_price"], str(0.00)) for category in material.categories.all(): self.assertTrue(category.name in row["tax:product_cat"]) self.check_images(row["images"], material) if material.stock_unit is not None: self.assertEqual(row["meta:stock_unit"], material.stock_unit) else: self.assertEqual(row["meta:stock_unit"], "")
def test_catalog_descendant_categories_included(self): parent_category = CategoryFactory() materials_parent_category = MaterialFactory.create_batch( 5, categories=[parent_category]) child_category = CategoryFactory(parent=parent_category) materials_child_category = MaterialFactory.create_batch( 5, categories=[child_category]) grandchild_category = CategoryFactory(parent=child_category) materials_grandchild_category = MaterialFactory.create_batch( 5, categories=[grandchild_category]) self.client.force_login(UserFactory()) response = self.client.get(parent_category.get_absolute_url()) self.assertTemplateUsed(response, "catalog/material_list.html") for material in [ *materials_parent_category, *materials_child_category, *materials_grandchild_category, ]: self.assertContainsMaterialOnce(response, material) response = self.client.get(child_category.get_absolute_url()) for material in [ *materials_child_category, *materials_grandchild_category, ]: self.assertContainsMaterialOnce(response, material) for material in materials_parent_category: self.assertNotContains(response, str(material)) response = self.client.get(grandchild_category.get_absolute_url()) for material in materials_grandchild_category: self.assertContainsMaterialOnce(response, material) for material in [ *materials_parent_category, *materials_child_category ]: self.assertNotContains(response, str(material))
def test_can_navigate_catalog_page(self, mock_get_paginate_by): mock_get_paginate_by.return_value = 2 category = CategoryFactory() # Note there is also the self.material, so 6 materials in total materials = MaterialFactory.create_batch(5, categories=[category]) # Get the materials sorted sorted_materials = sorted( list(Material.objects.all()), key=lambda m: str(m).lower() ) def verify_material_order(catalog_view_page, i): # Get element from page item = catalog_view_page.get_catalog_item(i % mock_get_paginate_by()) # Get text text = catalog_view_page.get_catalog_item_text(item) self.assertEqual( text.lower(), str(sorted_materials[i]).lower(), f"material {i} is not sorted", ) # Bob is a logged in user bob = UserFactory() self.create_pre_authenticated_session(bob) # Bob opens the catalog page self.browser.get(self.live_server_url + reverse("catalog:catalog")) catalog_view_page = CatalogViewPage(self) # The first page contains the first two materials in order verify_material_order(catalog_view_page, 0) verify_material_order(catalog_view_page, 1) # Bob finds 3 pages in the catalog self.assertEqual(catalog_view_page.get_page_count(), 3) # Bob can navigate to page 2 catalog_view_page.navigate_to_page(2) # The second page contains the second two materials in order verify_material_order(catalog_view_page, 2) verify_material_order(catalog_view_page, 3) # Bob can navigate to page 3 catalog_view_page.navigate_to_page(3) # The third page contains the third two materials in order verify_material_order(catalog_view_page, 4) verify_material_order(catalog_view_page, 5)
def setUp(self): super().setUp() self.active_event = EventFactory() self.materials = MaterialFactory.create_batch(size=10) MaterialFactory(name="beschuiten (rol)") MaterialFactory(name="EHBO doos")