Skip to content

Commit 08dbffd

Browse files
committed
test(bats): use random database names instead of the hardcoded "unittest"
Previously if a database named "unittest" pre-existed, it was dropped and recreated unconditionally. Now the tests generate a random database name, and use the environment variable PPM_DATABASE to refer to it. Additionally, PGDATABASE is used for the partition manager instead of a hardcoded URL in the template file. Signed-off-by: Daniel Vérité <[email protected]>
1 parent 8fc1619 commit 08dbffd

File tree

7 files changed

+53
-30
lines changed

7 files changed

+53
-30
lines changed

scripts/bats/20_check.bats

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ load 'test/libs/seeds'
55
load 'test/libs/sql'
66

77
setup_file() {
8-
reset_database
8+
init_database
9+
}
10+
11+
teardown_file() {
12+
drop_database
913
}
1014

1115
setup() {

scripts/bats/30_provisioning.bats

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ load 'test/libs/sql'
66
load 'test/libs/time'
77

88
setup_file() {
9-
reset_database
9+
init_database
10+
}
11+
12+
teardown_file() {
13+
drop_database
1014
}
1115

1216
setup() {
@@ -237,7 +241,7 @@ EOF
237241
assert_success
238242
assert_output --partial "All partitions are correctly provisioned"
239243

240-
run list_existing_partitions "unittest" "public" ${TABLE}
244+
run list_existing_partitions "public" ${TABLE}
241245

242246
local expected_monthly=$(cat <<EOF
243247
public|test_interv_2024_12|2024-12-01|2025-01-01
@@ -270,7 +274,7 @@ public|test_interv_2025_w15|2025-04-07|2025-04-14
270274
EOF
271275
)
272276

273-
run list_existing_partitions "unittest" "public" ${TABLE}
277+
run list_existing_partitions "public" ${TABLE}
274278
assert_output "$expected_mix"
275279

276280
rm "$CONFIGURATION_FILE"
@@ -311,7 +315,7 @@ public|table_unittest1_2025_02_01|2025-02-01|2025-02-02
311315
public|table_unittest1_2025_02_02|2025-02-02|2025-02-03
312316
EOF
313317
)
314-
run list_existing_partitions "unittest" "public" "table_unittest1"
318+
run list_existing_partitions "public" "table_unittest1"
315319
assert_output "$expected1"
316320

317321
local expected2=$(cat <<'EOF'
@@ -323,7 +327,7 @@ public|table_unittest2_2025_02_03|2025-02-03|2025-02-04
323327
EOF
324328
)
325329

326-
run list_existing_partitions "unittest" "public" "table_unittest2"
330+
run list_existing_partitions "public" "table_unittest2"
327331
assert_output "$expected2"
328332

329333
rm "$CONFIGURATION_FILE"
@@ -373,7 +377,7 @@ public|${TABLE}_2025_02_03|2025-02-03|2025-02-04
373377
EOF
374378
)
375379

376-
run list_existing_partitions "unittest" "public" "${TABLE}"
380+
run list_existing_partitions "public" "${TABLE}"
377381
assert_output "$expected2"
378382

379383
rm "$CONFIGURATION_FILE"
@@ -406,7 +410,7 @@ public|${TABLE}_2025_w12|2025-03-17 00:00:00+01|2025-03-24 00:00:00+01
406410
EOF
407411
)
408412

409-
run list_existing_partitions "unittest" "public" ${TABLE}
413+
run list_existing_partitions "public" ${TABLE}
410414
assert_output "$expected_1"
411415

412416
# Now advance one week up to the end of March 2025
@@ -426,7 +430,7 @@ public|${TABLE}_2025_w12|2025-03-17 00:00:00+01|2025-03-24 00:00:00+01
426430
public|${TABLE}_2025_w13|2025-03-24 00:00:00+01|2025-03-31 00:00:00+02
427431
EOF
428432
)
429-
run list_existing_partitions "unittest" "public" ${TABLE}
433+
run list_existing_partitions "public" ${TABLE}
430434
assert_output "$expected_2"
431435

432436
rm "$CONFIGURATION_FILE"

scripts/bats/40_cleanup.bats

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ load 'test/libs/seeds'
55
load 'test/libs/sql'
66

77
setup_file() {
8-
reset_database
8+
init_database
9+
}
10+
11+
teardown_file() {
12+
drop_database
913
}
1014

1115
setup() {
@@ -119,7 +123,7 @@ EOF
119123
expected+="public|${PARTS[i]}|${PARTS[i+1]}|${PARTS[i+2]}"
120124
(( i+=3 ))
121125
done
122-
run list_existing_partitions "unittest" "public" "${TABLE}"
126+
run list_existing_partitions "public" "${TABLE}"
123127

124128
assert_output "$expected"
125129

scripts/bats/configuration/template.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ debug: true
33

44
log-format: text
55

6-
connection-url: postgres://postgres:hackme@localhost/unittest
6+
#connection-url: postgres://user:password@localhost/dbname

scripts/bats/test/libs/partitions.bash

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ generate_daily_partition() {
4141
local UPPER_BOUND=$(date -d "@$(( $(date +%s) + 86400 * $TIMEDELTA + 86400))" +"%Y-%m-%d")
4242

4343
local QUERY="CREATE TABLE ${TABLE_NAME} PARTITION OF ${PARENT_TABLE} FOR VALUES FROM ('${LOWER_BOUND}') TO ('${UPPER_BOUND}');"
44-
execute_sql "${QUERY}"
44+
execute_sql "${QUERY}" "$PPM_DATABASE"
4545
}
4646

4747
generate_daily_partition_name() {
@@ -85,5 +85,5 @@ create_partitions() {
8585
done
8686
sql_block="$sql_block
8787
COMMIT;";
88-
execute_sql_commands "$sql_block"
88+
execute_sql_commands "$sql_block" "$PPM_DATABASE"
8989
}

scripts/bats/test/libs/seeds.bash

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@ random_suffix() {
55
}
66

77
init_database() {
8-
QUERY="CREATE DATABASE unittest;"
8+
local dbname="ppm_test_"$(random_suffix)
9+
QUERY="CREATE DATABASE \"$dbname\" ;"
910
execute_sql "${QUERY}" postgres
11+
export PPM_DATABASE="$dbname"
12+
export PGDATABASE="$dbname"
1013
}
1114

1215
drop_database() {
13-
QUERY="set lock_timeout to '5s'; DROP DATABASE IF EXISTS unittest;"
16+
QUERY="set lock_timeout to '5s'; DROP DATABASE IF EXISTS \"$PPM_DATABASE\" ;"
1417
execute_sql_commands "${QUERY}" postgres
18+
unset PPM_DATABASE
1519
}
1620

1721
reset_database() {
@@ -29,7 +33,7 @@ create_table_from_template() {
2933
created_at DATE NOT NULL
3034
) PARTITION BY RANGE (created_at);
3135
EOQ
32-
execute_sql "${QUERY}"
36+
execute_sql "${QUERY}" "${PPM_DATABASE}"
3337
}
3438

3539
create_table_uuid_range() {

scripts/bats/test/libs/sql.bash

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ execute_sql() {
22
local SQL_COMMAND=$1
33
local DATABASE=$2
44

5-
if [ -z ${DATABASE} ]; then
5+
if [ -z "$DATABASE" ]; then
66
psql --echo-all --echo-errors -c "${SQL_COMMAND}"
77
else
88
psql --echo-all --echo-errors --dbname="${DATABASE}" -c "${SQL_COMMAND}"
@@ -13,7 +13,7 @@ execute_sql_file() {
1313
local SQL_FILE=$1
1414
local DATABASE=$2
1515

16-
if [ -z ${DATABASE} ]; then
16+
if [ -z "$DATABASE" ]; then
1717
psql --echo-all --echo-errors -f "${SQL_FILE}"
1818
else
1919
psql --echo-all --echo-errors --dbname="${DATABASE}" -f "${SQL_FILE}"
@@ -25,16 +25,23 @@ execute_sql_commands() {
2525
# $1: sql commands
2626
# $2: dbname (if unset, psql defaults to $PGDATABASE and then $USER)
2727

28-
psql --tuples-only --no-align --quiet "$2" <<EOSQL
28+
local command="psql --tuples-only --no-align --quiet"
29+
30+
if [ ! -z "$2" ]; then
31+
command="$command --dbname=$2"
32+
fi
33+
34+
$command <<EOSQL
2935
$1
3036
EOSQL
3137
}
3238

3339
list_existing_partitions() {
34-
local DATABASE=$1
35-
local PARENT_SCHEMA=$2
36-
local PARENT_TABLE=$3
37-
PGDATABASE="$DATABASE" psql --tuples-only --no-align --quiet -v parent_schema=${PARENT_SCHEMA} -v parent_table=${PARENT_TABLE} <<'EOSQL'
40+
# mandatory arguments
41+
local PARENT_SCHEMA=$1
42+
local PARENT_TABLE=$2
43+
44+
psql --tuples-only --no-align --quiet --dbname="$PPM_DATABASE" -v parent_schema=${PARENT_SCHEMA} -v parent_table=${PARENT_TABLE} <<'EOSQL'
3845
WITH parts as (
3946
SELECT
4047
relnamespace::regnamespace as schema,
@@ -58,8 +65,8 @@ EOSQL
5865
}
5966

6067
assert_table_exists() {
61-
local SCHEMA=$1
62-
local TABLE=$2
68+
local SCHEMA="$1"
69+
local TABLE="$2"
6370

6471
local QUERY="SELECT EXISTS (
6572
SELECT
@@ -68,15 +75,15 @@ assert_table_exists() {
6875
AND table_name = '${TABLE}'
6976
);"
7077

71-
run psql --tuples-only --no-align -c "${QUERY}"
78+
run psql --dbname="$PPM_DATABASE" --tuples-only --no-align -c "${QUERY}"
7279

7380
assert_success
7481
assert_output t
7582
}
7683

7784
assert_table_not_exists() {
78-
local SCHEMA=$1
79-
local TABLE=$2
85+
local SCHEMA="$1"
86+
local TABLE="$2"
8087

8188
local QUERY="SELECT EXISTS (
8289
SELECT
@@ -85,7 +92,7 @@ assert_table_not_exists() {
8592
AND table_name = '${TABLE}'
8693
);"
8794

88-
run psql --tuples-only --no-align -c "${QUERY}"
95+
run psql --dbname="$PPM_DATABASE" --tuples-only --no-align -c "${QUERY}"
8996

9097
assert_success
9198
assert_output f

0 commit comments

Comments
 (0)