Skip to content

Commit

Permalink
wip - tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tomhoule committed Feb 5, 2021
1 parent fb0e693 commit 7d2c117
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
use std::sync::Arc;

use migration_core::{
commands::{ApplyMigrationsInput, CreateMigrationInput},
GenericApi,
};
use tempfile::TempDir;
use test_macros::test_each_connector;
use test_setup::TestAPIArgs;

type TestResult = Result<(), anyhow::Error>;

struct TestApi {
args: TestAPIArgs,
source: String,
url: String,
}

impl TestApi {
/// Create a temporary directory to serve as a test migrations directory.
pub fn create_migrations_directory(&self) -> anyhow::Result<TempDir> {
Ok(tempfile::tempdir()?)
}

async fn new_engine(&self) -> anyhow::Result<Arc<dyn GenericApi>> {
Ok(migration_core::migration_api(&self.source).await?)
}

async fn initialize(&self) -> anyhow::Result<()> {
test_setup::create_postgres_database(&self.url.parse()?).await.unwrap();

Ok(())
}
}

async fn postgres9_test_api(args: TestAPIArgs) -> TestApi {
TestApi {
source: test_setup::postgres_9_test_config(&args.test_function_name),
url: test_setup::postgres_9_url(&args.test_function_name),
args,
}
}

async fn postgres_test_api(args: TestAPIArgs) -> TestApi {
TestApi {
source: test_setup::postgres_10_test_config(&args.test_function_name),
url: test_setup::postgres_10_url(&args.test_function_name),
args,
}
}

async fn postgres11_test_api(args: TestAPIArgs) -> TestApi {
TestApi {
source: test_setup::postgres_11_test_config(&args.test_function_name),
url: test_setup::postgres_11_url(&args.test_function_name),
args,
}
}

async fn postgres12_test_api(args: TestAPIArgs) -> TestApi {
TestApi {
source: test_setup::postgres_12_test_config(&args.test_function_name),
url: test_setup::postgres_12_url(&args.test_function_name),
args,
}
}

async fn postgres13_test_api(args: TestAPIArgs) -> TestApi {
TestApi {
source: test_setup::postgres_13_test_config(&args.test_function_name),
url: test_setup::postgres_13_url(&args.test_function_name),
args,
}
}

#[test_each_connector(tags("postgres"), log = "debug")]
async fn advisory_locking_works(api: &TestApi) -> TestResult {
api.initialize().await?;

let first_me = api.new_engine().await?;
let migrations_directory = api.create_migrations_directory()?;
let p = migrations_directory.path().to_string_lossy().into_owned();

let dm = r#"
model Cat {
id Int @id
inBox Boolean
}
"#;

let output = first_me
.create_migration(&CreateMigrationInput {
migrations_directory_path: p.clone(),
prisma_schema: dm.into(),
migration_name: "01initial".into(),
draft: true,
})
.await?;

let migration_name = output.generated_migration_name.expect("generated no migration");

let second_me = api.new_engine().await?;
let third_me = api.new_engine().await?;

let input = ApplyMigrationsInput {
migrations_directory_path: p,
};

let (result_1, result_2, result_3) = tokio::join!(
second_me.apply_migrations(&input),
first_me.apply_migrations(&input),
third_me.apply_migrations(&input),
);

panic!("here");
dbg!(result_1, result_2, result_3);

Ok(())
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod advisory_locking;
mod apply_script_tests;
mod dev_diagnostic_tests;
mod enums;
Expand Down

0 comments on commit 7d2c117

Please sign in to comment.