Skip to content

Commit

Permalink
Add unit test for stockitemtracking API (#7459)
Browse files Browse the repository at this point in the history
  • Loading branch information
SchrodingersGat authored Jun 17, 2024
1 parent db7632e commit e688678
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions src/backend/InvenTree/stock/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io
import os
import random
from datetime import datetime, timedelta
from enum import IntEnum

Expand All @@ -22,6 +23,7 @@
from stock.models import (
StockItem,
StockItemTestResult,
StockItemTracking,
StockLocation,
StockLocationType,
)
Expand Down Expand Up @@ -1770,6 +1772,96 @@ def test_value_choices(self):
)


class StockTrackingTest(StockAPITestCase):
"""Tests for the StockTracking API endpoints."""

fixtures = [
*StockAPITestCase.fixtures,
'build',
'order',
'return_order',
'sales_order',
]

@classmethod
def setUpTestData(cls):
"""Initialize some test data for the StockTracking tests."""
super().setUpTestData()

import build.models
import company.models
import order.models
import stock.models
import stock.status_codes

entries = []

N_BO = build.models.Build.objects.count()
N_PO = order.models.PurchaseOrder.objects.count()
N_RO = order.models.ReturnOrder.objects.count()
N_SO = order.models.SalesOrder.objects.count()

N_COMPANY = company.models.Company.objects.count()
N_LOCATION = stock.models.StockLocation.objects.count()

# Generate a large quantity of tracking items
# Note that the pk values are not guaranteed to exist in the database
for item in StockItem.objects.all():
for i in range(50):
entries.append(
StockItemTracking(
item=item,
notes='This is a test entry',
tracking_type=stock.status_codes.StockHistoryCode.LEGACY.value,
deltas={
'quantity': 50 - i,
'buildorder': random.randint(0, N_BO + 1),
'purchaseorder': random.randint(0, N_PO + 1),
'returnorder': random.randint(0, N_RO + 1),
'salesorder': random.randint(0, N_SO + 1),
'customer': random.randint(0, N_COMPANY + 1),
'location': random.randint(0, N_LOCATION + 1),
},
)
)

StockItemTracking.objects.bulk_create(entries)

def get_url(self):
"""Helper function to get stock tracking api url."""
return reverse('api-stock-tracking-list')

def test_count(self):
"""Test list endpoint with limit = 1."""
url = self.get_url()

N = StockItemTracking.objects.count()

# Test counting
response = self.get(url, {'limit': 1})
self.assertEqual(response.data['count'], N)

def test_list(self):
"""Test list endpoint."""
url = self.get_url()

N = StockItemTracking.objects.count()
self.assertGreater(N, 1000)

response = self.client.get(url, max_query_count=25)
self.assertEqual(len(response.data), N)

# Check expected delta values
keys = ['quantity', 'returnorder', 'buildorder', 'customer']

for item in response.data:
deltas = item['deltas']

for key in keys:
self.assertIn(key, deltas)
self.assertIsNotNone(deltas.get(key, None))


class StockAssignTest(StockAPITestCase):
"""Unit tests for the stock assignment API endpoint, where stock items are manually assigned to a customer."""

Expand Down

0 comments on commit e688678

Please sign in to comment.