Skip to content

Commit 74d066f

Browse files
mpalmerjxs
andauthored
Allow Postgres tests to be run on a different database (#329)
* Allow Postgres tests to be run on a different database Not everyone has a "scratch" PostgreSQL running on localhost:5432 for refinery to scribble all over. Now you can specify an arbitrary PostgreSQL server to work on with the `DB_URI` environment variable (which appears to be what `refinery-cli` already uses) to test in. * Improve DB reset process * Use a more appropriate name for the function that does the work * Clean just the `public` schema, rather than drop/create the whole DB. This means that running the tests no longer requires superuser privs, and that we don't have to temporarily hide out in `template1`. * Drop the `catch_unwind`, because it's needed any more. --------- Co-authored-by: João Oliveira <[email protected]>
1 parent a2d6a61 commit 74d066f

File tree

1 file changed

+38
-69
lines changed

1 file changed

+38
-69
lines changed

refinery/tests/postgres.rs

Lines changed: 38 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@ mod postgres {
55
use assert_cmd::prelude::*;
66
use predicates::str::contains;
77
use refinery::{
8-
config::{Config, ConfigDbType},
9-
embed_migrations,
10-
error::Kind,
11-
Migrate, Migration, Runner, Target,
8+
config::Config, embed_migrations, error::Kind, Migrate, Migration, Runner, Target,
129
};
1310
use refinery_core::postgres::{Client, NoTls};
1411
use std::process::Command;
12+
use std::str::FromStr;
1513
use time::OffsetDateTime;
1614

1715
const DEFAULT_TABLE_NAME: &str = "refinery_schema_history";
@@ -31,6 +29,10 @@ mod postgres {
3129
embed_migrations!("./tests/migrations_missing");
3230
}
3331

32+
fn db_uri() -> String {
33+
std::env::var("DB_URI").unwrap_or("postgres://postgres@localhost:5432/postgres".to_string())
34+
}
35+
3436
fn get_migrations() -> Vec<Migration> {
3537
embed_migrations!("./tests/migrations");
3638

@@ -64,36 +66,32 @@ mod postgres {
6466
vec![migration1, migration2, migration3, migration4, migration5]
6567
}
6668

67-
fn clean_database() {
68-
let mut client =
69-
Client::connect("postgres://postgres@localhost:5432/template1", NoTls).unwrap();
69+
fn prep_database() {
70+
let uri = db_uri();
71+
72+
let mut client = Client::connect(&db_uri(), NoTls).unwrap();
7073

7174
client
72-
.execute(
73-
"SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname='postgres'",
74-
&[],
75-
)
75+
.execute("DROP SCHEMA IF EXISTS public CASCADE", &[])
76+
.unwrap();
77+
client
78+
.execute("CREATE SCHEMA IF NOT EXISTS public", &[])
7679
.unwrap();
77-
client.execute("DROP DATABASE POSTGRES", &[]).unwrap();
78-
client.execute("CREATE DATABASE POSTGRES", &[]).unwrap();
7980
}
8081

8182
fn run_test<T>(test: T)
8283
where
83-
T: FnOnce() + std::panic::UnwindSafe,
84+
T: FnOnce(),
8485
{
85-
let result = std::panic::catch_unwind(test);
86-
87-
clean_database();
86+
prep_database();
8887

89-
assert!(result.is_ok())
88+
test();
9089
}
9190

9291
#[test]
9392
fn report_contains_applied_migrations() {
9493
run_test(|| {
95-
let mut client =
96-
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
94+
let mut client = Client::connect(&db_uri(), NoTls).unwrap();
9795

9896
let report = embedded::migrations::runner().run(&mut client).unwrap();
9997

@@ -122,8 +120,7 @@ mod postgres {
122120
#[test]
123121
fn creates_migration_table() {
124122
run_test(|| {
125-
let mut client =
126-
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
123+
let mut client = Client::connect(&db_uri(), NoTls).unwrap();
127124
embedded::migrations::runner().run(&mut client).unwrap();
128125
for row in &client
129126
.query(
@@ -144,8 +141,7 @@ mod postgres {
144141
#[test]
145142
fn creates_migration_table_grouped_transaction() {
146143
run_test(|| {
147-
let mut client =
148-
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
144+
let mut client = Client::connect(&db_uri(), NoTls).unwrap();
149145

150146
embedded::migrations::runner()
151147
.set_grouped(true)
@@ -171,8 +167,7 @@ mod postgres {
171167
#[test]
172168
fn applies_migration() {
173169
run_test(|| {
174-
let mut client =
175-
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
170+
let mut client = Client::connect(&db_uri(), NoTls).unwrap();
176171
embedded::migrations::runner().run(&mut client).unwrap();
177172
client
178173
.execute(
@@ -192,8 +187,7 @@ mod postgres {
192187
#[test]
193188
fn applies_migration_grouped_transaction() {
194189
run_test(|| {
195-
let mut client =
196-
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
190+
let mut client = Client::connect(&db_uri(), NoTls).unwrap();
197191

198192
embedded::migrations::runner()
199193
.set_grouped(false)
@@ -218,8 +212,7 @@ mod postgres {
218212
#[test]
219213
fn updates_schema_history() {
220214
run_test(|| {
221-
let mut client =
222-
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
215+
let mut client = Client::connect(&db_uri(), NoTls).unwrap();
223216

224217
embedded::migrations::runner().run(&mut client).unwrap();
225218

@@ -239,8 +232,7 @@ mod postgres {
239232
#[test]
240233
fn updates_schema_history_grouped_transaction() {
241234
run_test(|| {
242-
let mut client =
243-
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
235+
let mut client = Client::connect(&db_uri(), NoTls).unwrap();
244236

245237
embedded::migrations::runner()
246238
.set_grouped(false)
@@ -262,8 +254,7 @@ mod postgres {
262254
#[test]
263255
fn updates_to_last_working_if_not_grouped() {
264256
run_test(|| {
265-
let mut client =
266-
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
257+
let mut client = Client::connect(&db_uri(), NoTls).unwrap();
267258

268259
let result = broken::migrations::runner().run(&mut client);
269260

@@ -300,8 +291,7 @@ mod postgres {
300291
#[test]
301292
fn doesnt_update_to_last_working_if_grouped() {
302293
run_test(|| {
303-
let mut client =
304-
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
294+
let mut client = Client::connect(&db_uri(), NoTls).unwrap();
305295

306296
let result = broken::migrations::runner()
307297
.set_grouped(true)
@@ -320,8 +310,7 @@ mod postgres {
320310
#[test]
321311
fn gets_applied_migrations() {
322312
run_test(|| {
323-
let mut client =
324-
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
313+
let mut client = Client::connect(&db_uri(), NoTls).unwrap();
325314

326315
embedded::migrations::runner().run(&mut client).unwrap();
327316

@@ -349,8 +338,7 @@ mod postgres {
349338
#[test]
350339
fn applies_new_migration() {
351340
run_test(|| {
352-
let mut client =
353-
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
341+
let mut client = Client::connect(&db_uri(), NoTls).unwrap();
354342

355343
embedded::migrations::runner().run(&mut client).unwrap();
356344

@@ -381,8 +369,7 @@ mod postgres {
381369
#[test]
382370
fn migrates_to_target_migration() {
383371
run_test(|| {
384-
let mut client =
385-
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
372+
let mut client = Client::connect(&db_uri(), NoTls).unwrap();
386373

387374
let report = embedded::migrations::runner()
388375
.set_target(Target::Version(3))
@@ -417,8 +404,7 @@ mod postgres {
417404
#[test]
418405
fn migrates_to_target_migration_grouped() {
419406
run_test(|| {
420-
let mut client =
421-
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
407+
let mut client = Client::connect(&db_uri(), NoTls).unwrap();
422408

423409
let report = embedded::migrations::runner()
424410
.set_target(Target::Version(3))
@@ -454,8 +440,7 @@ mod postgres {
454440
#[test]
455441
fn aborts_on_missing_migration_on_filesystem() {
456442
run_test(|| {
457-
let mut client =
458-
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
443+
let mut client = Client::connect(&db_uri(), NoTls).unwrap();
459444

460445
embedded::migrations::runner().run(&mut client).unwrap();
461446

@@ -488,8 +473,7 @@ mod postgres {
488473
#[test]
489474
fn aborts_on_divergent_migration() {
490475
run_test(|| {
491-
let mut client =
492-
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
476+
let mut client = Client::connect(&db_uri(), NoTls).unwrap();
493477

494478
embedded::migrations::runner().run(&mut client).unwrap();
495479

@@ -523,8 +507,7 @@ mod postgres {
523507
#[test]
524508
fn aborts_on_missing_migration_on_database() {
525509
run_test(|| {
526-
let mut client =
527-
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
510+
let mut client = Client::connect(&db_uri(), NoTls).unwrap();
528511

529512
missing::migrations::runner().run(&mut client).unwrap();
530513

@@ -568,11 +551,7 @@ mod postgres {
568551
#[test]
569552
fn migrates_from_config() {
570553
run_test(|| {
571-
let mut config = Config::new(ConfigDbType::Postgres)
572-
.set_db_name("postgres")
573-
.set_db_user("postgres")
574-
.set_db_host("localhost")
575-
.set_db_port("5432");
554+
let mut config = Config::from_str(&db_uri()).unwrap();
576555

577556
let migrations = get_migrations();
578557
let runner = Runner::new(&migrations)
@@ -608,11 +587,7 @@ mod postgres {
608587
#[test]
609588
fn migrate_from_config_report_contains_migrations() {
610589
run_test(|| {
611-
let mut config = Config::new(ConfigDbType::Postgres)
612-
.set_db_name("postgres")
613-
.set_db_user("postgres")
614-
.set_db_host("localhost")
615-
.set_db_port("5432");
590+
let mut config = Config::from_str(&db_uri()).unwrap();
616591

617592
let migrations = get_migrations();
618593
let runner = Runner::new(&migrations)
@@ -648,11 +623,7 @@ mod postgres {
648623
#[test]
649624
fn migrate_from_config_report_returns_last_applied_migration() {
650625
run_test(|| {
651-
let mut config = Config::new(ConfigDbType::Postgres)
652-
.set_db_name("postgres")
653-
.set_db_user("postgres")
654-
.set_db_host("localhost")
655-
.set_db_port("5432");
626+
let mut config = Config::from_str(&db_uri()).unwrap();
656627

657628
let migrations = get_migrations();
658629
let runner = Runner::new(&migrations)
@@ -677,8 +648,7 @@ mod postgres {
677648
#[test]
678649
fn doesnt_run_migrations_if_fake() {
679650
run_test(|| {
680-
let mut client =
681-
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
651+
let mut client = Client::connect(&db_uri(), NoTls).unwrap();
682652

683653
let report = embedded::migrations::runner()
684654
.set_target(Target::Fake)
@@ -712,8 +682,7 @@ mod postgres {
712682
#[test]
713683
fn doesnt_run_migrations_if_fake_version() {
714684
run_test(|| {
715-
let mut client =
716-
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
685+
let mut client = Client::connect(&db_uri(), NoTls).unwrap();
717686

718687
let report = embedded::migrations::runner()
719688
.set_target(Target::FakeVersion(2))

0 commit comments

Comments
 (0)