Skip to content

Commit

Permalink
updated tests to utilize make_preview_request
Browse files Browse the repository at this point in the history
  • Loading branch information
danielfmiranda committed Feb 13, 2024
1 parent 043a17b commit 2ea26ae
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
>
</div>
<p class="tw-body-small mt-3 mb-1">{{ related_product.company }}</p>
<p>{{ related_product.title }}</p>
<p class="product-title">{{ related_product.title }}</p>
</a>

{% include "fragments/buyersguide/privacy_ding.html" with product=related_product %}
Expand Down
4 changes: 2 additions & 2 deletions network-api/networkapi/utility/orderables.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from typing import TYPE_CHECKING, Optional
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from django.db import models


def get_related_items(
queryset: "models.QuerySet", related_item_field: str, order_by: Optional[str] = None
queryset: "models.QuerySet", related_item_field: str, order_by: str | None = None
) -> list["models.Model"]:
"""
Return list of the related items from the queryset.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1038,9 +1038,8 @@ def preview_related_products(self) -> list:
related_products = orderables.get_related_items(
self.related_product_pages.all(), "related_product", order_by="sort_order"
)
# This method, while inneficient, is neccesary as we are using it to render
# any related_product updates that are stored in memory, but not yet saved in the DB.
return [product.localized for product in related_products]

return related_products

@property
def local_categories(self):
Expand Down
185 changes: 101 additions & 84 deletions network-api/networkapi/wagtailpages/tests/buyersguide/test_products.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json

import bs4
from django.test import TestCase
from django.test.utils import override_settings
from rest_framework.test import APITestCase
Expand Down Expand Up @@ -123,7 +124,8 @@ def test_localized_related_products_non_default_locale(self):

def test_preview_related_products(self):
"""
Tests if `preview_related_products` accurately orders and includes related products for page previews.
Tests if `preview_related_products` returns related products according to sort order,
and renders them in the ProductPage CMS preview panel.
"""
product_page = self.product_page

Expand All @@ -132,34 +134,41 @@ def test_preview_related_products(self):
related_product_3 = buyersguide_factories.ProductPageFactory(parent=self.bg)

buyersguide_factories.RelatedProductsFactory(
page=product_page,
related_product=related_product_1,
sort_order=2,
page=product_page, related_product=related_product_1, sort_order=2
)
buyersguide_factories.RelatedProductsFactory(
page=product_page,
related_product=related_product_2,
sort_order=0,
page=product_page, related_product=related_product_2, sort_order=1
)
buyersguide_factories.RelatedProductsFactory(
page=product_page,
related_product=related_product_3,
sort_order=1,
page=product_page, related_product=related_product_3, sort_order=3
)

related_preview_products = product_page.preview_related_products
preview_methods_related_products = product_page.preview_related_products

self.assertEqual(len(related_preview_products), 3)
# Order should be product 2, product 3, product 1.
# Order should be product 2, product 1, product 3.
self.assertListEqual(
related_preview_products,
[related_product_2, related_product_3, related_product_1],
preview_methods_related_products,
[related_product_2, related_product_1, related_product_3],
)

# Grab preview template response, find all related product titles in HTML.
preview_response = product_page.make_preview_request()
soup = bs4.BeautifulSoup(preview_response.content, "html.parser")
related_product_titles_from_preview_template = [
p.get_text(strip=True) for p in soup.find_all("p", class_="product-title")
]

# Check if template renders related products as expected.
# Order should be: product 2, product 1, product 3.
self.assertEqual(
related_product_titles_from_preview_template,
[related_product_2.title, related_product_1.title, related_product_3.title],
)

def test_preview_related_products_after_deletion(self):
"""
Tests if `preview_related_products` accurately orders and includes related products for page previews,
even after a product relation is deleted.
Tests if `preview_related_products` accurately returns related products for CMS page previews,
even after a related product relation is deleted.
"""
product_page = self.product_page

Expand All @@ -170,44 +179,68 @@ def test_preview_related_products_after_deletion(self):
product_relation_1 = buyersguide_factories.RelatedProductsFactory(
page=product_page,
related_product=related_product_1,
sort_order=2,
)
buyersguide_factories.RelatedProductsFactory(
page=product_page,
related_product=related_product_2,
sort_order=0,
)
buyersguide_factories.RelatedProductsFactory(
page=product_page,
related_product=related_product_3,
sort_order=1,
)

related_preview_products = product_page.preview_related_products
preview_methods_related_products = product_page.preview_related_products

self.assertEqual(len(related_preview_products), 3)
# Order should be product 2, product 3, product 1.
# Check if method returns related products as expected.
# Order should be: product 1, product 2, product 3.
self.assertListEqual(
related_preview_products,
[related_product_2, related_product_3, related_product_1],
preview_methods_related_products,
[related_product_1, related_product_2, related_product_3],
)

# Grab preview template response, find all related product titles in HTML.
preview_response = product_page.make_preview_request()
soup = bs4.BeautifulSoup(preview_response.content, "html.parser")
related_product_titles_from_preview_template = [
p.get_text(strip=True) for p in soup.find_all("p", class_="product-title")
]

# Check if template returns related products as expected.
# Order should be: product 1, product 2, product 3.
self.assertEqual(
related_product_titles_from_preview_template,
[related_product_1.title, related_product_2.title, related_product_3.title],
)

# Remove one product relation
# Remove one product relation.
product_relation_1.delete()

# Fetch the related products again after modification
updated_preview_products = product_page.preview_related_products
# Fetch the related products again after modification.
preview_methods_related_products_after_deletion = product_page.preview_related_products

self.assertEqual(len(updated_preview_products), 2)
# Make sure list returned reflects the deletion.
# Order should be product 2, product 3.
self.assertListEqual(
updated_preview_products,
preview_methods_related_products_after_deletion,
[related_product_2, related_product_3],
)

# Double check that preview template reflects deletion as expected.
preview_response = product_page.make_preview_request()
soup = bs4.BeautifulSoup(preview_response.content, "html.parser")
related_product_titles_from_preview_template = [
p.get_text(strip=True) for p in soup.find_all("p", class_="product-title")
]

# Check if template reflects update.
# Order should be: product 2, product 3, product 1.
self.assertEqual(
related_product_titles_from_preview_template, [related_product_2.title, related_product_3.title]
)

def test_preview_related_products_after_reorder(self):
"""
Tests if `preview_related_products` accurately orders and includes related products for page previews,
Tests if `preview_related_products` accurately returns related products for CMS page previews,
even after related products are reordered.
"""
product_page = self.product_page
Expand All @@ -219,81 +252,65 @@ def test_preview_related_products_after_reorder(self):
product_relation_1 = buyersguide_factories.RelatedProductsFactory(
page=product_page,
related_product=related_product_1,
sort_order=2,
)
product_relation_2 = buyersguide_factories.RelatedProductsFactory(
page=product_page,
related_product=related_product_2,
sort_order=0,
)
product_relation_3 = buyersguide_factories.RelatedProductsFactory(
page=product_page,
related_product=related_product_3,
sort_order=1,
)

related_preview_products = product_page.preview_related_products
preview_methods_related_products = product_page.preview_related_products

# Order should be product 2, product 3, product 1.
# Order should be: product 1, product 2, product 3.
self.assertListEqual(
related_preview_products,
[related_product_2, related_product_3, related_product_1],
preview_methods_related_products,
[related_product_1, related_product_2, related_product_3],
)

# Grab preview template response, find all related product titles in HTML.
preview_response = product_page.make_preview_request()
soup = bs4.BeautifulSoup(preview_response.content, "html.parser")
related_product_titles_from_preview_template = [
p.get_text(strip=True) for p in soup.find_all("p", class_="product-title")
]

# Check if template renders related products as expected.
# Order should be: product 1, product 2, product 3.
self.assertEqual(
related_product_titles_from_preview_template,
[related_product_1.title, related_product_2.title, related_product_3.title],
)

product_relation_1.sort_order = 0
product_relation_1.save()
product_relation_2.sort_order = 1
product_relation_2.save()
product_relation_3.sort_order = 2
product_relation_3.save()
# Update product order in memory
# New order is: product 2, product 1, product 3
product_page.related_product_pages = [product_relation_2, product_relation_1, product_relation_3]

# Fetch the related products again after modification
updated_preview_products = product_page.preview_related_products
preview_methods_related_products_after_reorder = product_page.preview_related_products

# Updated order should now be product 1, product 2, product 3.
# Updated order should now be: product 2, product 1, product 3.
self.assertListEqual(
updated_preview_products,
[related_product_1, related_product_2, related_product_3],
preview_methods_related_products_after_reorder,
[related_product_2, related_product_1, related_product_3],
)

def test_preview_related_products_with_non_default_locale(self):
"""
Verifies `preview_related_products` returns localized related products when applicable.
"""
product_page = self.product_page

first_product = buyersguide_factories.ProductPageFactory(parent=self.bg, title="First product")
second_product = buyersguide_factories.ProductPageFactory(parent=self.bg, title="Second product")

buyersguide_factories.RelatedProductsFactory(
page=product_page,
related_product=first_product,
)
buyersguide_factories.RelatedProductsFactory(
page=product_page,
related_product=second_product,
# Grab preview template response, find all related product titles in HTML.
preview_response = product_page.make_preview_request()
soup = bs4.BeautifulSoup(preview_response.content, "html.parser")
related_product_titles_from_preview_template = [
p.get_text(strip=True) for p in soup.find_all("p", class_="product-title")
]

# Check if template reflects update.
# Order should be: product 2, product 1, product 3.
self.assertEqual(
related_product_titles_from_preview_template,
[related_product_2.title, related_product_1.title, related_product_3.title],
)

related_preview_products_en = product_page.preview_related_products
self.assertIn(first_product, related_preview_products_en)
self.assertIn(second_product, related_preview_products_en)

self.translate_page(product_page, self.fr_locale)
fr_product_page = product_page.get_translation(self.fr_locale)

self.translate_page(first_product, self.fr_locale)
first_product_fr = first_product.get_translation(self.fr_locale)
first_product_fr.title = "Premier produit"
first_product_fr.save_revision().publish()

self.activate_locale(self.fr_locale)

related_preview_products_fr = fr_product_page.preview_related_products
# If there is a localized version, that should be returned:
self.assertIn(first_product_fr, related_preview_products_fr)
# If there is no localized version, the default version should be returned:
self.assertIn(second_product, related_preview_products_fr)

def test_preview_related_products_with_no_products(self):
"""
Tests if `preview_related_products` returns an empty list if no related products are set.
Expand Down

0 comments on commit 2ea26ae

Please sign in to comment.