Skip to content

Commit 2d4b1f8

Browse files
committed
fix: boolean parsing + coverage
1 parent a701ab8 commit 2d4b1f8

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

influxdb_client_3/__init__.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,8 @@ def _parse_write_no_sync(write_no_sync):
159159
:type write_no_sync: Any
160160
:return: The validated write no sync value as an boolean.
161161
:rtype: bool
162-
:raises ValueError: If the provided value is not a boolean.
163162
"""
164-
try:
165-
write_no_sync = bool(write_no_sync)
166-
except (TypeError, ValueError):
167-
raise ValueError(f"Invalid write no sync value: {write_no_sync}. Must be boolean.")
168-
return write_no_sync
163+
return write_no_sync.strip().lower() in ['true', '1', 't', 'y', 'yes']
169164

170165

171166
class InfluxDBClient3:

tests/test_influxdb_client_3.py

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,26 +152,65 @@ def test_from_env_missing_variables(self):
152152
self.assertIn("Missing required environment variables", str(context.exception))
153153

154154
@patch.dict('os.environ', {'INFLUX_HOST': 'localhost', 'INFLUX_TOKEN': 'test_token',
155-
'INFLUX_DATABASE': 'test_db', 'INFLUX_PRECISION': WritePrecision.MS})
155+
'INFLUX_DATABASE': 'test_db', 'INFLUX_PRECISION': WritePrecision.S})
156156
def test_parse_valid_write_precision(self):
157157
client = InfluxDBClient3.from_env()
158158
self.assertIsInstance(client, InfluxDBClient3)
159-
self.assertEqual(client._write_client_options.get('write_options').write_precision, WritePrecision.MS)
159+
self.assertEqual(client._write_client_options.get('write_options').write_precision, WritePrecision.S)
160160

161161
@patch.dict('os.environ', {'INFLUX_HOST': 'localhost', 'INFLUX_TOKEN': 'test_token',
162162
'INFLUX_DATABASE': 'test_db', 'INFLUX_PRECISION': "microsecond"})
163-
def test_parse_valid_long_write_precision(self):
163+
def test_parse_valid_long_write_precision_us(self):
164164
client = InfluxDBClient3.from_env()
165165
self.assertIsInstance(client, InfluxDBClient3)
166166
self.assertEqual(client._write_client_options.get('write_options').write_precision, WritePrecision.US)
167167

168+
@patch.dict('os.environ', {'INFLUX_HOST': 'localhost', 'INFLUX_TOKEN': 'test_token',
169+
'INFLUX_DATABASE': 'test_db', 'INFLUX_PRECISION': "nanosecond"})
170+
def test_parse_valid_long_write_precision_ns(self):
171+
client = InfluxDBClient3.from_env()
172+
self.assertIsInstance(client, InfluxDBClient3)
173+
self.assertEqual(client._write_client_options.get('write_options').write_precision, WritePrecision.NS)
174+
168175
@patch.dict('os.environ', {'INFLUX_HOST': 'localhost', 'INFLUX_TOKEN': 'test_token',
169176
'INFLUX_DATABASE': 'test_db', 'INFLUX_PRECISION': 'invalid_value'})
170177
def test_parse_invalid_write_precision(self):
171178
with self.assertRaises(ValueError) as context:
172179
InfluxDBClient3.from_env()
173180
self.assertIn("Invalid precision value: invalid_value", str(context.exception))
174181

182+
@patch.dict('os.environ', {'INFLUX_HOST': 'localhost', 'INFLUX_TOKEN': 'test_token',
183+
'INFLUX_DATABASE': 'test_db', 'INFLUX_WRITE_NO_SYNC': 'true'})
184+
def test_parse_write_no_sync_true(self):
185+
client = InfluxDBClient3.from_env()
186+
self.assertIsInstance(client, InfluxDBClient3)
187+
write_options = client._write_client_options.get("write_options")
188+
self.assertEqual(write_options.no_sync, True)
189+
190+
@patch.dict('os.environ', {'INFLUX_HOST': 'localhost', 'INFLUX_TOKEN': 'test_token',
191+
'INFLUX_DATABASE': 'test_db', 'INFLUX_WRITE_NO_SYNC': 'TrUe'})
192+
def test_parse_write_no_sync_true_mixed_chars(self):
193+
client = InfluxDBClient3.from_env()
194+
self.assertIsInstance(client, InfluxDBClient3)
195+
write_options = client._write_client_options.get("write_options")
196+
self.assertEqual(write_options.no_sync, True)
197+
198+
@patch.dict('os.environ', {'INFLUX_HOST': 'localhost', 'INFLUX_TOKEN': 'test_token',
199+
'INFLUX_DATABASE': 'test_db', 'INFLUX_WRITE_NO_SYNC': 'false'})
200+
def test_parse_write_no_sync_false(self):
201+
client = InfluxDBClient3.from_env()
202+
self.assertIsInstance(client, InfluxDBClient3)
203+
write_options = client._write_client_options.get("write_options")
204+
self.assertEqual(write_options.no_sync, False)
205+
206+
@patch.dict('os.environ', {'INFLUX_HOST': 'localhost', 'INFLUX_TOKEN': 'test_token',
207+
'INFLUX_DATABASE': 'test_db', 'INFLUX_WRITE_NO_SYNC': 'anything-else'})
208+
def test_parse_write_no_sync_anything_else_is_false(self):
209+
client = InfluxDBClient3.from_env()
210+
self.assertIsInstance(client, InfluxDBClient3)
211+
write_options = client._write_client_options.get("write_options")
212+
self.assertEqual(write_options.no_sync, False)
213+
175214
def test_query_with_arrow_error(self):
176215
f = ErrorFlightServer()
177216
with InfluxDBClient3(f"http://localhost:{f.port}", "my_org", "my_db", "my_token") as c:

0 commit comments

Comments
 (0)