diff --git a/assets/release_notes.md b/assets/release_notes.md index dd7ad947..36549e71 100644 --- a/assets/release_notes.md +++ b/assets/release_notes.md @@ -8,6 +8,7 @@ - Multiple bug fixes for build order support - Improved role management for BOM items - Bug fix for stock item test results display +- Bug fixes for stock adjustment actions ### 1.1.0 - November 2026 --- diff --git a/lib/inventree/model.dart b/lib/inventree/model.dart index 0f281da6..1a045155 100644 --- a/lib/inventree/model.dart +++ b/lib/inventree/model.dart @@ -705,8 +705,15 @@ class InvenTreeModel { var response = await api.post(URL, body: data); + // Request rejected (e.g. validation errors) - already reported by post() + if (response.isValid() && !response.successful()) { + return null; + } + + final Map? created = createdObjectData(response); + // Invalid response returned from server - if (!response.isValid() || response.data == null || response.data is! Map) { + if (!response.isValid() || created == null) { reportModelError( "InvenTreeModel.create() returned invalid response", response, @@ -718,7 +725,28 @@ class InvenTreeModel { return null; } - return createFromJson(response.asMap()); + return createFromJson(created); + } + + /* + * Extract the data for a newly created object from a "create" response. + * + * Most endpoints return the created object, but some return a list of + * created objects (e.g. stock items since API v383, as multiple serialized + * items can be created at once). In that case, the first item is returned. + */ + static Map? createdObjectData(APIResponse response) { + final dynamic data = response.data; + + if (data is Map) { + return Map.from(data); + } + + if (data is List && data.isNotEmpty && data.first is Map) { + return Map.from(data.first as Map); + } + + return null; } Future listPaginated( diff --git a/lib/inventree/stock.dart b/lib/inventree/stock.dart index 74fd147f..a84de769 100644 --- a/lib/inventree/stock.dart +++ b/lib/inventree/stock.dart @@ -553,7 +553,8 @@ class InvenTreeStockItem extends InvenTreeModel { data = { "items": [ - {"pk": "${pk}", "quantity": "${quantity}"}, + // The quantity to count / add / remove / transfer + {"pk": "${pk}", "quantity": "${q}"}, ], "notes": notes ?? "", }; diff --git a/test/model_json_test.dart b/test/model_json_test.dart index dfb4ac26..1a179cc5 100644 --- a/test/model_json_test.dart +++ b/test/model_json_test.dart @@ -21,6 +21,44 @@ import "package:inventree/inventree/stock.dart"; import "package:inventree/l10.dart"; void main() { + // InvenTreeModel.create() response handling + group("Created Object Data:", () { + Map? created(dynamic data) => + InvenTreeModel.createdObjectData( + APIResponse(statusCode: 201, data: data), + ); + + test("Single object", () { + expect(created({"pk": 3}), equals({"pk": 3})); + }); + + // Stock item creation returns a list (API v383+) + test("List of objects", () { + expect( + created([ + {"pk": 7}, + ]), + equals({"pk": 7}), + ); + + // Multiple (serialized) items: the first is returned + expect( + created([ + {"pk": 7}, + {"pk": 8}, + ]), + equals({"pk": 7}), + ); + }); + + test("Invalid data", () { + expect(created(null), isNull); + expect(created([]), isNull); + expect(created(["x"]), isNull); + expect(created("x"), isNull); + }); + }); + group("Model Accessors:", () { test("Empty Data", () async { final model = InvenTreeModel.fromJson({}); diff --git a/test/stock_adjust_test.dart b/test/stock_adjust_test.dart new file mode 100644 index 00000000..9f84e42d --- /dev/null +++ b/test/stock_adjust_test.dart @@ -0,0 +1,228 @@ +/* + * Tests for stock adjustment actions (regression tests for B10). + * + * adjustStock() previously posted the full item quantity, + * ignoring the requested quantity. + * + * These tests require a running InvenTree server (see setup.dart) + * with the demo dataset loaded. Any stock items created are deleted again. + */ + +import "package:test/test.dart"; + +import "package:inventree/inventree/part.dart"; +import "package:inventree/inventree/stock.dart"; + +import "setup.dart"; + +void main() { + setupTestEnv(); + + late InvenTreePart part; + late InvenTreeStockLocation source; + late InvenTreeStockLocation destination; + + // Stock items created by the current test (deleted after each test) + final List created = []; + + Future first(T model, Map filters) async { + final results = await (model as dynamic).list( + filters: {...filters, "limit": "2"}, + ); + + expect(results, isNotEmpty, reason: "No ${T} matching ${filters}"); + return results.first as T; + } + + // Create a new stock item with [quantity] at the source location + // Note: Stock creation returns a list of created items (API v383+), + // which InvenTreeModel.create() must handle + Future createItem(double quantity) async { + final item = await InvenTreeStockItem().create({ + "part": part.pk, + "location": source.pk, + "quantity": quantity, + "notes": "Created by stock_adjust_test", + }); + + expect(item, isA()); + expect(item!.pk, greaterThan(0)); + + created.add(item.pk); + + expect((item as InvenTreeStockItem).quantity, equals(quantity)); + expect(item.partId, equals(part.pk)); + + return item; + } + + // Reload [item] from the server + Future reload(InvenTreeStockItem item) async { + expect(await item.reload(), isTrue); + return item; + } + + // Stock items for the test part at [location], excluding [exclude] + Future> itemsAt( + InvenTreeStockLocation location, { + Set exclude = const {}, + }) async { + final results = await InvenTreeStockItem().list( + filters: { + "part": part.pk.toString(), + "location": location.pk.toString(), + "cascade": "false", + }, + ); + + return results + .whereType() + .where((item) => !exclude.contains(item.pk)) + .toList(); + } + + setUpAll(() async { + await connectToTestServer(); + + part = await first(InvenTreePart(), { + "active": "true", + "trackable": "false", + "virtual": "false", + "is_template": "false", + }); + + final locations = await InvenTreeStockLocation().list( + filters: {"structural": "false", "limit": "2"}, + ); + + expect(locations, hasLength(2)); + source = locations[0] as InvenTreeStockLocation; + destination = locations[1] as InvenTreeStockLocation; + }); + + tearDown(() async { + for (final pk in created) { + await InvenTreeStockItem.fromJson({"pk": pk}).delete(); + } + + created.clear(); + }); + + group("Adjust Quantity:", () { + test("Count (partial)", () async { + final item = await createItem(10); + + expect(await item.countStock(7), isTrue); + expect((await reload(item)).quantity, equals(7)); + }); + + test("Count (full)", () async { + final item = await createItem(10); + + expect(await item.countStock(10), isTrue); + expect((await reload(item)).quantity, equals(10)); + }); + + test("Add", () async { + final item = await createItem(10); + + expect(await item.addStock(3), isTrue); + expect((await reload(item)).quantity, equals(13)); + }); + + test("Remove (partial)", () async { + final item = await createItem(10); + + expect(await item.removeStock(4), isTrue); + expect((await reload(item)).quantity, equals(6)); + }); + + test("Remove (full)", () async { + final item = await createItem(10); + + expect(await item.removeStock(10), isTrue); + + // The item may be deleted when depleted (depending on server settings) + final InvenTreeStockItem check = InvenTreeStockItem.fromJson({ + "pk": item.pk, + }); + + if (await check.reload()) { + expect(check.quantity, equals(0)); + } + }); + + test("Negative quantity is rejected", () async { + final item = await createItem(10); + + expect(await item.addStock(-1), isFalse); + expect(await item.removeStock(-1), isFalse); + expect((await reload(item)).quantity, equals(10)); + }); + }); + + group("Transfer:", () { + test("Full quantity (default)", () async { + final item = await createItem(10); + + expect(await item.transferStock(destination.pk), isTrue); + + final moved = await reload(item); + expect(moved.locationId, equals(destination.pk)); + expect(moved.quantity, equals(10)); + }); + + test("Full quantity (explicit)", () async { + final item = await createItem(10); + + expect(await item.transferStock(destination.pk, quantity: 10), isTrue); + + final moved = await reload(item); + expect(moved.locationId, equals(destination.pk)); + expect(moved.quantity, equals(10)); + }); + + test("Partial quantity", () async { + final item = await createItem(10); + + // Existing stock at the destination (not created by this test) + final before = (await itemsAt(destination)).map((i) => i.pk).toSet(); + + expect(await item.transferStock(destination.pk, quantity: 4), isTrue); + + // The remainder stays at the source location + final remainder = await reload(item); + expect(remainder.locationId, equals(source.pk)); + expect(remainder.quantity, equals(6)); + + // The transferred quantity is split into a new item at the destination + final split = await itemsAt(destination, exclude: {...before, item.pk}); + + created.addAll(split.map((i) => i.pk)); + + expect(split, hasLength(1)); + expect(split.first.quantity, equals(4)); + }); + }); + + test("Create is rejected with invalid data", () async { + // Missing part - the server returns validation errors (not an item) + final item = await InvenTreeStockItem().create({ + "location": source.pk, + "quantity": 1, + }); + + expect(item, isNull); + }); + + test("Serialized stock cannot be counted", () async { + final item = InvenTreeStockItem.fromJson({ + "pk": 1, + "quantity": 1, + "serial": "123", + }); + + expect(item.isSerialized(), isTrue); + expect(await item.countStock(1), isFalse); + }); +}