2024-02-18 16:40:52 +00:00
|
|
|
import random
|
|
|
|
|
|
|
|
import pandas as pd
|
|
|
|
import pytest
|
|
|
|
from pydantic import BaseModel
|
|
|
|
|
2024-02-21 07:46:11 +00:00
|
|
|
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():
|
2024-02-21 07:46:11 +00:00
|
|
|
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():
|
2024-02-21 07:46:11 +00:00
|
|
|
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():
|
2024-02-21 07:46:11 +00:00
|
|
|
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",
|
2024-02-21 07:46:11 +00:00
|
|
|
"ValidationInterseptorFunc": "df_generator",
|
|
|
|
"ValidationInterseptorArgs": (),
|
|
|
|
"ValidationInterseptorKwrds": {},
|
|
|
|
"ValidationInterseptorIndex": 1,
|
2024-02-18 16:40:52 +00:00
|
|
|
}
|
|
|
|
]
|