44from django .contrib .gis .geos import Point
55
66from api .models import Country , Region
7+ from deployments .factories .user import UserFactory
78from main .test_case import APITestCase
89
910from .models import (
@@ -117,10 +118,10 @@ def test_filter(self):
117118class TestLocalUnitsDetailView (APITestCase ):
118119 def setUp (self ):
119120 super ().setUp ()
120- region = Region .objects .create (name = 2 )
121- country = Country .objects .create (name = "Nepal" , iso3 = "NLP" , region = region )
122- type = LocalUnitType .objects .create (code = 0 , name = "Code 0" )
123- LocalUnitFactory .create_batch (2 , country = country , type = type )
121+ self . region = Region .objects .create (name = 2 )
122+ self . country = Country .objects .create (name = "Nepal" , iso3 = "NLP" , region = self . region )
123+ self . type = LocalUnitType .objects .create (code = 0 , name = "Code 0" )
124+ LocalUnitFactory .create_batch (2 , country = self . country , type = self . type )
124125
125126 def test_detail (self ):
126127 local_unit = LocalUnit .objects .all ().first ()
@@ -133,6 +134,26 @@ def test_detail(self):
133134 self .assertEqual (response .data ["type_details" ]["name" ], "Code 0" )
134135 self .assertEqual (response .data ["type_details" ]["code" ], 0 )
135136
137+ def test_get_updated_at_updated_by (self ):
138+ self .authenticate ()
139+ user_1 = UserFactory .create ()
140+ user_2 = UserFactory .create ()
141+ user_1_local_units = LocalUnitFactory .create_batch (
142+ 2 , country = self .country , type = self .type , created_by = user_1 , modified_by = user_1
143+ )
144+ user_2_local_units = LocalUnitFactory .create_batch (
145+ 2 , country = self .country , type = self .type , created_by = user_1 , modified_by = user_2
146+ )
147+ user_1_local_unit = LocalUnit .objects .filter (id__in = [unit .id for unit in user_1_local_units ]).first ()
148+ user_2_local_unit = LocalUnit .objects .filter (id__in = [unit .id for unit in user_2_local_units ]).first ()
149+ response = self .client .get (f"/api/v2/local-units/{ user_1_local_unit .id } /" )
150+ self .assertIsNotNone (response .data ["modified_at" ])
151+ self .assertEqual (response .data ["modified_by_details" ]["id" ], user_1 .id )
152+
153+ response = self .client .get (f"/api/v2/local-units/{ user_2_local_unit .id } /" )
154+ self .assertIsNotNone (response .data ["modified_at" ])
155+ self .assertEqual (response .data ["modified_by_details" ]["id" ], user_2 .id )
156+
136157 def test_validate_local_units (self ):
137158 local_unit = LocalUnit .objects .all ().first ()
138159 self .authenticate ()
@@ -285,7 +306,7 @@ def test_create_local_unit_administrative(self):
285306 response = self .client .post ("/api/v2/local-units/" , data = data , format = "json" )
286307 self .assertEqual (response .status_code , 201 )
287308
288- def test_create_local_unit_health (self ):
309+ def test_create_update_local_unit_health (self ):
289310 region = Region .objects .create (name = 2 )
290311 country = Country .objects .create (name = "Philippines" , iso3 = "PHL" , iso = "PH" , region = region )
291312 type = LocalUnitType .objects .create (code = 2 , name = "Code 0" )
@@ -368,3 +389,21 @@ def test_create_local_unit_health(self):
368389 self .client .force_authenticate (self .root_user )
369390 response = self .client .post ("/api/v2/local-units/" , data = data , format = "json" )
370391 self .assertEqual (response .status_code , 201 )
392+
393+ # test update
394+ response = response .json ()
395+ local_unit_id = response ["id" ]
396+ response_updated_1 = self .client .put (f"/api/v2/local-units/{ local_unit_id } /" , data = data , format = "json" ).json ()
397+ local_unit_obj = LocalUnit .objects .get (id = local_unit_id )
398+ self .assertIsNotNone (local_unit_obj .created_by )
399+ self .assertIsNotNone (response_updated_1 ["modified_by" ])
400+ self .assertIsNotNone (response_updated_1 ["modified_at" ])
401+ self .assertEqual (response_updated_1 ["modified_by" ], local_unit_obj .created_by .id )
402+
403+ # update existing local_unit with new user
404+ user_1 = UserFactory ()
405+ self .client .force_authenticate (user_1 )
406+ response_updated_2 = self .client .put (f"/api/v2/local-units/{ local_unit_id } /" , data = data , format = "json" ).json ()
407+ self .assertEqual (response_updated_2 ["modified_by_details" ]["id" ], user_1 .id )
408+ self .assertEqual (response_updated_2 ["created_by_details" ]["id" ], self .root_user .id )
409+ assert response_updated_1 ["modified_at" ] < response_updated_2 ["modified_at" ]
0 commit comments