def test_validate_okay(self): self.handler.returns.coerce = 'eggs' arg = Arg(self.param, 'spam', self.handler) arg.validate() self.assertEqual(self.stub.calls, [ ('coerce', ('spam',), {}), ('validate', ('eggs',), {}), ])
def test_coerce(self): self.handler.returns.coerce = 'eggs' arg = Arg(self.param, 'spam', self.handler) value = arg.coerce() self.assertEqual(value, 'eggs') self.assertEqual(self.stub.calls, [ ('coerce', ('spam',), {}), ])
def test_as_data_use_handler(self): self.handler.returns.as_data = 'spam' arg = Arg(self.param, 'eggs', self.handler, israw=False) data = arg.as_data() self.assertEqual(data, 'spam') self.assertEqual(self.stub.calls, [ ('validate', ('eggs',), {}), ('as_data', ('eggs',), {}), ])
def test_validate_use_coerced(self): handler = FakeHandler() other = Arg(Parameter(str, handler), 'spam', handler, israw=False) arg = Arg(Parameter(str, self.handler), other, self.handler, israw=False) arg.validate() self.assertEqual(self.stub.calls, []) self.assertEqual(handler.stub.calls, [ ('validate', ('spam',), {}), ])
def test_as_data_use_coerced(self): handler = FakeHandler() other = Arg(Parameter(str, handler), 'spam', handler, israw=False) handler.returns.as_data = 'spam' arg = Arg(Parameter(str, self.handler), other, self.handler, israw=False) data = arg.as_data(other) self.assertEqual(data, 'spam') self.assertEqual(self.stub.calls, []) self.assertEqual(handler.stub.calls, [ ('validate', ('spam',), {}), ('as_data', ('spam',), {}), ])
def test_validate_invalid(self): self.stub.set_exceptions( None, ValueError('oops'), ) self.handler.returns.coerce = 'eggs' arg = Arg(self.param, 'spam', self.handler) with self.assertRaises(ValueError): arg.validate() self.assertEqual(self.stub.calls, [ ('coerce', ('spam',), {}), ('validate', ('eggs',), {}), ])
def test_transform_datatype_simple(self): datatypes = [ REF, TYPE_REFERENCE, ANY, None, int, str, bool, {str, int}, frozenset([str, int]), [str], (str, ), Parameter(object()), DatatypeHandler(str), Arg(SimpleParameter(str), 'spam'), SimpleParameter(str), UnionParameter(Union(str, int)), ArrayParameter(Array(str)), ComplexParameter(Fields()), NOT_SET, object(), object, type, ] for expected in datatypes: transformed = [] op = (lambda dt: transformed.append(dt) or dt) with self.subTest(expected): datatype = _transform_datatype(expected, op) self.assertIs(datatype, expected) self.assertEqual(transformed, [expected])
def test_value_valid(self): arg = Arg(self.param, 'eggs', self.handler, israw=False) value = arg.value self.assertEqual(value, 'eggs') self.assertEqual(self.stub.calls, [ ('validate', ('eggs',), {}), ])
def test_raw_valid(self): self.handler.returns.coerce = 'eggs' arg = Arg(self.param, 'spam', self.handler) raw = arg.raw self.assertEqual(raw, 'spam') self.assertEqual(self.stub.calls, [ ('coerce', ('spam',), {}), ('validate', ('eggs',), {}), ])
def test_value_generated(self): self.handler.returns.coerce = 'eggs' arg = Arg(self.param, 'spam', self.handler) value = arg.value self.assertEqual(value, 'eggs') self.assertEqual(self.stub.calls, [ ('coerce', ('spam',), {}), ('validate', ('eggs',), {}), ])
def test_raw_generated(self): self.handler.returns.as_data = 'spam' arg = Arg(self.param, 'eggs', self.handler, israw=False) raw = arg.raw self.assertEqual(raw, 'spam') self.assertEqual(self.stub.calls, [ ('validate', ('eggs',), {}), ('as_data', ('eggs',), {}), ])
def test_value_invalid(self): self.stub.set_exceptions( ValueError('oops'), ) arg = Arg(self.param, 'eggs', self.handler, israw=False) with self.assertRaises(ValueError): arg.value self.assertEqual(self.stub.calls, [ ('validate', ('eggs',), {}), ])
def test_normalize_datatype(self): class Spam: @classmethod def normalize(cls): return OKAY OKAY = object() NOOP = object() param = SimpleParameter(str) tests = [ # explicitly handled (REF, TYPE_REFERENCE), (TYPE_REFERENCE, NOOP), (ANY, NOOP), (None, NOOP), (int, NOOP), (str, NOOP), (bool, NOOP), (Enum(str, ('spam', )), NOOP), (Union(str, int), NOOP), ({str, int}, Union(str, int)), (frozenset([str, int]), Union(str, int)), (Array(str), NOOP), ([str], Array(str)), ((str, ), Array(str)), (Mapping(str), NOOP), ({ str: str }, Mapping(str)), # others (Field('spam'), NOOP), (Fields(Field('spam')), NOOP), (param, NOOP), (DatatypeHandler(str), NOOP), (Arg(param, 'spam'), NOOP), (SimpleParameter(str), NOOP), (UnionParameter(Union(str)), NOOP), (ArrayParameter(Array(str)), NOOP), (ComplexParameter(Fields()), NOOP), (NOT_SET, NOOP), (object(), NOOP), (object, NOOP), (type, NOOP), (Spam, OKAY), ] for datatype, expected in tests: if expected is NOOP: expected = datatype with self.subTest(datatype): datatype = _normalize_datatype(datatype) self.assertEqual(datatype, expected)
def test_bind_with_param_type(self): class Param(Parameter): HANDLER = DatatypeHandler(ANY) match_type = (lambda self, raw: self.HANDLER) class Spam(FieldsNamespace): PARAM_TYPE = Param FIELDS = [ Field('a'), ] arg = Spam.bind({'a': 'x'}) self.assertIsInstance(arg, Arg) self.assertEqual(arg, Arg(Param(Spam.FIELDS), {'a': 'x'}))
def test_bind_matched(self): param = Parameter(str, self.handler) arg = param.bind('spam') self.assertEqual(arg, Arg(param, 'spam', self.handler)) self.assertEqual(self.stub.calls, [])