mirror of
https://github.com/inventree/InvenTree.git
synced 2025-04-28 03:26:45 +00:00
* Squashed commit of the following: commit f5cf7b2e7872fc19633321713965763d1890b495 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 20:36:57 2024 +0100 fixed reqs commit 9d845bee98befa4e53c2ac3c783bd704369e3ad2 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 20:32:35 2024 +0100 disable autofix/format commit aff5f271484c3500df7ddde043767c008ce4af21 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 20:28:50 2024 +0100 adjust checks commit 47271cf1efa848ec8374a0d83b5646d06fffa6e7 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 20:28:22 2024 +0100 reorder order of operations commit e1bf178b40b3f0d2d59ba92209156c43095959d2 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 20:01:09 2024 +0100 adapted ruff settings to better fit code base commit ad7d88a6f4f15c9552522131c4e207256fc2bbf6 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 19:59:45 2024 +0100 auto fixed docstring commit a2e54a760e17932dbbc2de0dec23906107f2cda9 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 19:46:35 2024 +0100 fix getattr useage commit cb80c73bc6c0be7f5d2ed3cc9b2ac03fdefd5c41 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 19:25:09 2024 +0100 fix requirements file commit b7780bbd21a32007f3b0ce495b519bf59bb19bf5 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 18:42:28 2024 +0100 fix removed sections commit 71f1681f55c15f62c16c1d7f30a745adc496db97 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 18:41:21 2024 +0100 fix djlint syntax commit a0bcf1bccef8a8ffd482f38e2063bc9066e1d759 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 18:35:28 2024 +0100 remove flake8 from code base commit 22475b31cc06919785be046e007915e43f356793 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 18:34:56 2024 +0100 remove flake8 from code base commit 0413350f14773ac6161473e0cfb069713c13c691 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 18:24:39 2024 +0100 moved ruff section commit d90c48a0bf98befdfacbbb093ee56cdb28afb40d Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 18:24:24 2024 +0100 move djlint config to pyproject commit c5ce55d5119bf2e35e429986f62f875c86178ae1 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 18:20:39 2024 +0100 added isort again commit 42a41d23afc280d4ee6f0e640148abc6f460f05a Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 18:19:02 2024 +0100 move config section commit 85692331816348cb1145570340d1f6488a8265cc Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 18:17:52 2024 +0100 fix codespell error commit 2897c6704d1311a800ce5aa47878d96d6980b377 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 17:29:21 2024 +0100 replaced flake8 with ruff mostly for speed improvements * enable autoformat * added autofixes * switched to single quotes everywhere * switched to ruff for import sorting * fix wrong url response * switched to pathlib for lookup * fixed lookup * Squashed commit of the following: commit d3b795824b5d6d1c0eda67150b45b5cd672b3f6b Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 22:56:17 2024 +0100 fixed source path commit 0bac0c19b88897a19d5c995e4ff50427718b827e Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 22:47:53 2024 +0100 fixed req commit 9f61f01d9cc01f1fb7123102f3658c890469b8ce Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 22:45:18 2024 +0100 added missing toml req commit 91b71ed24a6761b629768d0ad8829fec2819a966 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 20:49:50 2024 +0100 moved isort config commit 12460b04196b12d0272d40552402476d5492fea5 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 20:43:22 2024 +0100 remove flake8 section from setup.cfg commit f5cf7b2e7872fc19633321713965763d1890b495 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 20:36:57 2024 +0100 fixed reqs commit 9d845bee98befa4e53c2ac3c783bd704369e3ad2 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 20:32:35 2024 +0100 disable autofix/format commit aff5f271484c3500df7ddde043767c008ce4af21 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 20:28:50 2024 +0100 adjust checks commit 47271cf1efa848ec8374a0d83b5646d06fffa6e7 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 20:28:22 2024 +0100 reorder order of operations commit e1bf178b40b3f0d2d59ba92209156c43095959d2 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 20:01:09 2024 +0100 adapted ruff settings to better fit code base commit ad7d88a6f4f15c9552522131c4e207256fc2bbf6 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 19:59:45 2024 +0100 auto fixed docstring commit a2e54a760e17932dbbc2de0dec23906107f2cda9 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 19:46:35 2024 +0100 fix getattr useage commit cb80c73bc6c0be7f5d2ed3cc9b2ac03fdefd5c41 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 19:25:09 2024 +0100 fix requirements file commit b7780bbd21a32007f3b0ce495b519bf59bb19bf5 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 18:42:28 2024 +0100 fix removed sections commit 71f1681f55c15f62c16c1d7f30a745adc496db97 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 18:41:21 2024 +0100 fix djlint syntax commit a0bcf1bccef8a8ffd482f38e2063bc9066e1d759 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 18:35:28 2024 +0100 remove flake8 from code base commit 22475b31cc06919785be046e007915e43f356793 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 18:34:56 2024 +0100 remove flake8 from code base commit 0413350f14773ac6161473e0cfb069713c13c691 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 18:24:39 2024 +0100 moved ruff section commit d90c48a0bf98befdfacbbb093ee56cdb28afb40d Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 18:24:24 2024 +0100 move djlint config to pyproject commit c5ce55d5119bf2e35e429986f62f875c86178ae1 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 18:20:39 2024 +0100 added isort again commit 42a41d23afc280d4ee6f0e640148abc6f460f05a Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 18:19:02 2024 +0100 move config section commit 85692331816348cb1145570340d1f6488a8265cc Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 18:17:52 2024 +0100 fix codespell error commit 2897c6704d1311a800ce5aa47878d96d6980b377 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 17:29:21 2024 +0100 replaced flake8 with ruff mostly for speed improvements * fix coverage souce format --------- Co-authored-by: Oliver Walters <oliver.henry.walters@gmail.com>
194 lines
6.0 KiB
Python
194 lines
6.0 KiB
Python
"""Tests for the generic states module."""
|
|
|
|
from django.test.client import RequestFactory
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
from rest_framework.test import force_authenticate
|
|
|
|
from InvenTree.unit_test import InvenTreeTestCase
|
|
|
|
from .api import StatusView
|
|
from .states import StatusCode
|
|
|
|
|
|
class GeneralStatus(StatusCode):
|
|
"""Defines a set of status codes for tests."""
|
|
|
|
PENDING = 10, _('Pending'), 'secondary'
|
|
PLACED = 20, _('Placed'), 'primary'
|
|
COMPLETE = 30, _('Complete'), 'success'
|
|
ABC = None # This should be ignored
|
|
_DEF = None # This should be ignored
|
|
jkl = None # This should be ignored
|
|
|
|
def GHI(self): # This should be ignored
|
|
"""A invalid function"""
|
|
pass
|
|
|
|
|
|
class GeneralStateTest(InvenTreeTestCase):
|
|
"""Test that the StatusCode class works."""
|
|
|
|
def test_code_definition(self):
|
|
"""Test that the status code class has been defined correctly."""
|
|
self.assertEqual(GeneralStatus.PENDING, 10)
|
|
self.assertEqual(GeneralStatus.PLACED, 20)
|
|
self.assertEqual(GeneralStatus.COMPLETE, 30)
|
|
|
|
def test_code_functions(self):
|
|
"""Test that the status code class functions work correctly"""
|
|
# render
|
|
self.assertEqual(
|
|
GeneralStatus.render(10),
|
|
"<span class='badge rounded-pill bg-secondary'>Pending</span>",
|
|
)
|
|
self.assertEqual(
|
|
GeneralStatus.render(20),
|
|
"<span class='badge rounded-pill bg-primary'>Placed</span>",
|
|
)
|
|
# render with invalid key
|
|
self.assertEqual(GeneralStatus.render(100), 100)
|
|
|
|
# list
|
|
self.assertEqual(
|
|
GeneralStatus.list(),
|
|
[
|
|
{
|
|
'color': 'secondary',
|
|
'key': 10,
|
|
'label': 'Pending',
|
|
'name': 'PENDING',
|
|
},
|
|
{'color': 'primary', 'key': 20, 'label': 'Placed', 'name': 'PLACED'},
|
|
{
|
|
'color': 'success',
|
|
'key': 30,
|
|
'label': 'Complete',
|
|
'name': 'COMPLETE',
|
|
},
|
|
],
|
|
)
|
|
|
|
# text
|
|
self.assertEqual(GeneralStatus.text(10), 'Pending')
|
|
self.assertEqual(GeneralStatus.text(20), 'Placed')
|
|
|
|
# items
|
|
self.assertEqual(
|
|
list(GeneralStatus.items()),
|
|
[(10, 'Pending'), (20, 'Placed'), (30, 'Complete')],
|
|
)
|
|
|
|
# keys
|
|
self.assertEqual(list(GeneralStatus.keys()), ([10, 20, 30]))
|
|
|
|
# labels
|
|
self.assertEqual(
|
|
list(GeneralStatus.labels()), ['Pending', 'Placed', 'Complete']
|
|
)
|
|
|
|
# names
|
|
self.assertEqual(
|
|
GeneralStatus.names(), {'PENDING': 10, 'PLACED': 20, 'COMPLETE': 30}
|
|
)
|
|
|
|
# dict
|
|
self.assertEqual(
|
|
GeneralStatus.dict(),
|
|
{
|
|
'PENDING': {
|
|
'key': 10,
|
|
'name': 'PENDING',
|
|
'label': 'Pending',
|
|
'color': 'secondary',
|
|
},
|
|
'PLACED': {
|
|
'key': 20,
|
|
'name': 'PLACED',
|
|
'label': 'Placed',
|
|
'color': 'primary',
|
|
},
|
|
'COMPLETE': {
|
|
'key': 30,
|
|
'name': 'COMPLETE',
|
|
'label': 'Complete',
|
|
'color': 'success',
|
|
},
|
|
},
|
|
)
|
|
|
|
# label
|
|
self.assertEqual(GeneralStatus.label(10), 'Pending')
|
|
|
|
def test_tag_function(self):
|
|
"""Test that the status code tag functions."""
|
|
from .tags import status_label
|
|
|
|
self.assertEqual(
|
|
status_label('general', 10),
|
|
"<span class='badge rounded-pill bg-secondary'>Pending</span>",
|
|
)
|
|
|
|
# invalid type
|
|
with self.assertRaises(ValueError) as e:
|
|
status_label('invalid', 10)
|
|
self.assertEqual(str(e.exception), "Unknown status type 'invalid'")
|
|
|
|
# Test non-existent key
|
|
self.assertEqual(status_label('general', 100), '100')
|
|
|
|
def test_api(self):
|
|
"""Test StatusView API view."""
|
|
view = StatusView.as_view()
|
|
rqst = RequestFactory().get('status/')
|
|
force_authenticate(rqst, user=self.user)
|
|
|
|
# Correct call
|
|
resp = view(rqst, **{StatusView.MODEL_REF: GeneralStatus})
|
|
self.assertEqual(
|
|
resp.data,
|
|
{
|
|
'class': 'GeneralStatus',
|
|
'values': {
|
|
'COMPLETE': {
|
|
'key': 30,
|
|
'name': 'COMPLETE',
|
|
'label': 'Complete',
|
|
'color': 'success',
|
|
},
|
|
'PENDING': {
|
|
'key': 10,
|
|
'name': 'PENDING',
|
|
'label': 'Pending',
|
|
'color': 'secondary',
|
|
},
|
|
'PLACED': {
|
|
'key': 20,
|
|
'name': 'PLACED',
|
|
'label': 'Placed',
|
|
'color': 'primary',
|
|
},
|
|
},
|
|
},
|
|
)
|
|
|
|
# No status defined
|
|
resp = view(rqst, **{StatusView.MODEL_REF: None})
|
|
self.assertEqual(resp.status_code, 400)
|
|
self.assertEqual(
|
|
str(resp.rendered_content, 'utf-8'),
|
|
'["StatusView view called without \'statusmodel\' parameter"]',
|
|
)
|
|
|
|
# Invalid call - not a class
|
|
with self.assertRaises(NotImplementedError) as e:
|
|
resp = view(rqst, **{StatusView.MODEL_REF: 'invalid'})
|
|
self.assertEqual(str(e.exception), '`status_class` not a class')
|
|
|
|
# Invalid call - not the right class
|
|
with self.assertRaises(NotImplementedError) as e:
|
|
resp = view(rqst, **{StatusView.MODEL_REF: object})
|
|
self.assertEqual(
|
|
str(e.exception), '`status_class` not a valid StatusCode class'
|
|
)
|