Files
doczyai-pipelines/fieldExtraction/tests/qcqa/date_format_test.py
T
Chris Bull 26c12469bc Merged in QCQA_Additions (pull request #331)
QCQA Additions

* tested on batch 10, small fixes + change to s3 output

* Merged main into QCQA_Additions

* unit tests for qc qa helpers

* static error fix

* another static fix

* static fix again

* fixed None check

* static fix none

* Merged main into QCQA_Additions

* Small changes based on Katon's review


Approved-by: Katon Minhas
2024-12-13 19:23:04 +00:00

84 lines
2.6 KiB
Python

import pytest
from datetime import datetime
import sys
sys.path.append('src')
from qa_qc_helpers import date_format_check
def test_valid_date_formats():
"""Test valid date formats are correctly validated"""
valid_dates = {
"01/01/2024" : '1/1/2024',
"12/31/2023" : '12/31/2023',
"02/29/2024" : '2/29/2024', # Leap year
"07/04/1776" : '7/4/1776',
"09/08/2024" : '9/8/2024'
}
for date in valid_dates.keys():
result, is_valid = date_format_check(date)
assert result == valid_dates[date]
assert is_valid is True
def test_invalid_date_formats():
"""Test various invalid date formats"""
invalid_dates = [
"13/01/2024", # Invalid month
"01/32/2024", # Invalid day
"02/29/2023", # Not a leap year
"00/01/2024", # Zero month
"01/00/2024", # Zero day
"01-01-2024", # Wrong separator
"2024/01/01", # Wrong order
"01/01/24", # Two-digit year
"Jan/01/2024", # Text month
]
for date in invalid_dates:
result, is_valid = date_format_check(date)
assert result == date
assert is_valid is False
def test_non_string_inputs():
"""Test handling of non-string inputs"""
test_cases = [
(20240101, "20240101"),
(None, None),
(1.234, "1.234"),
(["01/01/2024"], "['01/01/2024']"),
({"date": "01/01/2024"}, "{'date': '01/01/2024'}"),
]
for input_val, expected_str in test_cases:
result, is_valid = date_format_check(input_val)
assert result == str(input_val)
assert is_valid is False
def test_edge_cases():
"""Test edge cases with valid-looking but incorrect formats"""
test_cases = [
"01/01/2024 ", # Extra space at end
" 01/01/2024", # Extra space at start
"01/01/2024\n", # Newline at end
"01 /01/2024", # Space in middle
"01/01/ 2024", # Space in middle
]
for date in test_cases:
result, is_valid = date_format_check(date)
assert result == date
assert is_valid is False
def test_special_dates():
"""Test boundary cases for dates"""
test_cases = [
("12/31/9999", True), # Far future
("2/28/2024", True), # Day before leap day
("3/1/2024", True), # Day after leap day
("12/31/2023", True), # Last day of year
("1/1/2024", True), # First day of year
]
for date, expected_valid in test_cases:
result, is_valid = date_format_check(date)
assert result == date
assert is_valid == expected_valid