/* * Unit tests for model classes constructed from JSON data * * These tests do not require a connection to the server */ import "package:flutter_test/flutter_test.dart"; import "package:inventree/api.dart"; import "package:inventree/inventree/model.dart"; import "package:inventree/inventree/orders.dart"; import "package:inventree/inventree/part.dart"; import "package:inventree/inventree/stock.dart"; import "package:inventree/l10.dart"; void main() { group("Model Accessors:", () { test("Empty Data", () async { final model = InvenTreeModel.fromJson({}); expect(model.pk, equals(-1)); expect(model.name, equals("")); expect(model.description, equals("")); expect(model.getString("missing", backup: "x"), equals("x")); expect(model.getInt("missing", backup: 7), equals(7)); expect(model.getDouble("missing"), equals(0.0)); expect(model.getDoubleOrNull("missing"), isNull); expect(model.getBool("missing"), isFalse); expect(model.getBool("missing", backup: true), isTrue); expect(model.getDate("missing"), isNull); expect(model.getDateString("missing"), equals("")); expect(model.getMap("missing"), isEmpty); expect(model.link, equals("")); expect(model.hasLink, isFalse); expect(model.customIcon, isNull); expect(model.customBarcode, equals("")); expect(model.reloadedWithin(const Duration(days: 1)), isFalse); }); test("Typed Values", () async { final model = InvenTreeModel.fromJson({ "pk": 12, "name": "Widget", "count": "42", "bad_int": "abc", "ratio": "1.5", "bad_double": "x", "flag": "True", "not_flag": "yes", "null_value": null, "date": "2024-03-07T10:15:00Z", "detail": {"name": "Sub", "count": 3}, }); expect(model.pk, equals(12)); expect(model.pkString, equals("12")); expect(model.name, equals("Widget")); expect(model.getInt("count"), equals(42)); expect(model.getInt("bad_int"), equals(-1)); expect(model.getDouble("ratio"), equals(1.5)); expect(model.getDoubleOrNull("bad_double"), isNull); expect(model.getBool("flag"), isTrue); expect(model.getBool("not_flag"), isFalse); // Explicit null values return the backup expect(model.getString("null_value", backup: "b"), equals("b")); expect(model.getInt("null_value", backup: 3), equals(3)); expect(model.getBool("null_value", backup: true), isTrue); expect(model.getMap("null_value"), isEmpty); expect(model.getDate("date")?.year, equals(2024)); expect(model.getDateString("date"), equals("2024-03-07")); // Sub-key lookups expect(model.getString("name", subKey: "detail"), equals("Sub")); expect(model.getInt("count", subKey: "detail"), equals(3)); expect(model.getMap("detail")["name"], equals("Sub")); expect( model.getString("name", subKey: "missing", backup: "none"), equals("none"), ); model.setValue("name", "Gadget"); expect(model.name, equals("Gadget")); }); test("Status", () async { var model = InvenTreeModel.fromJson({"status": 10}); expect(model.status, equals(10)); expect(model.hasCustomStatus, isFalse); model = InvenTreeModel.fromJson({"status": 10, "status_custom_key": 101}); expect(model.logicalStatus, equals(10)); expect(model.customStatus, equals(101)); expect(model.status, equals(101)); }); test("Link", () async { expect( InvenTreeModel.fromJson({"link": "https://a.com"}).link, equals("https://a.com"), ); // Legacy API used "URL" final legacy = InvenTreeModel.fromJson({"URL": "https://b.com"}); expect(legacy.link, equals("https://b.com")); expect(legacy.hasLink, isTrue); }); test("Custom Icon", () async { expect(InvenTreeModel.fromJson({"icon": " "}).customIcon, isNull); expect(InvenTreeModel.fromJson({"icon": "fa:box"}).customIcon, isNull); expect(InvenTreeModel.fromJson({"icon": "ti:"}).customIcon, isNull); expect( InvenTreeModel.fromJson({"icon": "ti:not-an-icon:outline"}).customIcon, isNull, ); expect( InvenTreeModel.fromJson({"icon": "ti:box:outline"}).customIcon, isNotNull, ); expect( InvenTreeModel.fromJson({"icon": "ti:arrow-left:outline"}).customIcon, isNotNull, ); }); test("Custom Barcode", () async { expect( InvenTreeModel.fromJson({ "uid": "abc", "barcode_hash": "def", }).customBarcode, equals("abc"), ); expect( InvenTreeModel.fromJson({ "barcode_hash": "def", "barcode": "ghi", }).customBarcode, equals("def"), ); expect( InvenTreeModel.fromJson({"barcode": "ghi"}).customBarcode, equals("ghi"), ); }); test("Detail URL", () async { final part = InvenTreePart.fromJson({"pk": 5}); expect(part.url, equals("part/5/")); final item = InvenTreeStockItem.fromJson({"pk": 9}); expect(item.url, equals("stock/9/")); }); test("Reloaded Within", () async { final model = InvenTreeModel.fromJson({}); model.lastReload = DateTime.now(); expect(model.reloadedWithin(const Duration(minutes: 1)), isTrue); model.lastReload = DateTime.now().subtract(const Duration(hours: 1)); expect(model.reloadedWithin(const Duration(minutes: 1)), isFalse); }); }); group("Part Category:", () { test("Path", () async { var category = InvenTreePartCategory.fromJson({ "pathstring": "Electronics/Passives/Resistors", }); expect(category.parentPathString, equals("Electronics/Passives")); category = InvenTreePartCategory.fromJson({"pathstring": "Electronics"}); expect(category.parentPathString, equals(L10().partCategoryTopLevel)); }); test("Part Count", () async { expect(InvenTreePartCategory.fromJson({}).partcount, equals(0)); expect( InvenTreePartCategory.fromJson({"part_count": 4}).partcount, equals(4), ); // Legacy API used "parts" expect(InvenTreePartCategory.fromJson({"parts": 3}).partcount, equals(3)); }); }); group("Part:", () { test("Full Name", () async { expect( InvenTreePart.fromJson({ "full_name": "IPN | Name", "name": "x", }).fullname, equals("IPN | Name"), ); expect( InvenTreePart.fromJson({"name": "Widget"}).fullname, equals("Widget"), ); expect( InvenTreePart.fromJson({ "name": "Widget", "IPN": "W-001", "revision": "B", }).fullname, equals("W-001 | Widget | B"), ); }); test("Stock", () async { var part = InvenTreePart.fromJson({"in_stock": 10}); expect(part.inStock, equals(10)); expect(part.unallocatedStock, equals(10)); expect(part.stockString(), equals("10")); part = InvenTreePart.fromJson({ "in_stock": 1, "total_in_stock": 12.5, "unallocated_stock": "4", "units": "m", "ordering": 3, }); expect(part.inStock, equals(12.5)); expect(part.inStockString, equals("12.5")); expect(part.unallocatedStock, equals(4)); expect(part.stockString(), equals("4 / 12.5 m")); expect(part.stockString(includeUnits: false), equals("4 / 12.5")); expect(part.onOrderString, equals("3")); // Available stock cannot be negative part = InvenTreePart.fromJson({"in_stock": 1, "unallocated_stock": -5}); expect(part.unallocatedStock, equals(0)); }); test("Category", () async { final detail = {"name": "Resistors", "description": "Fixed resistors"}; var part = InvenTreePart.fromJson({ "category": 4, "category_detail": detail, }); expect(part.categoryId, equals(4)); expect(part.categoryName, equals("Resistors")); expect(part.categoryDescription, equals("Fixed resistors")); // Missing detail part = InvenTreePart.fromJson({"category": 4}); expect(part.categoryName, equals("")); expect(part.categoryDescription, equals("")); // No category part = InvenTreePart.fromJson({"category_detail": detail}); expect(part.categoryName, equals("")); expect(part.categoryDescription, equals("")); }); test("Images", () async { var part = InvenTreePart.fromJson({}); expect(part.image, equals(InvenTreeAPI.staticImage)); expect(part.thumbnail, equals(InvenTreeAPI.staticThumb)); part = InvenTreePart.fromJson({"image": "/media/a.png"}); expect(part.image, equals("/media/a.png")); expect(part.thumbnail, equals("/media/a.png")); part = InvenTreePart.fromJson({"thumbnail": "/media/a.thumb.png"}); expect(part.image, equals("/media/a.thumb.png")); expect(part.thumbnail, equals("/media/a.thumb.png")); }); test("Attributes", () async { final part = InvenTreePart.fromJson({ "assembly": true, "component": false, "purchaseable": true, "salable": false, "active": true, "virtual": false, "is_template": true, "trackable": true, "testable": false, "starred": true, "used_in": 6, "variant_of": 2, "default_location": 8, }); expect(part.isAssembly, isTrue); expect(part.isComponent, isFalse); expect(part.isPurchaseable, isTrue); expect(part.isSalable, isFalse); expect(part.isActive, isTrue); expect(part.isVirtual, isFalse); expect(part.isTemplate, isTrue); expect(part.isTrackable, isTrue); expect(part.isTestable, isFalse); expect(part.starred, isTrue); expect(part.usedInCount, equals(6)); expect(part.variantOf, equals(2)); expect(part.defaultLocation, equals(8)); expect(InvenTreePart.fromJson({}).usedInCount, equals(0)); }); test("Pricing", () async { var pricing = InvenTreePartPricing.fromJson({}); expect(pricing.currency, equals("USD")); expect(pricing.overallMin, isNull); pricing = InvenTreePartPricing.fromJson({ "currency": "AUD", "overall_min": "1.25", "overall_max": 3, }); expect(pricing.currency, equals("AUD")); expect(pricing.overallMin, equals(1.25)); expect(pricing.overallMax, equals(3)); }); }); group("Stock Item:", () { test("Quantity", () async { var item = InvenTreeStockItem.fromJson({"quantity": 10}); expect(item.isSerialized(), isFalse); expect(item.quantityString(), equals("10")); expect(item.serialOrQuantityDisplay(), equals("10")); expect(item.displayQuantity, equals("10")); item = InvenTreeStockItem.fromJson({ "quantity": 10, "allocated": 4, "part_detail": {"units": "kg"}, }); expect(item.available, equals(6)); expect(item.quantityString(), equals("6 / 10 kg")); expect(item.quantityString(includeUnits: false), equals("6 / 10")); expect(item.serialOrQuantityDisplay(), equals("6.0 / 10.0")); expect(item.displayQuantity, equals("10 kg")); }); test("Serial Number", () async { var item = InvenTreeStockItem.fromJson({"quantity": 1, "serial": "123"}); expect(item.isSerialized(), isTrue); expect(item.serialOrQuantityDisplay(), equals("SN 123")); expect(item.displayQuantity, equals("SN: 123")); // Serial number with quantity > 1 is not considered serialized item = InvenTreeStockItem.fromJson({"quantity": 2, "serial": "123"}); expect(item.isSerialized(), isFalse); }); test("Part Information", () async { var item = InvenTreeStockItem.fromJson({ "part_detail": { "full_name": "Widget | A", "description": "A widget", "thumbnail": "/media/w.thumb.png", }, }); expect(item.partName, equals("Widget | A")); expect(item.partDescription, equals("A widget")); expect(item.partImage, equals("/media/w.thumb.png")); expect(item.partThumbnail, equals("/media/w.thumb.png")); // Legacy API fields item = InvenTreeStockItem.fromJson({ "part__name": "Old", "part__description": "Old desc", "part__thumbnail": "/media/old.png", }); expect(item.partName, equals("Old")); expect(item.partDescription, equals("Old desc")); expect(item.partImage, equals("/media/old.png")); expect(item.partThumbnail, equals("/media/old.png")); // Image used as thumbnail backup item = InvenTreeStockItem.fromJson({ "part_detail": {"image": "/media/w.png"}, }); expect(item.partThumbnail, equals("/media/w.png")); // No image at all expect( InvenTreeStockItem.fromJson({}).partThumbnail, equals(InvenTreeAPI.staticThumb), ); }); test("Supplier Information", () async { var item = InvenTreeStockItem.fromJson({ "supplier_part": 3, "supplier_part_detail": { "SKU": "SKU-1", "supplier_detail": {"image": "/media/s1.png"}, }, "supplier_detail": {"supplier_name": "ACME"}, }); expect(item.supplierPartId, equals(3)); expect(item.supplierSKU, equals("SKU-1")); expect(item.supplierImage, equals("/media/s1.png")); expect(item.supplierName, equals("ACME")); item = InvenTreeStockItem.fromJson({ "supplier_detail": {"image": "/media/s2.png"}, }); expect(item.supplierImage, equals("/media/s2.png")); expect(InvenTreeStockItem.fromJson({}).supplierImage, equals("")); }); test("Location", () async { var item = InvenTreeStockItem.fromJson({}); expect(item.locationName, equals("Unknown Location")); expect(item.locationPathString, equals(L10().locationNotSet)); item = InvenTreeStockItem.fromJson({ "location": 7, "location_detail": {"name": "Shelf", "pathstring": "Store/Shelf"}, }); expect(item.locationId, equals(7)); expect(item.locationName, equals("Shelf")); expect(item.locationPathString, equals("Store/Shelf")); // Legacy name, and no pathstring item = InvenTreeStockItem.fromJson({ "location": 7, "location_detail": {}, "location__name": "Bin", }); expect(item.locationName, equals("Bin")); expect(item.locationPathString, equals("Bin")); }); test("Purchase Price", () async { var item = InvenTreeStockItem.fromJson({}); expect(item.purchasePrice, isNull); expect(item.hasPurchasePrice, isFalse); item = InvenTreeStockItem.fromJson({ "purchase_price": "2.50", "purchase_price_currency": "AUD", }); expect(item.purchasePrice, equals(2.5)); expect(item.hasPurchasePrice, isTrue); expect(item.purchasePriceCurrency, equals("AUD")); item = InvenTreeStockItem.fromJson({"purchase_price": "0"}); expect(item.hasPurchasePrice, isFalse); }); test("Attributes", () async { var item = InvenTreeStockItem.fromJson({}); expect(item.isInStock, isTrue); expect(item.hasSalesOrder, isFalse); expect(item.hasCustomer, isFalse); item = InvenTreeStockItem.fromJson({ "in_stock": false, "sales_order": 4, "customer": 2, "stale": true, "expired": false, "expiry_date": "2025-01-31", "batch": "B1", "packaging": "Reel", }); expect(item.isInStock, isFalse); expect(item.hasSalesOrder, isTrue); expect(item.hasCustomer, isTrue); expect(item.stale, isTrue); expect(item.expired, isFalse); expect(item.expiryDateString, equals("2025-01-31")); expect(item.batch, equals("B1")); expect(item.packaging, equals("Reel")); }); }); group("Orders:", () { test("Order", () async { var order = InvenTreeOrder.fromJson({}); expect(order.lineItemCount, equals(0)); expect(order.complete, isTrue); expect(order.totalPrice, isNull); expect(order.totalPriceCurrency, equals("")); expect(order.hasProjectCode, isFalse); order = InvenTreeOrder.fromJson({ "reference": "PO-0001", "line_items": 5, "completed_lines": 2, "total_price": "100.5", "order_currency": "AUD", "total_price_currency": "USD", "target_date": "2025-06-01", "project_code_detail": {"code": "PRJ", "description": "Project"}, "responsible_detail": {"name": "team", "label": "Team"}, }); expect(order.reference, equals("PO-0001")); expect(order.complete, isFalse); expect(order.totalPrice, equals(100.5)); expect(order.totalPriceCurrency, equals("AUD")); expect(order.targetDate, equals("2025-06-01")); expect(order.hasProjectCode, isTrue); expect(order.projectCodeDescription, equals("Project")); expect(order.responsibleName, equals("team")); expect(order.responsibleLabel, equals("Team")); // Legacy currency field order = InvenTreeOrder.fromJson({"total_price_currency": "USD"}); expect(order.totalPriceCurrency, equals("USD")); }); test("Order Line", () async { var line = InvenTreeOrderLine.fromJson({"quantity": 3}); expect(line.quantity, equals(3)); expect(line.part, isNull); expect(line.partImage, equals("")); line = InvenTreeOrderLine.fromJson({ "part_detail": { "pk": 4, "name": "Widget", "description": "A widget", "image": "/media/w.png", }, }); expect(line.part?.name, equals("Widget")); expect(line.partId, equals(4)); expect(line.partName, equals("Widget")); expect(line.partDescription, equals("A widget")); expect(line.partImage, equals("/media/w.png")); }); test("Extra Line", () async { final line = InvenTreeExtraLineItem.fromJson({ "order": 2, "quantity": "1.5", "price": "10", "price_currency": "EUR", }); expect(line.orderId, equals(2)); expect(line.quantity, equals(1.5)); expect(line.price, equals(10)); expect(line.priceCurrency, equals("EUR")); expect(line.formFields()["order"]?["hidden"], isTrue); }); }); }