66 lines
1.6 KiB
Python
66 lines
1.6 KiB
Python
import random
|
|
|
|
import pandas as pd
|
|
import pytest
|
|
from pydantic import BaseModel
|
|
|
|
from scripts.intersept_not_valid import ValidationInterseptor
|
|
|
|
|
|
class FakeModel(BaseModel):
|
|
name: str
|
|
age: int
|
|
|
|
|
|
def test_init_composed():
|
|
interceptor = ValidationInterseptor(FakeModel)
|
|
|
|
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)
|
|
|
|
@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)
|
|
|
|
@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,
|
|
}
|
|
]
|