feat: add asset ID history API
This commit is contained in:
1 parent
a87ab2709f
commit
d738b8e27e
6 files changed
+254
-9
No files matched your search
@@ -17,7 +17,9 @@ from app.inventory import (
|
||||
InventoryService,
|
||||
InventoryUpdateRequest,
|
||||
InventoryValidationError,
|
||||
MinerNotFound,
|
||||
)
|
||||
from app.inventory.models import InventoryAssetIDHistoryEntry
|
||||
|
||||
|
||||
def inventory_row(**overrides):
|
||||
@@ -121,6 +123,7 @@ class FakeInventoryRepository:
|
||||
"new_asset_id": new_asset_id,
|
||||
"changed_at": datetime.now(timezone.utc),
|
||||
"changed_by": changed_by,
|
||||
"changed_by_display_name": "Test Actor",
|
||||
}
|
||||
self.asset_id_history.append(history)
|
||||
return history
|
||||
@@ -207,6 +210,60 @@ class InventoryServiceTests(unittest.TestCase):
|
||||
with self.assertRaises(InventoryValidationError):
|
||||
self.service.normalize_tags(["valid", "\t\n"])
|
||||
|
||||
def test_asset_id_history_empty_for_known_miner(self):
|
||||
row = inventory_row()
|
||||
self.repository.rows[row["id"]] = row
|
||||
|
||||
self.assertEqual(
|
||||
self.service.get_asset_id_history(row["id"]),
|
||||
[],
|
||||
)
|
||||
|
||||
def test_asset_id_history_maps_populated_repository_results(self):
|
||||
row = inventory_row()
|
||||
self.repository.rows[row["id"]] = row
|
||||
first_changed_at = datetime(2026, 7, 27, tzinfo=timezone.utc)
|
||||
second_changed_at = datetime(2026, 7, 28, tzinfo=timezone.utc)
|
||||
self.repository.asset_id_history = [
|
||||
{
|
||||
"id": uuid4(),
|
||||
"miner_id": row["id"],
|
||||
"old_asset_id": "ASSET-FIRST",
|
||||
"new_asset_id": "ASSET-SECOND",
|
||||
"changed_at": first_changed_at,
|
||||
"changed_by": self.actor_user_id,
|
||||
"changed_by_display_name": "Test Actor",
|
||||
},
|
||||
{
|
||||
"id": uuid4(),
|
||||
"miner_id": row["id"],
|
||||
"old_asset_id": "ASSET-SECOND",
|
||||
"new_asset_id": "ASSET-THIRD",
|
||||
"changed_at": second_changed_at,
|
||||
"changed_by": self.actor_user_id,
|
||||
"changed_by_display_name": "Test Actor",
|
||||
},
|
||||
]
|
||||
|
||||
history = self.service.get_asset_id_history(row["id"])
|
||||
|
||||
self.assertEqual(len(history), 2)
|
||||
self.assertTrue(
|
||||
all(
|
||||
isinstance(entry, InventoryAssetIDHistoryEntry)
|
||||
for entry in history
|
||||
)
|
||||
)
|
||||
self.assertEqual(
|
||||
[entry.new_asset_id for entry in history],
|
||||
["ASSET-SECOND", "ASSET-THIRD"],
|
||||
)
|
||||
self.assertEqual(history[0].changed_by_display_name, "Test Actor")
|
||||
|
||||
def test_asset_id_history_unknown_miner_raises_not_found(self):
|
||||
with self.assertRaises(MinerNotFound):
|
||||
self.service.get_asset_id_history(uuid4())
|
||||
|
||||
def test_metadata_update_preserves_unspecified_fields(self):
|
||||
row = inventory_row(
|
||||
asset_tag="ASSET-001",
|
||||
@@ -472,6 +529,14 @@ class InventoryRepositoryIntegrationTests(unittest.TestCase):
|
||||
InventoryMetadataUpdateRequest(asset_id="ASSET-THIRD"),
|
||||
actor_user_id=self.actor_user_id,
|
||||
)
|
||||
other = service.create_miner(
|
||||
InventoryCreateRequest(asset_id="OTHER-FIRST")
|
||||
)
|
||||
service.update_metadata(
|
||||
other.id,
|
||||
InventoryMetadataUpdateRequest(asset_id="OTHER-SECOND"),
|
||||
actor_user_id=self.actor_user_id,
|
||||
)
|
||||
|
||||
self.assertEqual(created.id, second.id)
|
||||
self.assertEqual(second.id, third.id)
|
||||
@@ -491,6 +556,17 @@ class InventoryRepositoryIntegrationTests(unittest.TestCase):
|
||||
history[0]["changed_at"],
|
||||
history[1]["changed_at"],
|
||||
)
|
||||
self.assertTrue(
|
||||
all(
|
||||
row["miner_id"] == created.id
|
||||
and row["changed_by_display_name"]
|
||||
== "Inventory History Test"
|
||||
for row in history
|
||||
)
|
||||
)
|
||||
other_history = self.repository.list_asset_id_history(other.id)
|
||||
self.assertEqual(len(other_history), 1)
|
||||
self.assertEqual(other_history[0]["new_asset_id"], "OTHER-SECOND")
|
||||
|
||||
def test_history_failure_rolls_back_asset_id_update(self):
|
||||
service = InventoryService(self.repository)
|
||||
|
||||
Reference in new issue
Block a user