Skip to content

Commit

Permalink
Fix integration tests
Browse files Browse the repository at this point in the history
Signed-off-by: Heinz Gies <[email protected]>
  • Loading branch information
Licenser committed Aug 19, 2024
1 parent c4f5a2d commit 5a12885
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ tracing-subscriber = { version = "0.3", features = ["ansi", "env-filter"] }
cli-table = "0.4.9"

[features]
default = ["tokio", "default-tls"]
default = ["tokio", "default-tls", "integration-tests"]
tokio = ["backoff/tokio", "dep:tokio"]
async-std = ["backoff/async-std", "dep:async-std"]
default-tls = ["reqwest/default-tls"]
Expand Down
8 changes: 0 additions & 8 deletions src/datasets/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,14 +591,6 @@ pub struct QueryResult {

/// The tables that were queried.
pub tables: Vec<table::Table>,
// /// The datasets that were queried.
// #[serde(default, deserialize_with = "deserialize_null_default")]
// pub dataset_names: Vec<String>,
// /// The events that matched the query.
// #[serde(default, deserialize_with = "deserialize_null_default")]
// pub matches: Vec<Entry>,
// /// The time series buckets.
// pub buckets: Timeseries,
/// The ID of the query that generated this result when it was saved on the
/// server. This is only set when the query was send with the `SaveKind`
/// option specified.
Expand Down
28 changes: 21 additions & 7 deletions src/datasets/model/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,12 +267,13 @@ impl Table {
&self.columns
}

pub fn rows(&self) -> usize {
self.columns.iter().map(Vec::len).max().unwrap_or(0)
/// Returns the maximum length of the first column
pub fn len(&self) -> usize {
self.columns.first().map(Vec::len).unwrap_or_default()
}

pub fn get_row(&self, row: usize) -> Option<Row> {
if self.rows() > row {
if self.len() > row {
Some(Row { table: self, row })
} else {
None
Expand Down Expand Up @@ -301,23 +302,23 @@ impl<'table> Iterator for RowIter<'table> {
}

fn size_hint(&self) -> (usize, Option<usize>) {
let size = self.table.rows();
let size = self.table.len();
(size - self.row, Some(size - self.row))
}

fn count(self) -> usize
where
Self: Sized,
{
self.table.rows() - self.row
self.table.len() - self.row
}

fn last(self) -> Option<Self::Item>
where
Self: Sized,
{
if self.table.rows() > 0 {
self.table.get_row(self.table.rows() - 1)
if self.table.len() > 0 {
self.table.get_row(self.table.len() - 1)
} else {
None
}
Expand All @@ -331,6 +332,19 @@ pub struct Row<'table> {
}

impl<'table> Row<'table> {
/// Returns the value of the row by name
pub fn get_field(&self, field: &str) -> Option<&JsonValue> {
let mut index = None;

for (i, f) in self.table.fields.iter().enumerate() {
if f.name() == field {
index = Some(i);
break;
}
}

self.get(index?)
}
/// Returns the value of the row.
pub fn get(&self, column: usize) -> Option<&JsonValue> {
self.table.columns.get(column).and_then(|c| c.get(self.row))
Expand Down
11 changes: 8 additions & 3 deletions tests/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,13 @@ async fn test_cursor_impl(ctx: &mut Context) {
.await
.unwrap();
assert!(apl_query_result.saved_query_id.is_some());
assert_eq!(1000, apl_query_result.matches.len());
assert_eq!(1000, apl_query_result.tables[0].len());

let mid_row_id = &apl_query_result.matches[500].row_id;
let table = &apl_query_result.tables[0];

let row = table.get_row(500).unwrap();

let mid_row_id = &row.get_field("row_id").unwrap();

let apl_query_result = ctx
.client
Expand All @@ -138,5 +142,6 @@ async fn test_cursor_impl(ctx: &mut Context) {
.await
.unwrap();
assert!(apl_query_result.saved_query_id.is_some());
assert_eq!(500, apl_query_result.matches.len());
assert_eq!(500, apl_query_result.tables.len());
assert_eq!(500, apl_query_result.tables[0].len());
}
15 changes: 5 additions & 10 deletions tests/datasets.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#![cfg(feature = "integration-tests")]
use axiom_rs::{datasets::*, Client};
use chrono::{Duration, Utc};
use futures::StreamExt;
use serde_json::json;
use std::{env, time::Duration as StdDuration};
Expand Down Expand Up @@ -43,7 +42,7 @@ impl AsyncTestContext for Context {
#[test_context(Context)]
#[tokio::test]
async fn test_datasets(ctx: &mut Context) -> Result<(), Box<dyn std::error::Error>> {
Ok(test_datasets_impl(ctx).await?)
test_datasets_impl(ctx).await
}
#[cfg(feature = "async-std")]
#[test_context(Context)]
Expand Down Expand Up @@ -79,7 +78,7 @@ async fn test_datasets_impl(ctx: &mut Context) -> Result<(), Box<dyn std::error:
.expect("Expected dataset to be in the list");

// Let's ingest some data
const PAYLOAD: &'static str = r#"[
const PAYLOAD: &str = r#"[
{
"time": "17/May/2015:08:05:30 +0000",
"remote_ip": "93.180.71.1",
Expand Down Expand Up @@ -164,7 +163,7 @@ async fn test_datasets_impl(ctx: &mut Context) -> Result<(), Box<dyn std::error:
let info = ctx.client.datasets().info(&ctx.dataset.name).await?;

Check warning on line 163 in tests/datasets.rs

View workflow job for this annotation

GitHub Actions / Run tests with Tokio on development

use of deprecated method `axiom_rs::datasets::Client::<'client>::info`: The info method will go away in the future, but come back in a different version.
assert_eq!(ctx.dataset.name, info.stat.name);
assert_eq!(4327, info.stat.num_events);
assert!(info.fields.len() > 0);
assert!(!info.fields.is_empty());

// Run another query but using APL.
let apl_query_result = ctx
Expand All @@ -181,12 +180,8 @@ async fn test_datasets_impl(ctx: &mut Context) -> Result<(), Box<dyn std::error:
// assert_eq!(1, apl_query_result.status.blocks_examined);
assert_eq!(4327, apl_query_result.status.rows_examined);
assert_eq!(4327, apl_query_result.status.rows_matched);
assert_eq!(1000, apl_query_result.matches.len());
assert_eq!(2, apl_query_result.tables.len());
assert_eq!(1000, apl_query_result.tables[0].len());

// Trim the dataset down to a minimum.
ctx.client
.datasets()
.trim(&ctx.dataset.name, Duration::seconds(1))
.await?;
Ok(())
}

0 comments on commit 5a12885

Please sign in to comment.