mirror of
https://github.com/inventree/inventree-app.git
synced 2026-11-28 16:25:05 +00:00
[bug] Stock test fix (#930)
* [bug] fix stock test results - Update to match server API - Regression test * bump release notes * set default result value
This commit is contained in:
1 parent
ac4a0a7bdb
commit
3e0a08c669
4 files changed
+179
-49
No files matched your search
@@ -47,11 +47,12 @@ class InvenTreeStockItemTestResult extends InvenTreeModel {
|
||||
return fields;
|
||||
}
|
||||
|
||||
String get key => getString("key");
|
||||
// The test key and name are provided by the linked test template
|
||||
String get key => getString("key", subKey: "template_detail");
|
||||
|
||||
int get templateId => getInt("template");
|
||||
|
||||
String get testName => getString("test");
|
||||
String get testName => getString("test_name", subKey: "template_detail");
|
||||
|
||||
bool get result => getBool("result");
|
||||
|
||||
@@ -70,6 +71,64 @@ class InvenTreeStockItemTestResult extends InvenTreeModel {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Combine test templates and test results for display.
|
||||
*
|
||||
* - Each result is attached to its template (matched by template ID,
|
||||
* or by test key when the result has one)
|
||||
* - Results without a listed template (e.g. a disabled or deleted template)
|
||||
* are displayed separately, keeping only the latest result for each template
|
||||
*
|
||||
* Results are expected in date order, oldest first (the server default)
|
||||
*/
|
||||
List<InvenTreeModel> mergeTestResults(
|
||||
List<InvenTreePartTestTemplate> templates,
|
||||
List<InvenTreeStockItemTestResult> results,
|
||||
) {
|
||||
final List<InvenTreeModel> outputs = [];
|
||||
|
||||
for (final template in templates) {
|
||||
// Results are re-attached on each call
|
||||
template.results.clear();
|
||||
outputs.add(template);
|
||||
}
|
||||
|
||||
// Template ID -> position in outputs, for results without a listed template
|
||||
final Map<int, int> unlisted = {};
|
||||
|
||||
for (final result in results) {
|
||||
InvenTreePartTestTemplate? template;
|
||||
|
||||
for (final t in templates) {
|
||||
if ((result.templateId > 0 && result.templateId == t.pk) ||
|
||||
(result.key.isNotEmpty && result.key == t.key)) {
|
||||
template = t;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (template != null) {
|
||||
template.results.add(result);
|
||||
continue;
|
||||
}
|
||||
|
||||
final int id = result.templateId;
|
||||
|
||||
if (id > 0 && unlisted.containsKey(id)) {
|
||||
// A newer result for the same template
|
||||
outputs[unlisted[id]!] = result;
|
||||
} else {
|
||||
if (id > 0) {
|
||||
unlisted[id] = outputs.length;
|
||||
}
|
||||
|
||||
outputs.add(result);
|
||||
}
|
||||
}
|
||||
|
||||
return outputs;
|
||||
}
|
||||
|
||||
/*
|
||||
* Class representing a StockItem database instance
|
||||
*/
|
||||
|
||||
@@ -81,9 +81,10 @@ class _StockItemTestResultDisplayState
|
||||
L10().testResultAdd,
|
||||
fields: fields,
|
||||
data: {
|
||||
"template": "${templateId}",
|
||||
"stock_item": "${item.pk}",
|
||||
"test": "${name}",
|
||||
if (templateId > 0) "template": "${templateId}",
|
||||
// New results default to "pass"
|
||||
"result": true,
|
||||
},
|
||||
onSuccess: (data) {
|
||||
refresh(context);
|
||||
@@ -93,51 +94,8 @@ class _StockItemTestResultDisplayState
|
||||
}
|
||||
|
||||
// Squish together templates and results
|
||||
List<InvenTreeModel> getTestResults() {
|
||||
var templates = item.testTemplates;
|
||||
var results = item.testResults;
|
||||
|
||||
List<InvenTreeModel> outputs = [];
|
||||
|
||||
// Add each template to the list
|
||||
for (var t in templates) {
|
||||
outputs.add(t);
|
||||
}
|
||||
|
||||
// Add each result (compare to existing items / templates
|
||||
for (var result in results) {
|
||||
bool match = false;
|
||||
|
||||
for (var ii = 0; ii < outputs.length; ii++) {
|
||||
// Check against templates
|
||||
if (outputs[ii] is InvenTreePartTestTemplate) {
|
||||
var template = outputs[ii] as InvenTreePartTestTemplate;
|
||||
|
||||
// Match the result to a template
|
||||
if (result.templateId == template.pk || result.key == template.key) {
|
||||
template.results.add(result);
|
||||
match = true;
|
||||
break;
|
||||
}
|
||||
} else if (outputs[ii] is InvenTreeStockItemTestResult) {
|
||||
var r = outputs[ii] as InvenTreeStockItemTestResult;
|
||||
|
||||
if (r.key == result.key) {
|
||||
// Overwrite with a newer result
|
||||
outputs[ii] = result;
|
||||
match = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!match) {
|
||||
outputs.add(result);
|
||||
}
|
||||
}
|
||||
|
||||
return outputs;
|
||||
}
|
||||
List<InvenTreeModel> getTestResults() =>
|
||||
mergeTestResults(item.testTemplates, item.testResults);
|
||||
|
||||
@override
|
||||
List<Widget> getTiles(BuildContext context) {
|
||||
|
||||
Reference in new issue
Block a user