|
1 | 1 | import unittest |
2 | 2 | from unittest import mock |
| 3 | +from urllib3 import response |
3 | 4 |
|
4 | 5 | from influxdb_client_3.write_client._sync.api_client import ApiClient |
5 | 6 | from influxdb_client_3.write_client.configuration import Configuration |
| 7 | +from influxdb_client_3.write_client.client.exceptions import InfluxDBError |
6 | 8 | from influxdb_client_3.write_client.service import WriteService |
7 | 9 | from influxdb_client_3.version import VERSION |
8 | 10 |
|
9 | | - |
10 | 11 | _package = "influxdb3-python" |
11 | 12 | _sentHeaders = {} |
12 | 13 |
|
@@ -69,3 +70,38 @@ def test_call_api(self, mock_post): |
69 | 70 | self.assertEqual("Bearer TEST_TOKEN", _sentHeaders["Authorization"]) |
70 | 71 | self.assertIsNotNone(_sentHeaders["User-Agent"]) |
71 | 72 | self.assertEqual(f"{_package}/{VERSION}", _sentHeaders["User-Agent"]) |
| 73 | + |
| 74 | + def _test_api_error(self, body): |
| 75 | + conf = Configuration() |
| 76 | + client = ApiClient(conf) |
| 77 | + client.rest_client.pool_manager.request \ |
| 78 | + = mock.Mock(return_value=response.HTTPResponse(status=400, |
| 79 | + reason='Bad Request', |
| 80 | + body=body.encode())) |
| 81 | + service = WriteService(client) |
| 82 | + service.post_write("TEST_ORG", "TEST_BUCKET", "data,foo=bar val=3.14") |
| 83 | + |
| 84 | + def test_api_error_cloud(self): |
| 85 | + response_body = '{"message": "parsing failed for write_lp endpoint"}' |
| 86 | + with self.assertRaises(InfluxDBError) as err: |
| 87 | + self._test_api_error(response_body) |
| 88 | + self.assertEqual('parsing failed for write_lp endpoint', err.exception.message) |
| 89 | + |
| 90 | + def test_api_error_oss_without_detail(self): |
| 91 | + response_body = '{"error": "parsing failed for write_lp endpoint"}' |
| 92 | + with self.assertRaises(InfluxDBError) as err: |
| 93 | + self._test_api_error(response_body) |
| 94 | + self.assertEqual('parsing failed for write_lp endpoint', err.exception.message) |
| 95 | + |
| 96 | + def test_api_error_oss_with_detail(self): |
| 97 | + response_body = ('{"error":"parsing failed for write_lp endpoint","data":{"error_message":"invalid field value ' |
| 98 | + 'in line protocol for field \'val\' on line 1"}}') |
| 99 | + with self.assertRaises(InfluxDBError) as err: |
| 100 | + self._test_api_error(response_body) |
| 101 | + self.assertEqual('invalid field value in line protocol for field \'val\' on line 1', err.exception.message) |
| 102 | + |
| 103 | + def test_api_error_unknown(self): |
| 104 | + response_body = '{"detail":"no info"}' |
| 105 | + with self.assertRaises(InfluxDBError) as err: |
| 106 | + self._test_api_error(response_body) |
| 107 | + self.assertEqual(response_body, err.exception.message) |
0 commit comments