Skip to content

Commit ba5cf96

Browse files
Merge pull request #1748 from IFRCGo/feature/admin2-can-be-deprecated
Districts and Admin2 areas can be deprecated – unit tests
2 parents 7225a0f + e28c294 commit ba5cf96

File tree

1 file changed

+154
-6
lines changed

1 file changed

+154
-6
lines changed

api/test_views.py

Lines changed: 154 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -333,8 +333,8 @@ class HistoricalEventTest(APITestCase):
333333
def test_historical_events(self):
334334
region1 = models.Region.objects.create(name=1)
335335
region2 = models.Region.objects.create(name=2)
336-
country1 = models.Country.objects.create(name='Nepal', iso3='nlp', region=region1)
337-
country2 = models.Country.objects.create(name='India', iso3='ind', region=region2)
336+
country1 = models.Country.objects.create(name='Nepal', iso3='NPL', region=region1)
337+
country2 = models.Country.objects.create(name='India', iso3='IND', region=region2)
338338
dtype1 = models.DisasterType.objects.get(pk=1)
339339
dtype2 = models.DisasterType.objects.get(pk=2)
340340
EventFactory.create(
@@ -403,13 +403,13 @@ class Admin2Test(APITestCase):
403403

404404
def test_admin2_api(self):
405405
region = models.Region.objects.create(name=1)
406-
country = models.Country.objects.create(name='Nepal', iso3='NLP', region=region)
407-
admin1_1 = models.District.objects.create(name='admin1 1', code='NLP01', country=country)
408-
admin1_2 = models.District.objects.create(name='admin1 2', code='NLP02', country=country)
406+
country = models.Country.objects.create(name='Nepal', iso3='NPL', region=region)
407+
admin1_1 = models.District.objects.create(name='admin1 1', code='NPL01', country=country)
408+
admin1_2 = models.District.objects.create(name='admin1 2', code='NPL02', country=country)
409409
admin2_1 = models.Admin2.objects.create(name='test 1', admin1=admin1_1, code='1')
410410
admin2_2 = models.Admin2.objects.create(name='test 2', admin1=admin1_2, code='2')
411411

412-
# test fetching all admin2
412+
# test fetching all admin2-s
413413
response = self.client.get('/api/v2/admin2/').json()
414414
self.assertEqual(response['count'], 2)
415415

@@ -418,3 +418,151 @@ def test_admin2_api(self):
418418
self.assertEqual(response['count'], 1)
419419
self.assertEqual(response['results'][0]['name'], 'test 1')
420420

421+
def test_admin2_deprecation(self):
422+
region = models.Region.objects.create(name=1)
423+
country = models.Country.objects.create(name='Nepal', iso3='NPL', region=region)
424+
admin1_1 = models.District.objects.create(name='admin1 1', code='NPL01', country=country)
425+
admin1_2 = models.District.objects.create(name='admin1 2', code='NPL02', country=country)
426+
admin2_1 = models.Admin2.objects.create(name='test 1', admin1=admin1_1, code='1')
427+
admin2_2 = models.Admin2.objects.create(name='test 2', admin1=admin1_2, code='2')
428+
admin2_3 = models.Admin2.objects.create(name='test 3', admin1=admin1_2, code='3')
429+
430+
# test fetching all admin2-s
431+
response = self.client.get('/api/v2/admin2/').json()
432+
self.assertEqual(response['count'], 3)
433+
434+
admin2_2.is_deprecated = True
435+
admin2_2.save()
436+
response = self.client.get('/api/v2/admin2/').json()
437+
# 2 instead of 3, because 1 admin2 area became deprecated: api does not show it.
438+
self.assertEqual(response['count'], 2)
439+
440+
# Tear down
441+
admin2_2.is_deprecated = False
442+
admin2_2.save()
443+
response = self.client.get('/api/v2/admin2/').json()
444+
self.assertEqual(response['count'], 3)
445+
446+
admin2_1.is_deprecated = True
447+
admin2_2.is_deprecated = True
448+
admin2_1.save()
449+
admin2_2.save()
450+
# 2 admin2-s are deprecated, so 3-2 = 1
451+
response = self.client.get('/api/v2/admin2/').json()
452+
self.assertEqual(response['count'], 1)
453+
454+
# Tear down
455+
admin2_1.is_deprecated = False
456+
admin2_2.is_deprecated = False
457+
admin2_1.save()
458+
admin2_2.save()
459+
response = self.client.get('/api/v2/admin2/').json()
460+
self.assertEqual(response['count'], 3)
461+
462+
admin2_1.is_deprecated = True
463+
admin2_2.is_deprecated = True
464+
admin2_3.is_deprecated = True
465+
admin2_1.save()
466+
admin2_2.save()
467+
admin2_3.save()
468+
# All admin2-s are deprecated
469+
response = self.client.get('/api/v2/admin2/').json()
470+
self.assertEqual(response['count'], 0)
471+
472+
# Tear down
473+
admin2_1.is_deprecated = False
474+
admin2_2.is_deprecated = False
475+
admin2_3.is_deprecated = False
476+
admin2_1.save()
477+
admin2_2.save()
478+
admin2_3.save()
479+
response = self.client.get('/api/v2/admin2/').json()
480+
self.assertEqual(response['count'], 3)
481+
482+
admin1_2.is_deprecated = True
483+
admin1_2.save()
484+
response = self.client.get('/api/v2/admin2/').json()
485+
# There are 2 admin2-s in this district, so 3-2 = 1
486+
self.assertEqual(response['count'], 1)
487+
488+
# Tear down
489+
admin1_2.is_deprecated = False
490+
admin1_2.save()
491+
response = self.client.get('/api/v2/admin2/').json()
492+
self.assertEqual(response['count'], 3)
493+
494+
admin1_1.is_deprecated = True
495+
admin1_1.save()
496+
response = self.client.get('/api/v2/admin2/').json()
497+
# There is only 1 admin2-s in this district, so 3-1 = 2
498+
self.assertEqual(response['count'], 2)
499+
500+
# Tear down
501+
admin1_1.is_deprecated = False
502+
admin1_1.save()
503+
response = self.client.get('/api/v2/admin2/').json()
504+
self.assertEqual(response['count'], 3)
505+
506+
country.is_deprecated = True
507+
country.save()
508+
response = self.client.get('/api/v2/admin2/').json()
509+
# There are 3 admin2-s in this country, so 3-3 = 0
510+
self.assertEqual(response['count'], 0)
511+
512+
# Tear down
513+
country.is_deprecated = False
514+
country.save()
515+
response = self.client.get('/api/v2/admin2/').json()
516+
self.assertEqual(response['count'], 3)
517+
518+
519+
class DistrictTest(APITestCase):
520+
def test_district_deprecation(self):
521+
region = models.Region.objects.create(name=1)
522+
country = models.Country.objects.create(name='Nepal', iso3='NPL', region=region)
523+
admin1_1 = models.District.objects.create(name='admin1 1', code='NPL01', country=country)
524+
admin1_2 = models.District.objects.create(name='admin1 2', code='NPL02', country=country)
525+
526+
# test fetching all districts
527+
response = self.client.get('/api/v2/district/').json()
528+
self.assertEqual(response['count'], 2)
529+
530+
admin1_2.is_deprecated = True
531+
admin1_2.save()
532+
response = self.client.get('/api/v2/district/').json()
533+
# one district deprecated, 2-1 = 1
534+
self.assertEqual(response['count'], 1)
535+
536+
# Tear down
537+
admin1_2.is_deprecated = False
538+
admin1_2.save()
539+
response = self.client.get('/api/v2/district/').json()
540+
self.assertEqual(response['count'], 2)
541+
542+
admin1_1.is_deprecated = True
543+
admin1_2.is_deprecated = True
544+
admin1_1.save()
545+
admin1_2.save()
546+
response = self.client.get('/api/v2/district/').json()
547+
# two districts deprecated, 2-2 = 0
548+
self.assertEqual(response['count'], 0)
549+
550+
# Tear down
551+
admin1_1.is_deprecated = False
552+
admin1_2.is_deprecated = False
553+
admin1_1.save()
554+
admin1_2.save()
555+
response = self.client.get('/api/v2/district/').json()
556+
self.assertEqual(response['count'], 2)
557+
558+
country.is_deprecated = True
559+
country.save()
560+
response = self.client.get('/api/v2/district/').json()
561+
# There are 2 districts in this country, so 2-2 = 0
562+
self.assertEqual(response['count'], 0)
563+
564+
# Tear down
565+
country.is_deprecated = False
566+
country.save()
567+
response = self.client.get('/api/v2/district/').json()
568+
self.assertEqual(response['count'], 2)

0 commit comments

Comments
 (0)