The start of unit testing and cleaner code for driver fetching :D
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
use crate::pog::SqlStruct;
|
||||
use actix_web::{get, web, HttpResponse, Responder};
|
||||
use async_trait::async_trait;
|
||||
use chrono::NaiveDate;
|
||||
use serde::{Deserialize, Serialize};
|
||||
@@ -37,3 +38,13 @@ impl SqlStruct<i32> for Driver {
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
use actix_web::{web, App, HttpServer};
|
||||
use dotenv::dotenv;
|
||||
use sqlx::postgres::PgPoolOptions;
|
||||
use std::env;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
pub mod admin_data;
|
||||
pub mod driver_data;
|
||||
pub mod pog;
|
||||
@@ -10,18 +14,25 @@ pub mod ticket_data;
|
||||
pub mod vehicle_data;
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> Result<(), sqlx::Error> {
|
||||
async fn main() -> std::io::Result<()> {
|
||||
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 pool = PgPoolOptions::new()
|
||||
.max_connections(10)
|
||||
.connect(database_url.as_str())
|
||||
.await?;
|
||||
let row: (i64,) = sqlx::query_as("SELECT $1 FROM drivers")
|
||||
.bind(150_i64)
|
||||
.fetch_one(&pool)
|
||||
.await?;
|
||||
assert_eq!(row.0, 150);
|
||||
println!("my nuts {}", row.0);
|
||||
Ok(())
|
||||
.await
|
||||
.expect("No pool connection man :(");
|
||||
HttpServer::new(move || {
|
||||
App::new()
|
||||
.app_data(web::Data::new(pool.clone()))
|
||||
.service(driver_data::list_all)
|
||||
})
|
||||
.bind((address.as_str(), port))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
||||
18
backend/src/tests.rs
Normal file
18
backend/src/tests.rs
Normal 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());
|
||||
}
|
||||
Reference in New Issue
Block a user