def verify_max_constraint(self, colname, constraint, detect=False): """ Verify whether a given column satisfies the maximum 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 maximum is not considered to be an return True # active constraint, so is always satisfied M = self.get_max(colname) if self.is_null(M): # If there are no values, no value can return True # the maximum 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_less_than(M, value, self.epsilon) if detect and not bool(result): self.detect_max_constraint(colname, value, precision, self.epsilon) return result
def test_fuzzy_less_than(self): goods = ( (1.0, 1.0), (1.00999, 1.0), (MILLION, MILLION), (MILLION + 9999, MILLION), (MILLION + 10000, MILLION), (REAL_MILLION, REAL_MILLION), (MILLION + 9999.0, REAL_MILLION), (MILLION + 10000.0, MILLION), (1e-100, 1e-100), (1.00999e-100, 1e-100), (-1.0, -1.0), (-.990001, -1.0), (-MILLION, -MILLION), (-MILLION + 9999, -MILLION), (-MILLION + 10000, -MILLION), (-REAL_MILLION, -REAL_MILLION), (-REAL_MILLION + 9999.0, -REAL_MILLION), (-REAL_MILLION + 10000.0, -MILLION), (-1e-100, -1e-100), (-0.999999e-100, -1e-100), ) bad_goods = ( (MILLION + 10000 + SMALL, MILLION), # Should fail mathematically. # But rounding ) bads = ( (1.01000001, 1.0), (MILLION + 10001, MILLION), (MILLION + 10000.0000000001, MILLION), (1.010001e-100, 1.0e-100), (-0.989999, -1.0), (-MILLION + 100001, -MILLION), (-MILLION + 10000.0000000001, -MILLION), (-0.98999e-100, -1.0e-100), ) cvt = ConstraintVerificationTester(self, df=None) self.assertEqual(MILLION + 10000 + SMALL, MILLION + 10000) epsilon = 0.01 for (x, y) in goods + bad_goods: self.assertTrue(fuzzy_less_than(x, y, epsilon)) for (x, y) in bads: self.assertFalse(fuzzy_less_than(x, y, epsilon))
def test_fuzzy_less_than_zero(self): verifier = pdc.PandasConstraintVerifier(df=None) epsilon = 0.01 for x in NEG_REALS: self.assertTrue(fuzzy_less_than(x, 0.0, epsilon)) self.assertFalse(fuzzy_less_than(0.0, x, epsilon)) for x in POS_REALS: self.assertFalse(fuzzy_less_than(x, 0.0, epsilon)) self.assertTrue(fuzzy_less_than(0.0, x, epsilon)) self.assertTrue(fuzzy_less_than(0.0, 0.0, epsilon)) self.assertTrue(fuzzy_less_than(0.0, SMALL / 2, epsilon)) # == 0.0 self.assertEqual(SMALL / 2, 0.0)
def verify_max_constraint(self, colname, constraint, detect=False): """ Verify whether a given column satisfies the maximum 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 maximum is not considered to be an return True # active constraint, so is always satisfied M = self.get_max(colname) if self.is_null(M): # If there are no values, no value can return True # the maximum 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_less_than(M, value, self.epsilon) if detect and not bool(result): self.detect_max_constraint(colname, value, precision, self.epsilon) return result