Skip to content

Commit ffbb376

Browse files
authored
Merge pull request #213 from praekeltfoundation/add-clinic-lookup
Add import clinics management command
2 parents af58ffa + 4235423 commit ffbb376

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import csv
2+
from pathlib import Path
3+
4+
from django.core.management.base import BaseCommand, CommandError
5+
6+
from momconnect.models import Clinic
7+
8+
9+
class Command(BaseCommand):
10+
help = (
11+
"Import clinics from a CSV file and update or create them in the Clinic model"
12+
)
13+
14+
def add_arguments(self, parser):
15+
parser.add_argument(
16+
"csv_file", type=str, help="Path to the CSV file containing clinic data"
17+
)
18+
19+
def handle(self, *args, **options):
20+
csv_file = options["csv_file"]
21+
try:
22+
with Path(csv_file).open(newline="", encoding="utf-8") as file:
23+
reader = csv.DictReader(file)
24+
for row in reader:
25+
clinic, created = Clinic.objects.update_or_create(
26+
value=row["value"],
27+
defaults={
28+
"code": row["code"],
29+
"uid": row["uid"],
30+
"name": row["name"],
31+
"province": row["province"],
32+
"location": row["location"],
33+
"area_type": row["area_type"],
34+
"unit_type": row["unit_type"],
35+
"district": row["district"],
36+
"municipality": row["municipality"],
37+
},
38+
)
39+
if created:
40+
self.stdout.write(
41+
self.style.SUCCESS(f"Created clinic: {clinic.name}")
42+
)
43+
else:
44+
self.stdout.write(
45+
self.style.SUCCESS(f"Updated clinic: {clinic.name}")
46+
)
47+
except FileNotFoundError as e:
48+
raise CommandError(f"File '{csv_file}' does not exist.") from e
49+
except KeyError as e:
50+
raise CommandError(f"Missing required column in CSV: {e}") from e
51+
except Exception as e:
52+
raise CommandError(f"An error occurred: {e}") from e
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
uid,code,name,province,location,area_type,unit_type,district
2+
123,123456,Clinic One,GP,Location One,Urban,Type A,District A
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
uid,value,code,name,province,location,area_type,unit_type,district,municipality
2+
123,123456,123456,Clinic One,GP,Location One,Urban,Type A,District A,Municipality A
3+
456,123457,123457,Clinic Two,WC,Location Two,Rural,Type B,District B,Municipality B
+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
from django.core.management import CommandError, call_command
2+
from django.test import TestCase
3+
4+
from momconnect.models import Clinic
5+
6+
7+
class ImportClinicsCommandTest(TestCase):
8+
def get_file_path(self, name):
9+
return f"./momconnect/tests/clinic-data/{name}.csv"
10+
11+
def test_import_clinics_success(self):
12+
call_command("import_clinics", self.get_file_path("import_valid"))
13+
14+
self.assertEqual(Clinic.objects.count(), 2)
15+
clinic1 = Clinic.objects.get(uid="123")
16+
self.assertEqual(clinic1.name, "Clinic One")
17+
self.assertEqual(clinic1.province, "GP")
18+
self.assertEqual(clinic1.value, "123456")
19+
self.assertEqual(clinic1.code, "123456")
20+
self.assertEqual(clinic1.location, "Location One")
21+
self.assertEqual(clinic1.area_type, "Urban")
22+
self.assertEqual(clinic1.unit_type, "Type A")
23+
self.assertEqual(clinic1.district, "District A")
24+
self.assertEqual(clinic1.municipality, "Municipality A")
25+
26+
clinic2 = Clinic.objects.get(uid="456")
27+
self.assertEqual(clinic2.name, "Clinic Two")
28+
self.assertEqual(clinic2.province, "WC")
29+
self.assertEqual(clinic2.value, "123457")
30+
self.assertEqual(clinic2.code, "123457")
31+
self.assertEqual(clinic2.location, "Location Two")
32+
self.assertEqual(clinic2.area_type, "Rural")
33+
self.assertEqual(clinic2.unit_type, "Type B")
34+
self.assertEqual(clinic2.district, "District B")
35+
self.assertEqual(clinic2.municipality, "Municipality B")
36+
37+
def test_import_clinics_update_existing(self):
38+
Clinic.objects.create(
39+
uid="123", value="123456", code="123456", name="Old Clinic"
40+
)
41+
call_command("import_clinics", self.get_file_path("import_valid"))
42+
43+
clinic = Clinic.objects.get(uid="123")
44+
self.assertEqual(clinic.name, "Clinic One")
45+
self.assertEqual(clinic.province, "GP")
46+
self.assertEqual(clinic.value, "123456")
47+
self.assertEqual(clinic.code, "123456")
48+
self.assertEqual(clinic.location, "Location One")
49+
self.assertEqual(clinic.area_type, "Urban")
50+
self.assertEqual(clinic.unit_type, "Type A")
51+
self.assertEqual(clinic.district, "District A")
52+
self.assertEqual(clinic.municipality, "Municipality A")
53+
self.assertEqual(Clinic.objects.count(), 2)
54+
55+
def test_import_clinics_missing_column(self):
56+
with self.assertRaises(CommandError) as context:
57+
call_command("import_clinics", self.get_file_path("import_invalid"))
58+
self.assertIn("Missing required column", str(context.exception))
59+
60+
def test_import_clinics_file_not_found(self):
61+
with self.assertRaises(CommandError) as context:
62+
call_command("import_clinics", "nonexistent.csv")
63+
self.assertIn("does not exist", str(context.exception))

0 commit comments

Comments
 (0)