From 1c4f9034d4b850d57ab7d4cdfc96329e56b3af2a Mon Sep 17 00:00:00 2001 From: SebRut Date: Fri, 28 Jun 2019 13:47:22 +0200 Subject: [PATCH] Fixes #9 --- pygrocy/grocy.py | 6 ++++++ pygrocy/grocy_api_client.py | 12 +++++++++++- setup.py | 2 +- test/test_productDetailsResponse.py | 10 +++++++++- 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/pygrocy/grocy.py b/pygrocy/grocy.py index 87f0f90..d23ac97 100644 --- a/pygrocy/grocy.py +++ b/pygrocy/grocy.py @@ -15,12 +15,14 @@ def __init__(self, stock_response: CurrentStockResponse): self._best_before_date = stock_response.best_before_date self._name = None + self._barcodes = None def get_details(self, api_client: GrocyApiClient): details = api_client.get_product(self.product_id) if details is None: return self._name = details.product.name + self._barcodes = details.product.barcodes @property def name(self) -> str: @@ -38,6 +40,10 @@ def available_amount(self) -> float: def best_before_date(self) -> datetime: return self._best_before_date + @property + def barcodes(self) -> List[str]: + return self._barcodes + class Chore(object): def __init__(self, raw_chore: CurrentChoreResponse): diff --git a/pygrocy/grocy_api_client.py b/pygrocy/grocy_api_client.py index b162144..7fac87c 100644 --- a/pygrocy/grocy_api_client.py +++ b/pygrocy/grocy_api_client.py @@ -34,13 +34,19 @@ def __init__(self, parsed_json): self._qu_id_stock = parse_int(parsed_json.get('qu_id_stock', None)) self._qu_id_purchase = parse_int(parsed_json.get('qu_id_purchsase', None)) self._qu_factor_purchase_to_stock = parse_float(parsed_json.get('qu_factor_purchase_to_stock', None)) - self._barcodes = parsed_json.get('barcode', "").split(",") self._picture_file_name = parsed_json.get('picture_file_name', None) self._allow_partial_units_in_stock = bool(parsed_json.get('allow_partial_units_in_stock', None) == "true") self._row_created_timestamp = parse_date(parsed_json.get('row_created_timestamp', None)) self._min_stock_amount = parse_int(parsed_json.get('min_stock_amount', None), 0) self._default_best_before_days = parse_int(parsed_json.get('default_best_before_days', None)) + barcodes_raw = parsed_json.get('barcode', "") + if barcodes_raw is None: + self._barcodes = None + else: + self._barcodes = barcodes_raw.split(",") + + @property def id(self) -> int: return self._id @@ -49,6 +55,10 @@ def id(self) -> int: def name(self) -> str: return self._name + @property + def barcodes(self) -> List[str]: + return self._barcodes + class ChoreData(object): def __init__(self, parsed_json): diff --git a/setup.py b/setup.py index cd734e6..f066d73 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setuptools.setup( name="pygrocy", - version="0.7", + version="0.8", author="Sebastian Rutofski", author_email="kontakt@sebastian-rutofski.de", description="", diff --git a/test/test_productDetailsResponse.py b/test/test_productDetailsResponse.py index 77cabd8..d77b15c 100644 --- a/test/test_productDetailsResponse.py +++ b/test/test_productDetailsResponse.py @@ -5,8 +5,14 @@ class TestProductDetailsResponse(TestCase): + def test_barcode_null(self): + input_json = """{ "product": { "id": 0, "name": "string", "description": "string", "location_id": 0, "qu_id_purchase": 0, "qu_id_stock": 0, "qu_factor_purchase_to_stock": 0, "barcode": null, "min_stock_amount": 0, "default_best_before_days": 0, "picture_file_name": "string", "allow_partial_units_in_stock": true, "row_created_timestamp": "2019-04-22T09:54:15.835Z" }, "quantity_unit_purchase": { "id": 0, "name": "string", "name_plural": "string", "description": "string", "row_created_timestamp": "2019-04-22T09:54:15.835Z" }, "quantity_unit_stock": { "id": 0, "name": "string", "name_plural": "string", "description": "string", "row_created_timestamp": "2019-04-22T09:54:15.835Z" }, "last_purchased": "2019-04-22", "last_used": "2019-04-22T09:54:15.835Z", "stock_amount": 10, "stock_amount_opened": 2, "next_best_before_date": "2019-04-22T09:54:15.835Z", "last_price": 0, "location": { "id": 0, "name": "string", "description": "string", "row_created_timestamp": "2019-04-22T09:54:15.835Z" } }""" + response = ProductDetailsResponse(json.loads(input_json)) + + assert response.product.barcodes is None + def test_parse(self): - input_json = """{ "product": { "id": 0, "name": "string", "description": "string", "location_id": 0, "qu_id_purchase": 0, "qu_id_stock": 0, "qu_factor_purchase_to_stock": 0, "barcode": "string", "min_stock_amount": 0, "default_best_before_days": 0, "picture_file_name": "string", "allow_partial_units_in_stock": true, "row_created_timestamp": "2019-04-22T09:54:15.835Z" }, "quantity_unit_purchase": { "id": 0, "name": "string", "name_plural": "string", "description": "string", "row_created_timestamp": "2019-04-22T09:54:15.835Z" }, "quantity_unit_stock": { "id": 0, "name": "string", "name_plural": "string", "description": "string", "row_created_timestamp": "2019-04-22T09:54:15.835Z" }, "last_purchased": "2019-04-22", "last_used": "2019-04-22T09:54:15.835Z", "stock_amount": 10, "stock_amount_opened": 2, "next_best_before_date": "2019-04-22T09:54:15.835Z", "last_price": 0, "location": { "id": 0, "name": "string", "description": "string", "row_created_timestamp": "2019-04-22T09:54:15.835Z" } }""" + input_json = """{ "product": { "id": 0, "name": "string", "description": "string", "location_id": 0, "qu_id_purchase": 0, "qu_id_stock": 0, "qu_factor_purchase_to_stock": 0, "barcode": "string,123", "min_stock_amount": 0, "default_best_before_days": 0, "picture_file_name": "string", "allow_partial_units_in_stock": true, "row_created_timestamp": "2019-04-22T09:54:15.835Z" }, "quantity_unit_purchase": { "id": 0, "name": "string", "name_plural": "string", "description": "string", "row_created_timestamp": "2019-04-22T09:54:15.835Z" }, "quantity_unit_stock": { "id": 0, "name": "string", "name_plural": "string", "description": "string", "row_created_timestamp": "2019-04-22T09:54:15.835Z" }, "last_purchased": "2019-04-22", "last_used": "2019-04-22T09:54:15.835Z", "stock_amount": 10, "stock_amount_opened": 2, "next_best_before_date": "2019-04-22T09:54:15.835Z", "last_price": 0, "location": { "id": 0, "name": "string", "description": "string", "row_created_timestamp": "2019-04-22T09:54:15.835Z" } }""" response = ProductDetailsResponse(json.loads(input_json)) assert response.stock_amount == 10 @@ -30,3 +36,5 @@ def test_parse(self): assert response.last_purchased.year == 2019 assert response.last_purchased.month == 4 assert response.last_purchased.day == 22 + + assert response.product.barcodes == ["string","123"]