From 2a203be5ccbb535e0c53bd7bb8f1c61c7bb8fbaf Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sun, 8 Sep 2019 20:18:21 +1000 Subject: [PATCH] Tests for part parameters --- InvenTree/InvenTree/settings.py | 4 +-- InvenTree/part/fixtures/params.yaml | 32 ++++++++++++++++++++++ InvenTree/part/models.py | 12 +++------ InvenTree/part/test_param.py | 42 +++++++++++++++++++++++++++++ InvenTree/part/test_part.py | 5 ++++ 5 files changed, 83 insertions(+), 12 deletions(-) create mode 100644 InvenTree/part/fixtures/params.yaml create mode 100644 InvenTree/part/test_param.py diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py index 476be9aa28..2f5572a5f8 100644 --- a/InvenTree/InvenTree/settings.py +++ b/InvenTree/InvenTree/settings.py @@ -58,9 +58,7 @@ cors_opt = CONFIG.get('cors', None) if cors_opt: CORS_ORIGIN_ALLOW_ALL = cors_opt.get('allow_all', False) - if CORS_ORIGIN_ALLOW_ALL: - eprint("Warning: CORS requests are allowed for any domain!") - else: + if not CORS_ORIGIN_ALLOW_ALL: CORS_ORIGIN_WHITELIST = cors_opt.get('whitelist', []) if DEBUG: diff --git a/InvenTree/part/fixtures/params.yaml b/InvenTree/part/fixtures/params.yaml new file mode 100644 index 0000000000..4827d0987b --- /dev/null +++ b/InvenTree/part/fixtures/params.yaml @@ -0,0 +1,32 @@ +# Create some PartParameter templtes + +- model: part.PartParameterTemplate + pk: 1 + fields: + name: Length + units: mm + +- model: part.PartParameterTemplate + pk: 2 + fields: + name: Width + units: mm + +- model: part.PartParameterTemplate + pk: 3 + fields: + name: Thickness + units: mm + +# And some parameters (requires part.yaml) +- model: part.PartParameter + fields: + part: 1 + template: 1 + data: 4 + +- model: part.PartParameter + fields: + part: 2 + template: 1 + data: 12 \ No newline at end of file diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index 1831962a44..7a1a31a02a 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -1065,7 +1065,7 @@ class PartParameterTemplate(models.Model): super().validate_unique(exclude) try: - others = PartParameterTemplate.objects.exclude(id=self.id).filter(name__iexact=self.name) + others = PartParameterTemplate.objects.filter(name__iexact=self.name).exclude(pk=self.pk) if others.exists(): msg = _("Parameter template name must be unique") @@ -1073,11 +1073,6 @@ class PartParameterTemplate(models.Model): except PartParameterTemplate.DoesNotExist: pass - @property - def instance_count(self): - """ Return the number of instances of this Parameter Template """ - return self.instances.count() - name = models.CharField(max_length=100, help_text='Parameter Name', unique=True) units = models.CharField(max_length=25, help_text='Parameter Units', blank=True) @@ -1096,7 +1091,7 @@ class PartParameter(models.Model): def __str__(self): # String representation of a PartParameter (used in the admin interface) return "{part} : {param} = {data}{units}".format( - part=str(self.part), + part=str(self.part.full_name), param=str(self.template.name), data=str(self.data), units=str(self.template.units) @@ -1106,8 +1101,7 @@ class PartParameter(models.Model): # Prevent multiple instances of a parameter for a single part unique_together = ('part', 'template') - part = models.ForeignKey(Part, on_delete=models.CASCADE, - related_name='parameters', help_text='Parent Part') + part = models.ForeignKey(Part, on_delete=models.CASCADE, related_name='parameters', help_text='Parent Part') template = models.ForeignKey(PartParameterTemplate, on_delete=models.CASCADE, related_name='instances', help_text='Parameter Template') diff --git a/InvenTree/part/test_param.py b/InvenTree/part/test_param.py new file mode 100644 index 0000000000..6876a2b5df --- /dev/null +++ b/InvenTree/part/test_param.py @@ -0,0 +1,42 @@ +# Tests for Part Parameters + +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.test import TestCase +import django.core.exceptions as django_exceptions + +from .models import PartParameter, PartParameterTemplate + + +class TestParams(TestCase): + + fixtures = [ + 'location', + 'category', + 'part', + 'params' + ] + + def test_str(self): + + t1 = PartParameterTemplate.objects.get(pk=1) + self.assertEquals(str(t1), 'Length (mm)') + + p1 = PartParameter.objects.get(pk=1) + self.assertEqual(str(p1), "M2x4 LPHS : Length = 4mm") + + def test_validate(self): + + n = PartParameterTemplate.objects.all().count() + + t1 = PartParameterTemplate(name='abcde', units='dd') + t1.save() + + self.assertEqual(n + 1, PartParameterTemplate.objects.all().count()) + + # Test that the case-insensitive name throws a ValidationError + with self.assertRaises(django_exceptions.ValidationError): + t3 = PartParameterTemplate(name='aBcde', units='dd') + t3.full_clean() + t3.save() diff --git a/InvenTree/part/test_part.py b/InvenTree/part/test_part.py index c9028cd406..e6ce9168ee 100644 --- a/InvenTree/part/test_part.py +++ b/InvenTree/part/test_part.py @@ -1,3 +1,8 @@ +# Tests for the Part model + +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + from django.test import TestCase import os