Skip to content

Commit

Permalink
Use fake request for images, make the children mapping work
Browse files Browse the repository at this point in the history
  • Loading branch information
viggo-devries committed Jan 2, 2024
1 parent 17f0684 commit c0cfc6e
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 15 deletions.
7 changes: 2 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,15 @@ lint: fail-if-no-virtualenv
pylint oscar_odin/

test: fail-if-no-virtualenv
python3 manage.py makemigrations --check --dry-run
pip install .[test]
@python3 manage.py test tests/

black:
@black oscar_odin/
@black tests/

test:
python3 manage.py test tests/

ill:
rm db.sqlite3
cp klaas.sqlite3 db.sqlite3
python3 manage.py migrate
python3 manage.py test_illshit
python3 manage.py test_queries
4 changes: 2 additions & 2 deletions oscar_odin/management/commands/test_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def create_product(i):
attributes=attributes,
)

products = list(map(create_product, range(0, 5000)))
products = list(map(create_product, range(0, 1000)))

with querycounter("COMMANDO"):
products_to_db(
Expand All @@ -128,4 +128,4 @@ def create_product(i):
+ ALL_PRODUCTIMAGE_FIELDS,
)

print("AANTAL PRODUCTEN AANGEMAAKT:", Product.objects.count())
print("Amount of products created:", Product.objects.count())
16 changes: 15 additions & 1 deletion oscar_odin/mappings/catalogue.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,19 @@ def attributes(self) -> Dict[str, Any]:
for item in self.source.get_attribute_values()
}

@odin.assign_field
def children(self) -> Tuple[Optional[List[resources.catalogue.Product]]]:
"""Children of parent products."""

if self.context.get("include_children", False) and self.source.is_parent:
# Return a tuple as an optional list causes problems.
return (
map_queryset(
ProductToResource, self.source.children, context=self.context
),
)
return (None,)

@odin.assign_field(to_field=("price", "currency", "availability"))
def map_stock_price(self) -> Tuple[Decimal, str, int]:
"""Resolve stock price using strategy and decompose into price/currency/availability."""
Expand All @@ -237,6 +250,8 @@ class ProductToModel(ModelMapping):
from_obj = resources.catalogue.Product
to_obj = ProductModel

mappings = (odin.define(from_field="children", skip_if_none=True),)

@odin.map_list_field
def images(self, values) -> List[ProductImageModel]:
"""Map related image. We save these later in bulk"""
Expand Down Expand Up @@ -383,7 +398,6 @@ def products_to_model(

def products_to_db(
products,
rollback=True,
fields_to_update=ALL_CATALOGUE_FIELDS,
identifier_mapping=MODEL_IDENTIFIERS_MAPPING,
product_mapper=ProductToModel,
Expand Down
4 changes: 4 additions & 0 deletions oscar_odin/resources/catalogue.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,7 @@ class Product(OscarCatalogue):

date_created: datetime
date_updated: datetime

children: Optional[List["Product"]] = odin.ListOf.delayed(
lambda: Product, null=True
)
33 changes: 26 additions & 7 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ pre-commit = {version = "*", optional = true}
test = ["coverage", "pylint", "black", "pylint-django", "poetry"]
dev = ["ruff", "isort", "pre-commit"]

[tool.poetry.group.test.dependencies]
responses = "^0.24.1"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
Expand Down
10 changes: 10 additions & 0 deletions tests/mappings/test_catalogue.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,13 @@ def test_product_to_resource__where_is_a_parent_product_do_not_include_children(
actual = catalogue.product_to_resource(product)

self.assertEqual(product.title, actual.title)
self.assertIsNone(actual.children)

def test_mapping__where_is_a_parent_product_include_children(self):
product = Product.objects.get(id=8)

actual = catalogue.product_to_resource(product, include_children=True)

self.assertEqual(product.title, actual.title)
self.assertIsNotNone(actual.children)
self.assertEqual(3, len(actual.children))
10 changes: 10 additions & 0 deletions tests/reverse/test_reallifecase.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import PIL
import odin
import requests
import responses

from urllib.parse import urlparse

Expand Down Expand Up @@ -139,7 +140,16 @@ def is_discountable(self):


class RealLifeTest(TestCase):
@responses.activate
def test_mapping(self):
responses.add(
responses.GET,
"https://picsum.photos/200/300",
body="Dit is nep content van een image",
status=200,
content_type="image/jpeg",
)

for partner_id in ["1049", "1052", "1053", "1049"]:
Partner.objects.get_or_create(
code=partner_id, defaults={"name": partner_id}
Expand Down

0 comments on commit c0cfc6e

Please sign in to comment.