mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Fixes for stock api unit tests
- Remove old unit tests - Require quantity when creating a new stock item
This commit is contained in:
parent
f27acde934
commit
78ac40083a
@ -12,39 +12,39 @@ from django.conf.urls import url, include
|
|||||||
from django.http import JsonResponse
|
from django.http import JsonResponse
|
||||||
from django.db.models import Q
|
from django.db.models import Q
|
||||||
from django.db import transaction
|
from django.db import transaction
|
||||||
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
|
||||||
|
from django_filters.rest_framework import DjangoFilterBackend
|
||||||
|
from django_filters import rest_framework as rest_filters
|
||||||
|
|
||||||
from rest_framework import status
|
from rest_framework import status
|
||||||
from rest_framework.serializers import ValidationError
|
from rest_framework.serializers import ValidationError
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework import generics, filters
|
from rest_framework import generics, filters
|
||||||
|
|
||||||
from django_filters.rest_framework import DjangoFilterBackend
|
import common.settings
|
||||||
from django_filters import rest_framework as rest_filters
|
import common.models
|
||||||
|
|
||||||
from .models import StockLocation, StockItem
|
|
||||||
from .models import StockItemTracking
|
|
||||||
from .models import StockItemAttachment
|
|
||||||
from .models import StockItemTestResult
|
|
||||||
|
|
||||||
from part.models import BomItem, Part, PartCategory
|
|
||||||
from part.serializers import PartBriefSerializer
|
|
||||||
|
|
||||||
from company.models import Company, SupplierPart
|
from company.models import Company, SupplierPart
|
||||||
from company.serializers import CompanySerializer, SupplierPartSerializer
|
from company.serializers import CompanySerializer, SupplierPartSerializer
|
||||||
|
|
||||||
|
from InvenTree.helpers import str2bool, isNull, extract_serial_numbers
|
||||||
|
from InvenTree.api import AttachmentMixin
|
||||||
|
from InvenTree.filters import InvenTreeOrderingFilter
|
||||||
|
|
||||||
from order.models import PurchaseOrder
|
from order.models import PurchaseOrder
|
||||||
from order.models import SalesOrder, SalesOrderAllocation
|
from order.models import SalesOrder, SalesOrderAllocation
|
||||||
from order.serializers import POSerializer
|
from order.serializers import POSerializer
|
||||||
|
|
||||||
import common.settings
|
from part.models import BomItem, Part, PartCategory
|
||||||
import common.models
|
from part.serializers import PartBriefSerializer
|
||||||
|
|
||||||
|
from stock.models import StockLocation, StockItem
|
||||||
|
from stock.models import StockItemTracking
|
||||||
|
from stock.models import StockItemAttachment
|
||||||
|
from stock.models import StockItemTestResult
|
||||||
import stock.serializers as StockSerializers
|
import stock.serializers as StockSerializers
|
||||||
|
|
||||||
from InvenTree.helpers import str2bool, isNull, extract_serial_numbers
|
|
||||||
from InvenTree.api import AttachmentMixin
|
|
||||||
from InvenTree.filters import InvenTreeOrderingFilter
|
|
||||||
|
|
||||||
|
|
||||||
class StockDetail(generics.RetrieveUpdateDestroyAPIView):
|
class StockDetail(generics.RetrieveUpdateDestroyAPIView):
|
||||||
""" API detail endpoint for Stock object
|
""" API detail endpoint for Stock object
|
||||||
@ -411,7 +411,12 @@ class StockList(generics.ListCreateAPIView):
|
|||||||
# Check if a set of serial numbers was provided
|
# Check if a set of serial numbers was provided
|
||||||
serial_numbers = data.get('serial_numbers', '')
|
serial_numbers = data.get('serial_numbers', '')
|
||||||
|
|
||||||
quantity = data['quantity']
|
quantity = data.get('quantity', None)
|
||||||
|
|
||||||
|
if quantity is None:
|
||||||
|
raise ValidationError({
|
||||||
|
'quantity': _('Quantity is required'),
|
||||||
|
})
|
||||||
|
|
||||||
notes = data.get('notes', '')
|
notes = data.get('notes', '')
|
||||||
|
|
||||||
|
@ -364,24 +364,22 @@ class StockItemTest(StockAPITestCase):
|
|||||||
'part': 1,
|
'part': 1,
|
||||||
'location': 1,
|
'location': 1,
|
||||||
},
|
},
|
||||||
expected_code=201,
|
expected_code=400
|
||||||
)
|
)
|
||||||
|
|
||||||
# Item should have been created with default quantity
|
self.assertIn('Quantity is required', str(response.data))
|
||||||
self.assertEqual(response.data['quantity'], 1)
|
|
||||||
|
|
||||||
# POST with quantity and part and location
|
# POST with quantity and part and location
|
||||||
response = self.client.post(
|
response = self.post(
|
||||||
self.list_url,
|
self.list_url,
|
||||||
data={
|
data={
|
||||||
'part': 1,
|
'part': 1,
|
||||||
'location': 1,
|
'location': 1,
|
||||||
'quantity': 10,
|
'quantity': 10,
|
||||||
}
|
},
|
||||||
|
expected_code=201
|
||||||
)
|
)
|
||||||
|
|
||||||
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
|
||||||
|
|
||||||
def test_default_expiry(self):
|
def test_default_expiry(self):
|
||||||
"""
|
"""
|
||||||
Test that the "default_expiry" functionality works via the API.
|
Test that the "default_expiry" functionality works via the API.
|
||||||
|
@ -60,70 +60,6 @@ class StockListTest(StockViewTestCase):
|
|||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
|
|
||||||
|
|
||||||
class StockItemTest(StockViewTestCase):
|
|
||||||
"""" Tests for StockItem views """
|
|
||||||
|
|
||||||
def test_qr_code(self):
|
|
||||||
# QR code for a valid item
|
|
||||||
response = self.client.get(reverse('stock-item-qr', args=(1,)), HTTP_X_REQUESTED_WITH='XMLHttpRequest')
|
|
||||||
self.assertEqual(response.status_code, 200)
|
|
||||||
|
|
||||||
# QR code for an invalid item
|
|
||||||
response = self.client.get(reverse('stock-item-qr', args=(9999,)), HTTP_X_REQUESTED_WITH='XMLHttpRequest')
|
|
||||||
self.assertEqual(response.status_code, 200)
|
|
||||||
|
|
||||||
def test_create_item(self):
|
|
||||||
"""
|
|
||||||
Test creation of StockItem
|
|
||||||
"""
|
|
||||||
|
|
||||||
url = reverse('stock-item-create')
|
|
||||||
|
|
||||||
response = self.client.get(url, {'part': 1}, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
|
|
||||||
self.assertEqual(response.status_code, 200)
|
|
||||||
|
|
||||||
response = self.client.get(url, {'part': 999}, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
|
|
||||||
self.assertEqual(response.status_code, 200)
|
|
||||||
|
|
||||||
# Copy from a valid item, valid location
|
|
||||||
response = self.client.get(url, {'location': 1, 'copy': 1}, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
|
|
||||||
self.assertEqual(response.status_code, 200)
|
|
||||||
|
|
||||||
# Copy from an invalid item, invalid location
|
|
||||||
response = self.client.get(url, {'location': 999, 'copy': 9999}, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
|
|
||||||
self.assertEqual(response.status_code, 200)
|
|
||||||
|
|
||||||
def test_create_stock_with_expiry(self):
|
|
||||||
"""
|
|
||||||
Test creation of stock item of a part with an expiry date.
|
|
||||||
The initial value for the "expiry_date" field should be pre-filled,
|
|
||||||
and should be in the future!
|
|
||||||
"""
|
|
||||||
|
|
||||||
# First, ensure that the expiry date feature is enabled!
|
|
||||||
InvenTreeSetting.set_setting('STOCK_ENABLE_EXPIRY', True, self.user)
|
|
||||||
|
|
||||||
url = reverse('stock-item-create')
|
|
||||||
|
|
||||||
response = self.client.get(url, {'part': 25}, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
|
|
||||||
|
|
||||||
self.assertEqual(response.status_code, 200)
|
|
||||||
|
|
||||||
# We are expecting 10 days in the future
|
|
||||||
expiry = datetime.now().date() + timedelta(10)
|
|
||||||
|
|
||||||
expected = f'name=\\\\"expiry_date\\\\" value=\\\\"{expiry.isoformat()}\\\\"'
|
|
||||||
|
|
||||||
self.assertIn(expected, str(response.content))
|
|
||||||
|
|
||||||
# Now check with a part which does *not* have a default expiry period
|
|
||||||
response = self.client.get(url, {'part': 1}, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
|
|
||||||
|
|
||||||
expected = 'name=\\\\"expiry_date\\\\" placeholder=\\\\"\\\\"'
|
|
||||||
|
|
||||||
self.assertIn(expected, str(response.content))
|
|
||||||
|
|
||||||
|
|
||||||
class StockOwnershipTest(StockViewTestCase):
|
class StockOwnershipTest(StockViewTestCase):
|
||||||
""" Tests for stock ownership views """
|
""" Tests for stock ownership views """
|
||||||
|
|
||||||
|
@ -45,8 +45,6 @@ stock_urls = [
|
|||||||
# Stock location
|
# Stock location
|
||||||
url(r'^location/', include(location_urls)),
|
url(r'^location/', include(location_urls)),
|
||||||
|
|
||||||
url(r'^item/new/?', views.StockItemCreate.as_view(), name='stock-item-create'),
|
|
||||||
|
|
||||||
url(r'^item/uninstall/', views.StockItemUninstall.as_view(), name='stock-item-uninstall'),
|
url(r'^item/uninstall/', views.StockItemUninstall.as_view(), name='stock-item-uninstall'),
|
||||||
|
|
||||||
url(r'^track/', include(stock_tracking_urls)),
|
url(r'^track/', include(stock_tracking_urls)),
|
||||||
|
Loading…
Reference in New Issue
Block a user