mirror of
https://github.com/inventree/inventree-app.git
synced 2026-11-18 19:58:06 +00:00
Additional coverage testing (#911)
This commit is contained in:
1 parent
7fb46a3a77
commit
4430bfa9c7
3 files changed
+506
-4
No files matched your search
@@ -117,7 +117,8 @@ Future<void> confirmationDialog(
|
||||
* Convert a raw API field name (snake_case) into a human-readable label,
|
||||
* e.g. "target_date" -> "Target Date"
|
||||
*/
|
||||
String _humanizeFieldName(String field) {
|
||||
@visibleForTesting
|
||||
String humanizeFieldName(String field) {
|
||||
return field
|
||||
.split("_")
|
||||
.where((word) => word.isNotEmpty)
|
||||
@@ -129,7 +130,8 @@ String _humanizeFieldName(String field) {
|
||||
* Build the body content for an error dialog, given either a plain
|
||||
* description string or a structured APIResponse.
|
||||
*/
|
||||
Widget _buildErrorContent(String description, APIResponse? response) {
|
||||
@visibleForTesting
|
||||
Widget buildErrorContent(String description, APIResponse? response) {
|
||||
List<Widget> children = [];
|
||||
|
||||
if (description.isNotEmpty) {
|
||||
@@ -185,7 +187,7 @@ Widget _buildErrorContent(String description, APIResponse? response) {
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
_humanizeFieldName(field),
|
||||
humanizeFieldName(field),
|
||||
style: const TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
for (String message in messages) Text(message),
|
||||
@@ -269,7 +271,7 @@ Future<void> showErrorDialog(
|
||||
icon: Icon(icon, color: dialogColor),
|
||||
iconColor: dialogColor,
|
||||
title: Text(title),
|
||||
content: _buildErrorContent(description, response),
|
||||
content: buildErrorContent(description, response),
|
||||
actions: [
|
||||
TextButton(
|
||||
child: Text(L10().ok),
|
||||
|
||||
@@ -0,0 +1,237 @@
|
||||
/*
|
||||
* Unit tests for the error message helpers in dialogs.dart
|
||||
*
|
||||
* These tests do not require a connection to the server
|
||||
*/
|
||||
|
||||
import "package:flutter/material.dart";
|
||||
import "package:flutter_test/flutter_test.dart";
|
||||
|
||||
import "package:inventree/api.dart";
|
||||
import "package:inventree/l10.dart";
|
||||
import "package:inventree/widget/dialogs.dart";
|
||||
|
||||
// Display the error content for the given description / response
|
||||
Future<void> pumpErrorContent(
|
||||
WidgetTester tester, {
|
||||
String description = "",
|
||||
APIResponse? response,
|
||||
}) async {
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(home: Scaffold(body: buildErrorContent(description, response))),
|
||||
);
|
||||
}
|
||||
|
||||
// Return the text of every Text widget which is displayed
|
||||
List<String> displayedText(WidgetTester tester) => tester
|
||||
.widgetList<Text>(find.byType(Text))
|
||||
.map((text) => text.data ?? "")
|
||||
.toList();
|
||||
|
||||
void main() {
|
||||
group("Status Codes:", () {
|
||||
test("Known Codes", () async {
|
||||
final Map<int, String> codes = {
|
||||
400: L10().response400,
|
||||
401: L10().response401,
|
||||
403: L10().response403,
|
||||
404: L10().response404,
|
||||
405: L10().response405,
|
||||
429: L10().response429,
|
||||
500: L10().response500,
|
||||
501: L10().response501,
|
||||
502: L10().response502,
|
||||
503: L10().response503,
|
||||
504: L10().response504,
|
||||
505: L10().response505,
|
||||
};
|
||||
|
||||
for (final entry in codes.entries) {
|
||||
expect(
|
||||
statusCodeToString(entry.key),
|
||||
equals(entry.value),
|
||||
reason: "status ${entry.key}",
|
||||
);
|
||||
}
|
||||
|
||||
// Each code has a distinct message
|
||||
expect(codes.values.toSet().length, equals(codes.length));
|
||||
});
|
||||
|
||||
test("Unknown Codes", () async {
|
||||
for (final status in [-1, 0, 200, 418, 599]) {
|
||||
expect(
|
||||
statusCodeToString(status),
|
||||
equals("${L10().responseInvalid} : ${status}"),
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
group("Error Details:", () {
|
||||
test("Preferred Keys", () async {
|
||||
expect(extractErrorDetails({"detail": "Not found"}), equals("Not found"));
|
||||
expect(extractErrorDetails({"error": "Bad barcode"}), "Bad barcode");
|
||||
expect(
|
||||
extractErrorDetails({
|
||||
"errors": ["first", "second"],
|
||||
}),
|
||||
equals("first\nsecond"),
|
||||
);
|
||||
|
||||
// 'detail' takes priority over 'error'
|
||||
expect(extractErrorDetails({"error": "B", "detail": "A"}), equals("A"));
|
||||
|
||||
// Null values are skipped
|
||||
expect(extractErrorDetails({"detail": null, "error": "B"}), equals("B"));
|
||||
|
||||
// Non-string values are converted
|
||||
expect(extractErrorDetails({"detail": 42}), equals("42"));
|
||||
});
|
||||
|
||||
test("Fallback To Key Value Pairs", () async {
|
||||
expect(
|
||||
extractErrorDetails({
|
||||
"quantity": ["Must be positive"],
|
||||
"part": "Required",
|
||||
}),
|
||||
equals("quantity: [Must be positive]\npart: Required"),
|
||||
);
|
||||
|
||||
expect(extractErrorDetails({}), equals(""));
|
||||
|
||||
// Null values for preferred keys fall back to the raw data
|
||||
expect(extractErrorDetails({"detail": null}), equals("detail: null"));
|
||||
});
|
||||
|
||||
test("Other Types", () async {
|
||||
expect(extractErrorDetails(["a", 1]), equals("a\n1"));
|
||||
expect(extractErrorDetails([]), equals(""));
|
||||
expect(extractErrorDetails("plain text"), equals("plain text"));
|
||||
expect(extractErrorDetails(null), equals(""));
|
||||
expect(extractErrorDetails(123), equals(""));
|
||||
});
|
||||
});
|
||||
|
||||
group("Field Names:", () {
|
||||
test("Humanize", () async {
|
||||
expect(humanizeFieldName("target_date"), equals("Target Date"));
|
||||
expect(humanizeFieldName("quantity"), equals("Quantity"));
|
||||
expect(humanizeFieldName("__all__"), equals("All"));
|
||||
expect(humanizeFieldName("part__name"), equals("Part Name"));
|
||||
expect(humanizeFieldName(""), equals(""));
|
||||
});
|
||||
});
|
||||
|
||||
group("Error Content:", () {
|
||||
testWidgets("Description", (tester) async {
|
||||
// A description takes priority over the response
|
||||
await pumpErrorContent(
|
||||
tester,
|
||||
description: "Something went wrong",
|
||||
response: APIResponse(statusCode: 400, data: {"detail": "Ignored"}),
|
||||
);
|
||||
|
||||
expect(displayedText(tester), equals(["Something went wrong"]));
|
||||
});
|
||||
|
||||
testWidgets("Empty", (tester) async {
|
||||
await pumpErrorContent(tester);
|
||||
|
||||
expect(find.byType(Text), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets("Detail", (tester) async {
|
||||
await pumpErrorContent(
|
||||
tester,
|
||||
response: APIResponse(
|
||||
statusCode: 403,
|
||||
data: {"detail": "Permission denied", "other": "Ignored"},
|
||||
),
|
||||
);
|
||||
|
||||
expect(displayedText(tester), equals(["Permission denied"]));
|
||||
});
|
||||
|
||||
testWidgets("Non Field Errors", (tester) async {
|
||||
await pumpErrorContent(
|
||||
tester,
|
||||
response: APIResponse(
|
||||
statusCode: 409,
|
||||
data: {
|
||||
"non_field_errors": ["Order is locked", "Order is closed"],
|
||||
"__all__": "Invalid order",
|
||||
"errors": ["Something else"],
|
||||
"quantity": ["Not displayed (not a 400 response)"],
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
expect(
|
||||
displayedText(tester),
|
||||
equals([
|
||||
"Order is locked",
|
||||
"Order is closed",
|
||||
"Invalid order",
|
||||
"Something else",
|
||||
]),
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets("Field Errors", (tester) async {
|
||||
await pumpErrorContent(
|
||||
tester,
|
||||
response: APIResponse(
|
||||
statusCode: 400,
|
||||
data: {
|
||||
"non_field_errors": ["Form is invalid"],
|
||||
"target_date": ["Date is in the past", "Date is invalid"],
|
||||
"quantity": "Must be positive",
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
expect(
|
||||
displayedText(tester),
|
||||
equals([
|
||||
"Form is invalid",
|
||||
"Target Date",
|
||||
"Date is in the past",
|
||||
"Date is invalid",
|
||||
"Quantity",
|
||||
"Must be positive",
|
||||
]),
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets("Unrecognized Data", (tester) async {
|
||||
// Not a map - fall back to the raw data
|
||||
await pumpErrorContent(
|
||||
tester,
|
||||
response: APIResponse(statusCode: 500, data: "Internal Server Error"),
|
||||
);
|
||||
|
||||
expect(
|
||||
displayedText(tester),
|
||||
equals([
|
||||
L10().response500,
|
||||
L10().responseData,
|
||||
"Internal Server Error",
|
||||
]),
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets("Empty Field Errors", (tester) async {
|
||||
// A 400 response with no data
|
||||
await pumpErrorContent(
|
||||
tester,
|
||||
response: APIResponse(statusCode: 400, data: <String, dynamic>{}),
|
||||
);
|
||||
|
||||
expect(
|
||||
displayedText(tester),
|
||||
equals([L10().response400, L10().responseData, "{}"]),
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -4,11 +4,19 @@
|
||||
* These tests do not require a connection to the server
|
||||
*/
|
||||
|
||||
import "package:flutter/material.dart";
|
||||
import "package:flutter_tabler_icons/flutter_tabler_icons.dart";
|
||||
import "package:flutter_test/flutter_test.dart";
|
||||
import "package:inventree/api.dart";
|
||||
import "package:inventree/inventree/attachment.dart";
|
||||
import "package:inventree/inventree/bom.dart";
|
||||
import "package:inventree/inventree/model.dart";
|
||||
import "package:inventree/inventree/notes.dart";
|
||||
import "package:inventree/inventree/notification.dart";
|
||||
import "package:inventree/inventree/orders.dart";
|
||||
import "package:inventree/inventree/parameter.dart";
|
||||
import "package:inventree/inventree/part.dart";
|
||||
import "package:inventree/inventree/project_code.dart";
|
||||
import "package:inventree/inventree/stock.dart";
|
||||
import "package:inventree/l10.dart";
|
||||
|
||||
@@ -580,4 +588,259 @@ void main() {
|
||||
expect(line.formFields()["order"]?["hidden"], isTrue);
|
||||
});
|
||||
});
|
||||
|
||||
group("Attachment:", () {
|
||||
test("Empty Data", () async {
|
||||
final attachment = InvenTreeAttachment.fromJson({});
|
||||
|
||||
expect(attachment.URL, equals("attachment/"));
|
||||
expect(attachment.attachment, equals(""));
|
||||
expect(attachment.hasAttachment, isFalse);
|
||||
expect(attachment.filename, equals(""));
|
||||
expect(attachment.icon, equals(TablerIcons.file));
|
||||
expect(attachment.comment, equals(""));
|
||||
expect(attachment.uploadDate, isNull);
|
||||
expect(attachment.modelType, equals(""));
|
||||
expect(attachment.modelId, equals(-1));
|
||||
|
||||
// No link, so the 'link' field is not displayed
|
||||
expect(attachment.formFields().keys, equals(["comment"]));
|
||||
|
||||
expect(
|
||||
attachment.createFromJson({"pk": 3}),
|
||||
isA<InvenTreeAttachment>().having((a) => a.pk, "pk", 3),
|
||||
);
|
||||
});
|
||||
|
||||
test("File Attachment", () async {
|
||||
final attachment = InvenTreeAttachment.fromJson({
|
||||
"attachment": "/media/attachments/part/7/Datasheet.PDF",
|
||||
"comment": "A datasheet",
|
||||
"upload_date": "2024-03-15",
|
||||
"model_type": "part",
|
||||
"model_id": 7,
|
||||
});
|
||||
|
||||
expect(attachment.hasAttachment, isTrue);
|
||||
expect(attachment.filename, equals("Datasheet.PDF"));
|
||||
expect(attachment.icon, equals(TablerIcons.file_type_pdf));
|
||||
expect(attachment.comment, equals("A datasheet"));
|
||||
expect(attachment.uploadDate, equals(DateTime(2024, 3, 15)));
|
||||
expect(attachment.modelType, equals("part"));
|
||||
expect(attachment.modelId, equals(7));
|
||||
expect(attachment.formFields().keys, equals(["comment"]));
|
||||
});
|
||||
|
||||
test("Link Attachment", () async {
|
||||
final attachment = InvenTreeAttachment.fromJson({
|
||||
"link": "https://inventree.org",
|
||||
"upload_date": null,
|
||||
});
|
||||
|
||||
expect(attachment.hasAttachment, isFalse);
|
||||
expect(attachment.formFields().keys, equals(["link", "comment"]));
|
||||
|
||||
// Null (or invalid) dates are not parsed
|
||||
expect(attachment.uploadDate, isNull);
|
||||
expect(
|
||||
InvenTreeAttachment.fromJson({"upload_date": "junk"}).uploadDate,
|
||||
isNull,
|
||||
);
|
||||
});
|
||||
|
||||
test("File Icons", () async {
|
||||
final Map<String, IconData> icons = {
|
||||
"report.pdf": TablerIcons.file_type_pdf,
|
||||
"data.csv": TablerIcons.file_type_csv,
|
||||
"notes.doc": TablerIcons.file_type_doc,
|
||||
"notes.docx": TablerIcons.file_type_doc,
|
||||
"sheet.xls": TablerIcons.file_type_xls,
|
||||
"sheet.XLSX": TablerIcons.file_type_xls,
|
||||
"photo.png": TablerIcons.file_type_jpg,
|
||||
"photo.jpg": TablerIcons.file_type_jpg,
|
||||
"anim.gif": TablerIcons.file_type_jpg,
|
||||
"image.bmp": TablerIcons.file_type_jpg,
|
||||
"logo.svg": TablerIcons.file_type_jpg,
|
||||
"archive.zip": TablerIcons.file,
|
||||
"no_extension": TablerIcons.file,
|
||||
};
|
||||
|
||||
for (final entry in icons.entries) {
|
||||
final attachment = InvenTreeAttachment.fromJson({
|
||||
"attachment": "/media/attachments/${entry.key}",
|
||||
});
|
||||
|
||||
expect(attachment.icon, equals(entry.value), reason: entry.key);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
group("Notification:", () {
|
||||
test("Accessors", () async {
|
||||
final notification = InvenTreeNotification.fromJson({
|
||||
"pk": 12,
|
||||
"message": "Stock is low",
|
||||
"creation": "2024-05-01T10:30:00",
|
||||
});
|
||||
|
||||
expect(notification.URL, equals("notifications/"));
|
||||
expect(notification.defaultListFilters(), equals({"read": "false"}));
|
||||
expect(notification.message, equals("Stock is low"));
|
||||
expect(notification.creationDate, equals(DateTime(2024, 5, 1, 10, 30)));
|
||||
|
||||
expect(
|
||||
notification.createFromJson({"pk": 1}),
|
||||
isA<InvenTreeNotification>(),
|
||||
);
|
||||
});
|
||||
|
||||
test("Missing Date", () async {
|
||||
expect(InvenTreeNotification.fromJson({}).creationDate, isNull);
|
||||
expect(
|
||||
InvenTreeNotification.fromJson({"creation": null}).creationDate,
|
||||
isNull,
|
||||
);
|
||||
expect(
|
||||
InvenTreeNotification.fromJson({"creation": "junk"}).creationDate,
|
||||
isNull,
|
||||
);
|
||||
expect(InvenTreeNotification.fromJson({}).message, equals(""));
|
||||
});
|
||||
});
|
||||
|
||||
group("Project Code:", () {
|
||||
test("Accessors", () async {
|
||||
final code = InvenTreeProjectCode.fromJson({
|
||||
"pk": 4,
|
||||
"code": "PRJ-001",
|
||||
"description": "Test project",
|
||||
});
|
||||
|
||||
expect(InvenTreeProjectCode.MODEL_TYPE, equals("projectcode"));
|
||||
expect(code.URL, equals("project-code/"));
|
||||
expect(code.code, equals("PRJ-001"));
|
||||
expect(code.description, equals("Test project"));
|
||||
expect(code.formFields().keys, equals(["code", "description"]));
|
||||
expect(InvenTreeProjectCode().code, equals(""));
|
||||
|
||||
expect(
|
||||
code.createFromJson({"pk": 5}),
|
||||
isA<InvenTreeProjectCode>().having((c) => c.pk, "pk", 5),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
group("Parameter:", () {
|
||||
test("Accessors", () async {
|
||||
final parameter = InvenTreeParameter.fromJson({
|
||||
"pk": 9,
|
||||
"data": "12",
|
||||
"model_type": "part",
|
||||
"model_id": 3,
|
||||
"template_detail": {
|
||||
"name": "Length",
|
||||
"description": "Overall length",
|
||||
"units": "mm",
|
||||
"checkbox": false,
|
||||
},
|
||||
});
|
||||
|
||||
expect(parameter.URL, equals("parameter/"));
|
||||
expect(parameter.name, equals("Length"));
|
||||
expect(parameter.description, equals("Overall length"));
|
||||
expect(parameter.value, equals("12"));
|
||||
expect(parameter.units, equals("mm"));
|
||||
expect(parameter.valueString, equals("12 mm"));
|
||||
expect(parameter.is_checkbox, isFalse);
|
||||
expect(parameter.as_bool, isFalse);
|
||||
expect(parameter.modelType, equals("part"));
|
||||
expect(parameter.modelId, equals(3));
|
||||
|
||||
final fields = parameter.formFields();
|
||||
expect(fields.keys, equals(["header", "data", "note"]));
|
||||
expect(fields["header"]?["label"], equals("Length"));
|
||||
expect(fields["header"]?["help_text"], equals("Overall length"));
|
||||
expect(fields["header"]?["read_only"], isTrue);
|
||||
});
|
||||
|
||||
test("Checkbox", () async {
|
||||
final parameter = InvenTreeParameter.fromJson({
|
||||
"data": "True",
|
||||
"template_detail": {"name": "RoHS", "checkbox": true},
|
||||
});
|
||||
|
||||
expect(parameter.is_checkbox, isTrue);
|
||||
expect(parameter.as_bool, isTrue);
|
||||
|
||||
// No units, so the value is displayed alone
|
||||
expect(parameter.valueString, equals("True"));
|
||||
|
||||
// No template detail
|
||||
final empty = InvenTreeParameter.fromJson({"data": "x"});
|
||||
expect(empty.is_checkbox, isFalse);
|
||||
expect(empty.as_bool, isFalse);
|
||||
expect(empty.valueString, equals("x"));
|
||||
});
|
||||
});
|
||||
|
||||
group("BOM Item:", () {
|
||||
test("Accessors", () async {
|
||||
final item = InvenTreeBomItem.fromJson({
|
||||
"pk": 1,
|
||||
"part": 10,
|
||||
"sub_part": 20,
|
||||
"quantity": "2.5",
|
||||
"reference": "R1, R2",
|
||||
"part_detail": {"pk": 10, "name": "Assembly"},
|
||||
"sub_part_detail": {"pk": 20, "name": "Resistor"},
|
||||
});
|
||||
|
||||
expect(item.URL, equals("bom/"));
|
||||
expect(item.defaultFilters()["sub_part_detail"], equals("true"));
|
||||
expect(item.partId, equals(10));
|
||||
expect(item.subPartId, equals(20));
|
||||
expect(item.quantity, equals(2.5));
|
||||
expect(item.reference, equals("R1, R2"));
|
||||
expect(item.part?.name, equals("Assembly"));
|
||||
expect(item.subPart?.name, equals("Resistor"));
|
||||
expect(item.createFromJson({"pk": 2}), isA<InvenTreeBomItem>());
|
||||
});
|
||||
|
||||
test("Missing Detail", () async {
|
||||
final item = InvenTreeBomItem.fromJson({
|
||||
"sub_part_detail": null,
|
||||
"part_detail": "invalid",
|
||||
});
|
||||
|
||||
expect(item.part, isNull);
|
||||
expect(item.subPart, isNull);
|
||||
expect(InvenTreeBomItem.fromJson({}).subPart, isNull);
|
||||
expect(InvenTreeBomItem.fromJson({}).subPartId, equals(-1));
|
||||
});
|
||||
});
|
||||
|
||||
group("Note Entry:", () {
|
||||
test("Accessors", () async {
|
||||
final note = InvenTreeNoteEntry.fromJson({
|
||||
"pk": 2,
|
||||
"title": "Assembly notes",
|
||||
"content": "<p>Handle with care</p>",
|
||||
"updated": "2024-06-30",
|
||||
"model_type": "build",
|
||||
"model_id": 8,
|
||||
});
|
||||
|
||||
expect(note.URL, equals("note/"));
|
||||
expect(note.title, equals("Assembly notes"));
|
||||
expect(note.content, equals("<p>Handle with care</p>"));
|
||||
expect(note.updated, equals(DateTime(2024, 6, 30)));
|
||||
expect(note.modelType, equals("build"));
|
||||
expect(note.modelId, equals(8));
|
||||
expect(note.createFromJson({"pk": 3}), isA<InvenTreeNoteEntry>());
|
||||
|
||||
final empty = InvenTreeNoteEntry.fromJson({});
|
||||
expect(empty.content, equals(""));
|
||||
expect(empty.updated, isNull);
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user