From f370ac7ce21f8d17e4f82a5e68adbb3081aef6fe Mon Sep 17 00:00:00 2001 From: Juergen Edelbluth Date: Wed, 17 Aug 2022 13:10:24 +0200 Subject: [PATCH] Additional Example Cases --- pyproject.toml | 2 +- tests/assets/blog-example.csv | 4 +++ tests/test_blog_example.py | 57 +++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 tests/assets/blog-example.csv create mode 100644 tests/test_blog_example.py diff --git a/pyproject.toml b/pyproject.toml index 32e3c76..3606bd8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "pytest-csv-params" -version = "0.3.2" +version = "0.3.3" description = "Pytest plugin for Test Case Parametrization with CSV files" authors = ["Juergen Edelbluth "] license = "MIT" diff --git a/tests/assets/blog-example.csv b/tests/assets/blog-example.csv new file mode 100644 index 0000000..79ba5e9 --- /dev/null +++ b/tests/assets/blog-example.csv @@ -0,0 +1,4 @@ +"Order-Ref #", "Anz. Schrauben-Päck.", "Dim. Schrauben-Päck.", "Anz. Scheiben-Päck.", "Dim. Scheiben-Päck.", "Volumen Container" +"221-12-A-24", "670", "30 x 50 x 70 mm", "150", "40 x 50 x 70 mm", "1 m³" +"281-13-C-15", "5000", "30 x 50 x 70 mm", "10000", "40 x 50 x 70 mm", "5 m³" +"281-13-C-76", "50000", "35 x 55 x 75 mm", "5000", "50 x 60 x 90 mm", "10 m³" diff --git a/tests/test_blog_example.py b/tests/test_blog_example.py new file mode 100644 index 0000000..b1cb06b --- /dev/null +++ b/tests/test_blog_example.py @@ -0,0 +1,57 @@ +""" +This is a test example for a blog post +""" +import re +from math import ceil +from os.path import join, dirname + +from pytest_csv_params.decorator import csv_params + + +def get_volume(size_data: str) -> int: + """ + Get the volume from size data, return it as mm³ + """ + matcher = re.compile(r"^\D*(?P\d+)\D+(?P\d+)\D+(?P\d+)\D+$").match(size_data) + return int(matcher.group("l")) * int(matcher.group("d")) * int(matcher.group("h")) + + +def get_container_volume(container_size: str) -> int: + """ + Get the container size (remove the unit, as mm³) + """ + + matcher = re.compile(r"^\D*(?P\d+)\D+$").match(container_size) + return int(matcher.group("size")) * 1_000_000_000 + + +@csv_params( + data_file=join(dirname(__file__), "assets", "blog-example.csv"), + id_col="Order-Ref #", + header_renames={ + "Anz. Schrauben-Päck.": "anz_schrauben", + "Dim. Schrauben-Päck.": "vol_schrauben", + "Anz. Scheiben-Päck.": "anz_scheiben", + "Dim. Scheiben-Päck.": "vol_scheiben", + "Volumen Container": "vol_container", + }, + data_casts={ + "anz_schrauben": int, + "anz_scheiben": int, + "vol_schrauben": get_volume, + "vol_scheiben": get_volume, + "vol_container": get_container_volume, + }, +) +def test_does_it_fit(anz_schrauben: int, vol_schrauben: int, anz_scheiben: int, vol_scheiben: int, vol_container: int) -> None: + """ + Simple test to see if all fits in an container + """ + + available_container_sizes = map(lambda x: x * 1_000_000_000, [1, 5, 10]) + + size_required = (anz_schrauben * vol_schrauben) + (anz_scheiben * vol_scheiben) + + # Smallest possible Container ordered? + smallest_possible_container = min(filter(lambda x: x >= size_required, available_container_sizes)) + assert vol_container == smallest_possible_container