|
4 | 4 | import urllib.parse |
5 | 5 | import random |
6 | 6 | import weakref |
| 7 | +import uuid |
7 | 8 | import os |
8 | 9 | import one.webclient as wc |
9 | 10 | import one.params |
@@ -170,7 +171,7 @@ def setUp(self): |
170 | 171 | 'users': [TEST_DB_1['username']], |
171 | 172 | }) for _ in range(2)] |
172 | 173 |
|
173 | | - self.eids = [x['url'].split('/')[-1] for x in sessions] |
| 174 | + self.eids = [uuid.UUID(x['url'].split('/')[-1]) for x in sessions] |
174 | 175 | self.endpoint = 'sessions' |
175 | 176 | self.field_name = 'extended_qc' |
176 | 177 | # We filter by key value so we use randint to avoid race condition in concurrent tests |
@@ -253,6 +254,14 @@ def test_empty(self): |
253 | 254 | modified = self.ac.json_field_remove_key(self.endpoint, eid, self.field_name) |
254 | 255 | self.assertIsNone(modified) |
255 | 256 |
|
| 257 | + def test_uuid_serialize(self): |
| 258 | + """Check that UUID objects are serialized to JSON.""" |
| 259 | + data = {'uid': self.eids[-1], **self.data_dict} |
| 260 | + written = self.ac.json_field_write(self.endpoint, self.eids[0], self.field_name, data) |
| 261 | + self.assertIsInstance(written, dict) |
| 262 | + # Encoder should have cast uuid to str |
| 263 | + self.assertEqual(str(self.eids[-1]), written.get('uid')) |
| 264 | + |
256 | 265 | def tearDown(self): |
257 | 266 | self.ac.rest('subjects', 'delete', id=self.subj['nickname']) |
258 | 267 |
|
@@ -581,6 +590,17 @@ def _check_get_query(self, call_args, limit, offset): |
581 | 590 | expected = {'foo': ['bar'], 'offset': [str(offset)], 'limit': [str(limit)]} |
582 | 591 | self.assertDictEqual(query, expected) |
583 | 592 |
|
| 593 | + def test_json_encoder(self): |
| 594 | + """Test that the JSONEncoder subclass serializes UUID objects.""" |
| 595 | + uid = uuid.uuid4() |
| 596 | + data = {'foo': 12, 'bar': uid} |
| 597 | + # First check that the default encoder raises; |
| 598 | + # python could add support for UUID objects in the future |
| 599 | + self.assertRaises(TypeError, json.dumps, data) |
| 600 | + serialized = json.dumps(data, cls=wc._JSONEncoder) |
| 601 | + expected = '{"foo": 12, "bar": "' + str(uid) + '"}' |
| 602 | + self.assertEqual(expected, serialized) |
| 603 | + |
584 | 604 |
|
585 | 605 | if __name__ == '__main__': |
586 | 606 | unittest.main(exit=False, verbosity=2) |
0 commit comments