|
44 | 44 | from setuptools.ssl_support import find_ca_bundle |
45 | 45 |
|
46 | 46 | from .http import Client, CrateJsonEncoder, _get_socket_opts, _remove_certs_for_non_https |
47 | | -from .exceptions import ConnectionError, ProgrammingError |
48 | | - |
| 47 | +from .exceptions import ConnectionError, ProgrammingError, IntegrityError |
49 | 48 |
|
50 | 49 | REQUEST = 'crate.client.http.Server.request' |
51 | 50 | CA_CERT_PATH = find_ca_bundle() |
@@ -91,6 +90,17 @@ def bad_bulk_response(): |
91 | 90 | return r |
92 | 91 |
|
93 | 92 |
|
| 93 | +def duplicate_key_exception(): |
| 94 | + r = fake_response(409, 'Conflict') |
| 95 | + r.data = json.dumps({ |
| 96 | + "error": { |
| 97 | + "code": 4091, |
| 98 | + "message": "DuplicateKeyException[A document with the same primary key exists already]" |
| 99 | + } |
| 100 | + }).encode() |
| 101 | + return r |
| 102 | + |
| 103 | + |
94 | 104 | def fail_sometimes(*args, **kwargs): |
95 | 105 | if random.randint(1, 100) % 10 == 0: |
96 | 106 | raise urllib3.exceptions.MaxRetryError(None, '/_sql', '') |
@@ -302,6 +312,18 @@ def test_uuid_serialization(self, request): |
302 | 312 | self.assertEqual(data['args'], [str(uid)]) |
303 | 313 | client.close() |
304 | 314 |
|
| 315 | + @patch(REQUEST, fake_request(duplicate_key_exception())) |
| 316 | + def test_duplicate_key_error(self): |
| 317 | + """ |
| 318 | + Verify that an `IntegrityError` is raised on duplicate key errors, |
| 319 | + instead of the more general `ProgrammingError`. |
| 320 | + """ |
| 321 | + client = Client(servers="localhost:4200") |
| 322 | + with self.assertRaises(IntegrityError) as cm: |
| 323 | + client.sql('INSERT INTO testdrive (foo) VALUES (42)') |
| 324 | + self.assertEqual(cm.exception.message, |
| 325 | + "DuplicateKeyException[A document with the same primary key exists already]") |
| 326 | + |
305 | 327 |
|
306 | 328 | @patch(REQUEST, fail_sometimes) |
307 | 329 | class ThreadSafeHttpClientTest(TestCase): |
|
0 commit comments