def test_sortdict(self): cases = [ ({ 'z': 1, 'g': 1, 'a': 1 }, OrderedDict([('a', 1), ('g', 1), ('z', 1)])), # nested items are ordered as well ({ 'z': 1, 'a': { 'z': 1, 'g': 1, 'a': 1 } }, OrderedDict([('a', OrderedDict([('a', 1), ('g', 1), ('z', 1)])), ('z', 1)])), # nested lists of dicts are ordered as well ({ 'a': [{ 'z': 1, 'g': 1, 'a': 1 }] }, OrderedDict([('a', [OrderedDict([('a', 1), ('g', 1), ('z', 1)])])])) ] for given, expected in cases: self.assertEqual(utils.json_dumps(expected), utils.json_dumps(utils.sortdict(given)))
def json_representation(data, code, headers=None): resp = utils.json_dumps(data) signature = utils.sign(resp) response = make_response(resp, code) response.headers.extend(headers or {}) response.headers.add('signature', signature) response.headers.set("Content-Type", "application/json") return response
def test_fs_outgoing_invalid(self): inc = fs_adaptor.IncomingQueue(self.ingest_dir, conf.PUBLISH) out = fs_adaptor.OutgoingQueue() valid_request = list(inc)[0] valid_response = adapt.mkresponse(conf.PUBLISHED, "dummy-published-message", request=valid_request) valid_response_json = utils.json_dumps(valid_response) out.write(valid_response_json) self.assertEqual(len(out.valids), 1) self.assertEqual(len(out.invalids), 0) self.assertEqual(len(out.errors), 0)
def test_fs_outgoing_valid(self): "valid responses can be written without errors" inc = fs_adaptor.IncomingQueue(self.ingest_dir, 'ingest') out = fs_adaptor.OutgoingQueue() valid_request = list(inc)[0] valid_response = adapt.mkresponse(conf.INGESTED, "dummy-ingested-message", request=valid_request) valid_response_json = utils.json_dumps(valid_response) out.write(valid_response_json) self.assertEqual(len(out.valids), 1) self.assertEqual(len(out.invalids), 0) self.assertEqual(len(out.errors), 0)
def test_json_dumps(self): then = datetime(year=2001, month=1, day=1, hour=23, minute=59) struct = {'now': then} expected = '{"now": "2001-01-01T23:59:00Z"}' self.assertEqual(utils.json_dumps(struct), expected)