Skip to content

Commit

Permalink
Merge pull request #65 from aprimadi/workflow/integration_tests
Browse files Browse the repository at this point in the history
Added integration Test Step to CI, clippy code cleanup
  • Loading branch information
Charles-Schleich authored Jul 19, 2024
2 parents e17b7c0 + 4a74241 commit d705dfd
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 48 deletions.
17 changes: 16 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ jobs:

build:
runs-on: ubuntu-latest
needs: typos
steps:
- name: Clone this repository
uses: actions/checkout@v4
Expand Down Expand Up @@ -50,5 +51,19 @@ jobs:
- name: Build
run: cargo build --verbose

test:
name: Run Tests
runs-on: ubuntu-latest
needs: build
steps:
- name: Clone this repository
uses: actions/checkout@v4

- name: Rust Toolchain
uses: actions-rust-lang/setup-rust-toolchain@v1

- name: Update Stable Rust toolchain
run: rustup update stable

- name: Run tests
run: cargo test --verbose
run: TEST_INTEGRATION=true cargo test --verbose
14 changes: 7 additions & 7 deletions examples/measurement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,29 @@ async fn main() -> Result<(), Box<dyn Error>> {

for m in measurements.iter() {
let field_keys = client
.list_measurement_field_keys(bucket, &m, Some("-365d"), Some("now()"))
.list_measurement_field_keys(bucket, m, Some("-365d"), Some("now()"))
.await
.unwrap();
println!("field keys: {:?}", field_keys);
}

for m in measurements.iter() {
let tag_values = client
.list_measurement_tag_values(bucket, &m, "host", Some("-365d"), None)
.list_measurement_tag_values(bucket, m, "host", Some("-365d"), None)
.await;
println!(
"tag values for measurement {} and tag {}: {:?}",
&m, "host", tag_values
"tag values for measurement {} and tag host: {:?}",
&m, tag_values
);
}

for m in measurements.iter() {
let tag_values = client
.list_measurement_tag_keys(bucket, &m, Some("-365d"), None)
.list_measurement_tag_keys(bucket, m, Some("-365d"), None)
.await;
println!(
"tag values for measurement {} and tag {}: {:?}",
&m, "host", tag_values
"tag values for measurement {} and tag host: {:?}",
&m, tag_values
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/api/authorizations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl Client {
return res;
}

Ok(response.json().await.context(ReqwestProcessing)?)
response.json().await.context(ReqwestProcessing)
}

/// Create a new authorization in the organization.
Expand Down
4 changes: 2 additions & 2 deletions src/api/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ use std::collections::HashMap;
impl Client {
/// List all Labels
pub async fn labels(&self) -> Result<LabelsResponse, RequestError> {
Ok(self.get_labels(None).await?)
self.get_labels(None).await
}

/// List all Labels by organization ID
pub async fn labels_by_org(&self, org_id: &str) -> Result<LabelsResponse, RequestError> {
Ok(self.get_labels(Some(org_id)).await?)
self.get_labels(Some(org_id)).await
}

async fn get_labels(&self, org_id: Option<&str>) -> Result<LabelsResponse, RequestError> {
Expand Down
59 changes: 24 additions & 35 deletions src/api/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,18 @@ pub struct QueryTableIter {
}

impl<'a> QueryTableIter {
fn new(text: String) -> QueryTableIter {
return QueryTableIter { text };
fn new(text: String) -> Self {
Self { text }
}

/// Get the iterator
pub fn result(
self: &'a QueryTableIter,
) -> impl FallibleIterator<Item = FluxRecord, Error = RequestError> + 'a {
pub fn result(&'a self) -> impl FallibleIterator<Item = FluxRecord, Error = RequestError> + 'a {
QueryTableResult::new(&self.text)
}

/// Is the response empty?
pub fn is_empty(self: &QueryTableIter) -> bool {
match QueryTableResult::new(&self.text).next() {
Ok(None) => true,
_ => false,
}
pub fn is_empty(&self) -> bool {
matches!(QueryTableResult::new(&self.text).next(), Ok(None))
}
}

Expand Down Expand Up @@ -402,18 +397,17 @@ impl Client {

match response.status() {
StatusCode::OK => {
let text = response.text().await.unwrap();
let text: String = response.text().await.unwrap();

let mut reader = csv::ReaderBuilder::new()
.has_headers(true)
.comment(Some(b'#'))
.from_reader(text.as_bytes());

Ok(reader
.records()
.into_iter()
.flatten()
.map(|r| r.get(3).map(|s| s.to_owned()))
.flatten()
.flat_map(|r: StringRecord| r.get(3).map(|s| s.to_owned()))
.collect())
}
status => {
Expand All @@ -439,17 +433,17 @@ enum DataType {
impl FromStr for DataType {
type Err = RequestError;

fn from_str(input: &str) -> Result<DataType, RequestError> {
fn from_str(input: &str) -> Result<Self, RequestError> {
match input {
"string" => Ok(DataType::String),
"double" => Ok(DataType::Double),
"boolean" => Ok(DataType::Bool),
"long" => Ok(DataType::Long),
"unsignedLong" => Ok(DataType::UnsignedLong),
"duration" => Ok(DataType::Duration),
"base64Binary" => Ok(DataType::Base64Binary),
"dateTime:RFC3339" => Ok(DataType::TimeRFC),
"dateTime:RFC3339Nano" => Ok(DataType::TimeRFC),
"string" => Ok(Self::String),
"double" => Ok(Self::Double),
"boolean" => Ok(Self::Bool),
"long" => Ok(Self::Long),
"unsignedLong" => Ok(Self::UnsignedLong),
"duration" => Ok(Self::Duration),
"base64Binary" => Ok(Self::Base64Binary),
"dateTime:RFC3339" => Ok(Self::TimeRFC),
"dateTime:RFC3339Nano" => Ok(Self::TimeRFC),
_ => Err(RequestError::Deserializing {
text: format!("unknown datatype: {}", input),
}),
Expand Down Expand Up @@ -528,7 +522,7 @@ impl<'a> FallibleIterator for QueryTableResult<'a> {
continue;
}
if let Some(s) = row.get(0) {
if s.len() > 0 && s.chars().nth(0).unwrap() == '#' {
if !s.is_empty() && s.chars().nth(0).unwrap() == '#' {
// Finding new table, prepare for annotation parsing
if parsing_state == ParsingState::Normal {
self.table = Some(FluxTableMetadata {
Expand Down Expand Up @@ -587,13 +581,13 @@ impl<'a> FallibleIterator for QueryTableResult<'a> {
continue;
}
ParsingState::Error => {
let msg = if row.len() > 1 && row.get(1).unwrap().len() > 0 {
let msg = if row.len() > 1 && !row.get(1).unwrap().is_empty() {
row.get(1).unwrap()
} else {
"unknown query error"
};
let mut reference = String::from("");
if row.len() > 2 && row.get(2).unwrap().len() > 0 {
if row.len() > 2 && !row.get(2).unwrap().is_empty() {
let s = row.get(2).unwrap();
reference = format!(",{}", s);
}
Expand All @@ -610,7 +604,7 @@ impl<'a> FallibleIterator for QueryTableResult<'a> {
if v.is_empty() {
v = &column.default_value[..];
}
let value = parse_value(v, column.data_type, &column.name.as_str())?;
let value = parse_value(v, column.data_type, column.name.as_str())?;
values.entry(column.name.clone()).or_insert(value);
}
record = FluxRecord {
Expand Down Expand Up @@ -656,7 +650,7 @@ struct QueryResult {
}

impl QueryResult {
fn new<'a>(qtr: QueryTableResult<'a>) -> Result<Self, RequestError> {
fn new(qtr: QueryTableResult<'_>) -> Result<Self, RequestError> {
let ignored_keys = vec!["_field", "_value", "table"];
let ignored_keys: HashSet<&str> = ignored_keys.into_iter().collect();

Expand Down Expand Up @@ -828,18 +822,13 @@ mod tests {
async fn query_opt() {
let token = "some-token";
let org = "some-org";
let query: Option<Query> = None;

let mock_server = mock("POST", "/api/v2/query")
.match_header("Authorization", format!("Token {}", token).as_str())
.match_header("Accepting-Encoding", "identity")
.match_header("Content-Type", "application/json")
.match_query(Matcher::UrlEncoded("org".into(), org.into()))
.match_body(
serde_json::to_string(&query.unwrap_or_default())
.unwrap()
.as_str(),
)
.match_body(serde_json::to_string(&Query::default()).unwrap().as_str())
.create();

let client = Client::new(mockito::server_url(), org, token);
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,8 @@ impl ClientBuilder {
};

let url: String = url.into();
let base = Url::parse(&url).expect(&format!("Invalid url was provided: {}", &url));
let base =
Url::parse(&url).unwrap_or_else(|_| panic!("Invalid url was provided: {}", &url));

Self {
base,
Expand Down
4 changes: 3 additions & 1 deletion tests/common/server_fixture.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::await_holding_lock)]

use once_cell::sync::OnceCell;
use std::{
fs::File,
Expand Down Expand Up @@ -223,7 +225,7 @@ impl TestServer {
.arg("--pull")
.arg("always")
.arg("--detach")
.arg(&ci_image)
.arg(ci_image)
.arg("influxd")
.output()
.expect("starting of docker server process");
Expand Down

0 comments on commit d705dfd

Please sign in to comment.