Skip to content

Commit

Permalink
Add common functions for integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Lun4m committed Jun 17, 2024
1 parent 1d448a9 commit 469ba38
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
12 changes: 12 additions & 0 deletions ingestion/src/permissions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ pub struct ParamPermit {
permit_id: i32,
}

// Only used in tests
#[doc(hidden)]
impl ParamPermit {
pub fn new(type_id: i32, param_id: i32, permit_id: i32) -> ParamPermit {
ParamPermit {
type_id,
param_id,
permit_id,
}
}
}

type StationId = i32;
/// This integer is used like an enum in stinfosys to define who data can be shared with. For
/// details on what each number means, refer to the `permit` table in stinfosys. Here we mostly
Expand Down
120 changes: 120 additions & 0 deletions lard_tests/tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
use std::{
collections::HashMap,
sync::{Arc, RwLock},
};

use bb8::Pool;
use bb8_postgres::PostgresConnectionManager;
use chrono::{DateTime, Utc};
use serde::Deserialize;
use tokio::task::JoinHandle;
use tokio_postgres::NoTls;

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

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

#[derive(Debug, Deserialize)]
pub struct IngestorResponse {
pub message: String,
pub message_id: usize,
pub res: u8,
pub retry: bool,
}

#[derive(Debug, Deserialize)]
pub struct StationsResponse {
pub tseries: Vec<Tseries>,
}

#[derive(Debug, Deserialize)]
pub struct Tseries {
pub regularity: String,
pub data: Vec<f64>,
// header: ...
}

#[derive(Debug, Deserialize)]
pub struct LatestResponse {
pub data: Vec<Data>,
}

#[derive(Debug, Deserialize)]
pub struct Data {
// TODO: Missing param_id here?
pub value: f64,
pub timestamp: DateTime<Utc>,
pub station_id: i32,
// loc: {lat, lon, hamsl, hag}
}

#[derive(Debug, Deserialize)]
pub struct TimesliceResponse {
pub tslices: Vec<Tslice>,
}

#[derive(Debug, Deserialize)]
pub struct Tslice {
pub timestamp: DateTime<Utc>,
pub param_id: i32,
pub data: Vec<f64>,
}

pub async fn init_api_server() -> JoinHandle<()> {
tokio::spawn(lard_api::run(CONNECT_STRING))
}

pub async fn init_db_pool() -> Result<PgConnectionPool, tokio_postgres::Error> {
let manager = PostgresConnectionManager::new_from_stringlike(CONNECT_STRING, NoTls)?;
let pool = Pool::builder().build(manager).await?;
Ok(pool)
}

pub fn mock_permit_tables() -> Arc<RwLock<(ParamPermitTable, StationPermitTable)>> {
let param_permit = HashMap::from([
// station_id -> (type_id, param_id, permit_id)
(1, vec![ParamPermit::new(0, 0, 0)]),
(2, vec![ParamPermit::new(0, 0, 1)]), // open
]);

let station_permit = HashMap::from([
// station_id -> permit_id
(1, 0),
(2, 0),
(3, 0),
(4, 1), // open
// used in e2e tests
(20000, 0),
(11000, 1),
(12000, 1),
(12100, 1),
(13000, 1),
(40000, 1),
]);

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(
CONNECT_STRING,
PARAMCONV_CSV,
mock_permit_tables(),
))
}

0 comments on commit 469ba38

Please sign in to comment.