def test_reverse_transform(self): """Test the ``ColumnFormula.reverse_transform`` method. It is expected to compute the indicated column by applying the given formula. Input: - Table data with the column with incorrect values (pandas.DataFrame) Output: - Table data with the computed column (pandas.DataFrame) """ # Setup column = 'c' instance = ColumnFormula(column=column, formula=new_column) # Run table_data = pd.DataFrame({ 'a': [1, 2, 3], 'b': [4, 5, 6], 'c': [1, 1, 1] }) out = instance.reverse_transform(table_data) # Assert expected_out = pd.DataFrame({ 'a': [1, 2, 3], 'b': [4, 5, 6], 'c': [5, 7, 9] }) pd.testing.assert_frame_equal(expected_out, out)
def test_transform(self): """Test the ``ColumnFormula.transform`` method. It is expected to drop the indicated column from the table. Input: - Table data (pandas.DataFrame) Output: - Table data without the indicated column (pandas.DataFrame) """ # Setup column = 'c' instance = ColumnFormula(column=column, formula=new_column) # Run table_data = pd.DataFrame({ 'a': [1, 2, 3], 'b': [4, 5, 6], 'c': [5, 7, 9] }) out = instance.transform(table_data) # Assert expected_out = pd.DataFrame({ 'a': [1, 2, 3], 'b': [4, 5, 6], }) pd.testing.assert_frame_equal(expected_out, out)
def test_is_valid_non_valid(self): """Test the ``ColumnFormula.is_valid`` method for a non-valid data. If the data does not fulfill the formula, result is a series of ``False`` values. Input: - Table data not fulfilling the formula (pandas.DataFrame) Output: - Series of ``False`` values (pandas.Series) """ # Setup column = 'c' instance = ColumnFormula(column=column, formula=new_column) # Run table_data = pd.DataFrame({ 'a': [1, 2, 3], 'b': [4, 5, 6], 'c': [1, 2, 3] }) instance = ColumnFormula(column=column, formula=new_column) out = instance.is_valid(table_data) # Assert expected_out = pd.Series([False, False, False]) pd.testing.assert_series_equal(expected_out, out)
def test_to_dict_column_formula_returned_function(self): """Test the ``Constraint.to_dict`` when the constraint is a ColumnFormula type and is passed a function returned from another function. If the ``Constraint`` type is ColumnFormula, and the formula argument is a function returned from another function, the dictionary should contain the function as the value. Output: - Dict with the right values. """ # Run def func_creator(): def func(x): return x + 1 return func instance = ColumnFormula(column='a', formula=func_creator(), handling_strategy='transform') constraint_dict = instance.to_dict() # Assert assert constraint_dict['formula'](1) == 2
def test_to_dict_column_formula_lambda(self): """Test the ``Constraint.to_dict`` when the constraint is a ColumnFormula type and is passed a lambda. If the ``Constraint`` type is ColumnFormula, and the formula argument is a lambda, the dictionary should contain the lambda object as the value. Output: - Dict with the right values. """ # Run instance = ColumnFormula(column='a', formula=lambda x: x + 1, handling_strategy='transform') constraint_dict = instance.to_dict() # Assert assert constraint_dict['formula'](1) == 2
def test___init__(self): """Test the ``ColumnFormula.__init__`` method. It is expected to create a new Constraint instance and import the formula to use for the computation. Input: - column = 'c' - formula = new_column """ # Setup column = 'c' # Run instance = ColumnFormula(column=column, formula=new_column) # Assert assert instance._column == column assert instance._formula == new_column