Separate db tests from unit tests

This commit is contained in:
Matthew Gordon 2024-03-05 13:59:28 -04:00
parent 04e4bc1f55
commit f9ac8b1e29
3 changed files with 45 additions and 19 deletions

8
dev.py
View File

@ -32,12 +32,20 @@ def unit_tests(args):
import_run().unit_tests(args) import_run().unit_tests(args)
def database_tests(args):
import_run().database_tests(args)
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(required=True) subparsers = parser.add_subparsers(required=True)
run_parser = subparsers.add_parser( run_parser = subparsers.add_parser(
'run', help='Run a test instance of locality' 'run', help='Run a test instance of locality'
) )
run_parser.set_defaults(func=run) run_parser.set_defaults(func=run)
run_parser = subparsers.add_parser(
'dbtest', help='Run database tests'
)
run_parser.set_defaults(func=database_tests)
run_parser = subparsers.add_parser( run_parser = subparsers.add_parser(
'unittest', help='Run unit tests' 'unittest', help='Run unit tests'
) )

View File

@ -8,7 +8,33 @@ from .postgres_container import PostgresContainer
ROOT_DIR = None ROOT_DIR = None
def cargo(*args): def cargo(*args, env=None):
global ROOT_DIR
if env is None:
env = {
'LOCALITY_DATABASE_URL': "",
'LOCALITY_TEST_DATABASE_URL': "",
'LOCALITY_STATIC_FILE_PATH': os.path.join(
ROOT_DIR,
'static'),
'LOCALITY_HMAC_SECRET': 'iknf4390-8guvmr3'
}
env = os.environ.copy() | env
cargo_bin = shutil.which('cargo')
locality_process = subprocess.Popen(
[cargo_bin] + list(args), env=env, cwd=ROOT_DIR
)
try:
while locality_process.poll() is None:
time.sleep(0.5)
except KeyboardInterrupt:
pass
finally:
if locality_process.poll() is None:
locality_process.terminate()
def cargo_with_db(*args):
global ROOT_DIR global ROOT_DIR
with PostgresContainer() as postgres: with PostgresContainer() as postgres:
locality_env = { locality_env = {
@ -19,26 +45,16 @@ def cargo(*args):
'static'), 'static'),
'LOCALITY_HMAC_SECRET': 'iknf4390-8guvmr3' 'LOCALITY_HMAC_SECRET': 'iknf4390-8guvmr3'
} }
locality_env = os.environ.copy() | locality_env cargo(env=locality_env, *args)
cargo_bin = shutil.which('cargo')
locality_process = subprocess.Popen(
[cargo_bin, *args], env=locality_env, cwd=ROOT_DIR
)
try:
while locality_process.poll() is None:
time.sleep(0.5)
except KeyboardInterrupt:
pass
finally:
if locality_process.poll() is None:
locality_process.terminate()
def run(args): def run(*args):
cargo('run') cargo_with_db('run')
def unit_tests(args): def database_tests(*args):
cargo_with_db("test", "db::migrations::test", "--", "--include-ignored")
def unit_tests(*args):
cargo("test") cargo("test")

View File

@ -220,6 +220,7 @@ mod tests {
PostgresDatabase::new(&url).unwrap() PostgresDatabase::new(&url).unwrap()
} }
#[ignore]
#[test] #[test]
fn migrations_have_sequential_versions() { fn migrations_have_sequential_versions() {
for i in 0..MIGRATIONS.len() { for i in 0..MIGRATIONS.len() {
@ -227,6 +228,7 @@ mod tests {
} }
} }
#[ignore]
#[tokio::test] #[tokio::test]
async fn migrate_up_and_down_all() { async fn migrate_up_and_down_all() {
let db = test_db().await; let db = test_db().await;