Skip to content

Commit

Permalink
Fix for list which raises an error (#247)
Browse files Browse the repository at this point in the history
* Fix for list which raises an error

* Test for raising error

* PEP fix
  • Loading branch information
SchrodingersGat authored Oct 16, 2024
1 parent 42aa14c commit 80ff58d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
18 changes: 15 additions & 3 deletions inventree/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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 = []

Expand Down
37 changes: 37 additions & 0 deletions test/test_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down

0 comments on commit 80ff58d

Please sign in to comment.