Skip to content

Commit

Permalink
Merge pull request #140 from inventree/unit-test-fixes
Browse files Browse the repository at this point in the history
Unit test fixes
  • Loading branch information
SchrodingersGat authored Sep 5, 2022
2 parents 0903ee8 + 57949b4 commit 255a9af
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 106 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
invoke wait
- name: Run Tests
run: |
invoke check-server
invoke check-server -d
coverage run -m unittest discover -s test/
- name: Upload Report
run: |
Expand Down
16 changes: 11 additions & 5 deletions inventree/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ class StatusMixin:
can be reached through _statusupdate function
"""

def _statusupdate(self, status: str, data=None, **kwargs):
def _statusupdate(self, status: str, reload=True, data=None, **kwargs):

# Check status
if status not in [
Expand All @@ -540,19 +540,25 @@ def _statusupdate(self, status: str, data=None, **kwargs):
# Set the url
URL = self.URL + f"/{self.pk}/{status}"

if data is None:
data = {}

data.update(kwargs)

# Send data
response = self._api.post(URL, data, **kwargs)
response = self._api.post(URL, data)

# Reload
self.reload()
if reload:
self.reload()

# Return
return response

def complete(self, **kwargs):

return self._statusupdate(status='complete')
return self._statusupdate(status='complete', **kwargs)

def cancel(self, **kwargs):

return self._statusupdate(status='cancel')
return self._statusupdate(status='cancel', **kwargs)
14 changes: 8 additions & 6 deletions inventree/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ def uploadAttachment(self, attachment, comment=''):
order=self.pk,
)

def issue(self):
def issue(self, **kwargs):
"""
Issue the purchase order
"""

# Return
return self._statusupdate(status='issue')
return self._statusupdate(status='issue', **kwargs)


class PurchaseOrderLineItem(inventree.base.InventreeObject):
Expand Down Expand Up @@ -202,9 +202,9 @@ def allocateToShipment(self, shipment, stockitems=None, quantity=None):
possibly because no stock items are available.
"""

# If stockitems are not defined, get the list
# If stockitems are not defined, get the list of available stock items
if stockitems is None:
stockitems = self.getPart().getStockItems()
stockitems = self.getPart().getStockItems(include_variants=False, in_stock=True, available=True)

# If no quantity is defined, calculate the number of required items
# This is the number of sold items not yet allocated, but can not
Expand All @@ -225,6 +225,7 @@ def allocateToShipment(self, shipment, stockitems=None, quantity=None):
# Look through stock items, assign items until the required amount
# is reached
items = list()

for SI in stockitems:

# Check if we are done
Expand Down Expand Up @@ -323,7 +324,8 @@ def complete(
shipment_date=None,
tracking_number='',
invoice_number='',
link=''
link='',
**kwargs
):
"""
Complete the shipment, with given shipment_date, or reasonable
Expand All @@ -339,7 +341,7 @@ def complete(
}

# Return
return self._statusupdate(status='ship', data=data)
return self._statusupdate(status='ship', data=data, **kwargs)

def ship(self, *args, **kwargs):
"""Alias for complete function"""
Expand Down
8 changes: 4 additions & 4 deletions inventree/part.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ def getSupplierParts(self):
""" Return the supplier parts associated with this part """
return inventree.company.SupplierPart.list(self._api, part=self.pk)

def getBomItems(self):
def getBomItems(self, **kwargs):
""" Return the items required to make this part """
return BomItem.list(self._api, part=self.pk)
return BomItem.list(self._api, part=self.pk, **kwargs)

def isUsedIn(self):
""" Return a list of all the parts this part is used in """
Expand All @@ -86,9 +86,9 @@ def getBuilds(self, **kwargs):
""" Return the builds associated with this part """
return inventree.build.Build.list(self._api, part=self.pk, **kwargs)

def getStockItems(self):
def getStockItems(self, **kwargs):
""" Return the stock items associated with this part """
return inventree.stock.StockItem.list(self._api, part=self.pk)
return inventree.stock.StockItem.list(self._api, part=self.pk, **kwargs)

def getParameters(self):
""" Return parameters associated with this part """
Expand Down
22 changes: 15 additions & 7 deletions tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ def reset_data(c, debug=False):
# Reset the database to a known state
print("Reset test database to a known state (this might take a little while...)")

c.run("docker-compose -f test/docker-compose.yml run inventree-py-test-server invoke migrate", hide=None if debug else 'both')
c.run("docker-compose -f test/docker-compose.yml run inventree-py-test-server invoke delete-data -f", hide=None if debug else 'both')
c.run("docker-compose -f test/docker-compose.yml run inventree-py-test-server invoke import-fixtures", hide=None if debug else 'both')
hide = None if debug else 'both'

c.run("docker-compose -f test/docker-compose.yml run inventree-py-test-server invoke migrate", hide=hide)
c.run("docker-compose -f test/docker-compose.yml run inventree-py-test-server invoke delete-data -f", hide=hide)
c.run("docker-compose -f test/docker-compose.yml run inventree-py-test-server invoke import-fixtures", hide=hide)


@task(post=[reset_data])
Expand All @@ -45,7 +47,10 @@ def update_image(c, debug=True):

print("Pulling latest InvenTree image from docker hub (maybe grab a coffee!)")

c.run("docker-compose -f test/docker-compose.yml pull", hide=None if debug else 'both')
hide = None if debug else 'both'

c.run("docker-compose -f test/docker-compose.yml pull", hide=hide)
c.run("docker-compose -f test/docker-compose.yml run inventree-py-test-server invoke update", hide=hide)


@task
Expand All @@ -70,11 +75,14 @@ def check_server(c, host="http://localhost:12345", username="testuser", password
print("Error:", str(e))

if response is None:
timeout -= 1

time.sleep(1)
if timeout > 0:
if debug:
print(f"No response from server. {timeout} seconds remaining")
timeout -= 1
time.sleep(1)

if timeout <= 0:
else:
return False

if response.status_code != 200:
Expand Down
7 changes: 2 additions & 5 deletions test/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,11 @@ services:
environment:
- INVENTREE_DEBUG=True
- INVENTREE_DB_ENGINE=sqlite
- INVENTREE_DB_NAME=/home/inventree/db/test_db.sqlite3
- INVENTREE_DB_NAME=/home/inventree/data/test_db.sqlite3
- INVENTREE_DEBUG_LEVEL=error
- INVENTREE_ADMIN_USER=testuser
- INVENTREE_ADMIN_PASSWORD=testpassword
- [email protected]
restart: unless-stopped
volumes:
# Note: media and static files are ephemeral
- ./data/media:/home/inventree/media/
- ./data/static:/home/inventree/static/
- ./data/db:/home/inventree/db/
- ./data:/home/inventree/data
Loading

0 comments on commit 255a9af

Please sign in to comment.