diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..768874a --- /dev/null +++ b/.travis.yml @@ -0,0 +1,69 @@ +language: rust + +sudo: required + +before_install: + - wget https://dl.influxdata.com/influxdb/releases/influxdb_1.7.6_amd64.deb + - sudo dpkg -i influxdb_1.7.6_amd64.deb + - sudo influxd > $HOME/influx.log 2>&1 & + +branches: + only: + - master + +cache: + directories: + - /home/travis/.cargo + +before_cache: + - rm -rf /home/travis/.cargo/registry + +env: +os: + - linux +rust: + - stable + - beta + - nightly + +matrix: + fast_finish: true + allow_failures: + include: + - rust: stable + env: NAME='linting' + before_script: + - rustup component add rustfmt-preview + - rustup component add clippy-preview + script: + - cargo fmt --all -- --check + - cargo clippy + + - env: NAME='cargo-travis' + sudo: required + before_script: + - cargo install cargo-update || echo "cargo-update already installed" + - cargo install cargo-travis || echo "cargo-travis already installed" + - cargo install-update -a + script: + - | + cargo build && + cargo coverage && + wget https://github.com/SimonKagstrom/kcov/archive/master.tar.gz && + tar xzf master.tar.gz && mkdir kcov-master/build && cd kcov-master/build && cmake .. && make && + sudo make install && cd ../.. && + kcov --coveralls-id=$TRAVIS_JOB_ID --exclude-pattern=/.cargo target/kcov target/debug/influxdb-* + addons: + apt: + packages: + - libcurl4-openssl-dev + - libelf-dev + - libdw-dev + - binutils-dev + - cmake + +script: | + export RUST_BACKTRACE=1 && + cargo build && + cargo test && + cargo doc --no-deps diff --git a/src/client/mod.rs b/src/client/mod.rs index a86bc24..a72c547 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -120,12 +120,7 @@ impl InfluxDbClient { { use futures::future; - let query_type = q.get_type(); - let endpoint = match query_type { - QueryType::ReadQuery => "query", - QueryType::WriteQuery => "write", - }; - + let q_type = q.get_type(); let query = match q.build() { Err(err) => { let error = InfluxDbError::InvalidQueryError { @@ -136,34 +131,32 @@ impl InfluxDbClient { Ok(query) => query, }; - let query_str = query.get(); - let url_params = match query_type { - QueryType::ReadQuery => format!("&q={}", query_str), - QueryType::WriteQuery => String::from(""), - }; - - let client = match query_type { - QueryType::ReadQuery => Client::new().get( - format!( - "{url}/{endpoint}?db={db}{url_params}", - url = self.url, - endpoint = endpoint, - db = self.database, - url_params = url_params - ) - .as_str(), - ), + let client = match q_type { + QueryType::ReadQuery => { + let read_query = query.get(); + let http_query_string = format!( + "{url}/query?db={db}&q={read_query}", + url = self.database_url(), + db = self.database_name(), + read_query = read_query, + ); + + if read_query.contains("SELECT") || read_query.contains("SHOW") { + Client::new().get(http_query_string.as_str()) + } else { + Client::new().post(http_query_string.as_str()) + } + } QueryType::WriteQuery => Client::new() .post( format!( - "{url}/{endpoint}?db={db}", - url = self.url, - endpoint = endpoint, - db = self.database, + "{url}/write?db={db}", + url = self.database_url(), + db = self.database_name(), ) .as_str(), ) - .body(query_str), + .body(query.get()), }; Box::new( diff --git a/src/error.rs b/src/error.rs index ce4fc6b..17caf1a 100644 --- a/src/error.rs +++ b/src/error.rs @@ -17,4 +17,4 @@ pub enum InfluxDbError { #[fail(display = "InfluxDB encountered the following error: {}", error)] /// Error which has happened inside InfluxDB DatabaseError { error: String }, -} \ No newline at end of file +} diff --git a/src/integrations/serde_integration.rs b/src/integrations/serde_integration.rs index ac0e0e2..54fa8e6 100644 --- a/src/integrations/serde_integration.rs +++ b/src/integrations/serde_integration.rs @@ -87,12 +87,7 @@ impl InfluxDbClient { { use futures::future; - let query_type = q.get_type(); - let endpoint = match query_type { - QueryType::ReadQuery => "query", - QueryType::WriteQuery => "write", - }; - + let q_type = q.get_type(); let query = match q.build() { Err(err) => { let error = InfluxDbError::InvalidQueryError { @@ -105,34 +100,32 @@ impl InfluxDbClient { Ok(query) => query, }; - let query_str = query.get(); - let url_params = match query_type { - QueryType::ReadQuery => format!("&q={}", query_str), - QueryType::WriteQuery => String::from(""), - }; - - let client = match query_type { - QueryType::ReadQuery => Client::new().get( - format!( - "{url}/{endpoint}?db={db}{url_params}", + let client = match q_type { + QueryType::ReadQuery => { + let read_query = query.get(); + let http_query_string = format!( + "{url}/query?db={db}&q={read_query}", url = self.database_url(), - endpoint = endpoint, db = self.database_name(), - url_params = url_params - ) - .as_str(), - ), + read_query = read_query, + ); + + if read_query.contains("SELECT") || read_query.contains("SHOW") { + Client::new().get(http_query_string.as_str()) + } else { + Client::new().post(http_query_string.as_str()) + } + } QueryType::WriteQuery => Client::new() .post( format!( - "{url}/{endpoint}?db={db}", + "{url}/write?db={db}", url = self.database_url(), - endpoint = endpoint, db = self.database_name(), ) .as_str(), ) - .body(query_str), + .body(query.get()), }; Box::new( @@ -169,4 +162,4 @@ impl InfluxDbClient { }), ) } -} \ No newline at end of file +} diff --git a/src/query/mod.rs b/src/query/mod.rs index 6527f11..2d8288e 100644 --- a/src/query/mod.rs +++ b/src/query/mod.rs @@ -1,4 +1,5 @@ -//! 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 +//! 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 //! //! # Examples //! @@ -103,4 +104,4 @@ impl PartialEq<&str> for ValidQuery { pub enum QueryType { ReadQuery, WriteQuery, -} \ No newline at end of file +} diff --git a/src/query/read_query.rs b/src/query/read_query.rs index 52b9c0b..027668e 100644 --- a/src/query/read_query.rs +++ b/src/query/read_query.rs @@ -30,4 +30,4 @@ impl InfluxDbQuery for InfluxDbReadQuery { fn get_type(&self) -> QueryType { QueryType::ReadQuery } -} \ No newline at end of file +} diff --git a/src/query/write_query.rs b/src/query/write_query.rs index 5030166..ad89a82 100644 --- a/src/query/write_query.rs +++ b/src/query/write_query.rs @@ -151,4 +151,4 @@ impl InfluxDbQuery for InfluxDbWriteQuery { fn get_type(&self) -> QueryType { QueryType::WriteQuery } -} \ No newline at end of file +} diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index cc0c19e..bbf72a0 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -28,6 +28,20 @@ fn test_ping_influx_db() { println!("build: {} version: {}", build, version); } +#[test] +/// INTEGRATION TEST +/// +/// Tests if a database can be created +fn test_create_database() { + let client = create_client(); + let query = InfluxDbQuery::raw_read_query("CREATE DATABASE test"); + let result = get_runtime().block_on(client.query(query)); + assert!( + result.is_ok(), + format!("Should be no error: {}", result.unwrap_err()) + ); +} + #[test] /// INTEGRATION TEST /// @@ -36,7 +50,10 @@ fn test_write_field() { let client = create_client(); let query = InfluxDbQuery::write_query("weather").add_field("temperature", 82); let result = get_runtime().block_on(client.query(query)); - assert!(result.is_ok(), "Should be no error"); + assert!( + result.is_ok(), + format!("Should be no error: {}", result.unwrap_err()) + ); } #[test] @@ -47,7 +64,10 @@ fn test_read() { let client = create_client(); let query = InfluxDbQuery::raw_read_query("SELECT * FROM weather"); let result = get_runtime().block_on(client.query(query)); - assert!(result.is_ok(), "Should be no error"); + assert!( + result.is_ok(), + format!("Should be no error: {}", result.unwrap_err()) + ); assert!( !result.unwrap().contains("error"), "Data contained a database error" @@ -72,5 +92,8 @@ fn test_json_query() { let query = InfluxDbQuery::raw_read_query("SELECT * FROM weather"); let result = get_runtime().block_on(client.json_query::(query)); - assert!(result.is_ok(), "We could read from the DB"); + assert!( + result.is_ok(), + format!("We couldn't read from the DB: {}", result.unwrap_err()) + ); }