|
1 | 1 | import unittest |
2 | 2 | from unittest.mock import patch |
3 | 3 |
|
4 | | -from influxdb_client_3 import InfluxDBClient3 |
| 4 | +from influxdb_client_3 import InfluxDBClient3, WritePrecision, DefaultWriteOptions |
5 | 5 | from tests.util import asyncio_run |
6 | 6 | from tests.util.mocks import ConstantFlightServer, ConstantData |
7 | 7 |
|
@@ -74,6 +74,63 @@ async def test_query_async(self): |
74 | 74 | assert {'data': 'sql_query', 'reference': query, 'value': -1.0} in result_list |
75 | 75 | assert {'data': 'query_type', 'reference': 'sql', 'value': -1.0} in result_list |
76 | 76 |
|
| 77 | + def test_default_client(self): |
| 78 | + expected_precision = DefaultWriteOptions.write_precision.value |
| 79 | + expected_write_type = DefaultWriteOptions.write_type.value |
| 80 | + expected_gzip_enabled = False |
| 81 | + |
| 82 | + def verify_client_write_options(c): |
| 83 | + write_options = c._write_client_options.get('write_options') |
| 84 | + self.assertEqual(write_options.write_precision, expected_precision) |
| 85 | + self.assertEqual(write_options.write_type, expected_write_type) |
| 86 | + |
| 87 | + self.assertEqual(c._write_api._write_options.write_precision, expected_precision) |
| 88 | + self.assertEqual(c._write_api._write_options.write_type, expected_write_type) |
| 89 | + |
| 90 | + env_client = InfluxDBClient3.from_env() |
| 91 | + verify_client_write_options(env_client) |
| 92 | + |
| 93 | + default_client = InfluxDBClient3() |
| 94 | + verify_client_write_options(default_client) |
| 95 | + |
| 96 | + @patch.dict('os.environ', {'INFLUX_HOST': 'localhost', 'INFLUX_TOKEN': 'test_token', |
| 97 | + 'INFLUX_DATABASE': 'test_db', 'INFLUX_ORG': 'test_org', |
| 98 | + 'INFLUX_PRECISION': WritePrecision.MS, 'INFLUX_AUTH_SCHEME': 'custom_scheme'}) |
| 99 | + def test_from_env_all_env_vars_set(self): |
| 100 | + client = InfluxDBClient3.from_env() |
| 101 | + self.assertIsInstance(client, InfluxDBClient3) |
| 102 | + self.assertEqual(client._token, "test_token") |
| 103 | + self.assertEqual(client._client.url, "https://localhost:443") |
| 104 | + self.assertEqual(client._client.auth_header_value, f"custom_scheme {client._token}") |
| 105 | + self.assertEqual(client._database, "test_db") |
| 106 | + self.assertEqual(client._org, "test_org") |
| 107 | + |
| 108 | + write_options = client._write_client_options.get("write_options") |
| 109 | + self.assertEqual(write_options.write_precision, WritePrecision.MS) |
| 110 | + |
| 111 | + client._write_api._point_settings = {} |
| 112 | + |
| 113 | + @patch.dict('os.environ', {'INFLUX_HOST': "", 'INFLUX_TOKEN': "", |
| 114 | + 'INFLUX_DATABASE': "", 'INFLUX_ORG': ""}) |
| 115 | + def test_from_env_missing_variables(self): |
| 116 | + with self.assertRaises(ValueError) as context: |
| 117 | + InfluxDBClient3.from_env() |
| 118 | + self.assertIn("Missing required environment variables", str(context.exception)) |
| 119 | + |
| 120 | + @patch.dict('os.environ', {'INFLUX_HOST': 'localhost', 'INFLUX_TOKEN': 'test_token', |
| 121 | + 'INFLUX_DATABASE': 'test_db', 'INFLUX_PRECISION': WritePrecision.MS}) |
| 122 | + def test_parse_valid_write_precision(self): |
| 123 | + client = InfluxDBClient3.from_env() |
| 124 | + self.assertIsInstance(client, InfluxDBClient3) |
| 125 | + self.assertEqual(client._write_client_options.get('write_options').write_precision, WritePrecision.MS) |
| 126 | + |
| 127 | + @patch.dict('os.environ', {'INFLUX_HOST': 'localhost', 'INFLUX_TOKEN': 'test_token', |
| 128 | + 'INFLUX_DATABASE': 'test_db', 'INFLUX_PRECISION': 'invalid_value'}) |
| 129 | + def test_parse_invalid_write_precision(self): |
| 130 | + with self.assertRaises(ValueError) as context: |
| 131 | + InfluxDBClient3.from_env() |
| 132 | + self.assertIn("Invalid precision value: invalid_value", str(context.exception)) |
| 133 | + |
77 | 134 |
|
78 | 135 | if __name__ == '__main__': |
79 | 136 | unittest.main() |
0 commit comments