def test_drop(self): t = TypedDfBuilder("a").reserve("column").drop("trash").build() typ: DfTyping = t.get_typing() assert typ.columns_to_drop == {"trash"} df = t.convert(pd.DataFrame([pd.Series(dict(x="x", zz="y"))])) assert df.column_names() == ["x", "zz"] df = t.convert(pd.DataFrame([pd.Series(dict(x="x", trash="y"))])) assert df.column_names() == ["x"]
def test_require_and_reserve_col(self): t = TypedDfBuilder("a").require("column").reserve("reserved").build() typ: DfTyping = t.get_typing() assert typ.required_columns == ["column"] assert typ.reserved_columns == ["reserved"] assert typ.required_index_names == [] assert typ.reserved_index_names == [] assert typ.verifications == []
def test_condition(self): t = TypedDfBuilder("a").verify(always_ok).build() typ: DfTyping = t.get_typing() assert typ.required_columns == [] assert typ.required_index_names == [] assert typ.verifications == [always_ok] TypedDf(pd.DataFrame()) t = TypedDfBuilder("a").verify(always_fail).build() with pytest.raises(VerificationFailedError): t.convert(pd.DataFrame())
def test_strict(self): # strict columns but not index t = TypedDfBuilder("a").strict(index=False, cols=True).build() typ: DfTyping = t.get_typing() assert typ.more_indices_allowed assert not typ.more_columns_allowed t.convert(pd.DataFrame([pd.Series(dict(x="x"))]).set_index("x")) with pytest.raises(UnexpectedColumnError): t.convert(pd.DataFrame([pd.Series(dict(x="x"))])) # strict index but not columns t = TypedDfBuilder("a").strict(True, False).build() typ: DfTyping = t.get_typing() assert typ.more_columns_allowed assert not typ.more_indices_allowed t.convert(pd.DataFrame([pd.Series(dict(x="x"))])) with pytest.raises(UnexpectedIndexNameError): df = PrettyDf( pd.DataFrame([pd.Series(dict(x="x"))]).set_index("x")) assert df.index_names() == ["x"] assert df.column_names() == [] t.convert(df) # neither strict t = TypedDfBuilder("a").strict(False, False).build() t.convert(pd.DataFrame([pd.Series(dict(x="x"))]))