Skip to content

Commit

Permalink
test: add Cloud and OSS error handling test
Browse files Browse the repository at this point in the history
  • Loading branch information
alespour committed Jul 23, 2024
1 parent 429d2fd commit 81a6fb4
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion tests/test_api_client.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import unittest
from unittest import mock
from urllib3 import response

from influxdb_client_3.write_client._sync.api_client import ApiClient
from influxdb_client_3.write_client.configuration import Configuration
from influxdb_client_3.write_client.client.exceptions import InfluxDBError
from influxdb_client_3.write_client.service import WriteService
from influxdb_client_3.version import VERSION


_package = "influxdb3-python"
_sentHeaders = {}

Expand Down Expand Up @@ -69,3 +70,30 @@ def test_call_api(self, mock_post):
self.assertEqual("Bearer TEST_TOKEN", _sentHeaders["Authorization"])
self.assertIsNotNone(_sentHeaders["User-Agent"])
self.assertEqual(f"{_package}/{VERSION}", _sentHeaders["User-Agent"])

def _test_api_error(self, body):
conf = Configuration()
client = ApiClient(conf)
client.rest_client.pool_manager.request \
= mock.Mock(return_value=response.HTTPResponse(status=400,
reason='Bad Request',
body=body.encode()))
service = WriteService(client)
service.post_write("TEST_ORG", "TEST_BUCKET", "data,foo=bar val=3.14")

def test_api_error_cloud(self):
response_body = '{"message": "parsing failed for write_lp endpoint"}'
try:
self._test_api_error(response_body)
self.fail("expected InfluxDBError")
except InfluxDBError as e:
self.assertEqual('parsing failed for write_lp endpoint', e.message)

def test_api_error_oss(self):
response_body = ('{"error":"parsing failed for write_lp endpoint","data":{"error_message":"invalid field value '
'in line protocol for field \'val\' on line 1"}}')
try:
self._test_api_error(response_body)
self.fail("expected InfluxDBError")
except InfluxDBError as e:
self.assertEqual('invalid field value in line protocol for field \'val\' on line 1', e.message)

0 comments on commit 81a6fb4

Please sign in to comment.