|
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, from_env |
5 | 5 | from tests.util import asyncio_run |
6 | 6 | from tests.util.mocks import ConstantFlightServer, ConstantData |
7 | 7 |
|
@@ -74,6 +74,40 @@ 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 | + @patch.dict('os.environ', {'INFLUX_HOST': 'localhost', 'INFLUX_TOKEN': 'test_token', |
| 78 | + 'INFLUX_DATABASE': 'test_db', 'INFLUX_ORG': 'test_org'}) |
| 79 | + def test_from_env_all_env_vars_set(self): |
| 80 | + client = from_env() |
| 81 | + self.assertIsInstance(client, InfluxDBClient3) |
| 82 | + self.assertEqual(client._client.url, "https://localhost:443") |
| 83 | + self.assertEqual(client._database, "test_db") |
| 84 | + self.assertEqual(client._org, "test_org") |
| 85 | + self.assertEqual(client._token, "test_token") |
| 86 | + |
| 87 | + @patch.dict('os.environ', {'INFLUX_HOST': 'localhost', 'INFLUX_DATABASE': 'test_db'}) |
| 88 | + def test_from_env_partial_env_vars_set(self): |
| 89 | + client = from_env() |
| 90 | + self.assertIsInstance(client, InfluxDBClient3) |
| 91 | + self.assertEqual(client._client.url, "https://localhost:443") |
| 92 | + self.assertEqual(client._database, "test_db") |
| 93 | + self.assertEqual(client._org, "default") |
| 94 | + self.assertIsNone(client._token) |
| 95 | + |
| 96 | + @patch.dict('os.environ', {}, clear=True) |
| 97 | + def test_from_env_no_env_vars_set(self): |
| 98 | + client = from_env() |
| 99 | + self.assertIsInstance(client, InfluxDBClient3) |
| 100 | + self.assertIsNotNone(client._client.url) |
| 101 | + self.assertIsNone(client._database) |
| 102 | + self.assertIsNone(client._token) |
| 103 | + self.assertEqual(client._org, "default") |
| 104 | + |
| 105 | + def test_from_env_with_kargs(self): |
| 106 | + client = from_env( |
| 107 | + write_client_options = write_client_options(batch_size=10000), |
| 108 | + ) |
| 109 | + self.assertIsInstance(client, InfluxDBClient3) |
| 110 | + self.assertEqual(client._write_client_options['batch_size'], 10000) |
77 | 111 |
|
78 | 112 | if __name__ == '__main__': |
79 | 113 | unittest.main() |
0 commit comments