1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Configuration ;
4+ using System . Threading . Tasks ;
5+ using InfluxDB . Client . Api . Domain ;
6+ using InfluxDB . Client . Core ;
7+ using InfluxDB . Client . Writes ;
8+ using NUnit . Framework ;
9+
10+ namespace InfluxDB . Client . Test
11+ {
12+ [ TestFixture ]
13+ public class ItDeleteApiTest : AbstractItClientTest
14+ {
15+ [ SetUp ]
16+ public new async Task SetUp ( )
17+ {
18+ _organization = await FindMyOrg ( ) ;
19+
20+ var retention = new BucketRetentionRules ( BucketRetentionRules . TypeEnum . Expire , 3600 ) ;
21+
22+ _bucket = await Client . GetBucketsApi ( )
23+ . CreateBucketAsync ( GenerateName ( "h2o" ) , retention , _organization ) ;
24+
25+ //
26+ // Add Permissions to read and write to the Bucket
27+ //
28+ var resource =
29+ new PermissionResource ( PermissionResource . TypeEnum . Buckets , _bucket . Id , null , _organization . Id ) ;
30+
31+ var readBucket = new Permission ( Permission . ActionEnum . Read , resource ) ;
32+ var writeBucket = new Permission ( Permission . ActionEnum . Write , resource ) ;
33+
34+ var loggedUser = await Client . GetUsersApi ( ) . MeAsync ( ) ;
35+ Assert . IsNotNull ( loggedUser ) ;
36+
37+ var authorization = await Client . GetAuthorizationsApi ( )
38+ . CreateAuthorizationAsync ( await FindMyOrg ( ) , new List < Permission > { readBucket , writeBucket } ) ;
39+
40+ _token = authorization . Token ;
41+
42+ Client . Dispose ( ) ;
43+ var options = new InfluxDBClientOptions . Builder ( ) . Url ( InfluxDbUrl ) . AuthenticateToken ( _token . ToCharArray ( ) )
44+ . Org ( _organization . Id ) . Bucket ( _bucket . Id ) . Build ( ) ;
45+ Client = InfluxDBClientFactory . Create ( options ) ;
46+ _queryApi = Client . GetQueryApi ( ) ;
47+ }
48+
49+ [ TearDown ]
50+ protected new void After ( )
51+ {
52+ _writeApi . Dispose ( ) ;
53+ }
54+
55+ private Bucket _bucket ;
56+ private QueryApi _queryApi ;
57+ private WriteApi _writeApi ;
58+ private DeleteApi _deleteApi ;
59+ private Organization _organization ;
60+ private string _token ;
61+
62+ [ Measurement ( "h2o" ) ]
63+ private class H20Measurement
64+ {
65+ [ Column ( "location" , IsTag = true ) ] public string Location { get ; set ; }
66+
67+ [ Column ( "level" ) ] public double ? Level { get ; set ; }
68+
69+ [ Column ( IsTimestamp = true ) ] public DateTime Time { get ; set ; }
70+ }
71+
72+ [ Test ]
73+ public async Task Delete ( )
74+ {
75+ Client . Dispose ( ) ;
76+
77+ Environment . SetEnvironmentVariable ( "point-datacenter" , "LA" ) ;
78+ ConfigurationManager . AppSettings [ "point-sensor.version" ] = "1.23a" ;
79+
80+ var options = new InfluxDBClientOptions . Builder ( ) . Url ( InfluxDbUrl )
81+ . AuthenticateToken ( _token . ToCharArray ( ) )
82+ . AddDefaultTag ( "id" , "132-987-655" )
83+ . AddDefaultTag ( "customer" , "California Miner" )
84+ . AddDefaultTag ( "env-var" , "${env.point-datacenter}" )
85+ . AddDefaultTag ( "sensor-version" , "${point-sensor.version}" )
86+ . Build ( ) ;
87+
88+ Client = InfluxDBClientFactory . Create ( options ) ;
89+
90+ var point = PointData . Measurement ( "h2o_feet" ) . Tag ( "location" , "west" ) . Field ( "water_level" , 1 ) ;
91+ var point2 = PointData . Measurement ( "h2o_feet" ) . Tag ( "location" , "west" ) . Field ( "water_level" , 2 ) ;
92+ var point3 = PointData . Measurement ( "h2o_feet" ) . Tag ( "location" , "west" ) . Field ( "water_level" , 3 ) ;
93+ var point4 = PointData . Measurement ( "h2o_feet" ) . Tag ( "location" , "west" ) . Field ( "water_level" , 4 ) ;
94+ var point5 = PointData . Measurement ( "h2o_feet" ) . Tag ( "location" , "west" ) . Field ( "water_level" , 5 ) ;
95+ var point6 = PointData . Measurement ( "h2o_feet" ) . Tag ( "location" , "west" ) . Field ( "water_level" , 6 ) ;
96+
97+ _writeApi = Client . GetWriteApi ( ) ;
98+ var listener = new WriteApiTest . EventListener ( _writeApi ) ;
99+ _writeApi . WritePoint ( _bucket . Name , _organization . Id , point ) ;
100+ _writeApi . Flush ( ) ;
101+
102+ listener . WaitToSuccess ( ) ;
103+
104+ _writeApi . WritePoint ( _bucket . Name , _organization . Id , point2 ) ;
105+ _writeApi . Flush ( ) ;
106+
107+ listener . WaitToSuccess ( ) ;
108+
109+ _writeApi . WritePoint ( _bucket . Name , _organization . Id , point3 ) ;
110+ _writeApi . Flush ( ) ;
111+
112+ listener . WaitToSuccess ( ) ;
113+
114+ _writeApi . WritePoint ( _bucket . Name , _organization . Id , point4 ) ;
115+ _writeApi . Flush ( ) ;
116+
117+ listener . WaitToSuccess ( ) ;
118+
119+ _writeApi . WritePoint ( _bucket . Name , _organization . Id , point5 ) ;
120+ _writeApi . Flush ( ) ;
121+
122+ listener . WaitToSuccess ( ) ;
123+
124+ _writeApi . WritePoint ( _bucket . Name , _organization . Id , point6 ) ;
125+ _writeApi . Flush ( ) ;
126+
127+ listener . WaitToSuccess ( ) ;
128+
129+ string query = "from(bucket:\" " + _bucket . Name +
130+ "\" ) |> range(start: 1970-01-01T00:00:00.000000001Z) |> pivot(rowKey:[\" _time\" ], columnKey: [\" _field\" ], valueColumn: \" _value\" )" ;
131+
132+ _queryApi = Client . GetQueryApi ( ) ;
133+ var tables = await _queryApi . QueryAsync ( query , _organization . Id ) ;
134+
135+ Assert . AreEqual ( 1 , tables . Count ) ;
136+ Assert . AreEqual ( 6 , tables [ 0 ] . Records . Count ) ;
137+
138+ _deleteApi = Client . GetDeleteApi ( ) ;
139+ await _deleteApi . Delete ( DateTime . Now . AddHours ( - 1 ) , DateTime . Now , "" , _bucket , _organization ) ;
140+
141+ var tablesAfterDelete = await _queryApi . QueryAsync ( query , _organization . Id ) ;
142+
143+ Assert . AreEqual ( 0 , tablesAfterDelete . Count ) ;
144+ }
145+ }
146+ }
0 commit comments