Skip to content

Commit

Permalink
Roll out database-level tests to datetime parser too
Browse files Browse the repository at this point in the history
  • Loading branch information
janbaykara committed Dec 12, 2024
1 parent 4052684 commit 89f5764
Showing 1 changed file with 65 additions and 31 deletions.
96 changes: 65 additions & 31 deletions hub/tests/test_source_parser.py
Original file line number Diff line number Diff line change
@@ -1,58 +1,92 @@
from datetime import datetime, timezone

from django.test import TestCase
from asgiref.sync import sync_to_async

from hub.models import LocalJSONSource
from hub.validation import validate_and_format_phone_number
from utils.py import parse_datetime
from hub.models import LocalJSONSource, GenericData


class TestSourceParser(TestCase):
dates_that_should_work = [
["01/06/2024, 09:30", datetime(2024, 6, 1, 9, 30, tzinfo=timezone.utc)],
["15/06/2024, 09:30", datetime(2024, 6, 15, 9, 30, tzinfo=timezone.utc)],
["15/06/2024, 09:30:00", datetime(2024, 6, 15, 9, 30, 0, tzinfo=timezone.utc)],
["2023-12-20 06:00:00", datetime(2023, 12, 20, 6, 0, 0, tzinfo=timezone.utc)],
]
async def test_date_field(self):
fixture = [
{
"id": "1",
"date": "01/06/2024, 09:30",
"expected": datetime(2024, 6, 1, 9, 30, tzinfo=timezone.utc),
},
{
"id": "2",
"date": "15/06/2024, 09:30",
"expected": datetime(2024, 6, 15, 9, 30, tzinfo=timezone.utc),
},
{
"id": "3",
"date": "15/06/2024, 09:30:00",
"expected": datetime(2024, 6, 15, 9, 30, 0, tzinfo=timezone.utc),
},
{
"id": "4",
"date": "2023-12-20 06:00:00",
"expected": datetime(2023, 12, 20, 6, 0, 0, tzinfo=timezone.utc),
},
]

def test_dateparse(self):
for date in self.dates_that_should_work:
self.assertEqual(parse_datetime(date[0]), date[1])
source = await LocalJSONSource.objects.acreate(
name="date_test",
id_field="id",
start_time_field="date",
data=[
{
"id": d["id"],
"date": d["date"],
}
for d in fixture
],
)

# generate GenericData records
await source.import_many(source.data)

# test that the GenericData records have valid dates
data = source.get_import_data()

for e in fixture:
d = await data.aget(data=e["id"])
self.assertEqual(d.start_time, e["expected"])


class TestPhoneField(TestCase):
async def test_save_phone_field(self):
async def test_phone_field(self):
fixture = [
{"id": "bad1", "phone": "123456789", "expected": None},
{"id": "good1", "phone": "07123456789", "expected": "+447123456789"},
{"id": "good2", "phone": "+447123456789", "expected": "+447123456789"},
]

source = await LocalJSONSource.objects.acreate(
name="test",
name="phone_test",
id_field="id",
phone_field="phone",
countries=["GB"],
data=[
{"id": "bad1", "phone": "123456789"},
{"id": "good1", "phone": "07123456789"},
{"id": "good2", "phone": "+447123456789"},
{
"id": e["id"],
"phone": e["phone"],
}
for e in fixture
],
)
await sync_to_async(source.save)()

# parse the raw data
# generate GenericData records
await source.import_many(source.data)

# tests
# test that the GenericData records have valid, formatted phone field
data = source.get_import_data()

bad1 = await data.aget(data="bad1")
self.assertIsNone(bad1.phone)
self.assertEqual(bad1.json["phone"], "123456789")

good1 = await data.aget(data="good1")
self.assertEqual(good1.phone, "+447123456789")
self.assertEqual(good1.json["phone"], "07123456789")

good2 = await data.aget(data="good2")
self.assertEqual(good2.phone, "+447123456789")
self.assertEqual(good2.json["phone"], "+447123456789")
for e in fixture:
d = await data.aget(data=e["id"])
self.assertEqual(d.phone, e["expected"])
self.assertEqual(d.json["phone"], e["phone"])

def test_valid_phone_number_for_usa(self):
phone = "4155552671"
Expand Down

0 comments on commit 89f5764

Please sign in to comment.