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_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']))
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_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)