def test_null_w_numeric(self): """ Tests the is_match method for a numeric comparison with a null value. """ rule = FieldRule(field_name='age', operator='FloatField:<=', value='20') data = {'age': None} self.assertFalse(rule.is_match(data))
def test_is_null(self): """ Tests the is_match method for 'is null'. """ rule = FieldRule( field_name='subject', operator='EmptyField', ) data = {'subject': 'this is a critical alert'} self.assertFalse(rule.is_match(data)) data = {'subject': None} self.assertTrue(rule.is_match(data))
def test_gte(self): """ Tests the is_match method for a 'greater than or equal to' numeric comparison. """ true_rule1 = FieldRule(field_name='age', operator='FloatField:>=', value='20') true_rule2 = FieldRule(field_name='age', operator='FloatField:>=', value='21') false_rule = FieldRule(field_name='age', operator='FloatField:>=', value='22') data = {'age': '21'} self.assertTrue(true_rule1.is_match(data)) self.assertTrue(true_rule2.is_match(data)) self.assertFalse(false_rule.is_match(data))
def test_equals(self): """ Tests the is_match method for an 'equals' regex pattern. """ true_rule = FieldRule(field_name='subject', operator='CharField:^x$', value='this is a critical alert') false_rule = FieldRule(field_name='subject', operator='CharField:^x$', value='critical') data = {'subject': 'this is a critical alert'} self.assertTrue(true_rule.is_match(data)) self.assertFalse(false_rule.is_match(data))
def test_does_not_contain(self): """ Tests the is_match method for a 'does not contain' regex pattern. """ true_rule = FieldRule(field_name='subject', operator='CharField:^((?!x).)*$', value='critical') false_rule = FieldRule(field_name='subject', operator='CharField:^((?!x).)*$', value='urgent') data = {'subject': 'this is an urgent alert'} self.assertTrue(true_rule.is_match(data)) self.assertFalse(false_rule.is_match(data))
def test_lt(self): """ Tests the is_match method for a 'less than' numeric comparison. """ true_rule = FieldRule(field_name='age', operator='FloatField:<', value='22') false_rule = FieldRule(field_name='age', operator='FloatField:<', value='21') data = {'age': '21'} self.assertTrue(true_rule.is_match(data)) self.assertFalse(false_rule.is_match(data))
def test_invalid_regex(self): """ Tests the clean method when the value is not a valid regex. """ valid_rule = FieldRule(field_name='subject', operator='CharField:x$', is_regex=False, value='[CRIT-999]') invalid_rule = FieldRule(field_name='subject', operator='CharField:x$', is_regex=True, value='[CRIT-999]') try: valid_rule.clean() except ValidationError: self.fail('Rule raised ValidationError unexpectedly') with self.assertRaises(ValidationError): self.assertFalse(invalid_rule.clean())
def test_special_characters(self): """ Tests the is_match method for a string that contains special characters. """ true_rule = FieldRule(field_name='subject', operator='CharField:x$', value='.* [CRIT]') false_rule = FieldRule( field_name='subject', operator='CharField:x', value='.*[CRIT]', ) data = {'subject': '*this is a critical alert.* [CRIT]'} self.assertTrue(true_rule.is_match(data)) self.assertFalse(false_rule.is_match(data))
def test_invalid_number(self): """ Tests the clean method when for a numeric comparison with a non-numeric value. """ valid_rule = FieldRule(field_name='age', operator='FloatField:>', is_regex=False, value='20') invalid_rule = FieldRule(field_name='age', operator='FloatField:>', is_regex=False, value='[20]') try: valid_rule.clean() except ValidationError: self.fail('Rule raised ValidationError unexpectedly') with self.assertRaises(ValidationError): self.assertFalse(invalid_rule.clean())