Skip to content

Commit

Permalink
Add modified_by and modified_at in local-unit api.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rup-Narayan-Rajbanshi committed Jul 11, 2024
1 parent 492feeb commit f6c63c4
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 7 deletions.
5 changes: 5 additions & 0 deletions local_units/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,12 +246,14 @@ class PrivateLocalUnitDetailSerializer(NestedCreateMixin, NestedUpdateMixin):
class Meta:
model = LocalUnit
fields = (
"id",
"local_branch_name",
"english_branch_name",
"type",
"country",
"created_at",
"modified_at",
"modified_by",
"draft",
"validated",
"postcode",
Expand Down Expand Up @@ -405,6 +407,7 @@ class PrivateLocalUnitSerializer(serializers.ModelSerializer):
type_details = LocalUnitTypeSerializer(source="type", read_only=True)
health_details = MiniHealthDataSerializer(read_only=True, source="health")
validated = serializers.BooleanField(read_only=True)
modified_by_details = LocalUnitMiniUserSerializer(source="modified_by", read_only=True)

class Meta:
model = LocalUnit
Expand All @@ -426,6 +429,8 @@ class Meta:
"focal_person_en",
"email",
"phone",
"modified_at",
"modified_by_details",
)

def get_location_details(self, unit) -> dict:
Expand Down
52 changes: 45 additions & 7 deletions local_units/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.contrib.gis.geos import Point

from api.models import Country, Region
from deployments.factories.user import UserFactory
from main.test_case import APITestCase

from .models import (
Expand Down Expand Up @@ -117,10 +118,10 @@ def test_filter(self):
class TestLocalUnitsDetailView(APITestCase):
def setUp(self):
super().setUp()
region = Region.objects.create(name=2)
country = Country.objects.create(name="Nepal", iso3="NLP", region=region)
type = LocalUnitType.objects.create(code=0, name="Code 0")
LocalUnitFactory.create_batch(2, country=country, type=type)
self.region = Region.objects.create(name=2)
self.country = Country.objects.create(name="Nepal", iso3="NLP", region=self.region)
self.type = LocalUnitType.objects.create(code=0, name="Code 0")
LocalUnitFactory.create_batch(2, country=self.country, type=self.type)

def test_detail(self):
local_unit = LocalUnit.objects.all().first()
Expand All @@ -133,6 +134,26 @@ def test_detail(self):
self.assertEqual(response.data["type_details"]["name"], "Code 0")
self.assertEqual(response.data["type_details"]["code"], 0)

def test_get_updated_at_updated_by(self):
self.authenticate()
user_1 = UserFactory.create()
user_2 = UserFactory.create()
user_1_local_units = LocalUnitFactory.create_batch(
2, country=self.country, type=self.type, created_by=user_1, modified_by=user_1
)
user_2_local_units = LocalUnitFactory.create_batch(
2, country=self.country, type=self.type, created_by=user_1, modified_by=user_2
)
user_1_local_unit = LocalUnit.objects.filter(id__in=[unit.id for unit in user_1_local_units]).first()
user_2_local_unit = LocalUnit.objects.filter(id__in=[unit.id for unit in user_2_local_units]).first()
response = self.client.get(f"/api/v2/local-units/{user_1_local_unit.id}/")
self.assertIsNotNone(response.data["modified_at"])
self.assertEqual(response.data["modified_by_details"]["id"], user_1.id)

response = self.client.get(f"/api/v2/local-units/{user_2_local_unit.id}/")
self.assertIsNotNone(response.data["modified_at"])
self.assertEqual(response.data["modified_by_details"]["id"], user_2.id)

def test_validate_local_units(self):
local_unit = LocalUnit.objects.all().first()
self.authenticate()
Expand Down Expand Up @@ -285,7 +306,7 @@ def test_create_local_unit_administrative(self):
response = self.client.post("/api/v2/local-units/", data=data, format="json")
self.assertEqual(response.status_code, 201)

def test_create_local_unit_health(self):
def test_create_update_local_unit_health(self):
region = Region.objects.create(name=2)
country = Country.objects.create(name="Philippines", iso3="PHL", iso="PH", region=region)
type = LocalUnitType.objects.create(code=2, name="Code 0")
Expand Down Expand Up @@ -366,5 +387,22 @@ def test_create_local_unit_health(self):
"phone": "",
}
self.client.force_authenticate(self.root_user)
response = self.client.post("/api/v2/local-units/", data=data, format="json")
self.assertEqual(response.status_code, 201)
response = self.client.post("/api/v2/local-units/", data=data, format="json").json()

# test update
local_unit_id = response["id"]
response_updated_1 = self.client.put(f"/api/v2/local-units/{local_unit_id}/", data=data, format="json").json()
local_unit_obj = LocalUnit.objects.get(id=local_unit_id)
self.assertIsNotNone(local_unit_obj.created_by)
self.assertIsNotNone(response_updated_1["modified_by"])
self.assertIsNotNone(response_updated_1["modified_at"])
self.assertEqual(response_updated_1["modified_by"], local_unit_obj.created_by.id)

# update existing local_unit with new user
user_1 = UserFactory()
self.client.force_authenticate(user_1)
response_updated_2 = self.client.put(f"/api/v2/local-units/{local_unit_id}/", data=data, format="json").json()
self.assertEqual(response_updated_2["modified_by_details"]["id"], user_1.id)
self.assertEqual(response_updated_2["created_by_details"]["id"], self.root_user.id)
assert response_updated_1["modified_at"] < response_updated_2["modified_at"]

0 comments on commit f6c63c4

Please sign in to comment.