mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Added tests for Category parameters methods, some code clean-up
This commit is contained in:
parent
9d3d9a190b
commit
496232ed6d
@ -114,7 +114,7 @@ class PartCategory(InvenTreeTree):
|
|||||||
def prefetch_parts_parameters(self, cascade=True):
|
def prefetch_parts_parameters(self, cascade=True):
|
||||||
""" Prefectch parts parameters """
|
""" Prefectch parts parameters """
|
||||||
|
|
||||||
return self.get_parts(cascade=cascade).prefetch_related('parameters', 'parameters__template')
|
return self.get_parts(cascade=cascade).prefetch_related('parameters', 'parameters__template').all()
|
||||||
|
|
||||||
def get_unique_parameters(self, cascade=True, prefetch=None):
|
def get_unique_parameters(self, cascade=True, prefetch=None):
|
||||||
""" Get all unique parameter names for all parts from this category """
|
""" Get all unique parameter names for all parts from this category """
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from django.core.exceptions import ValidationError
|
from django.core.exceptions import ValidationError
|
||||||
|
|
||||||
from .models import Part, PartCategory
|
from .models import Part, PartCategory, PartParameter, PartParameterTemplate
|
||||||
|
|
||||||
|
|
||||||
class CategoryTest(TestCase):
|
class CategoryTest(TestCase):
|
||||||
@ -15,6 +15,7 @@ class CategoryTest(TestCase):
|
|||||||
'category',
|
'category',
|
||||||
'part',
|
'part',
|
||||||
'location',
|
'location',
|
||||||
|
'params',
|
||||||
]
|
]
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
@ -94,6 +95,31 @@ class CategoryTest(TestCase):
|
|||||||
|
|
||||||
self.assertEqual(self.electronics.item_count, self.electronics.partcount())
|
self.assertEqual(self.electronics.item_count, self.electronics.partcount())
|
||||||
|
|
||||||
|
def test_parameters(self):
|
||||||
|
""" Test that the Category parameters are correctly fetched """
|
||||||
|
|
||||||
|
# Check number of SQL queries to iterate other parameters
|
||||||
|
with self.assertNumQueries(3):
|
||||||
|
# Prefetch: 3 queries (parts, parameters and parameters_template)
|
||||||
|
fasteners = self.fasteners.prefetch_parts_parameters()
|
||||||
|
# Iterate through all parts and parameters
|
||||||
|
for fastener in fasteners:
|
||||||
|
self.assertIsInstance(fastener, Part)
|
||||||
|
for parameter in fastener.parameters.all():
|
||||||
|
self.assertIsInstance(parameter, PartParameter)
|
||||||
|
self.assertIsInstance(parameter.template, PartParameterTemplate)
|
||||||
|
|
||||||
|
# Test number of unique parameters
|
||||||
|
self.assertEqual(len(self.fasteners.get_unique_parameters(prefetch=fasteners)), 1)
|
||||||
|
# Test number of parameters found for each part
|
||||||
|
parts_parameters = self.fasteners.get_parts_parameters(prefetch=fasteners)
|
||||||
|
part_infos = ['pk', 'name', 'description']
|
||||||
|
for part_parameter in parts_parameters:
|
||||||
|
# Remove part informations
|
||||||
|
for item in part_infos:
|
||||||
|
part_parameter.pop(item)
|
||||||
|
self.assertEqual(len(part_parameter), 1)
|
||||||
|
|
||||||
def test_invalid_name(self):
|
def test_invalid_name(self):
|
||||||
# Test that an illegal character is prohibited in a category name
|
# Test that an illegal character is prohibited in a category name
|
||||||
|
|
||||||
|
@ -164,18 +164,16 @@ function loadSimplePartTable(table, url, options={}) {
|
|||||||
|
|
||||||
|
|
||||||
function loadParametricPartTable(table, options={}) {
|
function loadParametricPartTable(table, options={}) {
|
||||||
/* Load parametric part data into specified table.
|
/* Load parametric table for part parameters
|
||||||
*
|
*
|
||||||
* Args:
|
* Args:
|
||||||
* - table: HTML reference to the table
|
* - table: HTML reference to the table
|
||||||
* - table_headers: Table headers/columns
|
* - table_headers: Unique parameters found in category
|
||||||
* - table_data: Parameters data
|
* - table_data: Parameters data
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var table_headers = options.headers
|
var table_headers = options.headers
|
||||||
var table_data = options.data
|
var table_data = options.data
|
||||||
/* console.log(table_headers)
|
|
||||||
console.log(table_data)*/
|
|
||||||
|
|
||||||
var columns = [];
|
var columns = [];
|
||||||
|
|
||||||
@ -211,19 +209,17 @@ function loadParametricPartTable(table, options={}) {
|
|||||||
title: header,
|
title: header,
|
||||||
sortable: true,
|
sortable: true,
|
||||||
filterControl: 'input',
|
filterControl: 'input',
|
||||||
clear: 'fa-times icon-red',
|
/* TODO: Search icons are not displayed */
|
||||||
|
/*clear: 'fa-times icon-red',*/
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$(table).inventreeTable({
|
$(table).inventreeTable({
|
||||||
/* url: url,*/
|
|
||||||
sortName: 'part',
|
sortName: 'part',
|
||||||
/* method: 'get',*/
|
|
||||||
queryParams: table_headers,
|
queryParams: table_headers,
|
||||||
groupBy: false,
|
groupBy: false,
|
||||||
name: options.name || 'parametric',
|
name: options.name || 'parametric',
|
||||||
/* original: params,*/
|
|
||||||
formatNoMatches: function() { return "{% trans "No parts found" %}"; },
|
formatNoMatches: function() { return "{% trans "No parts found" %}"; },
|
||||||
columns: columns,
|
columns: columns,
|
||||||
showColumns: true,
|
showColumns: true,
|
||||||
|
Loading…
Reference in New Issue
Block a user