def test_api_decode_from_mongo(self): field = "$section1.group01.question1" encoded = _encode_for_mongo(field) self.assertEqual( encoded, ("%(dollar)ssection1%(dot)sgroup01%(dot)squestion1" % { "dollar": base64.b64encode("$"), "dot": base64.b64encode(".") })) decoded = _decode_from_mongo(encoded) self.assertEqual(field, decoded)
def test_api_decode_from_mongo(self): field = "$section1.group01.question1" encoded = _encode_for_mongo(field) self.assertEqual(encoded, ( "%(dollar)ssection1%(dot)sgroup01%(dot)squestion1" % { "dollar": base64.b64encode( '$'.encode('utf-8')).decode('utf-8'), "dot": base64.b64encode('.'.encode('utf-8')).decode('utf-8')})) decoded = _decode_from_mongo(encoded) self.assertEqual(field, decoded)
def decode_mongo_encoded_section_names(data): """ Recursively decode mongo keys. :param data: A dictionary to decode. """ results = {} for (k, v) in iteritems(data): new_v = v if isinstance(v, dict): new_v = decode_mongo_encoded_section_names(v) elif isinstance(v, list): new_v = [decode_mongo_encoded_section_names(x) if isinstance(x, dict) else x for x in v] results[_decode_from_mongo(k)] = new_v return results
def decode_mongo_encoded_section_names(cls, data): return dict([(_decode_from_mongo(k), v) for k, v in data.iteritems()])