mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Tests for part parameters
This commit is contained in:
parent
35ebc69235
commit
2a203be5cc
@ -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:
|
||||
|
32
InvenTree/part/fixtures/params.yaml
Normal file
32
InvenTree/part/fixtures/params.yaml
Normal 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
|
@ -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')
|
||||
|
||||
|
42
InvenTree/part/test_param.py
Normal file
42
InvenTree/part/test_param.py
Normal 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()
|
@ -1,3 +1,8 @@
|
||||
# Tests for the Part model
|
||||
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.test import TestCase
|
||||
|
||||
import os
|
||||
|
Loading…
Reference in New Issue
Block a user