@@ -6,6 +6,14 @@ use influxdb::error::InfluxDbError;
66use influxdb:: query:: { InfluxDbQuery , Timestamp } ;
77use tokio:: runtime:: current_thread:: Runtime ;
88
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+
917fn get_runtime ( ) -> Runtime {
1018 Runtime :: new ( ) . expect ( "Unable to create a runtime" )
1119}
5058fn test_ping_influx_db ( ) {
5159 let client = create_client ( "notusedhere" ) ;
5260 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) ;
5862
5963 let ( build, version) = result. unwrap ( ) ;
6064 assert ! ( !build. is_empty( ) , "Build should not be empty" ) ;
@@ -73,19 +77,13 @@ fn test_connection_error() {
7377 . with_auth ( "nopriv_user" , "password" ) ;
7478 let read_query = InfluxDbQuery :: raw_read_query ( "SELECT * FROM weather" ) ;
7579 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) ;
8081 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+ ) ) ,
8987 }
9088}
9189
@@ -119,17 +117,11 @@ fn test_authed_write_and_read() {
119117 let write_query =
120118 InfluxDbQuery :: write_query ( Timestamp :: HOURS ( 11 ) , "weather" ) . add_field ( "temperature" , 82 ) ;
121119 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) ;
126121
127122 let read_query = InfluxDbQuery :: raw_read_query ( "SELECT * FROM weather" ) ;
128123 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) ;
133125 assert ! (
134126 !read_result. unwrap( ) . contains( "error" ) ,
135127 "Data contained a database error"
@@ -166,55 +158,37 @@ fn test_wrong_authed_write_and_read() {
166158 let write_query =
167159 InfluxDbQuery :: write_query ( Timestamp :: HOURS ( 11 ) , "weather" ) . add_field ( "temperature" , 82 ) ;
168160 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) ;
173162 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+ ) ) ,
182168 }
183169
184170 let read_query = InfluxDbQuery :: raw_read_query ( "SELECT * FROM weather" ) ;
185171 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) ;
190173 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+ ) ) ,
199179 }
200180
201181 let client = InfluxDbClient :: new ( "http://localhost:9086" , test_name)
202182 . with_auth ( "nopriv_user" , "password" ) ;
203183 let read_query = InfluxDbQuery :: raw_read_query ( "SELECT * FROM weather" ) ;
204184 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) ;
209186 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+ ) ) ,
218192 }
219193}
220194
@@ -246,36 +220,24 @@ fn test_non_authed_write_and_read() {
246220 let write_query =
247221 InfluxDbQuery :: write_query ( Timestamp :: HOURS ( 11 ) , "weather" ) . add_field ( "temperature" , 82 ) ;
248222 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) ;
253224 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+ ) ) ,
262230 }
263231
264232 let read_query = InfluxDbQuery :: raw_read_query ( "SELECT * FROM weather" ) ;
265233 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) ;
270235 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+ ) ) ,
279241 }
280242}
281243
@@ -296,17 +258,11 @@ fn test_write_and_read_field() {
296258 let write_query =
297259 InfluxDbQuery :: write_query ( Timestamp :: HOURS ( 11 ) , "weather" ) . add_field ( "temperature" , 82 ) ;
298260 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) ;
303262
304263 let read_query = InfluxDbQuery :: raw_read_query ( "SELECT * FROM weather" ) ;
305264 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) ;
310266 assert ! (
311267 !read_result. unwrap( ) . contains( "error" ) ,
312268 "Data contained a database error"
@@ -338,10 +294,7 @@ fn test_json_query() {
338294 let write_query =
339295 InfluxDbQuery :: write_query ( Timestamp :: HOURS ( 11 ) , "weather" ) . add_field ( "temperature" , 82 ) ;
340296 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) ;
345298
346299 #[ derive( Deserialize , Debug , PartialEq ) ]
347300 struct Weather {
@@ -354,11 +307,7 @@ fn test_json_query() {
354307 . json_query ( query)
355308 . and_then ( |mut db_result| db_result. deserialize_next :: < Weather > ( ) ) ;
356309 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) ;
362311
363312 assert_eq ! (
364313 result. unwrap( ) . series[ 0 ] . values[ 0 ] ,
@@ -411,12 +360,7 @@ fn test_json_query_vec() {
411360 . json_query ( query)
412361 . and_then ( |mut db_result| db_result. deserialize_next :: < Weather > ( ) ) ;
413362 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) ;
420364 assert_eq ! ( result. unwrap( ) . series[ 0 ] . values. len( ) , 3 ) ;
421365
422366 delete_db ( test_name) . expect ( "could not clean up db" ) ;
@@ -458,21 +402,13 @@ fn test_serde_multi_query() {
458402
459403 let write_result = get_runtime ( ) . block_on ( client. query ( & write_query) ) ;
460404 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) ;
471407
472408 let future = client
473409 . json_query (
474410 InfluxDbQuery :: raw_read_query ( "SELECT * FROM temperature" )
475- . add ( "SELECT * FROM humidity" ) ,
411+ . add_query ( "SELECT * FROM humidity" ) ,
476412 )
477413 . and_then ( |mut db_result| {
478414 let temp = db_result. deserialize_next :: < Temperature > ( ) ;
@@ -481,22 +417,16 @@ fn test_serde_multi_query() {
481417 ( temp, humidity)
482418 } ) ;
483419 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) ;
489421
490422 let ( temp, humidity) = result. unwrap ( ) ;
491-
492423 assert_eq ! (
493424 temp. series[ 0 ] . values[ 0 ] ,
494425 Temperature {
495426 time: "1970-01-01T11:00:00Z" . to_string( ) ,
496427 temperature: 16
497428 } ,
498429 ) ;
499-
500430 assert_eq ! (
501431 humidity. series[ 0 ] . values[ 0 ] ,
502432 Humidity {
0 commit comments