mirror of
https://github.com/inventree/inventree-app.git
synced 2026-11-23 14:16:08 +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
@@ -6,6 +6,7 @@
|
||||
- Fix handling of form response data for stock item creation
|
||||
- Improved allocation display for build orders
|
||||
- Multiple bug fixes for build order support
|
||||
- Bug fix for stock item test results display
|
||||
|
||||
### 1.1.0 - November 2026
|
||||
---
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -342,6 +342,118 @@ void main() {
|
||||
});
|
||||
});
|
||||
|
||||
// Regression tests for B7: test results provide the key and name
|
||||
// via template_detail (there are no "key" / "test" fields on the result)
|
||||
group("Stock Item Test Results:", () {
|
||||
InvenTreePartTestTemplate template(int pk, String key, String name) =>
|
||||
InvenTreePartTestTemplate.fromJson({
|
||||
"pk": pk,
|
||||
"key": key,
|
||||
"test_name": name,
|
||||
});
|
||||
|
||||
InvenTreeStockItemTestResult result(
|
||||
int pk,
|
||||
int templateId, {
|
||||
String key = "",
|
||||
String name = "",
|
||||
bool passed = true,
|
||||
String date = "2026-01-01",
|
||||
}) => InvenTreeStockItemTestResult.fromJson({
|
||||
"pk": pk,
|
||||
"template": templateId,
|
||||
"result": passed,
|
||||
"date": date,
|
||||
"template_detail": {"pk": templateId, "key": key, "test_name": name},
|
||||
});
|
||||
|
||||
test("Accessors", () async {
|
||||
final r = result(1, 7, key: "firmware", name: "Firmware");
|
||||
|
||||
expect(r.templateId, equals(7));
|
||||
expect(r.key, equals("firmware"));
|
||||
expect(r.testName, equals("Firmware"));
|
||||
expect(r.result, isTrue);
|
||||
|
||||
// Missing or null template detail
|
||||
final empty = InvenTreeStockItemTestResult.fromJson({
|
||||
"pk": 2,
|
||||
"template": null,
|
||||
"template_detail": null,
|
||||
});
|
||||
|
||||
expect(empty.key, isEmpty);
|
||||
expect(empty.testName, isEmpty);
|
||||
expect(InvenTreeStockItemTestResult.fromJson({}).testName, isEmpty);
|
||||
});
|
||||
|
||||
test("Results attach to templates", () async {
|
||||
final templates = [
|
||||
template(1, "firmware", "Firmware"),
|
||||
template(2, "voltage", "Voltage"),
|
||||
];
|
||||
|
||||
final outputs = mergeTestResults(templates, [
|
||||
result(10, 1, passed: false, date: "2026-01-01"),
|
||||
result(11, 1, passed: true, date: "2026-02-01"),
|
||||
// Matched by key (e.g. a different template ID for the same test)
|
||||
result(12, 99, key: "voltage", name: "Voltage"),
|
||||
]);
|
||||
|
||||
expect(outputs, equals(templates));
|
||||
expect(templates[0].results.map((r) => r.pk), equals([10, 11]));
|
||||
expect(templates[0].latestResult()?.pk, equals(11));
|
||||
expect(templates[0].passFailStatus(), isTrue);
|
||||
expect(templates[1].results.map((r) => r.pk), equals([12]));
|
||||
});
|
||||
|
||||
test("Results without a listed template", () async {
|
||||
final outputs = mergeTestResults(
|
||||
[template(1, "firmware", "Firmware")],
|
||||
[
|
||||
result(10, 5, key: "old_a", name: "Old Test A", date: "2026-01-01"),
|
||||
result(11, 6, key: "old_b", name: "Old Test B"),
|
||||
result(12, 5, key: "old_a", name: "Old Test A", date: "2026-02-01"),
|
||||
],
|
||||
);
|
||||
|
||||
// One row per template: separate tests are not collapsed together,
|
||||
// and the latest result is shown for a repeated test
|
||||
final unlisted = outputs.whereType<InvenTreeStockItemTestResult>();
|
||||
|
||||
expect(unlisted.map((r) => r.pk), equals([12, 11]));
|
||||
expect(
|
||||
unlisted.map((r) => r.testName),
|
||||
equals(["Old Test A", "Old Test B"]),
|
||||
);
|
||||
});
|
||||
|
||||
test("Results without a template are not merged", () async {
|
||||
final outputs = mergeTestResults(
|
||||
[template(1, "firmware", "Firmware")],
|
||||
[
|
||||
InvenTreeStockItemTestResult.fromJson({"pk": 20, "template": null}),
|
||||
InvenTreeStockItemTestResult.fromJson({"pk": 21, "template": null}),
|
||||
],
|
||||
);
|
||||
|
||||
expect(outputs, hasLength(3));
|
||||
|
||||
// Templates have no results, and do not match an empty key
|
||||
expect((outputs.first as InvenTreePartTestTemplate).results, isEmpty);
|
||||
});
|
||||
|
||||
test("Repeated merge does not duplicate results", () async {
|
||||
final templates = [template(1, "firmware", "Firmware")];
|
||||
final results = [result(10, 1)];
|
||||
|
||||
mergeTestResults(templates, results);
|
||||
mergeTestResults(templates, results);
|
||||
|
||||
expect(templates.first.results, hasLength(1));
|
||||
});
|
||||
});
|
||||
|
||||
group("Stock Item:", () {
|
||||
test("Quantity", () async {
|
||||
var item = InvenTreeStockItem.fromJson({"quantity": 10});
|
||||
|
||||
Reference in new issue
Block a user