Example #1
0
    def test__validate_data_meets_constraints_invalid_input(self):
        """Test the ``_validate_data_meets_constraint`` method.

        Expect that the method raises an error when the constraint columns
        are in the given data and the ``is_valid`` returns False for any row.

        Input:
        - Table data contains an invalid row
        Output:
        - None
        Side Effects:
        - A ``ConstraintsNotMetError`` is thrown
        """
        # Setup
        data = pd.DataFrame(
            {
                'a': [0, 1, 2, 3, 4, 5, 6, 7],
                'b': [3, 4, 5, 6, 7, 8, 9, 10]
            },
            index=[0, 1, 2, 3, 4, 5, 6, 7])
        constraint = Constraint()
        constraint.constraint_columns = ['a', 'b']
        is_valid_result = pd.Series(
            [True, False, True, False, False, False, False, False])
        constraint.is_valid = Mock(return_value=is_valid_result)

        # Run / Assert
        error_message = re.escape(
            "Data is not valid for the 'Constraint' constraint:\n   "
            'a  b\n1  1  4\n3  3  6\n4  4  7\n5  5  8\n6  6  9'
            '\n+1 more')
        with pytest.raises(ConstraintsNotMetError, match=error_message):
            constraint._validate_data_meets_constraint(data)
Example #2
0
    def test_is_valid(self):
        """Test the ``Constraint.is_valid` method. This should be overwritten by all the
        subclasses that have a way to decide which rows are valid and which are not.

        The ``Constraint.is_valid`` method is expected to:
        - Say whether the given table rows are valid.

        Input:
        - Table data (pandas.DataFrame)
        Output:
        - Series of ``True`` values (pandas.Series)
        """
        # Setup
        table_data = pd.DataFrame({'a': [1, 2, 3]})

        # Run
        instance = Constraint(handling_strategy='transform')
        out = instance.is_valid(table_data)

        # Assert
        expected_out = pd.Series([True, True, True])
        pd.testing.assert_series_equal(expected_out, out)
Example #3
0
    def test__validate_data_meets_constraints_missing_cols(self):
        """Test the ``_validate_data_meets_constraint`` method.

        Expect that the method doesn't do anything when the columns are not in the given data.

        Input:
        - Table data that is missing a constraint column
        Output:
        - None
        Side Effects:
        - No error
        """
        # Setup
        data = pd.DataFrame({'a': [0, 1, 2], 'b': [3, 4, 5]}, index=[0, 1, 2])
        constraint = Constraint()
        constraint.constraint_columns = ['a', 'b', 'c']
        constraint.is_valid = Mock()

        # Run
        constraint._validate_data_meets_constraint(data)

        # Assert
        assert not constraint.is_valid.called
Example #4
0
    def test__validate_data_meets_constraints(self):
        """Test the ``_validate_data_meets_constraint`` method.

        Expect that the method calls ``is_valid`` when the constraint columns
        are in the given data.

        Input:
        - Table data
        Output:
        - None
        Side Effects:
        - No error
        """
        # Setup
        data = pd.DataFrame({'a': [0, 1, 2], 'b': [3, 4, 5]}, index=[0, 1, 2])
        constraint = Constraint()
        constraint.constraint_columns = ['a', 'b']
        constraint.is_valid = Mock()

        # Run
        constraint._validate_data_meets_constraint(data)

        # Assert
        constraint.is_valid.assert_called_once_with(data)