refactor: separate out test setup
This commit is contained in:
parent
790da3cf1a
commit
45d6172814
1 changed files with 95 additions and 48 deletions
|
@ -3,7 +3,7 @@ use axum::{
|
||||||
http::{self, Request, StatusCode},
|
http::{self, Request, StatusCode},
|
||||||
};
|
};
|
||||||
use diesel::{PgConnection, RunQueryDsl};
|
use diesel::{PgConnection, RunQueryDsl};
|
||||||
use planetwars_server::{create_db_pool, create_pw_api, db, modules, GlobalConfig};
|
use planetwars_server::{create_db_pool, create_pw_api, db, modules, DbPool, GlobalConfig};
|
||||||
use serde_json::{self, json, Value as JsonValue};
|
use serde_json::{self, json, Value as JsonValue};
|
||||||
use std::{
|
use std::{
|
||||||
io,
|
io,
|
||||||
|
@ -41,27 +41,9 @@ fn clear_database(conn: &PgConnection) {
|
||||||
.expect("failed to clear database");
|
.expect("failed to clear database");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test(flavor = "multi_thread")]
|
/// Setup a simple text fixture, having simplebot and the hex map.
|
||||||
async fn test_application() -> io::Result<()> {
|
/// This is enough to run a simple match.
|
||||||
let _db_guard = DB_LOCK.lock();
|
fn setup_simple_fixture(db_conn: &PgConnection, config: &GlobalConfig) {
|
||||||
let data_dir = TempDir::new().expect("failed to create temp dir");
|
|
||||||
let config = Arc::new(GlobalConfig {
|
|
||||||
database_url: "postgresql://planetwars:planetwars@localhost/planetwars-test".to_string(),
|
|
||||||
python_runner_image: "python:3.10-slim-buster".to_string(),
|
|
||||||
container_registry_url: "localhost:9001".to_string(),
|
|
||||||
root_url: "localhost:3000".to_string(),
|
|
||||||
bots_directory: create_subdir(data_dir.path(), "bots")?,
|
|
||||||
match_logs_directory: create_subdir(data_dir.path(), "matches")?,
|
|
||||||
maps_directory: create_subdir(data_dir.path(), "maps")?,
|
|
||||||
registry_directory: create_subdir(data_dir.path(), "registry")?,
|
|
||||||
registry_admin_password: "secret_admin_password".to_string(),
|
|
||||||
ranker_enabled: false,
|
|
||||||
});
|
|
||||||
let db_pool = create_db_pool(&config).await;
|
|
||||||
{
|
|
||||||
let db_conn = db_pool.get().await.expect("failed to get db connection");
|
|
||||||
clear_database(&db_conn);
|
|
||||||
|
|
||||||
let bot = db::bots::create_bot(
|
let bot = db::bots::create_bot(
|
||||||
&db::bots::NewBot {
|
&db::bots::NewBot {
|
||||||
owner_id: None,
|
owner_id: None,
|
||||||
|
@ -91,7 +73,72 @@ async fn test_application() -> io::Result<()> {
|
||||||
)
|
)
|
||||||
.expect("could not save map");
|
.expect("could not save map");
|
||||||
}
|
}
|
||||||
let mut app = create_pw_api(config, db_pool);
|
|
||||||
|
struct TestApp<'a> {
|
||||||
|
// exclusive connection to the test database
|
||||||
|
#[allow(dead_code)]
|
||||||
|
db_guard: parking_lot::MutexGuard<'a, ()>,
|
||||||
|
db_pool: DbPool,
|
||||||
|
|
||||||
|
// temporary data directory
|
||||||
|
#[allow(dead_code)]
|
||||||
|
data_dir: TempDir,
|
||||||
|
|
||||||
|
config: Arc<GlobalConfig>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> TestApp<'a> {
|
||||||
|
async fn create() -> io::Result<TestApp<'a>> {
|
||||||
|
let data_dir = TempDir::new().expect("failed to create temp dir");
|
||||||
|
|
||||||
|
let config = Arc::new(GlobalConfig {
|
||||||
|
database_url: "postgresql://planetwars:planetwars@localhost/planetwars-test"
|
||||||
|
.to_string(),
|
||||||
|
python_runner_image: "python:3.10-slim-buster".to_string(),
|
||||||
|
container_registry_url: "localhost:9001".to_string(),
|
||||||
|
root_url: "localhost:3000".to_string(),
|
||||||
|
bots_directory: create_subdir(data_dir.path(), "bots")?,
|
||||||
|
match_logs_directory: create_subdir(data_dir.path(), "matches")?,
|
||||||
|
maps_directory: create_subdir(data_dir.path(), "maps")?,
|
||||||
|
registry_directory: create_subdir(data_dir.path(), "registry")?,
|
||||||
|
registry_admin_password: "secret_admin_password".to_string(),
|
||||||
|
ranker_enabled: false,
|
||||||
|
});
|
||||||
|
let db_guard = DB_LOCK.lock();
|
||||||
|
let db_pool = create_db_pool(&config).await;
|
||||||
|
|
||||||
|
Ok(TestApp {
|
||||||
|
db_guard,
|
||||||
|
config,
|
||||||
|
data_dir,
|
||||||
|
db_pool,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn with_db_conn<F, R>(&self, function: F) -> R
|
||||||
|
where
|
||||||
|
F: FnOnce(&PgConnection) -> R,
|
||||||
|
{
|
||||||
|
let db_conn = self
|
||||||
|
.db_pool
|
||||||
|
.get()
|
||||||
|
.await
|
||||||
|
.expect("could not get db connection");
|
||||||
|
function(&db_conn)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test(flavor = "multi_thread")]
|
||||||
|
async fn test_submit_bot() -> io::Result<()> {
|
||||||
|
let test_app = TestApp::create().await.unwrap();
|
||||||
|
test_app
|
||||||
|
.with_db_conn(|db_conn| {
|
||||||
|
clear_database(db_conn);
|
||||||
|
setup_simple_fixture(db_conn, &test_app.config);
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
|
||||||
|
let mut app = create_pw_api(test_app.config, test_app.db_pool);
|
||||||
|
|
||||||
let simplebot_code = std::fs::read_to_string("../simplebot/simplebot.py")
|
let simplebot_code = std::fs::read_to_string("../simplebot/simplebot.py")
|
||||||
.expect("could not read simplebot code");
|
.expect("could not read simplebot code");
|
||||||
|
|
Loading…
Reference in a new issue