Tests for part parameters

This commit is contained in:
Oliver Walters 2019-09-08 20:18:21 +10:00
parent 35ebc69235
commit 2a203be5cc
5 changed files with 83 additions and 12 deletions

View File

@ -58,9 +58,7 @@ cors_opt = CONFIG.get('cors', None)
if cors_opt: if cors_opt:
CORS_ORIGIN_ALLOW_ALL = cors_opt.get('allow_all', False) CORS_ORIGIN_ALLOW_ALL = cors_opt.get('allow_all', False)
if CORS_ORIGIN_ALLOW_ALL: if not CORS_ORIGIN_ALLOW_ALL:
eprint("Warning: CORS requests are allowed for any domain!")
else:
CORS_ORIGIN_WHITELIST = cors_opt.get('whitelist', []) CORS_ORIGIN_WHITELIST = cors_opt.get('whitelist', [])
if DEBUG: if DEBUG:

View File

@ -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

View File

@ -1065,7 +1065,7 @@ class PartParameterTemplate(models.Model):
super().validate_unique(exclude) super().validate_unique(exclude)
try: 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(): if others.exists():
msg = _("Parameter template name must be unique") msg = _("Parameter template name must be unique")
@ -1073,11 +1073,6 @@ class PartParameterTemplate(models.Model):
except PartParameterTemplate.DoesNotExist: except PartParameterTemplate.DoesNotExist:
pass 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) name = models.CharField(max_length=100, help_text='Parameter Name', unique=True)
units = models.CharField(max_length=25, help_text='Parameter Units', blank=True) units = models.CharField(max_length=25, help_text='Parameter Units', blank=True)
@ -1096,7 +1091,7 @@ class PartParameter(models.Model):
def __str__(self): def __str__(self):
# String representation of a PartParameter (used in the admin interface) # String representation of a PartParameter (used in the admin interface)
return "{part} : {param} = {data}{units}".format( return "{part} : {param} = {data}{units}".format(
part=str(self.part), part=str(self.part.full_name),
param=str(self.template.name), param=str(self.template.name),
data=str(self.data), data=str(self.data),
units=str(self.template.units) units=str(self.template.units)
@ -1106,8 +1101,7 @@ class PartParameter(models.Model):
# Prevent multiple instances of a parameter for a single part # Prevent multiple instances of a parameter for a single part
unique_together = ('part', 'template') unique_together = ('part', 'template')
part = models.ForeignKey(Part, on_delete=models.CASCADE, part = models.ForeignKey(Part, on_delete=models.CASCADE, related_name='parameters', help_text='Parent Part')
related_name='parameters', help_text='Parent Part')
template = models.ForeignKey(PartParameterTemplate, on_delete=models.CASCADE, related_name='instances', help_text='Parameter Template') template = models.ForeignKey(PartParameterTemplate, on_delete=models.CASCADE, related_name='instances', help_text='Parameter Template')

View File

@ -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()

View File

@ -1,3 +1,8 @@
# Tests for the Part model
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.test import TestCase from django.test import TestCase
import os import os