Skip to content

Commit

Permalink
Update compare_age tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hewanadun committed Dec 14, 2024
1 parent af5e482 commit 3169310
Showing 1 changed file with 58 additions and 15 deletions.
73 changes: 58 additions & 15 deletions tests/test_rules_compare_age.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""
Tests the custom compare_with_date rule (_validate_compare_with_date).
"""


def test_compare_age(date_constraint, create_nacc_validator):
""" Tests compare_age, case where compare_to is another field"""
schema = {
Expand Down Expand Up @@ -32,14 +34,18 @@ def test_compare_age(date_constraint, create_nacc_validator):
nv = create_nacc_validator(schema)

# valid cases
assert nv.validate({'frmdate': '2024/02/02', 'birthmo': 6, 'birthyr': 1950, 'behage': 50})
assert nv.validate({'frmdate': '2024/02/02', 'birthmo': 2, 'birthyr': 2024, 'behage': 0})
assert nv.validate(
{'frmdate': '2024/02/02', 'birthmo': 6, 'birthyr': 1950, 'behage': 50})
assert nv.validate(
{'frmdate': '2024/02/02', 'birthmo': 2, 'birthyr': 2024, 'behage': 0})

# invalid cases
assert not nv.validate({'frmdate': '2024/02/02', 'birthmo': 1, 'birthyr': 2024, 'behage': 50})
assert not nv.validate(
{'frmdate': '2024/02/02', 'birthmo': 1, 'birthyr': 2024, 'behage': 50})
assert nv.errors == {'frmdate': [
"input value behage doesn't satisfy the condition: age at frmdate >= behage"
]}
"input value behage doesn't satisfy the condition: age at frmdate >= behage"
]}


def test_compare_age_list(date_constraint, create_nacc_validator):
""" Tests compare_age, case where compare_to is a list """
Expand Down Expand Up @@ -85,15 +91,19 @@ def test_compare_age_list(date_constraint, create_nacc_validator):
nv = create_nacc_validator(schema)

# valid cases
assert nv.validate({'frmdate': '2024/02/02', 'birthmo': 6, 'birthyr': 1950, 'behage': 50, 'cogage': 40, 'perchage': 70})
assert nv.validate({'frmdate': '2024/02/02', 'birthmo': 2, 'birthyr': 2024, 'behage': 0, 'cogage': 0, 'perchage': -2})
assert nv.validate({'frmdate': '2024/02/02', 'birthmo': 6,
'birthyr': 1950, 'behage': 50, 'cogage': 40, 'perchage': 70})
assert nv.validate({'frmdate': '2024/02/02', 'birthmo': 2,
'birthyr': 2024, 'behage': 0, 'cogage': 0, 'perchage': -2})

# invalid cases
assert not nv.validate({'frmdate': '2024/02/02', 'birthmo': 1, 'birthyr': 2024, 'behage': 50, 'cogage': 0, 'perchage': 60})
assert not nv.validate({'frmdate': '2024/02/02', 'birthmo': 1,
'birthyr': 2024, 'behage': 50, 'cogage': 0, 'perchage': 60})
assert nv.errors == {'frmdate': [
"input value perchage doesn't satisfy the condition: age at frmdate >= behage, cogage, perchage, 0",
"input value behage doesn't satisfy the condition: age at frmdate >= behage, cogage, perchage, 0"
]}
"input value perchage doesn't satisfy the condition: age at frmdate >= behage, cogage, perchage, 0",
"input value behage doesn't satisfy the condition: age at frmdate >= behage, cogage, perchage, 0"
]}


def test_compare_age_invalid_field(date_constraint, create_nacc_validator):
""" Test case where invalid age to compare is provided """
Expand All @@ -118,8 +128,11 @@ def test_compare_age_invalid_field(date_constraint, create_nacc_validator):
}

nv = create_nacc_validator(schema)
assert not nv.validate({'frmdate': '2024/02/02', 'birthyr': 2024, 'behage': "dummy_str"})
assert nv.errors == {'frmdate': ["Error in comparing behage to age at frmdate (0.08761122518822724): '<=' not supported between instances of 'float' and 'str'"]}
assert not nv.validate(
{'frmdate': '2024/02/02', 'birthyr': 2024, 'behage': "dummy_str"})
assert nv.errors == {'frmdate': [
"Error in comparing behage to age at frmdate (0.08761122518822724): '<=' not supported between instances of 'float' and 'str'"]}


def test_compare_age_invalid_base(create_nacc_validator):
""" Test case where base_date is invalid """
Expand All @@ -142,5 +155,35 @@ def test_compare_age_invalid_base(create_nacc_validator):
}

nv = create_nacc_validator(schema)
assert not nv.validate({'frmdate': 'hello world', 'birthyr': 2024, 'behage': 50})
assert nv.errors == {'frmdate': ['failed to convert value hello world to a date: Unknown string format: hello world']}
assert not nv.validate(
{'frmdate': 'hello world', 'birthyr': 2024, 'behage': 50})
assert nv.errors == {'frmdate': [
'failed to convert value hello world to a date: Unknown string format: hello world']}


def test_compare_age_null_base(create_nacc_validator):
""" Test case where base_date is null """

schema = {
"frmdate": {
"type": "string",
"required": True,
"compare_age": {
"comparator": "<=",
"birth_year": "birthyr",
"compare_to": "behage",
}
},
"birthyr": {
"type": "integer"
},
"behage": {
"type": "integer"
}
}

nv = create_nacc_validator(schema)
assert not nv.validate(
nv.cast_record({'frmdate': '', 'birthyr': 2024, 'behage': 50}))
print(nv.errors)
assert nv.errors == {'frmdate': ['null value not allowed']}

0 comments on commit 3169310

Please sign in to comment.