@@ -6,6 +6,14 @@ use influxdb::error::InfluxDbError;
6
6
use influxdb:: query:: { InfluxDbQuery , Timestamp } ;
7
7
use tokio:: runtime:: current_thread:: Runtime ;
8
8
9
+ fn assert_result_err < A : std:: fmt:: Debug , B : std:: fmt:: Debug > ( result : & Result < A , B > ) {
10
+ result. as_ref ( ) . expect_err ( "assert_result_err failed" ) ;
11
+ }
12
+
13
+ fn assert_result_ok < A : std:: fmt:: Debug , B : std:: fmt:: Debug > ( result : & Result < A , B > ) {
14
+ result. as_ref ( ) . expect ( "assert_result_ok failed" ) ;
15
+ }
16
+
9
17
fn get_runtime ( ) -> Runtime {
10
18
Runtime :: new ( ) . expect ( "Unable to create a runtime" )
11
19
}
50
58
fn test_ping_influx_db ( ) {
51
59
let client = create_client ( "notusedhere" ) ;
52
60
let result = get_runtime ( ) . block_on ( client. ping ( ) ) ;
53
- assert ! (
54
- result. is_ok( ) ,
55
- "Should be no error: {}" ,
56
- result. unwrap_err( )
57
- ) ;
61
+ assert_result_ok ( & result) ;
58
62
59
63
let ( build, version) = result. unwrap ( ) ;
60
64
assert ! ( !build. is_empty( ) , "Build should not be empty" ) ;
@@ -73,19 +77,13 @@ fn test_connection_error() {
73
77
. with_auth ( "nopriv_user" , "password" ) ;
74
78
let read_query = InfluxDbQuery :: raw_read_query ( "SELECT * FROM weather" ) ;
75
79
let read_result = get_runtime ( ) . block_on ( client. query ( & read_query) ) ;
76
- assert ! (
77
- read_result. is_err( ) ,
78
- format!( "Should be an error: {}" , read_result. unwrap_err( ) )
79
- ) ;
80
+ assert_result_err ( & read_result) ;
80
81
match read_result {
81
- Err ( InfluxDbError :: ConnectionError { .. } ) => assert ! ( true ) ,
82
- _ => assert ! (
83
- false ,
84
- format!(
85
- "Should cause a ConnectionError: {}" ,
86
- read_result. unwrap_err( )
87
- )
88
- ) ,
82
+ Err ( InfluxDbError :: ConnectionError { .. } ) => { }
83
+ _ => panic ! ( format!(
84
+ "Should cause a ConnectionError: {}" ,
85
+ read_result. unwrap_err( )
86
+ ) ) ,
89
87
}
90
88
}
91
89
@@ -119,17 +117,11 @@ fn test_authed_write_and_read() {
119
117
let write_query =
120
118
InfluxDbQuery :: write_query ( Timestamp :: HOURS ( 11 ) , "weather" ) . add_field ( "temperature" , 82 ) ;
121
119
let write_result = get_runtime ( ) . block_on ( client. query ( & write_query) ) ;
122
- assert ! (
123
- write_result. is_ok( ) ,
124
- format!( "Should be no error: {}" , write_result. unwrap_err( ) )
125
- ) ;
120
+ assert_result_ok ( & write_result) ;
126
121
127
122
let read_query = InfluxDbQuery :: raw_read_query ( "SELECT * FROM weather" ) ;
128
123
let read_result = get_runtime ( ) . block_on ( client. query ( & read_query) ) ;
129
- assert ! (
130
- read_result. is_ok( ) ,
131
- format!( "Should be no error: {}" , read_result. unwrap_err( ) )
132
- ) ;
124
+ assert_result_ok ( & read_result) ;
133
125
assert ! (
134
126
!read_result. unwrap( ) . contains( "error" ) ,
135
127
"Data contained a database error"
@@ -166,55 +158,37 @@ fn test_wrong_authed_write_and_read() {
166
158
let write_query =
167
159
InfluxDbQuery :: write_query ( Timestamp :: HOURS ( 11 ) , "weather" ) . add_field ( "temperature" , 82 ) ;
168
160
let write_result = get_runtime ( ) . block_on ( client. query ( & write_query) ) ;
169
- assert ! (
170
- write_result. is_err( ) ,
171
- format!( "Should be an error: {}" , write_result. unwrap_err( ) )
172
- ) ;
161
+ assert_result_err ( & write_result) ;
173
162
match write_result {
174
- Err ( InfluxDbError :: AuthorizationError ) => assert ! ( true ) ,
175
- _ => assert ! (
176
- false ,
177
- format!(
178
- "Should be an AuthorizationError: {}" ,
179
- write_result. unwrap_err( )
180
- )
181
- ) ,
163
+ Err ( InfluxDbError :: AuthorizationError ) => { }
164
+ _ => panic ! ( format!(
165
+ "Should be an AuthorizationError: {}" ,
166
+ write_result. unwrap_err( )
167
+ ) ) ,
182
168
}
183
169
184
170
let read_query = InfluxDbQuery :: raw_read_query ( "SELECT * FROM weather" ) ;
185
171
let read_result = get_runtime ( ) . block_on ( client. query ( & read_query) ) ;
186
- assert ! (
187
- read_result. is_err( ) ,
188
- format!( "Should be an error: {}" , read_result. unwrap_err( ) )
189
- ) ;
172
+ assert_result_err ( & read_result) ;
190
173
match read_result {
191
- Err ( InfluxDbError :: AuthorizationError ) => assert ! ( true ) ,
192
- _ => assert ! (
193
- false ,
194
- format!(
195
- "Should be an AuthorizationError: {}" ,
196
- read_result. unwrap_err( )
197
- )
198
- ) ,
174
+ Err ( InfluxDbError :: AuthorizationError ) => { }
175
+ _ => panic ! ( format!(
176
+ "Should be an AuthorizationError: {}" ,
177
+ read_result. unwrap_err( )
178
+ ) ) ,
199
179
}
200
180
201
181
let client = InfluxDbClient :: new ( "http://localhost:9086" , test_name)
202
182
. with_auth ( "nopriv_user" , "password" ) ;
203
183
let read_query = InfluxDbQuery :: raw_read_query ( "SELECT * FROM weather" ) ;
204
184
let read_result = get_runtime ( ) . block_on ( client. query ( & read_query) ) ;
205
- assert ! (
206
- read_result. is_err( ) ,
207
- format!( "Should be an error: {}" , read_result. unwrap_err( ) )
208
- ) ;
185
+ assert_result_err ( & read_result) ;
209
186
match read_result {
210
- Err ( InfluxDbError :: AuthenticationError ) => assert ! ( true ) ,
211
- _ => assert ! (
212
- false ,
213
- format!(
214
- "Should be an AuthenticationError: {}" ,
215
- read_result. unwrap_err( )
216
- )
217
- ) ,
187
+ Err ( InfluxDbError :: AuthenticationError ) => { }
188
+ _ => panic ! ( format!(
189
+ "Should be an AuthenticationError: {}" ,
190
+ read_result. unwrap_err( )
191
+ ) ) ,
218
192
}
219
193
}
220
194
@@ -246,36 +220,24 @@ fn test_non_authed_write_and_read() {
246
220
let write_query =
247
221
InfluxDbQuery :: write_query ( Timestamp :: HOURS ( 11 ) , "weather" ) . add_field ( "temperature" , 82 ) ;
248
222
let write_result = get_runtime ( ) . block_on ( non_authed_client. query ( & write_query) ) ;
249
- assert ! (
250
- write_result. is_err( ) ,
251
- format!( "Should be an error: {}" , write_result. unwrap_err( ) )
252
- ) ;
223
+ assert_result_err ( & write_result) ;
253
224
match write_result {
254
- Err ( InfluxDbError :: AuthorizationError ) => assert ! ( true ) ,
255
- _ => assert ! (
256
- false ,
257
- format!(
258
- "Should be an AuthorizationError: {}" ,
259
- write_result. unwrap_err( )
260
- )
261
- ) ,
225
+ Err ( InfluxDbError :: AuthorizationError ) => { }
226
+ _ => panic ! ( format!(
227
+ "Should be an AuthorizationError: {}" ,
228
+ write_result. unwrap_err( )
229
+ ) ) ,
262
230
}
263
231
264
232
let read_query = InfluxDbQuery :: raw_read_query ( "SELECT * FROM weather" ) ;
265
233
let read_result = get_runtime ( ) . block_on ( non_authed_client. query ( & read_query) ) ;
266
- assert ! (
267
- read_result. is_err( ) ,
268
- format!( "Should be an error: {}" , read_result. unwrap( ) )
269
- ) ;
234
+ assert_result_err ( & read_result) ;
270
235
match read_result {
271
- Err ( InfluxDbError :: AuthorizationError ) => assert ! ( true ) ,
272
- _ => assert ! (
273
- false ,
274
- format!(
275
- "Should be an AuthorizationError: {}" ,
276
- read_result. unwrap_err( )
277
- )
278
- ) ,
236
+ Err ( InfluxDbError :: AuthorizationError ) => { }
237
+ _ => panic ! ( format!(
238
+ "Should be an AuthorizationError: {}" ,
239
+ read_result. unwrap_err( )
240
+ ) ) ,
279
241
}
280
242
}
281
243
@@ -296,17 +258,11 @@ fn test_write_and_read_field() {
296
258
let write_query =
297
259
InfluxDbQuery :: write_query ( Timestamp :: HOURS ( 11 ) , "weather" ) . add_field ( "temperature" , 82 ) ;
298
260
let write_result = get_runtime ( ) . block_on ( client. query ( & write_query) ) ;
299
- assert ! (
300
- write_result. is_ok( ) ,
301
- format!( "Should be no error: {}" , write_result. unwrap_err( ) )
302
- ) ;
261
+ assert_result_ok ( & write_result) ;
303
262
304
263
let read_query = InfluxDbQuery :: raw_read_query ( "SELECT * FROM weather" ) ;
305
264
let read_result = get_runtime ( ) . block_on ( client. query ( & read_query) ) ;
306
- assert ! (
307
- read_result. is_ok( ) ,
308
- format!( "Should be no error: {}" , read_result. unwrap_err( ) )
309
- ) ;
265
+ assert_result_ok ( & read_result) ;
310
266
assert ! (
311
267
!read_result. unwrap( ) . contains( "error" ) ,
312
268
"Data contained a database error"
@@ -338,10 +294,7 @@ fn test_json_query() {
338
294
let write_query =
339
295
InfluxDbQuery :: write_query ( Timestamp :: HOURS ( 11 ) , "weather" ) . add_field ( "temperature" , 82 ) ;
340
296
let write_result = get_runtime ( ) . block_on ( client. query ( & write_query) ) ;
341
- assert ! (
342
- write_result. is_ok( ) ,
343
- format!( "Should be no error: {}" , write_result. unwrap_err( ) )
344
- ) ;
297
+ assert_result_ok ( & write_result) ;
345
298
346
299
#[ derive( Deserialize , Debug , PartialEq ) ]
347
300
struct Weather {
@@ -354,11 +307,7 @@ fn test_json_query() {
354
307
. json_query ( query)
355
308
. and_then ( |mut db_result| db_result. deserialize_next :: < Weather > ( ) ) ;
356
309
let result = get_runtime ( ) . block_on ( future) ;
357
-
358
- assert ! (
359
- result. is_ok( ) ,
360
- format!( "We couldn't read from the DB: {}" , result. unwrap_err( ) )
361
- ) ;
310
+ assert_result_ok ( & result) ;
362
311
363
312
assert_eq ! (
364
313
result. unwrap( ) . series[ 0 ] . values[ 0 ] ,
@@ -411,12 +360,7 @@ fn test_json_query_vec() {
411
360
. json_query ( query)
412
361
. and_then ( |mut db_result| db_result. deserialize_next :: < Weather > ( ) ) ;
413
362
let result = get_runtime ( ) . block_on ( future) ;
414
-
415
- assert ! (
416
- result. is_ok( ) ,
417
- format!( "We couldn't read from the DB: {}" , result. unwrap_err( ) )
418
- ) ;
419
-
363
+ assert_result_ok ( & result) ;
420
364
assert_eq ! ( result. unwrap( ) . series[ 0 ] . values. len( ) , 3 ) ;
421
365
422
366
delete_db ( test_name) . expect ( "could not clean up db" ) ;
@@ -458,21 +402,13 @@ fn test_serde_multi_query() {
458
402
459
403
let write_result = get_runtime ( ) . block_on ( client. query ( & write_query) ) ;
460
404
let write_result2 = get_runtime ( ) . block_on ( client. query ( & write_query2) ) ;
461
-
462
- assert ! (
463
- write_result. is_ok( ) ,
464
- format!( "Write Query 1 failed: {}" , write_result. unwrap_err( ) )
465
- ) ;
466
-
467
- assert ! (
468
- write_result2. is_ok( ) ,
469
- format!( "Write Query 2 failed: {}" , write_result2. unwrap_err( ) )
470
- ) ;
405
+ assert_result_ok ( & write_result) ;
406
+ assert_result_ok ( & write_result2) ;
471
407
472
408
let future = client
473
409
. json_query (
474
410
InfluxDbQuery :: raw_read_query ( "SELECT * FROM temperature" )
475
- . add ( "SELECT * FROM humidity" ) ,
411
+ . add_query ( "SELECT * FROM humidity" ) ,
476
412
)
477
413
. and_then ( |mut db_result| {
478
414
let temp = db_result. deserialize_next :: < Temperature > ( ) ;
@@ -481,22 +417,16 @@ fn test_serde_multi_query() {
481
417
( temp, humidity)
482
418
} ) ;
483
419
let result = get_runtime ( ) . block_on ( future) ;
484
-
485
- assert ! (
486
- result. is_ok( ) ,
487
- format!( "No problems should be had: {}" , result. unwrap_err( ) )
488
- ) ;
420
+ assert_result_ok ( & result) ;
489
421
490
422
let ( temp, humidity) = result. unwrap ( ) ;
491
-
492
423
assert_eq ! (
493
424
temp. series[ 0 ] . values[ 0 ] ,
494
425
Temperature {
495
426
time: "1970-01-01T11:00:00Z" . to_string( ) ,
496
427
temperature: 16
497
428
} ,
498
429
) ;
499
-
500
430
assert_eq ! (
501
431
humidity. series[ 0 ] . values[ 0 ] ,
502
432
Humidity {
0 commit comments