From 80ff58de0757cc6ce37a83bab0a1355819745746 Mon Sep 17 00:00:00 2001 From: Oliver Date: Wed, 16 Oct 2024 20:23:25 +1100 Subject: [PATCH] Fix for list which raises an error (#247) * Fix for list which raises an error * Test for raising error * PEP fix --- inventree/base.py | 18 +++++++++++++++--- test/test_order.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/inventree/base.py b/inventree/base.py index 216de7a..4a56c48 100644 --- a/inventree/base.py +++ b/inventree/base.py @@ -2,11 +2,12 @@ import json import logging +import requests import os from . import api as inventree_api -INVENTREE_PYTHON_VERSION = "0.17.1" +INVENTREE_PYTHON_VERSION = "0.17.2" logger = logging.getLogger('inventree') @@ -231,10 +232,21 @@ def list(cls, api, **kwargs): else: url = cls.URL - response = api.get(url=url, params=kwargs) + try: + response = api.get(url=url, params=kwargs) + except requests.exceptions.HTTPError as e: + logger.error(f"Error during list request: {e}") + # Return an empty list + + raise_error = kwargs.get('raise_error', False) + + if raise_error: + raise e + else: + return [] if response is None: - return None + return [] items = [] diff --git a/test/test_order.py b/test/test_order.py index c823f2d..ef552d7 100644 --- a/test/test_order.py +++ b/test/test_order.py @@ -390,6 +390,43 @@ def test_po_attachment(self): attachments = po.getAttachments() self.assertEqual(len(attachments), 3) + def test_invalid_list(self): + """Test list with an invalid parameter. + + Ref: https://github.com/inventree/inventree-python/issues/246 + """ + + from inventree.project_code import ProjectCode + + results = order.PurchaseOrder.list(self.api, project_code=999999999) + self.assertEqual(len(results), 0) + + # Try the same again, but raise the eror + with self.assertRaises(HTTPError): + results = order.PurchaseOrder.list( + self.api, + project_code=999999999, + raise_error=True + ) + + # Find a valid project code + n = ProjectCode.count(self.api) + + # Create a new project code + pc = ProjectCode.create(self.api, { + 'code': f"TEST-{n+1}", + 'description': 'Test project code', + }) + + # Attach project code to an order + po = order.PurchaseOrder.list(self.api, limit=1)[0] + po.save({'project_code': pc.pk}) + + # Now, list orders with the project code + results = order.PurchaseOrder.list(self.api, project_code=pc.pk) + self.assertEqual(len(results), 1) + self.assertEqual(results[0].pk, po.pk) + class SOTest(InvenTreeTestCase): """