Skip to content

Commit

Permalink
Build history checking into other tests
Browse files Browse the repository at this point in the history
  • Loading branch information
WillNilges committed Jan 30, 2025
1 parent f42df56 commit b1b7bc4
Showing 1 changed file with 11 additions and 114 deletions.
125 changes: 11 additions & 114 deletions src/meshapi/tests/test_uisp_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,117 +352,6 @@ def test_notify_admins_autodetect_ap(self, mock_notify):
" - Mock change 2",
)


class TestUISPImportUpdateObjectsAndDontDuplicateHistory(TransactionTestCase):
def setUp(self):
self.node1 = Node(
network_number=1234,
status=Node.NodeStatus.ACTIVE,
type=Node.NodeType.STANDARD,
latitude=0,
longitude=0,
)
self.node1.save()

self.node2 = Node(
network_number=5678,
status=Node.NodeStatus.ACTIVE,
type=Node.NodeType.STANDARD,
latitude=0,
longitude=0,
)
self.node2.save()

self.node3 = Node(
network_number=9012,
status=Node.NodeStatus.ACTIVE,
type=Node.NodeType.STANDARD,
latitude=0,
longitude=0,
)
self.node3.save()

self.device1 = Device(
node=self.node1,
status=Device.DeviceStatus.ACTIVE,
name="nycmesh-1234-dev1",
)
self.device1.save()

self.device2 = Device(
node=self.node2,
status=Device.DeviceStatus.ACTIVE,
name="nycmesh-5678-dev2",
)
self.device2.save()

self.device3 = Device(
node=self.node3,
status=Device.DeviceStatus.ACTIVE,
name="nycmesh-9012-dev3",
)
self.device3.save()

self.link = Link(
from_device=self.device1,
to_device=self.device2,
status=Link.LinkStatus.ACTIVE,
type=Link.LinkType.FIVE_GHZ,
uisp_id="fake-uisp-uuid",
)
self.link.save()

@patch("meshapi.util.uisp_import.update_objects.get_uisp_link_last_seen")
def test_update_link_many_changes_and_dont_duplicate_history(self, mock_get_last_seen):
last_seen_date = datetime.datetime(2018, 11, 14, 15, 20, 32, 4000, tzinfo=tzutc())
mock_get_last_seen.return_value = last_seen_date

# Just one change from when we created it
self.assertEqual(1, len(self.link.history.all()))

change_messages = update_link_from_uisp_data(
self.link,
uisp_link_id="fake-uisp-uuid2",
uisp_from_device=self.device1,
uisp_to_device=self.device3,
uisp_status=Link.LinkStatus.INACTIVE,
)

self.link.refresh_from_db()
self.assertEqual(self.link.uisp_id, "fake-uisp-uuid2")
self.assertEqual(self.link.from_device, self.device1)
self.assertEqual(self.link.to_device, self.device3)
self.assertEqual(self.link.status, Link.LinkStatus.INACTIVE)
self.assertEqual(self.link.type, Link.LinkType.FIVE_GHZ)
self.assertEqual(self.link.abandon_date, last_seen_date.date())

# lol, uuid is non-deterministic.
self.assertEqual(
change_messages,
[
'Changed UISP link ID to fake-uisp-uuid2 for NN1234 ↔ NN5678 link (ID '
f'{self.link.id}). This is likely due to a UISP UUID '
'rotation',
"Changed connected device pair from [nycmesh-1234-dev1, nycmesh-5678-dev2] to [nycmesh-1234-dev1, nycmesh-9012-dev3]",
"Marked as Inactive due to it being offline in UISP for more than 30 days",
],
)

# Legitimite update
self.assertEqual(2, len(self.link.history.all()))

change_messages = update_link_from_uisp_data(
self.link,
uisp_link_id="fake-uisp-uuid2",
uisp_from_device=self.device1,
uisp_to_device=self.device3,
uisp_status=Link.LinkStatus.INACTIVE,
)

# Don't make another history object
self.link.refresh_from_db()
self.assertEqual(2, len(self.link.history.all()))

class TestUISPImportUpdateObjects(TransactionTestCase):
def setUp(self):
self.node1 = Node(
Expand Down Expand Up @@ -527,7 +416,7 @@ def test_update_link_many_changes(self, mock_get_last_seen):
last_seen_date = datetime.datetime(2018, 11, 14, 15, 20, 32, 4000, tzinfo=tzutc())
mock_get_last_seen.return_value = last_seen_date

# Just one change from when we created it
# Link should have just one history entry from when we created it
self.assertEqual(1, len(self.link.history.all()))

change_messages = update_link_from_uisp_data(
Expand Down Expand Up @@ -558,9 +447,10 @@ def test_update_link_many_changes(self, mock_get_last_seen):
],
)

# Legitimite update
# Link should have a second entry, due to legitimate update
self.assertEqual(2, len(self.link.history.all()))

# Run the update again...
change_messages = update_link_from_uisp_data(
self.link,
uisp_link_id="fake-uisp-uuid2",
Expand All @@ -569,7 +459,7 @@ def test_update_link_many_changes(self, mock_get_last_seen):
uisp_status=Link.LinkStatus.INACTIVE,
)

# Don't make another history object
# And it SHOULD NOT make another history object
self.link.refresh_from_db()
self.assertEqual(2, len(self.link.history.all()))

Expand Down Expand Up @@ -1977,6 +1867,7 @@ def test_sync_links_with_los_update_existing(self):
)
los2.save()

# LOS should have one history entry from creation
self.assertEqual(1, len(los1.history.all()))

sync_link_table_into_los_objects()
Expand All @@ -1993,12 +1884,18 @@ def test_sync_links_with_los_update_existing(self):
self.assertEqual(los2.source, LOS.LOSSource.EXISTING_LINK)
self.assertEqual(los2.analysis_date, datetime.date.today())

# Get another entry from legitimite update
self.assertEqual(2, len(los1.history.all()))
self.assertEqual(2, len(los2.history.all()))

# Run the sync again...
sync_link_table_into_los_objects()

# SHOULD NOT have a third entry
los1.refresh_from_db()
los2.refresh_from_db()
self.assertEqual(2, len(los1.history.all()))
self.assertEqual(2, len(los2.history.all()))

def test_sync_links_with_los_inactive_link(self):
self.link1.status = Link.LinkStatus.INACTIVE
Expand Down

0 comments on commit b1b7bc4

Please sign in to comment.