mirror of
https://github.com/inventree/InvenTree.git
synced 2025-10-24 18:07:38 +00:00
test failure modes
This commit is contained in:
@@ -91,3 +91,53 @@ class FilteredSerializers(InvenTreeAPITestCase):
|
|||||||
self.assertNotContains(response, 'field_b')
|
self.assertNotContains(response, 'field_b')
|
||||||
self.assertNotContains(response, 'field_c')
|
self.assertNotContains(response, 'field_c')
|
||||||
self.assertNotContains(response, 'field_d')
|
self.assertNotContains(response, 'field_d')
|
||||||
|
|
||||||
|
def test_failiure_enable_filter(self):
|
||||||
|
"""Test sanity check for enable_filter."""
|
||||||
|
# Allowed usage
|
||||||
|
field_b = InvenTree.serializers.enable_filter( # noqa: F841
|
||||||
|
InvenTree.serializers.FilterableSerializerMethodField(method_name='sample')
|
||||||
|
)
|
||||||
|
|
||||||
|
# Disallowed usage
|
||||||
|
with self.assertRaises(Exception) as cm:
|
||||||
|
field_a = InvenTree.serializers.enable_filter( # noqa: F841
|
||||||
|
SerializerMethodField(method_name='sample')
|
||||||
|
)
|
||||||
|
self.assertIn(
|
||||||
|
'INVE-I2: `enable_filter` can only be applied to serializer fields',
|
||||||
|
str(cm.exception),
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_failiure_FilterableSerializerMixin(self):
|
||||||
|
"""Test failure case for FilteredSerializerMixin."""
|
||||||
|
|
||||||
|
class BadSerializer(
|
||||||
|
InvenTree.serializers.FilterableSerializerMixin,
|
||||||
|
InvenTree.serializers.InvenTreeModelSerializer,
|
||||||
|
):
|
||||||
|
"""Bad serializer for testing FilterableSerializerMixin."""
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
"""Meta options."""
|
||||||
|
|
||||||
|
model = User
|
||||||
|
fields = ['field_a', 'id']
|
||||||
|
|
||||||
|
field_a = SerializerMethodField(method_name='sample')
|
||||||
|
|
||||||
|
def sample(self, obj):
|
||||||
|
"""Sample method field."""
|
||||||
|
return 'sample'
|
||||||
|
|
||||||
|
with self.assertRaises(Exception) as cm:
|
||||||
|
_ = BadSerializer()
|
||||||
|
self.assertIn(
|
||||||
|
'INVE-I2: No filter targets found in fields, remove `PathScopedMixin`',
|
||||||
|
str(cm.exception),
|
||||||
|
)
|
||||||
|
|
||||||
|
# Test override
|
||||||
|
BadSerializer.no_filters = True
|
||||||
|
_ = BadSerializer()
|
||||||
|
self.assertTrue(True) # Dummy assertion to ensure we reach here
|
||||||
|
|||||||
Reference in New Issue
Block a user