2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-06-19 05:25:42 +00:00

Label printing unit test (#3047)

* Adds a very simple sample plugin for label printing

* Test mixin install status and API query

* Better error reporting for label printing API

* pep fixes

* fix assertation

* remove broken assertation

* igonre for coverage

* test the base process of printing

* refactor tests

* clean up basic test

* refactor url

* fix url creation

* test printing multiples

* test all printing endpoints

* test all list options - move api tests

* test for invalid filters

* refactor

* test with no part

* these should not happen
checks are in place upstream

* fix assertation

* do not cover continue parts

* test for wrong implementation

* ignore DB not ready

* remove covage from default parts

* fix url generation

* test debug mode

* fix url assertation

* check that nothing was rendered

Co-authored-by: Oliver Walters <oliver.henry.walters@gmail.com>
This commit is contained in:
Matthias Mair
2022-05-23 00:54:44 +02:00
committed by GitHub
parent 6247eecf69
commit 840ade25cd
8 changed files with 252 additions and 59 deletions

View File

@ -4,12 +4,11 @@ from django.conf import settings
from django.core.exceptions import FieldError, ValidationError
from django.http import HttpResponse, JsonResponse
from django.urls import include, re_path
from django.utils.translation import gettext_lazy as _
from django_filters.rest_framework import DjangoFilterBackend
from PIL import Image
from rest_framework import filters, generics
from rest_framework.response import Response
from rest_framework.exceptions import NotFound
import common.models
import InvenTree.helpers
@ -62,10 +61,14 @@ class LabelPrintMixin:
"""
if not settings.PLUGINS_ENABLED:
return None
return None # pragma: no cover
plugin_key = request.query_params.get('plugin', None)
# No plugin provided, and that's OK
if plugin_key is None:
return None
plugin = registry.get_plugin(plugin_key)
if plugin:
@ -74,9 +77,10 @@ class LabelPrintMixin:
if config and config.active:
# Only return the plugin if it is enabled!
return plugin
# No matches found
return None
else:
raise ValidationError(f"Plugin '{plugin_key}' is not enabled")
else:
raise NotFound(f"Plugin '{plugin_key}' not found")
def print(self, request, items_to_print):
"""
@ -85,13 +89,11 @@ class LabelPrintMixin:
# Check the request to determine if the user has selected a label printing plugin
plugin = self.get_plugin(request)
if len(items_to_print) == 0:
# No valid items provided, return an error message
data = {
'error': _('No valid objects provided to template'),
}
return Response(data, status=400)
raise ValidationError('No valid objects provided to label template')
outputs = []
@ -281,7 +283,7 @@ class StockItemLabelList(LabelListView, StockItemLabelMixin):
# Filter string defined for the StockItemLabel object
try:
filters = InvenTree.helpers.validateFilterString(label.filters)
except ValidationError:
except ValidationError: # pragma: no cover
continue
for item in items:
@ -300,7 +302,7 @@ class StockItemLabelList(LabelListView, StockItemLabelMixin):
if matches:
valid_label_ids.add(label.pk)
else:
continue
continue # pragma: no cover
# Reduce queryset to only valid matches
queryset = queryset.filter(pk__in=[pk for pk in valid_label_ids])
@ -412,7 +414,7 @@ class StockLocationLabelList(LabelListView, StockLocationLabelMixin):
# Filter string defined for the StockLocationLabel object
try:
filters = InvenTree.helpers.validateFilterString(label.filters)
except:
except: # pragma: no cover
# Skip if there was an error validating the filters...
continue
@ -432,7 +434,7 @@ class StockLocationLabelList(LabelListView, StockLocationLabelMixin):
if matches:
valid_label_ids.add(label.pk)
else:
continue
continue # pragma: no cover
# Reduce queryset to only valid matches
queryset = queryset.filter(pk__in=[pk for pk in valid_label_ids])
@ -519,7 +521,7 @@ class PartLabelList(LabelListView, PartLabelMixin):
try:
filters = InvenTree.helpers.validateFilterString(label.filters)
except ValidationError:
except ValidationError: # pragma: no cover
continue
for part in parts:

View File

@ -46,7 +46,7 @@ class LabelConfig(AppConfig):
try:
from .models import StockLocationLabel
assert bool(StockLocationLabel is not None)
except AppRegistryNotReady:
except AppRegistryNotReady: # pragma: no cover
# Database might not yet be ready
warnings.warn('Database was not ready for creating labels')
return

View File

@ -171,7 +171,7 @@ class LabelTemplate(models.Model):
Note: Override this in any subclass
"""
return {}
return {} # pragma: no cover
def generate_filename(self, request, **kwargs):
"""
@ -242,7 +242,7 @@ class StockItemLabel(LabelTemplate):
@staticmethod
def get_api_url():
return reverse('api-stockitem-label-list')
return reverse('api-stockitem-label-list') # pragma: no cover
SUBDIR = "stockitem"
@ -302,7 +302,7 @@ class StockLocationLabel(LabelTemplate):
@staticmethod
def get_api_url():
return reverse('api-stocklocation-label-list')
return reverse('api-stocklocation-label-list') # pragma: no cover
SUBDIR = "stocklocation"
@ -349,7 +349,7 @@ class PartLabel(LabelTemplate):
@staticmethod
def get_api_url():
return reverse('api-part-label-list')
return reverse('api-part-label-list') # pragma: no cover
SUBDIR = 'part'

View File

@ -63,39 +63,3 @@ class TestReportTests(InvenTreeAPITestCase):
'items': [10, 11, 12],
}
)
class TestLabels(InvenTreeAPITestCase):
"""
Tests for the label APIs
"""
fixtures = [
'category',
'part',
'location',
'stock',
]
roles = [
'stock.view',
'stock_location.view',
]
def do_list(self, filters={}):
response = self.client.get(self.list_url, filters, format='json')
self.assertEqual(response.status_code, 200)
return response.data
def test_lists(self):
self.list_url = reverse('api-stockitem-label-list')
self.do_list()
self.list_url = reverse('api-stocklocation-label-list')
self.do_list()
self.list_url = reverse('api-part-label-list')
self.do_list()