Skip to content

Commit

Permalink
added 1580 for 24_25
Browse files Browse the repository at this point in the history
  • Loading branch information
WillLP-code committed Nov 12, 2024
1 parent c8939b5 commit 062f353
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions cin_validator/rules/cin2024_25/rule_1580.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from typing import Mapping

import pandas as pd

from cin_validator.rule_engine import (
CINTable,
IssueLocator,
RuleContext,
rule_definition,
)
from cin_validator.test_engine import run_rule

ChildIdentifiers = CINTable.ChildIdentifiers
LAchildID = ChildIdentifiers.LAchildID


@rule_definition(
code="1580",
module=CINTable.ChildIdentifiers,
message="LA Child ID must not contain any non-alphanumeric characters",
affected_fields=[LAchildID],
)
def validate(
data_container: Mapping[CINTable, pd.DataFrame], rule_context: RuleContext
):
df = data_container[ChildIdentifiers].copy()
non_alphanumeric = df[df["LAchildID"].str.isalnum() == False]

failing_indices = non_alphanumeric.index

rule_context.push_issue(
table=ChildIdentifiers, field=LAchildID, row=failing_indices
)


def test_validate():
# Create some sample data such that some values pass the validation and some fail.
# Sidenote: a typical Header table will only have one row.
sample_ci = pd.DataFrame(
{
"LAchildID": [
"XXXXXXXXXXXXXXXXXXXX",
"XXXXXXXXXXXXXXXXXXX",
"XXXXXXXXXXXXXXXXXXXX;", # non-alphanumeric characters, fail
]
}
)

result = run_rule(validate, {ChildIdentifiers: sample_ci})

issues = list(result.issues)
# Intended fail points in data.
assert len(issues) == 1
# Intended failures of test data by index.
assert issues == [
IssueLocator(CINTable.ChildIdentifiers, LAchildID, 2),
]

# Checks rule code and message are correct.
assert result.definition.code == "1580"
assert (
result.definition.message
== "LA Child ID must not contain any non-alphanumeric characters"
)

0 comments on commit 062f353

Please sign in to comment.