def test_category_slug(self): """Test Category slug creation""" title1 = CategoryFactory(name="title1", slug="title1-slug", is_static_url=True) title2 = CategoryFactory(name="title2", slug="title2-slug") title3 = CategoryFactory(name="title3") title1slug = CategoryFactory(name="title1-slug") assert title1.slug == "title1-slug" assert title2.slug == "title2" assert title3.slug == "title3" assert title1slug.slug != "title1-slug" assert "title1-slug-" in title1slug.slug
def test_category_delete(self, user_client: Client) -> None: category: Category = CategoryFactory(name="name", created_at=timezone.now()) response = user_client.get(f"/panel/category/delete/{category.id}") assert response.status_code == 302 assert response.url == "/panel/posts"
def test_delete_removes_category(self, user_client: Client): category: Category = CategoryFactory() response = user_client.delete( reverse("panel:category_api", kwargs={"name": category.name}), ) assert response.status_code == status.HTTP_204_NO_CONTENT, response.content assert Category.objects.count() == 0
def test_patch_updates_category(self, user_client: Client): initial_category: Category = CategoryFactory() new_category: Category = CategoryFactory(name="Personal") initial_content: Content = ContentFactory(category=initial_category) data: Dict[str, Any] = { "title": "Title2", "body": "Body text", "category": {"name": new_category.name}, } response = user_client.patch( reverse("panel:content_api", kwargs={"pk": str(initial_content.id)}), data=data, content_type="application/json", ) assert response.status_code == status.HTTP_200_OK, response.content assert Content.objects.count() == 1 assert Content.objects.first().category == new_category
def test_category_delete_rejects_if_contents_exist( self, user_client: Client) -> None: category: Category = CategoryFactory(name="name", created_at=timezone.now()) ContentFactory(category=category) response = user_client.get(f"/panel/category/delete/{category.id}") assert response.status_code == 400 assert (response.content == b"There are Contents with this category. Delete them first")
def test_category(self, user_client: Client): content: Content = ContentFactory(title="title", body="body", created_at=timezone.now()) category: Category = CategoryFactory(name="name", created_at=timezone.now()) content.category = category content.save() response = user_client.get(f"/panel/category/{category.id}") assert response.status_code == 200 assert len(response.context["posts"]) == 1
def test_delete_rejects_if_contents_exist_with_category(self, user_client: Client): category: Category = CategoryFactory() ContentFactory(category=category) response = user_client.delete( reverse("panel:category_api", kwargs={"name": category.name}), ) assert response.status_code == status.HTTP_400_BAD_REQUEST, response.content assert ( response.content == b'{"detail":"There are Contents with this category. Delete them first"}' )
def test_endpoint_returns_categories_in_db(self, user_client: Client): CategoryFactory(name="Software") keys: Set[str] = {"name", "created_at", "slug", "is_static_url"} response = user_client.get( reverse("panel:category_api"), ) assert response.status_code == status.HTTP_200_OK assert len(response.json()) == 1 assert keys == response.json()[0].keys(), response.json() assert response.json()[0]["name"] == "Software" assert response.json()[0]["slug"] == "software" assert response.json()[0]["is_static_url"] is False
def test_post_create(self, user_client: Client): response = user_client.get("/panel/post/create") assert response.status_code == 302 response = user_client.get(f"/panel/post/create/{uuid4()}") assert response.status_code == 404 category: Category = CategoryFactory(name="name", created_at=timezone.now()) response = user_client.get(f"/panel/post/create/{category.id}") assert response.status_code == 302
def test_post_rejects_creation_if_exists(self, user_client: Client): category: Category = CategoryFactory() data: Dict[str, Any] = {"name": category.name} response = user_client.post( reverse("panel:category_api"), data=data, content_type="application/json", ) assert response.status_code == status.HTTP_400_BAD_REQUEST, response.content assert ( response.content == b'{"detail":"UNIQUE constraint failed: panel_category.name"}' )
def test_post_creates_new_content_with_category(self, user_client: Client): category: Category = CategoryFactory() data: Dict[str, Any] = { "title": "Title1", "body": "Body text", "category": {"name": category.name}, } response = user_client.post( reverse("panel:content_api"), data=data, content_type="application/json", ) assert response.status_code == status.HTTP_201_CREATED, response.content assert Content.objects.count() == 1 assert Content.objects.first().category == category, category.name
def test_patch_partially_updates_category(self, user_client: Client): category: Category = CategoryFactory() data: Dict[str, Any] = {"name": "New Category"} response = user_client.patch( reverse("panel:category_api", kwargs={"name": category.name}), data=data, content_type="application/json", ) assert response.status_code == status.HTTP_200_OK, response.content assert Category.objects.count() == 1 category: Category = Category.objects.first() assert category.name == data["name"] assert category.slug == data["name"].lower().replace(" ", "-") assert category.is_static_url is False
def test_category(self, client: Client) -> None: category: Category = CategoryFactory() response = client.get(f"/category/{category.id}") assert response.status_code == 200, category.slug