|
8 | 8 | import time
|
9 | 9 | from multiprocessing.pool import ApplyResult
|
10 | 10 |
|
11 |
| -from influxdb_client import Point, WritePrecision |
| 11 | +from influxdb_client import Point, WritePrecision, InfluxDBClient |
12 | 12 | from influxdb_client.client.write_api import SYNCHRONOUS, ASYNCHRONOUS, PointSettings
|
13 | 13 | from influxdb_client.rest import ApiException
|
14 | 14 | from tests.base_test import BaseTest
|
@@ -77,7 +77,6 @@ def test_write_records_list(self):
|
77 | 77 | self.write_client.write(bucket.name, self.org, record_list)
|
78 | 78 |
|
79 | 79 | query = 'from(bucket:"' + bucket.name + '") |> range(start: 1970-01-01T00:00:00.000000001Z)'
|
80 |
| - print(query) |
81 | 80 |
|
82 | 81 | flux_result = self.client.query_api().query(query)
|
83 | 82 |
|
@@ -109,7 +108,6 @@ def test_write_points_unicode(self):
|
109 | 108 | p.field(field_name, utf8_val)
|
110 | 109 | p.tag(tag, tag_value)
|
111 | 110 | record_list = [p]
|
112 |
| - print(record_list) |
113 | 111 |
|
114 | 112 | self.write_client.write(bucket.name, self.org, record_list)
|
115 | 113 |
|
@@ -147,7 +145,6 @@ def test_write_using_default_tags(self):
|
147 | 145 | p2.time(2)
|
148 | 146 |
|
149 | 147 | record_list = [p, p2]
|
150 |
| - print(record_list) |
151 | 148 |
|
152 | 149 | self.write_client.write(bucket.name, self.org, record_list)
|
153 | 150 |
|
@@ -304,7 +301,6 @@ def test_write_dictionaries(self):
|
304 | 301 | time.sleep(1)
|
305 | 302 |
|
306 | 303 | query = 'from(bucket:"' + bucket.name + '") |> range(start: 1970-01-01T00:00:00.000000001Z)'
|
307 |
| - print(query) |
308 | 304 |
|
309 | 305 | flux_result = self.client.query_api().query(query)
|
310 | 306 |
|
@@ -344,7 +340,6 @@ def test_use_default_tags_with_dictionaries(self):
|
344 | 340 | time.sleep(1)
|
345 | 341 |
|
346 | 342 | query = 'from(bucket:"' + bucket.name + '") |> range(start: 1970-01-01T00:00:00.000000001Z)'
|
347 |
| - print(query) |
348 | 343 |
|
349 | 344 | flux_result = self.client.query_api().query(query)
|
350 | 345 |
|
@@ -379,7 +374,6 @@ def test_write_bytes(self):
|
379 | 374 | time.sleep(1)
|
380 | 375 |
|
381 | 376 | query = 'from(bucket:"' + bucket.name + '") |> range(start: 1970-01-01T00:00:00.000000001Z)'
|
382 |
| - print(query) |
383 | 377 |
|
384 | 378 | flux_result = self.client.query_api().query(query)
|
385 | 379 |
|
@@ -444,5 +438,91 @@ def test_point_settings_with_add(self):
|
444 | 438 | self.assertEqual("LA", default_tags.get("data_center"))
|
445 | 439 |
|
446 | 440 |
|
| 441 | +class DefaultTagsConfiguration(BaseTest): |
| 442 | + |
| 443 | + def setUp(self) -> None: |
| 444 | + super().setUp() |
| 445 | + |
| 446 | + os.environ['data_center'] = "LA" |
| 447 | + |
| 448 | + self.id_tag = "132-987-655" |
| 449 | + self.customer_tag = "California Miner" |
| 450 | + self.data_center_key = "data_center" |
| 451 | + |
| 452 | + os.environ['INFLUXDB_V2_TOKEN'] = "my-token" |
| 453 | + os.environ['INFLUXDB_V2_TIMEOUT'] = "6000" |
| 454 | + os.environ['INFLUXDB_V2_ORG'] = "my-org" |
| 455 | + |
| 456 | + os.environ['INFLUXDB_V2_TAG_ID'] = self.id_tag |
| 457 | + os.environ['INFLUXDB_V2_TAG_CUSTOMER'] = self.customer_tag |
| 458 | + os.environ['INFLUXDB_V2_TAG_DATA_CENTER'] = "${env.data_center}" |
| 459 | + |
| 460 | + def tearDown(self) -> None: |
| 461 | + self.write_client.__del__() |
| 462 | + super().tearDown() |
| 463 | + |
| 464 | + def test_connection_option_from_conf_file(self): |
| 465 | + self.client.close() |
| 466 | + self.client = InfluxDBClient.from_config_file(os.getcwd() + "/tests/config.ini", self.debug) |
| 467 | + |
| 468 | + self._check_connection_settings() |
| 469 | + |
| 470 | + def test_connection_option_from_env(self): |
| 471 | + self.client.close() |
| 472 | + self.client = InfluxDBClient.from_env_properties(self.debug) |
| 473 | + |
| 474 | + self._check_connection_settings() |
| 475 | + |
| 476 | + def _check_connection_settings(self): |
| 477 | + self.write_client = self.client.write_api(write_options=SYNCHRONOUS) |
| 478 | + |
| 479 | + self.assertEqual(os.getenv("INFLUXDB_V2_URL"), self.client.url) |
| 480 | + self.assertEqual("my-org", self.client.org) |
| 481 | + self.assertEqual("my-token", self.client.token) |
| 482 | + self.assertEqual(6000, self.client.timeout) |
| 483 | + |
| 484 | + def test_default_tags_from_conf_file(self): |
| 485 | + self.client.close() |
| 486 | + self.client = InfluxDBClient.from_config_file(os.getcwd() + "/tests/config.ini", self.debug) |
| 487 | + |
| 488 | + self._write_point() |
| 489 | + |
| 490 | + def test_default_tags_from_env(self): |
| 491 | + self.client.close() |
| 492 | + self.client = InfluxDBClient.from_env_properties(self.debug) |
| 493 | + |
| 494 | + self._write_point() |
| 495 | + |
| 496 | + def _write_point(self): |
| 497 | + self.write_client = self.client.write_api(write_options=SYNCHRONOUS) |
| 498 | + |
| 499 | + bucket = self.create_test_bucket() |
| 500 | + |
| 501 | + measurement = "h2o_feet" |
| 502 | + field_name = "water_level" |
| 503 | + val = "1.0" |
| 504 | + tag = "location" |
| 505 | + tag_value = "creek level" |
| 506 | + |
| 507 | + p = Point(measurement) |
| 508 | + p.field(field_name, val) |
| 509 | + p.tag(tag, tag_value) |
| 510 | + |
| 511 | + record_list = [p] |
| 512 | + |
| 513 | + self.write_client.write(bucket.name, self.org, record_list) |
| 514 | + |
| 515 | + query = 'from(bucket:"' + bucket.name + '") |> range(start: 1970-01-01T00:00:00.000000001Z)' |
| 516 | + flux_result = self.client.query_api().query(query) |
| 517 | + self.assertEqual(1, len(flux_result)) |
| 518 | + rec = flux_result[0].records[0] |
| 519 | + |
| 520 | + self.assertEqual(self.id_tag, rec["id"]) |
| 521 | + self.assertEqual(self.customer_tag, rec["customer"]) |
| 522 | + self.assertEqual("LA", rec[self.data_center_key]) |
| 523 | + |
| 524 | + self.delete_test_bucket(bucket) |
| 525 | + |
| 526 | + |
447 | 527 | if __name__ == '__main__':
|
448 | 528 | unittest.main()
|
0 commit comments