Skip to content

Commit 6c6f29b

Browse files
committed
Merge branch 'master' of github.com:Empty2k12/influxdb-rust
2 parents 2e530b2 + 1d86c5d commit 6c6f29b

File tree

8 files changed

+140
-61
lines changed

8 files changed

+140
-61
lines changed

.travis.yml

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
language: rust
2+
3+
sudo: required
4+
5+
before_install:
6+
- wget https://dl.influxdata.com/influxdb/releases/influxdb_1.7.6_amd64.deb
7+
- sudo dpkg -i influxdb_1.7.6_amd64.deb
8+
- sudo influxd > $HOME/influx.log 2>&1 &
9+
10+
branches:
11+
only:
12+
- master
13+
14+
cache:
15+
directories:
16+
- /home/travis/.cargo
17+
18+
before_cache:
19+
- rm -rf /home/travis/.cargo/registry
20+
21+
env:
22+
os:
23+
- linux
24+
rust:
25+
- stable
26+
- beta
27+
- nightly
28+
29+
matrix:
30+
fast_finish: true
31+
allow_failures:
32+
include:
33+
- rust: stable
34+
env: NAME='linting'
35+
before_script:
36+
- rustup component add rustfmt-preview
37+
- rustup component add clippy-preview
38+
script:
39+
- cargo fmt --all -- --check
40+
- cargo clippy
41+
42+
- env: NAME='cargo-travis'
43+
sudo: required
44+
before_script:
45+
- cargo install cargo-update || echo "cargo-update already installed"
46+
- cargo install cargo-travis || echo "cargo-travis already installed"
47+
- cargo install-update -a
48+
script:
49+
- |
50+
cargo build &&
51+
cargo coverage &&
52+
wget https://github.com/SimonKagstrom/kcov/archive/master.tar.gz &&
53+
tar xzf master.tar.gz && mkdir kcov-master/build && cd kcov-master/build && cmake .. && make &&
54+
sudo make install && cd ../.. &&
55+
kcov --coveralls-id=$TRAVIS_JOB_ID --exclude-pattern=/.cargo target/kcov target/debug/influxdb-*
56+
addons:
57+
apt:
58+
packages:
59+
- libcurl4-openssl-dev
60+
- libelf-dev
61+
- libdw-dev
62+
- binutils-dev
63+
- cmake
64+
65+
script: |
66+
export RUST_BACKTRACE=1 &&
67+
cargo build &&
68+
cargo test &&
69+
cargo doc --no-deps

src/client/mod.rs

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,7 @@ impl InfluxDbClient {
120120
{
121121
use futures::future;
122122

123-
let query_type = q.get_type();
124-
let endpoint = match query_type {
125-
QueryType::ReadQuery => "query",
126-
QueryType::WriteQuery => "write",
127-
};
128-
123+
let q_type = q.get_type();
129124
let query = match q.build() {
130125
Err(err) => {
131126
let error = InfluxDbError::InvalidQueryError {
@@ -136,34 +131,32 @@ impl InfluxDbClient {
136131
Ok(query) => query,
137132
};
138133

139-
let query_str = query.get();
140-
let url_params = match query_type {
141-
QueryType::ReadQuery => format!("&q={}", query_str),
142-
QueryType::WriteQuery => String::from(""),
143-
};
144-
145-
let client = match query_type {
146-
QueryType::ReadQuery => Client::new().get(
147-
format!(
148-
"{url}/{endpoint}?db={db}{url_params}",
149-
url = self.url,
150-
endpoint = endpoint,
151-
db = self.database,
152-
url_params = url_params
153-
)
154-
.as_str(),
155-
),
134+
let client = match q_type {
135+
QueryType::ReadQuery => {
136+
let read_query = query.get();
137+
let http_query_string = format!(
138+
"{url}/query?db={db}&q={read_query}",
139+
url = self.database_url(),
140+
db = self.database_name(),
141+
read_query = read_query,
142+
);
143+
144+
if read_query.contains("SELECT") || read_query.contains("SHOW") {
145+
Client::new().get(http_query_string.as_str())
146+
} else {
147+
Client::new().post(http_query_string.as_str())
148+
}
149+
}
156150
QueryType::WriteQuery => Client::new()
157151
.post(
158152
format!(
159-
"{url}/{endpoint}?db={db}",
160-
url = self.url,
161-
endpoint = endpoint,
162-
db = self.database,
153+
"{url}/write?db={db}",
154+
url = self.database_url(),
155+
db = self.database_name(),
163156
)
164157
.as_str(),
165158
)
166-
.body(query_str),
159+
.body(query.get()),
167160
};
168161

169162
Box::new(

src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ pub enum InfluxDbError {
1717
#[fail(display = "InfluxDB encountered the following error: {}", error)]
1818
/// Error which has happened inside InfluxDB
1919
DatabaseError { error: String },
20-
}
20+
}

src/integrations/serde_integration.rs

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,7 @@ impl InfluxDbClient {
8787
{
8888
use futures::future;
8989

90-
let query_type = q.get_type();
91-
let endpoint = match query_type {
92-
QueryType::ReadQuery => "query",
93-
QueryType::WriteQuery => "write",
94-
};
95-
90+
let q_type = q.get_type();
9691
let query = match q.build() {
9792
Err(err) => {
9893
let error = InfluxDbError::InvalidQueryError {
@@ -105,34 +100,32 @@ impl InfluxDbClient {
105100
Ok(query) => query,
106101
};
107102

108-
let query_str = query.get();
109-
let url_params = match query_type {
110-
QueryType::ReadQuery => format!("&q={}", query_str),
111-
QueryType::WriteQuery => String::from(""),
112-
};
113-
114-
let client = match query_type {
115-
QueryType::ReadQuery => Client::new().get(
116-
format!(
117-
"{url}/{endpoint}?db={db}{url_params}",
103+
let client = match q_type {
104+
QueryType::ReadQuery => {
105+
let read_query = query.get();
106+
let http_query_string = format!(
107+
"{url}/query?db={db}&q={read_query}",
118108
url = self.database_url(),
119-
endpoint = endpoint,
120109
db = self.database_name(),
121-
url_params = url_params
122-
)
123-
.as_str(),
124-
),
110+
read_query = read_query,
111+
);
112+
113+
if read_query.contains("SELECT") || read_query.contains("SHOW") {
114+
Client::new().get(http_query_string.as_str())
115+
} else {
116+
Client::new().post(http_query_string.as_str())
117+
}
118+
}
125119
QueryType::WriteQuery => Client::new()
126120
.post(
127121
format!(
128-
"{url}/{endpoint}?db={db}",
122+
"{url}/write?db={db}",
129123
url = self.database_url(),
130-
endpoint = endpoint,
131124
db = self.database_name(),
132125
)
133126
.as_str(),
134127
)
135-
.body(query_str),
128+
.body(query.get()),
136129
};
137130

138131
Box::new(
@@ -169,4 +162,4 @@ impl InfluxDbClient {
169162
}),
170163
)
171164
}
172-
}
165+
}

src/query/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
//! Used to create queries of type [`InfluxDbReadQuery`](crate::query::read_query::InfluxDbReadQuery) or [`InfluxDbWriteQuery`](crate::query::write_query::InfluxDbWriteQuery) which can be executed in InfluxDB
1+
//! Used to create queries of type [`InfluxDbReadQuery`](crate::query::read_query::InfluxDbReadQuery) or
2+
//! [`InfluxDbWriteQuery`](crate::query::write_query::InfluxDbWriteQuery) which can be executed in InfluxDB
23
//!
34
//! # Examples
45
//!
@@ -103,4 +104,4 @@ impl PartialEq<&str> for ValidQuery {
103104
pub enum QueryType {
104105
ReadQuery,
105106
WriteQuery,
106-
}
107+
}

src/query/read_query.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ impl InfluxDbQuery for InfluxDbReadQuery {
3030
fn get_type(&self) -> QueryType {
3131
QueryType::ReadQuery
3232
}
33-
}
33+
}

src/query/write_query.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,4 @@ impl InfluxDbQuery for InfluxDbWriteQuery {
151151
fn get_type(&self) -> QueryType {
152152
QueryType::WriteQuery
153153
}
154-
}
154+
}

tests/integration_tests.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@ fn test_ping_influx_db() {
2828
println!("build: {} version: {}", build, version);
2929
}
3030

31+
#[test]
32+
/// INTEGRATION TEST
33+
///
34+
/// Tests if a database can be created
35+
fn test_create_database() {
36+
let client = create_client();
37+
let query = InfluxDbQuery::raw_read_query("CREATE DATABASE test");
38+
let result = get_runtime().block_on(client.query(query));
39+
assert!(
40+
result.is_ok(),
41+
format!("Should be no error: {}", result.unwrap_err())
42+
);
43+
}
44+
3145
#[test]
3246
/// INTEGRATION TEST
3347
///
@@ -36,7 +50,10 @@ fn test_write_field() {
3650
let client = create_client();
3751
let query = InfluxDbQuery::write_query("weather").add_field("temperature", 82);
3852
let result = get_runtime().block_on(client.query(query));
39-
assert!(result.is_ok(), "Should be no error");
53+
assert!(
54+
result.is_ok(),
55+
format!("Should be no error: {}", result.unwrap_err())
56+
);
4057
}
4158

4259
#[test]
@@ -47,7 +64,10 @@ fn test_read() {
4764
let client = create_client();
4865
let query = InfluxDbQuery::raw_read_query("SELECT * FROM weather");
4966
let result = get_runtime().block_on(client.query(query));
50-
assert!(result.is_ok(), "Should be no error");
67+
assert!(
68+
result.is_ok(),
69+
format!("Should be no error: {}", result.unwrap_err())
70+
);
5171
assert!(
5272
!result.unwrap().contains("error"),
5373
"Data contained a database error"
@@ -72,5 +92,8 @@ fn test_json_query() {
7292
let query = InfluxDbQuery::raw_read_query("SELECT * FROM weather");
7393
let result = get_runtime().block_on(client.json_query::<Weather, _>(query));
7494

75-
assert!(result.is_ok(), "We could read from the DB");
95+
assert!(
96+
result.is_ok(),
97+
format!("We couldn't read from the DB: {}", result.unwrap_err())
98+
);
7699
}

0 commit comments

Comments
 (0)