The start of unit testing and cleaner code for driver fetching :D

This commit is contained in:
LinlyBoi
2022-12-31 19:07:09 +02:00
parent ebe885e02d
commit 042867e0db
3 changed files with 49 additions and 9 deletions

View File

@@ -1,4 +1,5 @@
use crate::pog::SqlStruct; use crate::pog::SqlStruct;
use actix_web::{get, web, HttpResponse, Responder};
use async_trait::async_trait; use async_trait::async_trait;
use chrono::NaiveDate; use chrono::NaiveDate;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@@ -37,3 +38,13 @@ impl SqlStruct<i32> for Driver {
Ok(rec.id) Ok(rec.id)
} }
} }
#[get("/api/json/drivers")]
pub async fn list_all(db_pool: web::Data<PgPool>) -> impl Responder {
let drivers = sqlx::query_as!(Driver, "SELECT * FROM drivers")
.fetch_all(db_pool.get_ref())
.await
.expect("deez");
HttpResponse::Ok()
.content_type("application/json")
.json(drivers)
}

View File

@@ -1,7 +1,11 @@
use actix_web::{web, App, HttpServer};
use dotenv::dotenv; use dotenv::dotenv;
use sqlx::postgres::PgPoolOptions; use sqlx::postgres::PgPoolOptions;
use std::env; use std::env;
#[cfg(test)]
mod tests;
pub mod admin_data; pub mod admin_data;
pub mod driver_data; pub mod driver_data;
pub mod pog; pub mod pog;
@@ -10,18 +14,25 @@ pub mod ticket_data;
pub mod vehicle_data; pub mod vehicle_data;
#[actix_web::main] #[actix_web::main]
async fn main() -> Result<(), sqlx::Error> { async fn main() -> std::io::Result<()> {
dotenv().ok(); dotenv().ok();
let port: u16 = env::var("ACTIX_PORT")
.expect("SET ACTIX_PORT PLOX")
.parse()
.expect("NOPE CANT PARSE THIS WHAT DID YOU PUT IN?!!!");
let address = env::var("ACTIX_IP").expect("SET ACTIX_IP PLOX");
let database_url = env::var("DATABASE_URL").expect("Put a DB url in the .env file dumbass"); let database_url = env::var("DATABASE_URL").expect("Put a DB url in the .env file dumbass");
let pool = PgPoolOptions::new() let pool = PgPoolOptions::new()
.max_connections(10) .max_connections(10)
.connect(database_url.as_str()) .connect(database_url.as_str())
.await?; .await
let row: (i64,) = sqlx::query_as("SELECT $1 FROM drivers") .expect("No pool connection man :(");
.bind(150_i64) HttpServer::new(move || {
.fetch_one(&pool) App::new()
.await?; .app_data(web::Data::new(pool.clone()))
assert_eq!(row.0, 150); .service(driver_data::list_all)
println!("my nuts {}", row.0); })
Ok(()) .bind((address.as_str(), port))?
.run()
.await
} }

18
backend/src/tests.rs Normal file
View File

@@ -0,0 +1,18 @@
use super::*;
use actix_web::{
http::header::ContentType,
test::{self, init_service},
};
//This test actually should pass but I think this just needs the pool to be inserted idk
//TODO: Fix this
#[actix_web::test]
async fn test_list_drivers_ok() {
let app = tests::init_service(App::new().service(driver_data::list_all)).await;
let req = test::TestRequest::get()
.uri("/api/json/drivers")
.insert_header(ContentType::plaintext())
.to_request();
let resp = test::call_service(&app, req).await;
assert!(resp.status().is_success());
}