Skip to content

Commit

Permalink
Update lard_tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Lun4m committed Jun 11, 2024
1 parent bd25259 commit dc57179
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 22 deletions.
27 changes: 26 additions & 1 deletion lard_tests/tests/api_integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ async fn api_test_wrapper<T: Future<Output = ()>>(test: T) {
}

#[tokio::test]
async fn test_stations_endpoint() {
async fn test_stations_endpoint_irregular() {
api_test_wrapper(async {
// NOTE: tseries.header.totime == Utc.now()
let station_id = 18;
Expand All @@ -36,6 +36,31 @@ async fn test_stations_endpoint() {
.await
}

// #[tokio::test]
// TODO: this generates a bunch of nulls
async fn test_stations_endpoint_regular() {
api_test_wrapper(async {
// NOTE: tseries.header.totime == Utc.now()
let station_id = 18;
let param_id = 103;
let query = "?time_resolution=P1D";

let url = format!(
"http://localhost:3000/stations/{}/params/{}{}",
station_id, param_id, query
);
let resp = reqwest::get(url).await.unwrap();
assert!(resp.status().is_success());

let json = resp.text().await.unwrap();
println!("\n{}\n", json);
// assert_eq!(json, expected);

// TODO: do something else with the response
})
.await
}

#[tokio::test]
async fn test_latest_endpoint() {
api_test_wrapper(async {
Expand Down
17 changes: 15 additions & 2 deletions lard_tests/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ use tokio::task::JoinHandle;
use tokio_postgres::NoTls;

use lard_ingestion::permissions::{ParamPermit, ParamPermitTable, StationPermitTable};
use lard_ingestion::PgConnectionPool;
use lard_ingestion::{PgConnectionPool, PooledPgConn};

const CONNECT_STRING: &str = "host=localhost user=postgres dbname=postgres password=postgres";
const PARAMCONV_CSV: &str = "../ingestion/resources/paramconversions.csv";
pub const PARAMCONV_CSV: &str = "../ingestion/resources/paramconversions.csv";

pub async fn init_api_server() -> JoinHandle<()> {
tokio::spawn(lard_api::run(CONNECT_STRING))
Expand Down Expand Up @@ -45,6 +45,19 @@ pub fn mock_permit_tables() -> Arc<RwLock<(ParamPermitTable, StationPermitTable)
Arc::new(RwLock::new((param_permit, station_permit)))
}

pub async fn number_of_data_rows(conn: &PooledPgConn<'_>, ts_id: i32) -> usize {
let rows = conn
.query(
"SELECT * FROM public.data
WHERE timeseries = $1",
&[&ts_id],
)
.await
.unwrap();

rows.len()
}

pub async fn init_ingestion_server(
) -> JoinHandle<Result<(), Box<dyn std::error::Error + Send + Sync>>> {
tokio::spawn(lard_ingestion::run(
Expand Down
24 changes: 5 additions & 19 deletions lard_tests/tests/ingestion_integration_test.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,18 @@
use chrono::{TimeZone, Utc};
use test_case::test_case;

use common::{init_db_pool, mock_permit_tables};
use lard_ingestion::{insert_data, permissions::timeseries_is_open, Data, Datum, PooledPgConn};
use lard_ingestion::{insert_data, permissions::timeseries_is_open, Data, Datum};

pub mod common;

async fn get_number_of_rows(conn: &PooledPgConn<'_>, id: i32) -> usize {
let rows = conn
.query(
"SELECT * FROM public.data
WHERE timeseries = $1",
&[&id],
)
.await
.unwrap();

rows.len()
}

#[tokio::test]
async fn test_insert_data() {
let pool = init_db_pool().await.unwrap();
let pool = common::init_db_pool().await.unwrap();
let mut conn = pool.get().await.unwrap();

// Timeseries ID 2 has hourly data
let id: i32 = 2;
let count_before_insertion = get_number_of_rows(&conn, id).await;
let count_before_insertion = common::number_of_data_rows(&conn, id).await;

let data: Data = (1..10)
.map(|i| {
Expand All @@ -38,7 +24,7 @@ async fn test_insert_data() {

insert_data(data, &mut conn).await.unwrap();

let count_after_insertion = get_number_of_rows(&conn, id).await;
let count_after_insertion = common::number_of_data_rows(&conn, id).await;
let rows_inserted = count_after_insertion - count_before_insertion;

// This makes sure the assert doesn't fail locally if the database hasn't been cleaned up between runs
Expand All @@ -53,7 +39,7 @@ async fn test_insert_data() {
#[test_case(4, 0, 1 => true; "stationid in StationPermitTable, timeseries open")]
#[test_case(2, 0, 0 => true; "stationid in ParamPermitTable, timeseries open")]
fn test_timeseries_is_open(station_id: i32, type_id: i32, permit_id: i32) -> bool {
let permit_tables = mock_permit_tables();
let permit_tables = common::mock_permit_tables();
timeseries_is_open(permit_tables, station_id, type_id, permit_id).unwrap()
}

Expand Down

0 comments on commit dc57179

Please sign in to comment.