Skip to content
This repository has been archived by the owner on Apr 23, 2024. It is now read-only.

Commit

Permalink
Fixes #9
Browse files Browse the repository at this point in the history
  • Loading branch information
SebRut committed Jun 28, 2019
1 parent b7b6962 commit 1c4f903
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
6 changes: 6 additions & 0 deletions pygrocy/grocy.py
Expand Up @@ -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:
Expand All @@ -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):
Expand Down
12 changes: 11 additions & 1 deletion pygrocy/grocy_api_client.py
Expand Up @@ -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
Expand All @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="pygrocy",
version="0.7",
version="0.8",
author="Sebastian Rutofski",
author_email="kontakt@sebastian-rutofski.de",
description="",
Expand Down
10 changes: 9 additions & 1 deletion test/test_productDetailsResponse.py
Expand Up @@ -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
Expand All @@ -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"]

0 comments on commit 1c4f903

Please sign in to comment.