|
| 1 | +import json |
1 | 2 | import logging |
2 | 3 | import os |
3 | 4 | import random |
|
13 | 14 | from influxdb_client_3 import InfluxDBClient3, write_client_options, WriteOptions, \ |
14 | 15 | WriteType, InfluxDB3ClientQueryError |
15 | 16 | from influxdb_client_3.exceptions import InfluxDBError, InfluxDBPartialWriteError |
| 17 | +from influxdb_client_3.write_client.rest import ApiException |
16 | 18 | from tests.util import asyncio_run, lp_to_py_object |
17 | 19 |
|
18 | 20 |
|
@@ -133,52 +135,64 @@ def test_v3_error(self): |
133 | 135 | for accept_partial in [True, False]: |
134 | 136 | with self.subTest(accept_partial=accept_partial): |
135 | 137 | with InfluxDBClient3( |
136 | | - host=self.host, |
137 | | - database=self.database, |
138 | | - token=self.token, |
139 | | - write_client_options=write_client_options(write_options=WriteOptions( |
140 | | - write_type=WriteType.synchronous, |
141 | | - use_v2_api=False, |
142 | | - accept_partial=accept_partial |
143 | | - )) |
| 138 | + host=self.host, |
| 139 | + database=self.database, |
| 140 | + token=self.token, |
| 141 | + write_client_options=write_client_options(write_options=WriteOptions( |
| 142 | + write_type=WriteType.synchronous, |
| 143 | + use_v2_api=False, |
| 144 | + accept_partial=accept_partial |
| 145 | + )) |
144 | 146 | ) as client: |
145 | | - with self.assertRaises(InfluxDBPartialWriteError) as err: |
146 | | - client.write(lp) |
147 | | - |
148 | | - msg = err.exception.message |
149 | | - self.assertTrue( |
150 | | - "partial write of line protocol occurred" in msg or "parsing failed for write_lp endpoint" in msg |
151 | | - ) |
152 | | - self.assertIn(( |
153 | | - "invalid column type for column 'temp', expected iox::column_type::field::float, " |
154 | | - "got iox::column_type::field::string" |
155 | | - ), msg) |
156 | | - self.assertIn("line 2", msg) |
157 | | - self.assertIn("home,room=Sunroom", msg) |
| 147 | + if accept_partial: |
| 148 | + with self.assertRaises(InfluxDBPartialWriteError) as err: |
| 149 | + client.write(lp) |
| 150 | + |
| 151 | + self.assertEqual(1, len(err.exception.line_errors)) |
| 152 | + line_error = err.exception.line_errors[0] |
| 153 | + self.assertEqual(2, line_error.line_number) |
| 154 | + self.assertIn("invalid column type for column 'temp'", line_error.error_message) |
| 155 | + self.assertIn("home,room=Sunroom", line_error.original_line) |
| 156 | + else: |
| 157 | + with self.assertRaises(ApiException) as err: |
| 158 | + client.write(lp) |
| 159 | + |
| 160 | + self.assertEqual(400, err.exception.status) |
| 161 | + self.assertEqual("line protocol parsing error", err.exception.message) |
| 162 | + body = json.loads(err.exception.body) |
| 163 | + self.assertEqual("line protocol parsing error", body["error"]) |
| 164 | + self.assertEqual(2, body["data"]["line_number"]) |
| 165 | + self.assertIn( |
| 166 | + "invalid column type for column 'temp'", |
| 167 | + body["data"]["error_message"], |
| 168 | + ) |
158 | 169 |
|
159 | 170 | def test_v2_error(self): |
160 | 171 | lp = "\n".join([ |
161 | 172 | "home,room=Sunroom temp=96 1735545600", |
162 | 173 | "home,room=Sunroom temp=\"hi\" 1735549200", |
163 | 174 | ]) |
164 | 175 |
|
165 | | - with InfluxDBClient3( |
166 | | - host=self.host, |
167 | | - database=self.database, |
168 | | - token=self.token, |
169 | | - write_client_options=write_client_options(write_options=WriteOptions( |
170 | | - write_type=WriteType.synchronous, |
171 | | - use_v2_api=True, |
172 | | - accept_partial=False |
173 | | - )) |
174 | | - ) as client: |
175 | | - with self.assertRaises(InfluxDBError) as err: |
176 | | - client.write(lp) |
177 | | - |
178 | | - self.assertNotIsInstance(err.exception, InfluxDBPartialWriteError) |
179 | | - self.assertIsNotNone(err.exception.response) |
180 | | - self.assertEqual(400, err.exception.response.status) |
181 | | - self.assertTrue(err.exception.message) |
| 176 | + for accept_partial in [True, False]: |
| 177 | + with self.subTest(accept_partial=accept_partial): |
| 178 | + with InfluxDBClient3( |
| 179 | + host=self.host, |
| 180 | + database=self.database, |
| 181 | + token=self.token, |
| 182 | + write_client_options=write_client_options(write_options=WriteOptions( |
| 183 | + write_type=WriteType.synchronous, |
| 184 | + use_v2_api=True, |
| 185 | + accept_partial=accept_partial |
| 186 | + )) |
| 187 | + ) as client: |
| 188 | + with self.assertRaises(ApiException) as err: |
| 189 | + client.write(lp) |
| 190 | + |
| 191 | + self.assertEqual(400, err.exception.status) |
| 192 | + self.assertNotIsInstance(err.exception, InfluxDBPartialWriteError) |
| 193 | + body = json.loads(err.exception.body) |
| 194 | + self.assertEqual("invalid", body["code"]) |
| 195 | + self.assertIn("write buffer error", body["message"]) |
182 | 196 |
|
183 | 197 | def test_auth_error_token(self): |
184 | 198 | self.client = InfluxDBClient3(host=self.host, database=self.database, token='fake token') |
|
0 commit comments