def testEnum(self): """parse_element_attr() converts Enum values to names. """ class TestEnum(enum.Enum): FOO = "1" BAR = "2" class TestClass: foobar: typing.Optional[TestEnum] = None # Enum must be added to ATTRIB_CONVERTERS in order to be converted. with patch.dict( "ibflex.parser.ATTRIB_CONVERTERS", { typing.Optional[TestEnum]: functools.partial(parser.convert_enum, Type=TestEnum) }): self.assertEqual( parser.parse_element_attr(TestClass, "foobar", "1"), ("foobar", TestEnum.FOO)) self.assertEqual( parser.parse_element_attr(TestClass, "foobar", "2"), ("foobar", TestEnum.BAR)) # Illegal enum values raise FlexParserError with self.assertRaises(parser.FlexParserError): parser.parse_element_attr(TestClass, "foobar", "3")
def testCurrency(self): """parse_element_attr() checks attributes named 'currency' vs ISO4217. """ class TestClass: fooCurREncY: str notcurrencies: str self.assertEqual( parser.parse_element_attr(TestClass, "notcurrencies", "FOO"), ("notcurrencies", "FOO")) with self.assertRaises(parser.FlexParserError): parser.parse_element_attr(TestClass, "fooCurREncY", "FOO")
def testSequence(self): """parse_element_attr(): Sequence is always converted to tuple.""" class TestClass: foo: str sequence: typing.Tuple[str, ...] self.assertEqual(parser.parse_element_attr(TestClass, "foo", "A,B,C"), ("foo", "A,B,C")) self.assertEqual( parser.parse_element_attr(TestClass, "sequence", "A,B,C"), ("sequence", ("A", "B", "C"))) # Sequence null data parses as empty tuple self.assertEqual(parser.parse_element_attr(TestClass, "sequence", ""), ("sequence", ()))
def testBasicType(self): """parse_element_attr(): class attribute type hint controls parsing.""" class TestClass: foo: str bar: int baz: bool datetime: datetime.datetime date: datetime.date time: datetime.time sequence: Tuple[str, ...] # Return (attr_name, type-converted value) # int self.assertEqual( parser.parse_element_attr(TestClass, "foo", "1"), ("foo", "1") ) self.assertEqual( parser.parse_element_attr(TestClass, "bar", "1"), ("bar", 1) ) # bool self.assertEqual( parser.parse_element_attr(TestClass, "foo", "Y"), ("foo", "Y") ) self.assertEqual( parser.parse_element_attr(TestClass, "baz", "Y"), ("baz", True) ) # datetime vs date self.assertEqual( parser.parse_element_attr(TestClass, "datetime", "20100411"), ("datetime", datetime.datetime(2010, 4, 11)) ) self.assertEqual( parser.parse_element_attr(TestClass, "date", "20100411"), ("date", datetime.date(2010, 4, 11)) ) # time self.assertEqual( parser.parse_element_attr(TestClass, "foo", "152559"), ("foo", "152559") ) self.assertEqual( parser.parse_element_attr(TestClass, "time", "152559"), ("time", datetime.time(15, 25, 59)) )
def testOptional(self): class TestClass: string: str optstring: typing.Optional[str] integer: int optinteger: typing.Optional[int] boolean: bool optboolean: typing.Optional[bool] decimal: decimal.Decimal optdecimal: typing.Optional[decimal.Decimal] datetime: datetime.datetime optdatetime: typing.Optional[datetime.datetime] date: datetime.date optdate: typing.Optional[datetime.date] time: datetime.time opttime: typing.Optional[datetime.time] # Strings always return None if empty, whether or not hinted Optional. self.assertEqual(parser.parse_element_attr(TestClass, "string", ""), ("string", None)) self.assertEqual(parser.parse_element_attr(TestClass, "optstring", ""), ("optstring", None)) # Other basic types return None for empty string if hinted Optional, # otherwise raise FlexParseError for input empty string. self.assertEqual( parser.parse_element_attr(TestClass, "optinteger", ""), ("optinteger", None)) with self.assertRaises(parser.FlexParserError): parser.parse_element_attr(TestClass, "integer", ""), self.assertEqual( parser.parse_element_attr(TestClass, "optboolean", ""), ("optboolean", None)) with self.assertRaises(parser.FlexParserError): parser.parse_element_attr(TestClass, "boolean", ""), self.assertEqual( parser.parse_element_attr(TestClass, "optdecimal", ""), ("optdecimal", None)) with self.assertRaises(parser.FlexParserError): parser.parse_element_attr(TestClass, "decimal", ""), self.assertEqual( parser.parse_element_attr(TestClass, "optdatetime", ""), ("optdatetime", None)) with self.assertRaises(parser.FlexParserError): parser.parse_element_attr(TestClass, "datetime", ""), self.assertEqual(parser.parse_element_attr(TestClass, "optdate", ""), ("optdate", None)) with self.assertRaises(parser.FlexParserError): parser.parse_element_attr(TestClass, "date", ""), self.assertEqual(parser.parse_element_attr(TestClass, "opttime", ""), ("opttime", None)) with self.assertRaises(parser.FlexParserError): parser.parse_element_attr(TestClass, "time", ""),