plesna/tests/test_intersept_not_valid.py

66 lines
1.6 KiB
Python
Raw Permalink Normal View History

2024-02-18 16:40:52 +00:00
import random
import pandas as pd
import pytest
from pydantic import BaseModel
from scripts.intersept_not_valid import ValidationInterseptor
2024-02-18 16:40:52 +00:00
class FakeModel(BaseModel):
name: str
age: int
def test_init_composed():
interceptor = ValidationInterseptor(FakeModel)
2024-02-18 16:40:52 +00:00
def df_generator(nrows=3):
records = [{"name": "plop", "age": random.randint(1, 50)} for _ in range(nrows)]
return pd.DataFrame.from_records(records)
df_generator_val = interceptor(df_generator)
df = df_generator_val(3)
assert len(df) == 3
assert interceptor.not_valid_rows == []
def test_init_decorator():
interceptor = ValidationInterseptor(FakeModel)
2024-02-18 16:40:52 +00:00
@interceptor
def df_generator(nrows=3):
records = [{"name": "plop", "age": random.randint(1, 50)} for _ in range(nrows)]
return pd.DataFrame.from_records(records)
df = df_generator(3)
assert len(df) == 3
assert interceptor.not_valid_rows == []
def test_intersept_not_valid():
interceptor = ValidationInterseptor(FakeModel)
2024-02-18 16:40:52 +00:00
@interceptor
def df_generator():
records = [
{"name": "plop", "age": 12},
{"name": "hop", "age": "ui"},
{"name": "pipo", "age": 12},
]
return pd.DataFrame.from_records(records)
df = df_generator()
assert len(df) == 2
assert interceptor.not_valid_rows == [
{
"name": "hop",
"age": "ui",
"ValidationInterseptorFunc": "df_generator",
"ValidationInterseptorArgs": (),
"ValidationInterseptorKwrds": {},
"ValidationInterseptorIndex": 1,
2024-02-18 16:40:52 +00:00
}
]