def test_encoder_raise_valueerror_when_intid_not_in_mapping(self): with self.login(self.regular_user): transporter = IResponseTransporter(self.task) transporter.intids_mapping = {} value = RelationValue(111) with self.assertRaises(ValueError): transporter._encode(value)
def test_encode(self): values = [ 'hello' 'hell\xc3\xb6', u'helllo', datetime(2012, 1, 1), DateTime(2012, 1, 1), RelationValue(1111), ['hello', u'hello', 'hell\xc3\xb6'], PersistentList(['hello', u'hello']), [RelationValue(1111), RelationValue(3333), RelationValue(5555)] ] task = self.providing_stub([ITask]) self.replay() transporter = IResponseTransporter(task) transporter.intids_mapping = {1111: 2222, 3333: 5555, 5555: 3333} for value in values: encoded_value = json.dumps(transporter._encode(value)) decoded_value = transporter._decode(json.loads(encoded_value)) # compare the value with the de- and encoded value if isinstance(value, list): for i in range(len(value)): self._compare( value[i], decoded_value[i], transporter.intids_mapping) else: self._compare(value, decoded_value, transporter.intids_mapping)
def test_encode(self): values = [ 'hello' 'hell\xc3\xb6', u'helllo', datetime(2012, 1, 1), DateTime(2012, 1, 1), RelationValue(1111), ['hello', u'hello', 'hell\xc3\xb6'], PersistentList(['hello', u'hello']), [RelationValue(1111), RelationValue(3333), RelationValue(5555)] ] task = self.providing_stub([ITask]) self.replay() transporter = IResponseTransporter(task) transporter.intids_mapping = {1111: 2222, 3333: 5555, 5555: 3333} for value in values: encoded_value = json.dumps(transporter._encode(value)) decoded_value = transporter._decode(json.loads(encoded_value)) # compare the value with the de- and encoded value if isinstance(value, list): for i in range(len(value)): self._compare(value[i], decoded_value[i], transporter.intids_mapping) else: self._compare(value, decoded_value, transporter.intids_mapping)
def test_unicode_encoding_decoding(self): with self.login(self.regular_user): transporter = IResponseTransporter(self.task) self.assertEquals([u'unicode', u'hello'], transporter._encode(u'hello')) self.assertEquals(u'hello', transporter._decode([u'string:utf8', u'hello']))
def test_relationvalue_encoding_decoding(self): with self.login(self.regular_user): transporter = IResponseTransporter(self.task) transporter.intids_mapping = {111: 222} value = RelationValue(111) self.assertEquals( RelationValue(222).to_id, transporter._decode(transporter._encode(value)).to_id)
def test_relationvalue_encoding_decoding(self): with self.login(self.regular_user): transporter = IResponseTransporter(self.task) transporter.intids_mapping = {111:222} value = RelationValue(111) self.assertEquals( RelationValue(222).to_id, transporter._decode(transporter._encode(value)).to_id)
def test_encoding_decoding(self): task = self.providing_stub([ITask]) # rel_value = self.mocker.replace(RelationValue) # rel_value = self.expect(rel_value('hans')).result('RELATION VALUE') # relation = self.stub() # self.expect(relation.to_id).result('123') self.replay() trans = IResponseTransporter(task) self.assertEquals(trans._decode('hans'), 'hans') self.assertEquals(trans._decode(None), None) self.assertEquals(trans._decode([111, 'hans']), [111, 'hans']) #string self.assertEquals('hans', trans._decode(trans._encode('hans'))) # unicode self.assertEquals(u'hans', trans._decode(trans._encode(u'hans'))) self.assertEquals(u'h\xe4ns', trans._decode(trans._encode(u'h\xe4ns'))) # datetime self.assertEquals( datetime(2012, 1, 1, 2, 2), trans._decode(trans._encode(datetime(2012, 1, 1, 2, 2)))) # DateTime self.assertEquals( DateTime(2012, 1, 1, 2, 2), trans._decode(trans._encode(DateTime(2012, 1, 1, 2, 2)))) # RelationValue trans.intids_mapping = {'123': '321'} value = RelationValue('123') self.assertEquals(trans._decode(trans._encode(value)).to_id, '321') #special type self.assertEquals(['special_type', 'special value'], trans._decode(['special_type', 'special value']))
class TestResponseTransporterSerialization(MockTestCase): def setUp(self): super(TestResponseTransporterSerialization, self).setUp() grok('opengever.task.transporter') task = self.providing_stub([ITask]) self.replay() self.transporter = IResponseTransporter(task) self.transporter.intids_mapping = {} def assert_serialization(self, value): encoded = json.dumps(self.transporter._encode(value)) self.assertEquals(value, self.transporter._decode(json.loads(encoded))) def test_string_encoding_decoding(self): self.assertEquals([u'string:utf8', u'hello'], self.transporter._encode('hello')) self.assertEquals('hello', self.transporter._decode([u'string:utf8', u'hello'])) def test_unicode_encoding_decoding(self): self.assertEquals([u'unicode', u'hello'], self.transporter._encode(u'hello')) self.assertEquals(u'hello', self.transporter._decode([u'string:utf8', u'hello'])) def test_handles_non_ascii_strings(self): self.assert_serialization('hell\xc3\xb6') def test_datetime_encoding_decoding(self): self.assert_serialization(datetime(2015, 11, 6, 13, 30)) def test_list_encoding_decoding(self): self.assert_serialization( ['hell\xc3\xb6', u'hello', datetime(2015, 11, 6, 13, 30)]) def test_dict_encoding_decoding(self): self.assert_serialization( {'key1': 'hell\xc3\xb6', 'key2': u'hello', 'key3': date(2015, 11, 6)}) def test_relationvalue_encoding_decoding(self): self.transporter.intids_mapping = {111:222} value = RelationValue(111) self.assertEquals( RelationValue(222).to_id, self.transporter._decode(self.transporter._encode(value)).to_id) def test_encoder_raise_valueerror_when_intid_not_in_mapping(self): value = RelationValue(111) with self.assertRaises(ValueError): self.transporter._encode(value) def test_DateTime_encoding_decoding(self): self.assert_serialization(DateTime(2015, 11, 6, 13, 30))