|
| 1 | +from influxdb_client.client.write_api import SYNCHRONOUS |
| 2 | + |
| 3 | +from influxdb_client import PermissionResource, Permission, InfluxDBClient, Point, WriteOptions |
| 4 | +from tests.base_test import BaseTest |
| 5 | + |
| 6 | + |
| 7 | +class DeleteApiTest(BaseTest): |
| 8 | + |
| 9 | + def setUp(self) -> None: |
| 10 | + super(DeleteApiTest, self).setUp() |
| 11 | + response = self.buckets_api.find_buckets() |
| 12 | + |
| 13 | + for bucket in response.buckets: |
| 14 | + if bucket.name.endswith("_IT"): |
| 15 | + print("Delete bucket: ", bucket.name) |
| 16 | + self.buckets_api.delete_bucket(bucket) |
| 17 | + |
| 18 | + self.bucket = self.create_test_bucket() |
| 19 | + self.organization = self.find_my_org() |
| 20 | + |
| 21 | + resource = PermissionResource(type="buckets", org_id=self.organization.id, id=self.bucket.id) |
| 22 | + read_bucket = Permission(resource=resource, action="read") |
| 23 | + write_bucket = Permission(resource=resource, action="write") |
| 24 | + |
| 25 | + authorization = self.client.authorizations_api().create_authorization(org_id=self.organization.id, |
| 26 | + permissions=[read_bucket, write_bucket]) |
| 27 | + self.auth_token = authorization.token |
| 28 | + self.client.close() |
| 29 | + self.client = InfluxDBClient(url=self.host, token=self.auth_token, debug=True, org=self.org) |
| 30 | + self.delete_api = self.client.delete_api() |
| 31 | + |
| 32 | + def test_delete_buckets(self): |
| 33 | + |
| 34 | + write_api = self.client.write_api(write_options=SYNCHRONOUS) |
| 35 | + p1 = Point(measurement_name="h2o").tag("location", "coyote_creek").field("watter_level", 7.0).time(1) |
| 36 | + write_api.write(bucket=self.bucket.name, org=self.organization.name, record=p1) |
| 37 | + |
| 38 | + p2 = Point(measurement_name="h2o").tag("location", "coyote_creek").field("watter_level", 8.0).time(2) |
| 39 | + write_api.write(bucket=self.bucket.name, org=self.organization.name, record=p2) |
| 40 | + |
| 41 | + p3 = Point(measurement_name="h2o").tag("location", "coyote_creek").field("watter_level", 9.0).time(3) |
| 42 | + p4 = Point(measurement_name="h2o").tag("location", "coyote_creek").field("watter_level", 10.0).time(4) |
| 43 | + write_api.write(bucket=self.bucket.name, org=self.organization.name, record=[p3, p4]) |
| 44 | + |
| 45 | + p5 = Point(measurement_name="h2o").tag("location", "coyote_creek").field("watter_level", 11.0).time(5) |
| 46 | + p6 = Point(measurement_name="h2o").tag("location", "coyote_creek").field("watter_level", 12.0).time(6) |
| 47 | + write_api.write(bucket=self.bucket.name, org=self.organization.name, record=[p5, p6]) |
| 48 | + |
| 49 | + p7 = Point(measurement_name="h2o").tag("location", "coyote_creek").field("watter_level", 8.0).time(7) |
| 50 | + write_api.write(bucket=self.bucket.name, org=self.organization.name, record=p7) |
| 51 | + p8 = Point(measurement_name="h2o").tag("location", "coyote_creek").field("watter_level", 9.0).time(8) |
| 52 | + write_api.write(bucket=self.bucket.name, org=self.organization.name, record=p8) |
| 53 | + |
| 54 | + p9 = Point(measurement_name="h2o").tag("location", "coyote_creek").field("watter_level", 9.0).time(9) |
| 55 | + p10 = Point(measurement_name="h2o").tag("location", "coyote_creek").field("watter_level", 11.0).time(10) |
| 56 | + write_api.write(bucket=self.bucket.name, org=self.organization.name, record=[p9, p10]) |
| 57 | + |
| 58 | + p11 = Point(measurement_name="h2o").tag("location", "coyote_creek").field("watter_level", 11.0).time(11) |
| 59 | + p12 = Point(measurement_name="h2o").tag("location", "coyote_creek").field("watter_level", 13.0).time(12) |
| 60 | + write_api.write(bucket=self.bucket.name, org=self.organization.name, record=[p11, p12]) |
| 61 | + |
| 62 | + q = f'from(bucket:\"{self.bucket.name}\") |> range(start: 1970-01-01T00:00:00.000000001Z)' |
| 63 | + print(q) |
| 64 | + flux_tables = self.client.query_api().query(query=q, org=self.organization.id) |
| 65 | + self.assertEqual(len(flux_tables), 1) |
| 66 | + self.assertEqual(len(flux_tables[0].records), 12) |
| 67 | + |
| 68 | + start = "1970-01-01T00:00:00.000000001Z" |
| 69 | + stop = "1970-01-01T00:00:00.000000012Z" |
| 70 | + self.delete_api.delete(start, stop, "", bucket_id=self.bucket.id, org_id=self.organization.id) |
| 71 | + |
| 72 | + flux_tables2 = self.client.query_api().query( |
| 73 | + f'from(bucket:"{self.bucket.name}") |> range(start: 1970-01-01T00:00:00.000000001Z)', |
| 74 | + org=self.organization.id) |
| 75 | + self.assertEqual(len(flux_tables2), 0) |
0 commit comments