def test_should_coerce_provided_conditions_to_boolean_expected_values( self): spec = {'some_field': bool} self.assertEqual( coerce(provided='true', field='some_field', spec=spec), True) self.assertEqual( coerce(provided='false', field='some_field', spec=spec), False) with self.assertRaises(click.Abort): coerce(provided='?', field='some_field', spec=spec)
def extract(entry): value = entry.get(self.field, None) if value is not None: return coerce(provided=normalize(value), field=self.field, spec=spec) else: logging.error( 'No value found for field [{}]; available fields are: [{}]' .format(self.field, ', '.join(spec.keys()))) raise click.Abort()
def apply(self, entry: dict, spec: dict) -> bool: """ Applies this filter to the provided entry. :param entry: entry to filter :param spec: specification of `field->field-type` mapping :return: True, if the filter matches the entry """ value = entry.get(self.field, None) if value is not None: value = normalize(value) condition = normalize(self.condition) return self.operator.apply( value, coerce(provided=condition, field=self.field, spec=spec)) else: logging.error( 'No value found for field [{}]; available fields are: [{}]'. format(self.field, ', '.join(spec.keys()))) raise click.Abort()
def test_should_fail_to_coerce_provided_conditions_to_unknown_expected_values( self): spec = {'some_field': type({})} with self.assertRaises(click.Abort): coerce(provided='other', field='some_field', spec=spec)
def test_should_coerce_provided_conditions_to_string_expected_values(self): spec = {'some_field': str} self.assertEqual( coerce(provided='test-string', field='some_field', spec=spec), 'test-string')
def test_should_coerce_provided_conditions_to_float_expected_values(self): spec = {'some_field': float} self.assertEqual(coerce(provided='4.2', field='some_field', spec=spec), 4.2)