Example #1
0
    def verify_min_constraint(self, colname, constraint, detect=False):
        """
        Verify whether a given column satisfies the minimum value
        constraint specified.
        """
        if not self.column_exists(colname):
            return False

        value = constraint.value
        precision = getattr(constraint, 'precision', 'fuzzy') or 'fuzzy'
        assert precision in PRECISIONS

        if self.is_null(value):  # a null minimum is not considered to be an
            return True  # active constraint, so is always satisfied

        m = self.get_min(colname)
        if self.is_null(m):  # If there are no values, no value can
            return True  # the minimum constraint

        if isinstance(value, datetime.datetime):
            m = self.to_datetime(m)

        if not self.types_compatible(m, value, colname):
            result = False
        elif precision == 'closed':
            result = m >= value
        elif precision == 'open':
            result = m > value
        else:
            result = fuzzy_greater_than(m, value, self.epsilon)

        if detect and not bool(result):
            self.detect_min_constraint(colname, value, precision, self.epsilon)
        return result
Example #2
0
 def test_fuzzy_greater_than(self):
     goods = (
         (1.0, 1.0),
         (.9900001, 1.0),
         (MILLION, MILLION),
         (999900, MILLION),
         (999899, MILLION),
         (999899.999, REAL_MILLION),
         (1e-100, 1e-100),
         (0.99000001e-100, 1e-100),
         (-1.0, -1.0),
         (-1.009999, -1.0),
         (-MILLION, -MILLION),
         (-MILLION + 10000, -MILLION),
         (-REAL_MILLION, -REAL_MILLION),
         (-REAL_MILLION + 10000.0001, -REAL_MILLION),
         (-REAL_MILLION + 10000.0, -MILLION),
         (-1e-100, -1e-100),
         (-0.99001e-100, -1e-100),
     )
     bad_goods = (
         (999900 - SMALL, MILLION),  # Should fail mathematically.
         # But MILLION + SMALL == MILLION
     )
     bads = (
         (0.9899999, 1.0),
         (MILLION - 10001, MILLION),
         (MILLION - 10000.0000000001, MILLION),
         (0.989999 - 100, 1.0e-100),
         (-1.01001, -1.0),
         (-MILLION - 10001, -MILLION),
         (-MILLION - 10000.0000000001, -MILLION),
         (-1.0100001e-100, -1.0e-100),
     )
     cvt = ConstraintVerificationTester(self, df=None)
     self.assertEqual(999900 - SMALL, 999900)
     epsilon = 0.01
     for (x, y) in goods + bad_goods:
         self.assertTrue(fuzzy_greater_than(x, y, epsilon))
     for (x, y) in bads:
         self.assertFalse(fuzzy_greater_than(x, y, epsilon))
Example #3
0
    def test_fuzzy_greater_than_zero(self):
        cvt = ConstraintVerificationTester(self, df=None)
        epsilon = 0.01
        for x in POS_REALS:
            self.assertTrue(fuzzy_greater_than(x, 0.0, epsilon))
            self.assertFalse(fuzzy_greater_than(0.0, x, epsilon))
        for x in NEG_REALS:
            self.assertFalse(fuzzy_greater_than(x, 0.0, epsilon))
            self.assertTrue(fuzzy_greater_than(0.0, x, epsilon))

        self.assertTrue(fuzzy_greater_than(0.0, 0.0, epsilon))
        self.assertTrue(fuzzy_greater_than(0.0, SMALL / 2, epsilon))  # == 0.0
        self.assertEqual(SMALL / 2, 0.0)
Example #4
0
    def verify_min_constraint(self, colname, constraint, detect=False):
        """
        Verify whether a given column satisfies the minimum value
        constraint specified.
        """
        if not self.column_exists(colname):
            return False

        value = constraint.value
        precision = getattr(constraint, 'precision', 'fuzzy') or 'fuzzy'
        assert precision in PRECISIONS

        if self.is_null(value):   # a null minimum is not considered to be an
            return True           # active constraint, so is always satisfied

        m = self.get_min(colname)
        if self.is_null(m):       # If there are no values, no value can
            return True           # the minimum constraint

        if (isinstance(value, datetime.datetime)
                or isinstance(value, datetime.date)):
            m = self.to_datetime(m)

        if not self.types_compatible(m, value):
            result = False
        elif (precision == 'closed' or isinstance(value, datetime.datetime)
                                    or isinstance(value, datetime.date)):
            result = m >= value
        elif precision == 'open':
            result = m > value
        else:
            result = fuzzy_greater_than(m, value, self.epsilon)

        if detect and not bool(result):
            self.detect_min_constraint(colname, value, precision, self.epsilon)
        return result