21
21
/// HELPER METHODS
22
22
///
23
23
24
+ /// Simple Helper that creates a Database in InfluxDB for every test that is being run.
25
+ /// Also implements the `Drop` trait so the database is being cleaned up, even when the test failed.
26
+ struct DatabaseIntegration {
27
+ test_name : String ,
28
+ }
29
+
30
+ /// Single method to prime the test. Since everything is blocking we don't have to handle any futures here.
31
+ impl DatabaseIntegration {
32
+ fn prime < S > ( test_name : S ) -> Self
33
+ where
34
+ S : ToString ,
35
+ {
36
+ let ret = DatabaseIntegration {
37
+ test_name : test_name. to_string ( ) ,
38
+ } ;
39
+ ret
40
+ }
41
+
42
+ fn set ( & self ) {
43
+ let query = format ! ( "CREATE DATABASE {}" , self . test_name) ;
44
+ let name = format ! ( "{}" , self . test_name) ;
45
+ get_runtime ( )
46
+ . block_on ( create_client ( name) . query ( & InfluxDbQuery :: raw_read_query ( query) ) )
47
+ . expect ( "could not create db for integration test" ) ;
48
+ }
49
+ }
50
+
51
+ /// Cleans up the Database - even in case of test failure.
52
+ impl Drop for DatabaseIntegration {
53
+ fn drop ( & mut self ) {
54
+ let query = format ! ( "DROP DATABASE {}" , self . test_name) ;
55
+ let name = format ! ( "{}" , self . test_name) ;
56
+ get_runtime ( )
57
+ . block_on ( create_client ( name) . query ( & InfluxDbQuery :: raw_read_query ( query) ) )
58
+ . expect ( "could not clear up db for integration test" ) ;
59
+ }
60
+ }
61
+
62
+ struct RunOnDrop {
63
+ closure : Box < dyn Fn ( ) -> ( ) > ,
64
+ }
65
+
66
+ impl Drop for RunOnDrop {
67
+ fn drop ( & mut self ) {
68
+ ( self . closure ) ( ) ;
69
+ }
70
+ }
71
+
24
72
fn create_db < T > ( test_name : T ) -> Result < String , InfluxDbError >
25
73
where
26
74
T : ToString ,
@@ -56,10 +104,15 @@ fn test_ping_influx_db() {
56
104
#[ test]
57
105
/// INTEGRATION TEST
58
106
///
59
- /// //todo: describe what this test is doing!
107
+ /// This integration tests that writing data and retrieving the data again is working
60
108
fn test_write_and_read_field ( ) {
61
109
let test_name = "test_write_field" ;
62
110
create_db ( test_name) . expect ( "could not setup db" ) ;
111
+ let _run_on_drop = RunOnDrop {
112
+ closure : Box :: new ( || {
113
+ delete_db ( "test_write_field" ) . expect ( "could not clean up db" ) ;
114
+ } ) ,
115
+ } ;
63
116
64
117
let client = create_client ( test_name) ;
65
118
let write_query =
@@ -88,12 +141,18 @@ fn test_write_and_read_field() {
88
141
#[ cfg( feature = "use-serde" ) ]
89
142
/// INTEGRATION TEST
90
143
///
91
- /// This test case tests whether JSON can be decoded from a InfluxDB response
144
+ /// This test case tests whether JSON can be decoded from a InfluxDB response and wether that JSON
145
+ /// is equal to the data which was written to the database
92
146
fn test_json_query ( ) {
93
147
use serde:: Deserialize ;
94
148
95
149
let test_name = "test_json_query" ;
96
150
create_db ( test_name) . expect ( "could not setup db" ) ;
151
+ let _run_on_drop = RunOnDrop {
152
+ closure : Box :: new ( || {
153
+ delete_db ( "test_json_query" ) . expect ( "could not clean up db" ) ;
154
+ } ) ,
155
+ } ;
97
156
98
157
let client = create_client ( test_name) ;
99
158
@@ -106,7 +165,7 @@ fn test_json_query() {
106
165
format!( "Should be no error: {}" , write_result. unwrap_err( ) )
107
166
) ;
108
167
109
- #[ derive( Deserialize , Debug ) ]
168
+ #[ derive( Deserialize , Debug , PartialEq ) ]
110
169
struct Weather {
111
170
time : String ,
112
171
temperature : i32 ,
@@ -115,24 +174,23 @@ fn test_json_query() {
115
174
let query = InfluxDbQuery :: raw_read_query ( "SELECT * FROM weather" ) ;
116
175
let future = client
117
176
. json_query ( query)
118
- . map ( |mut db_result| db_result. deserialize_next :: < Weather > ( ) ) ;
177
+ . and_then ( |mut db_result| db_result. deserialize_next :: < Weather > ( ) ) ;
119
178
let result = get_runtime ( ) . block_on ( future) ;
120
179
121
180
assert ! (
122
181
result. is_ok( ) ,
123
- format!( "We couldn't read from the DB: "
124
- //, result.unwrap_err() // todo
125
- )
182
+ format!( "We couldn't read from the DB: {}" , result. unwrap_err( ) )
183
+ ) ;
184
+
185
+ assert_eq ! (
186
+ result. unwrap( ) . series[ 0 ] . values[ 0 ] ,
187
+ Weather {
188
+ time: "1970-01-01T11:00:00Z" . to_string( ) ,
189
+ temperature: 82
190
+ }
126
191
) ;
127
192
128
- // todo check this out!
129
- // assert_eq!(
130
- // result.unwrap(),
131
- // Weather {
132
- // time: 11,
133
- // temperature: 82
134
- // }
135
- // )
193
+ delete_db ( test_name) . expect ( "could not clean up db" ) ;
136
194
}
137
195
138
196
#[ test]
@@ -143,59 +201,80 @@ fn test_json_query() {
143
201
fn test_serde_multi_query ( ) {
144
202
use serde:: Deserialize ;
145
203
146
- #[ derive( Deserialize , Debug ) ]
147
- struct Weather {
204
+ let test_name = "test_serde_multi_query" ;
205
+ create_db ( test_name) . expect ( "could not setup db" ) ;
206
+ let _run_on_drop = RunOnDrop {
207
+ closure : Box :: new ( || {
208
+ delete_db ( "test_serde_multi_query" ) . expect ( "could not clean up db" ) ;
209
+ } ) ,
210
+ } ;
211
+
212
+ #[ derive( Deserialize , Debug , PartialEq ) ]
213
+ struct Temperature {
148
214
time : String ,
149
215
temperature : i32 ,
150
216
}
151
217
152
- let client = create_client ( "todo" ) ;
153
- let query = InfluxDbQuery :: raw_read_query ( "CREATE database should_fail" ) ;
154
- let future = client
155
- . json_query ( query)
156
- . map ( |mut db_result| db_result. deserialize_next :: < Weather > ( ) ) ;
157
- let result = get_runtime ( ) . block_on ( future) ;
218
+ #[ derive( Deserialize , Debug , PartialEq ) ]
219
+ struct Humidity {
220
+ time : String ,
221
+ humidity : i32 ,
222
+ }
223
+
224
+ let client = create_client ( test_name) ;
225
+ let write_query = InfluxDbQuery :: write_query ( Timestamp :: HOURS ( 11 ) , "temperature" )
226
+ . add_field ( "temperature" , 16 ) ;
227
+ let write_query2 =
228
+ InfluxDbQuery :: write_query ( Timestamp :: HOURS ( 11 ) , "humidity" ) . add_field ( "humidity" , 69 ) ;
229
+
230
+ let write_result = get_runtime ( ) . block_on ( client. query ( & write_query) ) ;
231
+ let write_result2 = get_runtime ( ) . block_on ( client. query ( & write_query2) ) ;
158
232
159
233
assert ! (
160
- result. is_err( ) ,
161
- format!(
162
- "Should not be able to build JSON query that is not SELECT or SELECT .. INTO: " //result.unwrap_err()
163
- )
234
+ write_result. is_ok( ) ,
235
+ format!( "Write Query 1 failed: {}" , write_result. unwrap_err( ) )
164
236
) ;
165
- }
166
-
167
- #[ test]
168
- #[ cfg( feature = "use-serde" ) ]
169
- /// INTEGRATION TEST
170
- ///
171
- /// This test case tests whether JSON can be decoded from a InfluxDB response
172
- fn test_raw_query_build_error ( ) {
173
- let client = create_client ( "todo" ) ;
174
- let query =
175
- InfluxDbQuery :: write_query ( Timestamp :: HOURS ( 11 ) , "weather" ) . add_tag ( "season" , "summer" ) ;
176
- let result = get_runtime ( ) . block_on ( client. query ( & query) ) ;
177
237
178
238
assert ! (
179
- result. is_err( ) ,
180
- format!(
181
- "Should not be able to build JSON query that is not SELECT or SELECT .. INTO: {}" ,
182
- result. unwrap_err( )
183
- )
239
+ write_result2. is_ok( ) ,
240
+ format!( "Write Query 2 failed: {}" , write_result2. unwrap_err( ) )
184
241
) ;
185
- }
186
242
187
- #[ test]
188
- /// INTEGRATION TEST
189
- fn test_delete_database ( ) {
190
- let client = create_client ( "todo" ) ;
191
- let query = InfluxDbQuery :: raw_read_query ( "DELETE DATABASE test" ) ;
192
- let result = get_runtime ( ) . block_on ( client. query ( & query) ) ;
243
+ let future = client
244
+ . json_query (
245
+ InfluxDbQuery :: raw_read_query ( "SELECT * FROM temperature" )
246
+ . add ( "SELECT * FROM humidity" ) ,
247
+ )
248
+ . and_then ( |mut db_result| {
249
+ let temp = db_result. deserialize_next :: < Temperature > ( ) ;
250
+ let humidity = db_result. deserialize_next :: < Humidity > ( ) ;
251
+
252
+ ( temp, humidity)
253
+ } ) ;
254
+ let result = get_runtime ( ) . block_on ( future) ;
193
255
194
256
assert ! (
195
- result. is_err( ) ,
196
- format!(
197
- "Should not be able to build JSON query that is not SELECT or SELECT .. INTO: {}" ,
198
- result. unwrap_err( )
199
- )
257
+ result. is_ok( ) ,
258
+ format!( "No problems should be had: {}" , result. unwrap_err( ) )
200
259
) ;
201
- }
260
+
261
+ let ( temp, humidity) = result. unwrap ( ) ;
262
+
263
+ assert_eq ! (
264
+ temp. series[ 0 ] . values[ 0 ] ,
265
+ Temperature {
266
+ time: "1970-01-01T11:00:00Z" . to_string( ) ,
267
+ temperature: 16
268
+ } ,
269
+ ) ;
270
+
271
+ assert_eq ! (
272
+ humidity. series[ 0 ] . values[ 0 ] ,
273
+ Humidity {
274
+ time: "1970-01-01T11:00:00Z" . to_string( ) ,
275
+ humidity: 69
276
+ }
277
+ ) ;
278
+
279
+ delete_db ( test_name) . expect ( "could not clean up db" ) ;
280
+ }
0 commit comments