Skip to content

Commit a57df25

Browse files
committed
Progress
1 parent 3255d55 commit a57df25

File tree

2 files changed

+138
-57
lines changed

2 files changed

+138
-57
lines changed

src/query/write_query.rs

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use crate::error::InfluxDbError;
66
use crate::query::{InfluxDbQuery, QueryType, Timestamp, ValidQuery};
77
use itertools::Itertools;
88

9+
// todo: batch write queries
10+
911
/// Internal Representation of a Write query that has not yet been built
1012
pub struct InfluxDbWriteQuery {
1113
fields: Vec<(String, String)>,

tests/integration_tests.rs

+136-57
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,54 @@ where
2121
/// HELPER METHODS
2222
///
2323
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+
2472
fn create_db<T>(test_name: T) -> Result<String, InfluxDbError>
2573
where
2674
T: ToString,
@@ -56,10 +104,15 @@ fn test_ping_influx_db() {
56104
#[test]
57105
/// INTEGRATION TEST
58106
///
59-
/// //todo: describe what this test is doing!
107+
/// This integration tests that writing data and retrieving the data again is working
60108
fn test_write_and_read_field() {
61109
let test_name = "test_write_field";
62110
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+
};
63116

64117
let client = create_client(test_name);
65118
let write_query =
@@ -88,12 +141,18 @@ fn test_write_and_read_field() {
88141
#[cfg(feature = "use-serde")]
89142
/// INTEGRATION TEST
90143
///
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
92146
fn test_json_query() {
93147
use serde::Deserialize;
94148

95149
let test_name = "test_json_query";
96150
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+
};
97156

98157
let client = create_client(test_name);
99158

@@ -106,7 +165,7 @@ fn test_json_query() {
106165
format!("Should be no error: {}", write_result.unwrap_err())
107166
);
108167

109-
#[derive(Deserialize, Debug)]
168+
#[derive(Deserialize, Debug, PartialEq)]
110169
struct Weather {
111170
time: String,
112171
temperature: i32,
@@ -115,24 +174,23 @@ fn test_json_query() {
115174
let query = InfluxDbQuery::raw_read_query("SELECT * FROM weather");
116175
let future = client
117176
.json_query(query)
118-
.map(|mut db_result| db_result.deserialize_next::<Weather>());
177+
.and_then(|mut db_result| db_result.deserialize_next::<Weather>());
119178
let result = get_runtime().block_on(future);
120179

121180
assert!(
122181
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+
}
126191
);
127192

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");
136194
}
137195

138196
#[test]
@@ -143,59 +201,80 @@ fn test_json_query() {
143201
fn test_serde_multi_query() {
144202
use serde::Deserialize;
145203

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 {
148214
time: String,
149215
temperature: i32,
150216
}
151217

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));
158232

159233
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())
164236
);
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));
177237

178238
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())
184241
);
185-
}
186242

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);
193255

194256
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())
200259
);
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

Comments
 (0)