def test_add_converter(self): converter_map = ConverterMap(str) converter_map.add_converter(StringConverter()) converter_map.add_converter(ConflictingStringConverter()) with pytest.raises(Exception): converter_map.add_converter(StringConverter()) converter_map.add_converter(StringConverter(), replace_existing=True)
def test_add_converter(self): converter_list = PredicateConverterList(str) converter_list.add_converter(StringConverter()) converter_list.add_converter( StringConverter(), wire_schema_predicate=lambda wire_schema: True) converter_list.add_converter( StringConverter(), data_type_predicate=lambda data_type: True)
def testAddConverter(self): list = PredicateConverterList(str) list.addConverter(StringConverter()) list.addConverter(StringConverter(), wireSchemaPredicate=lambda wireSchema: True) list.addConverter(StringConverter(), dataTypePredicate=lambda dataType: True)
def testAddConverter(self): map = UnambiguousConverterMap(str) map.addConverter(StringConverter()) self.assertRaises(Exception, map.addConverter, ConflictingStringConverter()) self.assertRaises(Exception, map.addConverter, ConflictingStringConverter(), True) map.addConverter(StringConverter(), replaceExisting=True)
def test_get_converter(self): v1 = StringConverter() v2 = StringConverter() always_true = PredicateConverterList(str) always_true.add_converter( v1, wire_schema_predicate=lambda wire_schema: True, data_type_predicate=lambda data_type: True) assert always_true.get_converter_for_wire_schema("") is v1 assert always_true.get_converter_for_wire_schema("bla") is v1 regex = PredicateConverterList(str) regex.add_converter(v1, wire_schema_predicate=lambda wire_schema: re.match( ".*foo.*", wire_schema), data_type_predicate=lambda data_type: re.match( ".*foo.*", data_type)) with pytest.raises(KeyError): regex.get_converter_for_wire_schema("") with pytest.raises(KeyError): regex.get_converter_for_wire_schema("bla") assert regex.get_converter_for_wire_schema("foo") is v1 assert regex.get_converter_for_wire_schema("foobar") is v1 with pytest.raises(KeyError): regex.get_converter_for_data_type("") with pytest.raises(KeyError): regex.get_converter_for_data_type("bla") assert regex.get_converter_for_data_type("foo") is v1 assert regex.get_converter_for_data_type("foobar") is v1 mixed = PredicateConverterList(str) mixed.add_converter(v1, wire_schema_predicate=lambda wire_schema: re.match( ".*foo.*", wire_schema), data_type_predicate=lambda data_type: re.match( ".*foo.*", data_type)) mixed.add_converter(v2, wire_schema_predicate=lambda wire_schema: True, data_type_predicate=lambda data_type: True) assert mixed.get_converter_for_wire_schema("") is v2 assert mixed.get_converter_for_wire_schema("bla") is v2 assert mixed.get_converter_for_wire_schema("foo") is v1 assert mixed.get_converter_for_wire_schema("foobar") is v1 assert mixed.get_converter_for_data_type("") is v2 assert mixed.get_converter_for_data_type("bla") is v2 assert mixed.get_converter_for_data_type("foo") is v1 assert mixed.get_converter_for_data_type("foobar") is v1
def testCharsetErrors(self): asciiConverter = StringConverter(wireSchema="ascii-string", encoding="ascii") self.assertRaises(UnicodeEncodeError, asciiConverter.serialize, u"test" + unichr(266)) self.assertRaises(UnicodeDecodeError, asciiConverter.deserialize, bytearray(range(133)), 'ascii-string')
def testRoundtripAscii(self): converter = StringConverter(wireSchema="ascii-string", encoding="ascii", dataType=str) orig = "foooo" self.assertEquals(orig, converter.deserialize(*converter.serialize(orig)))
def testRoundtripUtf8(self): converter = StringConverter() orig = u"asd" + unichr(270) + unichr(40928) self.assertEquals(orig, converter.deserialize(*converter.serialize(orig))) orig = "i am a normal string" self.assertEquals(orig, converter.deserialize(*converter.serialize(orig)))
def testGetConverter(self): v1 = StringConverter() v2 = StringConverter() alwaysTrue = PredicateConverterList(str) alwaysTrue.addConverter(v1, wireSchemaPredicate=lambda wireSchema: True, dataTypePredicate=lambda dataType: True) self.assertIs(alwaysTrue.getConverterForWireSchema(""), v1) self.assertIs(alwaysTrue.getConverterForWireSchema("bla"), v1) regex = PredicateConverterList(str) regex.addConverter( v1, wireSchemaPredicate=lambda wireSchema: re.match( ".*foo.*", wireSchema), dataTypePredicate=lambda dataType: re.match(".*foo.*", dataType)) self.assertRaises(KeyError, regex.getConverterForWireSchema, "") self.assertRaises(KeyError, regex.getConverterForWireSchema, "bla") self.assertIs(regex.getConverterForWireSchema("foo"), v1) self.assertIs(regex.getConverterForWireSchema("foobar"), v1) self.assertRaises(KeyError, regex.getConverterForDataType, "") self.assertRaises(KeyError, regex.getConverterForDataType, "bla") self.assertIs(regex.getConverterForDataType("foo"), v1) self.assertIs(regex.getConverterForDataType("foobar"), v1) mixed = PredicateConverterList(str) mixed.addConverter( v1, wireSchemaPredicate=lambda wireSchema: re.match( ".*foo.*", wireSchema), dataTypePredicate=lambda dataType: re.match(".*foo.*", dataType)) mixed.addConverter(v2, wireSchemaPredicate=lambda wireSchema: True, dataTypePredicate=lambda dataType: True) self.assertIs(mixed.getConverterForWireSchema(""), v2) self.assertIs(mixed.getConverterForWireSchema("bla"), v2) self.assertIs(mixed.getConverterForWireSchema("foo"), v1) self.assertIs(mixed.getConverterForWireSchema("foobar"), v1) self.assertIs(mixed.getConverterForDataType(""), v2) self.assertIs(mixed.getConverterForDataType("bla"), v2) self.assertIs(mixed.getConverterForDataType("foo"), v1) self.assertIs(mixed.getConverterForDataType("foobar"), v1)
def test_charset_errors(self): ascii_converter = StringConverter(wire_schema="ascii-string", encoding="ascii") with pytest.raises(UnicodeEncodeError): ascii_converter.serialize("test" + chr(266)) with pytest.raises(UnicodeDecodeError): ascii_converter.deserialize(bytes(list(range(133))), 'ascii-string')
def test_roundtrip_ascii(self): converter = StringConverter(wire_schema="ascii-string", encoding="ascii") orig = "foooo" assert converter.deserialize(*converter.serialize(orig)) == orig
def test_roundtrip_utf8(self, data): converter = StringConverter() assert converter.deserialize(*converter.serialize(data)) == data