def get(self, obj_type, parent_class, name_in_parent) -> Parser: if obj_type in BASIC_TYPES: return BasicParserWithCast(obj_type) if obj_type in EXTRA_TYPES: return BasicParser() if obj_type in DATE_TYPES: return DateParser(parent_class, obj_type) if obj_type is Decimal: return DecimalParser() if type(obj_type) is str or type(obj_type) is ForwardRef: return UnresolvedParser(TypeProvider(parent_class, obj_type), self.model_provider) if obj_type is list: type_info = get_cls_attr(parent_class, PYCKSON_TYPEINFO, dict()) if name_in_parent in type_info: sub_type = type_info[name_in_parent] return ListParser( self.get(sub_type, parent_class, name_in_parent)) else: raise TypeError( 'list parameter {} in class {} has no subType'.format( name_in_parent, parent_class.__name__)) if obj_type is set: type_info = get_cls_attr(parent_class, PYCKSON_TYPEINFO, dict()) if name_in_parent in type_info: sub_type = type_info[name_in_parent] return SetParser( self.get(sub_type, parent_class, name_in_parent)) else: raise TypeError( 'set parameter {} in class {} has no subType'.format( name_in_parent, parent_class.__name__)) if is_list_annotation(obj_type): return ListParser( self.get(obj_type.__args__[0], parent_class, name_in_parent)) if is_set_annotation(obj_type): return SetParser( self.get(obj_type.__args__[0], parent_class, name_in_parent)) if is_enum_annotation(obj_type): options = get_cls_attr(obj_type, PYCKSON_ENUM_OPTIONS, {}) if options.get(ENUM_CASE_INSENSITIVE, False): return CaseInsensitiveEnumParser(obj_type) else: return DefaultEnumParser(obj_type) if is_basic_dict_annotation(obj_type): return BasicDictParser() if is_typing_dict_annotation(obj_type): if obj_type.__args__[0] != str: raise TypeError( 'typing.Dict key can only be str in class {}'.format( parent_class)) return TypingDictParser( self.get(obj_type.__args__[1], parent_class, name_in_parent)) if has_cls_attr(obj_type, PYCKSON_PARSER): return CustomDeferredParser(obj_type) return ClassParser(obj_type, self.model_provider)
class DefaultEnumParserTest(TestCase): def setUp(self): self.parser = DefaultEnumParser(MyEnum) def test_should_parse_value_in_enum(self): self.assertEqual(self.parser.parse('a'), MyEnum.a) self.assertEqual(self.parser.parse('A'), MyEnum.A) self.assertEqual(self.parser.parse('b'), MyEnum.b) def test_should_not_parse_uppercase_not_in_enum(self): with self.assertRaises(KeyError): self.parser.parse('B') def test_should_not_parse_value_not_in_enum(self): with self.assertRaises(KeyError): self.parser.parse('c')
class DefaultEnumParserTest(TestCase): def setUp(self): self.parser = DefaultEnumParser(MyEnum) def test_should_parse_value_in_enum(self): self.assertEqual(self.parser.parse("a"), MyEnum.a) self.assertEqual(self.parser.parse("A"), MyEnum.A) self.assertEqual(self.parser.parse("b"), MyEnum.b) def test_should_not_parse_uppercase_not_in_enum(self): with self.assertRaises(KeyError): self.parser.parse("B") def test_should_not_parse_value_not_in_enum(self): with self.assertRaises(KeyError): self.parser.parse("c")
def setUp(self): self.parser = DefaultEnumParser(MyEnum)