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,57 +41,104 @@ 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 bot = db::bots::create_bot(
|
||||||
let config = Arc::new(GlobalConfig {
|
&db::bots::NewBot {
|
||||||
database_url: "postgresql://planetwars:planetwars@localhost/planetwars-test".to_string(),
|
owner_id: None,
|
||||||
python_runner_image: "python:3.10-slim-buster".to_string(),
|
name: "simplebot",
|
||||||
container_registry_url: "localhost:9001".to_string(),
|
},
|
||||||
root_url: "localhost:3000".to_string(),
|
&db_conn,
|
||||||
bots_directory: create_subdir(data_dir.path(), "bots")?,
|
)
|
||||||
match_logs_directory: create_subdir(data_dir.path(), "matches")?,
|
.expect("could not create simplebot");
|
||||||
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 simplebot_code = std::fs::read_to_string("../simplebot/simplebot.py")
|
||||||
&db::bots::NewBot {
|
.expect("could not read simplebot code");
|
||||||
owner_id: None,
|
let _bot_version =
|
||||||
name: "simplebot",
|
modules::bots::save_code_string(&simplebot_code, Some(bot.id), &db_conn, &config)
|
||||||
},
|
.expect("could not save bot version");
|
||||||
&db_conn,
|
|
||||||
)
|
|
||||||
.expect("could not create simplebot");
|
|
||||||
|
|
||||||
let simplebot_code = std::fs::read_to_string("../simplebot/simplebot.py")
|
std::fs::copy(
|
||||||
.expect("could not read simplebot code");
|
"../maps/hex.json",
|
||||||
let _bot_version =
|
PathBuf::from(&config.maps_directory).join("hex.json"),
|
||||||
modules::bots::save_code_string(&simplebot_code, Some(bot.id), &db_conn, &config)
|
)
|
||||||
.expect("could not save bot version");
|
.expect("could not copy map");
|
||||||
|
db::maps::create_map(
|
||||||
|
db::maps::NewMap {
|
||||||
|
name: "hex",
|
||||||
|
file_path: "hex.json",
|
||||||
|
},
|
||||||
|
&db_conn,
|
||||||
|
)
|
||||||
|
.expect("could not save map");
|
||||||
|
}
|
||||||
|
|
||||||
std::fs::copy(
|
struct TestApp<'a> {
|
||||||
"../maps/hex.json",
|
// exclusive connection to the test database
|
||||||
PathBuf::from(&config.maps_directory).join("hex.json"),
|
#[allow(dead_code)]
|
||||||
)
|
db_guard: parking_lot::MutexGuard<'a, ()>,
|
||||||
.expect("could not copy map");
|
db_pool: DbPool,
|
||||||
db::maps::create_map(
|
|
||||||
db::maps::NewMap {
|
// temporary data directory
|
||||||
name: "hex",
|
#[allow(dead_code)]
|
||||||
file_path: "hex.json",
|
data_dir: TempDir,
|
||||||
},
|
|
||||||
&db_conn,
|
config: Arc<GlobalConfig>,
|
||||||
)
|
}
|
||||||
.expect("could not save map");
|
|
||||||
|
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,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
let mut app = create_pw_api(config, 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