Skip to content

Commit

Permalink
Add link and sector endpoints (#199)
Browse files Browse the repository at this point in the history
* Add endpoints for Link and Sector with recursive object resolution

* Add test for monster recursive result from link field

* Fix test failures from rebase
  • Loading branch information
Andrew-Dickinson authored Feb 16, 2024
1 parent b33b018 commit 6606095
Show file tree
Hide file tree
Showing 6 changed files with 532 additions and 4 deletions.
34 changes: 33 additions & 1 deletion src/meshapi/serializers/model_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from rest_framework.fields import empty
from rest_framework.serializers import raise_errors_on_nested_writes

from meshapi.models import Building, Install, Member
from meshapi.models import Building, Install, Link, Member, Sector
from meshapi.permissions import check_has_model_view_permission


Expand Down Expand Up @@ -148,9 +148,41 @@ class Meta:
building = BuildingSerializer(exclude_fields=["installs"])


class LinkSerializer(RecursiveSerializer):
class Meta:
model = Link
fields = "__all__"

from_building = BuildingSerializer(exclude_fields=["links_from", "links_to"])
to_building = BuildingSerializer(exclude_fields=["links_from", "links_to"])


class SectorSerializer(RecursiveSerializer):
class Meta:
model = Sector
fields = "__all__"

building = BuildingSerializer(exclude_fields=["sectors"])


# This is a bit hacky, but gets around the fact that we can't call InstallSerializer() until after
# MemberSerializer has already been declared
MemberSerializer._declared_fields["installs"] = InstallSerializer(exclude_fields=["member"], many=True, read_only=True)
BuildingSerializer._declared_fields["installs"] = InstallSerializer(
exclude_fields=["building"], many=True, read_only=True
)
BuildingSerializer._declared_fields["links_from"] = LinkSerializer(
exclude_fields=["building"],
many=True,
read_only=True,
)
BuildingSerializer._declared_fields["links_to"] = LinkSerializer(
exclude_fields=["building"],
many=True,
read_only=True,
)
BuildingSerializer._declared_fields["sectors"] = SectorSerializer(
exclude_fields=["building"],
many=True,
read_only=True,
)
134 changes: 134 additions & 0 deletions src/meshapi/tests/test_link.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import json

from django.contrib.auth.models import User
from django.test import Client, TestCase

from ..models import Building, Link


class TestLink(TestCase):
c = Client()

def setUp(self):
self.admin_user = User.objects.create_superuser(
username="admin", password="admin_password", email="[email protected]"
)
self.c.login(username="admin", password="admin_password")

self.building_1 = Building(
id=1,
building_status=Building.BuildingStatus.ACTIVE,
address_truth_sources="",
latitude=0,
longitude=0,
altitude=0,
invalid=True,
)
self.building_1.save()

self.building_2 = Building(
id=2,
building_status=Building.BuildingStatus.ACTIVE,
address_truth_sources="",
latitude=0,
longitude=0,
altitude=0,
invalid=True,
)
self.building_2.save()

def test_new_link(self):
response = self.c.post(
"/api/v1/links/",
{
"from_building": self.building_1.id,
"to_building": self.building_2.id,
"status": "Active",
},
)
code = 201
self.assertEqual(
code,
response.status_code,
f"status code incorrect. Should be {code}, but got {response.status_code}",
)

def test_broken_link(self):
response = self.c.post(
"/api/v1/links/",
{
"from_building": "",
"to_building": self.building_2.id,
"status": "Active",
},
)
code = 400
self.assertEqual(
code,
response.status_code,
f"status code incorrect. Should be {code}, but got {response.status_code}",
)

def test_recursive_get(self):
link = Link(
from_building=self.building_1,
to_building=self.building_2,
status=Link.LinkStatus.ACTIVE,
)
link.save()

response = self.c.get(f"/api/v1/links/{link.id}/")

code = 200
self.assertEqual(
code,
response.status_code,
f"status code incorrect. Should be {code}, but got {response.status_code}",
)

response_obj = json.loads(response.content)
self.assertEqual(response_obj["status"], "Active")
self.assertEqual(
response_obj["from_building"],
{
"id": 1,
"address_truth_sources": "",
"altitude": 0.0,
"bin": None,
"building_status": "Active",
"city": None,
"installs": [],
"sectors": [],
"invalid": True,
"latitude": 0.0,
"longitude": 0.0,
"node_name": None,
"notes": None,
"primary_nn": None,
"state": None,
"street_address": None,
"zip_code": None,
},
)
self.assertEqual(
response_obj["to_building"],
{
"id": 2,
"address_truth_sources": "",
"altitude": 0.0,
"bin": None,
"building_status": "Active",
"city": None,
"installs": [],
"sectors": [],
"invalid": True,
"latitude": 0.0,
"longitude": 0.0,
"node_name": None,
"notes": None,
"primary_nn": None,
"state": None,
"street_address": None,
"zip_code": None,
},
)
Loading

0 comments on commit 6606095

Please sign in to comment.