Skip to content

Commit

Permalink
Pick up existing node on install create
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew-Dickinson committed Jan 18, 2025
1 parent 813aec0 commit 2452cd4
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/meshapi/models/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,11 @@ def save(self, *args: Any, **kwargs: Any) -> None:
if self.install_number != original.install_number:
raise ValidationError("Install number is immutable")

if self._state.adding:
if not self.node:
if self.building.primary_node:
self.node = self.building.primary_node
elif self.building.nodes:
self.node = self.building.nodes.first()

super().save(*args, **kwargs)
74 changes: 73 additions & 1 deletion src/meshapi/tests/test_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django.core.exceptions import ValidationError
from django.test import TestCase

from meshapi.models import Building, Install, Member
from meshapi.models import Building, Install, Member, Node
from meshapi.tests.sample_data import sample_building, sample_install, sample_member


Expand Down Expand Up @@ -43,6 +43,78 @@ def test_construct_install_no_id_yes_install_number(self):
self.assertIsNotNone(install2.id)
self.assertEqual(install2.install_number, 89)

def test_construct_install_building_has_node(self):
building_node = Node(status=Node.NodeStatus.ACTIVE, latitude=0, longitude=0)
building_node.save()

self.building_1.primary_node = building_node
self.building_1.save()

install = Install(**self.sample_install_copy, install_number=45)
install.save()

install.refresh_from_db()
self.assertEqual(install.node, building_node)

def test_construct_install_building_has_node_without_primary(self):
building_node = Node(status=Node.NodeStatus.ACTIVE, latitude=0, longitude=0)
building_node.save()

self.building_1.nodes.add(building_node)
self.building_1.save()

install = Install(**self.sample_install_copy, install_number=45)
install.save()

install.refresh_from_db()
self.assertEqual(install.node, building_node)

def test_remove_node_when_building_has_one(self):
building_node = Node(status=Node.NodeStatus.ACTIVE, latitude=0, longitude=0)
building_node.save()

self.building_1.primary_node = building_node
self.building_1.save()

install = Install(**self.sample_install_copy, install_number=45)
install.node = building_node
install.save()

install.refresh_from_db()
self.assertEqual(install.node, building_node)

install.node = None
install.save()

install.refresh_from_db()
self.assertEqual(install.node, None)

def test_set_install_to_different_to_building(self):
building1_node = Node(status=Node.NodeStatus.ACTIVE, latitude=0, longitude=0)
building2_node = Node(status=Node.NodeStatus.ACTIVE, latitude=0, longitude=0)
building1_node.save()
building2_node.save()

self.building_1.primary_node = building1_node
self.building_1.save()

building2 = Building(**sample_building)
building2.primary_node = building2_node
building2.save()

install = Install(**self.sample_install_copy, install_number=45)
install.node = building1_node
install.save()

install.refresh_from_db()
self.assertEqual(install.node, building1_node)

install.building = building2
install.save()

install.refresh_from_db()
self.assertEqual(install.node, building1_node)

def test_construct_install_yes_id_no_install_number(self):
install = Install(
id=uuid.UUID("23ef170c-f37d-44e3-aaac-93dae636c86e"),
Expand Down

0 comments on commit 2452cd4

Please sign in to comment.