Skip to content

Commit bbe3df6

Browse files
authored
Merge pull request #63 from qonto/improve-tests
Improve bats tests
2 parents 2b90a38 + 08dbffd commit bbe3df6

File tree

7 files changed

+84
-34
lines changed

7 files changed

+84
-34
lines changed

scripts/bats/20_check.bats

Lines changed: 8 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() {
@@ -49,6 +53,7 @@ EOF
4953

5054
assert_success
5155
assert_output --partial "All partitions are correctly configured"
56+
rm "$CONFIGURATION_FILE"
5257
}
5358

5459
@test "Test check return an error when retention partitions are missing" {
@@ -79,6 +84,7 @@ EOF
7984

8085
assert_failure
8186
assert_output --partial "Found missing tables"
87+
rm "$CONFIGURATION_FILE"
8288
}
8389

8490
@test "Test check return an error when preProvisioned partitions are missing" {
@@ -109,6 +115,7 @@ EOF
109115

110116
assert_failure
111117
assert_output --partial "Found missing tables"
118+
rm "$CONFIGURATION_FILE"
112119
}
113120

114121
@test "Test check succeeding with an UUID partition key" {

scripts/bats/30_provisioning.bats

Lines changed: 29 additions & 9 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() {
@@ -45,6 +49,8 @@ EOF
4549
assert_table_exists public $(generate_daily_partition_name ${TABLE} -1) # retention partition
4650
assert_table_exists public $(generate_daily_partition_name ${TABLE} 0) # current partition
4751
assert_table_exists public $(generate_daily_partition_name ${TABLE} 1) # preProvisioned partition
52+
53+
rm "$CONFIGURATION_FILE"
4854
}
4955

5056
@test "Test that preProvisioned and retention partitions can be increased" {
@@ -90,6 +96,8 @@ EOF
9096
assert_output --partial "All partitions are correctly provisioned"
9197
assert_table_exists public $(generate_daily_partition_name ${TABLE} -${NEW_RETENTION}) # New retention partition
9298
assert_table_exists public $(generate_daily_partition_name ${TABLE} ${NEW_PREPROVISIONED}) # New preProvisioned partition
99+
100+
rm "$CONFIGURATION_FILE"
93101
}
94102

95103
@test "Test monthly partitions" {
@@ -127,6 +135,8 @@ EOF
127135
assert_table_exists public ${EXPECTED_LAST_TABLE}
128136
assert_table_exists public ${EXPECTED_CURRENT_TABLE}
129137
assert_table_exists public ${EXPECTED_NEXT_TABLE}
138+
139+
rm "$CONFIGURATION_FILE"
130140
}
131141

132142
@test "Test quarterly partitions" {
@@ -157,12 +167,13 @@ EOF
157167

158168
run "$PPM_PROG" run provisioning -c ${CONFIGURATION_FILE}
159169

160-
161170
assert_success
162171
assert_output --partial "All partitions are correctly provisioned"
163172
assert_table_exists public ${EXPECTED_LAST_TABLE}
164173
assert_table_exists public ${EXPECTED_CURRENT_TABLE}
165174
assert_table_exists public ${EXPECTED_NEXT_TABLE}
175+
176+
rm "$CONFIGURATION_FILE"
166177
}
167178

168179
@test "Test yearly partitions" {
@@ -198,6 +209,8 @@ EOF
198209
assert_table_exists public ${EXPECTED_LAST_TABLE}
199210
assert_table_exists public ${EXPECTED_CURRENT_TABLE}
200211
assert_table_exists public ${EXPECTED_NEXT_TABLE}
212+
213+
rm "$CONFIGURATION_FILE"
201214
}
202215

203216
@test "Test interval change" {
@@ -228,7 +241,7 @@ EOF
228241
assert_success
229242
assert_output --partial "All partitions are correctly provisioned"
230243

231-
run list_existing_partitions "unittest" "public" ${TABLE}
244+
run list_existing_partitions "public" ${TABLE}
232245

233246
local expected_monthly=$(cat <<EOF
234247
public|test_interv_2024_12|2024-12-01|2025-01-01
@@ -261,9 +274,10 @@ public|test_interv_2025_w15|2025-04-07|2025-04-14
261274
EOF
262275
)
263276

264-
run list_existing_partitions "unittest" "public" ${TABLE}
277+
run list_existing_partitions "public" ${TABLE}
265278
assert_output "$expected_mix"
266279

280+
rm "$CONFIGURATION_FILE"
267281
}
268282

269283
@test "Test provisioning with multiple partition sets in the configuration" {
@@ -301,7 +315,7 @@ public|table_unittest1_2025_02_01|2025-02-01|2025-02-02
301315
public|table_unittest1_2025_02_02|2025-02-02|2025-02-03
302316
EOF
303317
)
304-
run list_existing_partitions "unittest" "public" "table_unittest1"
318+
run list_existing_partitions "public" "table_unittest1"
305319
assert_output "$expected1"
306320

307321
local expected2=$(cat <<'EOF'
@@ -313,8 +327,10 @@ public|table_unittest2_2025_02_03|2025-02-03|2025-02-04
313327
EOF
314328
)
315329

316-
run list_existing_partitions "unittest" "public" "table_unittest2"
330+
run list_existing_partitions "public" "table_unittest2"
317331
assert_output "$expected2"
332+
333+
rm "$CONFIGURATION_FILE"
318334
}
319335

320336
@test "Test that provisioning continues after an error on a partition set" {
@@ -361,8 +377,10 @@ public|${TABLE}_2025_02_03|2025-02-03|2025-02-04
361377
EOF
362378
)
363379

364-
run list_existing_partitions "unittest" "public" "${TABLE}"
380+
run list_existing_partitions "public" "${TABLE}"
365381
assert_output "$expected2"
382+
383+
rm "$CONFIGURATION_FILE"
366384
}
367385

368386
@test "Test a timestamptz key with provisioning crossing a DST transition " {
@@ -392,7 +410,7 @@ public|${TABLE}_2025_w12|2025-03-17 00:00:00+01|2025-03-24 00:00:00+01
392410
EOF
393411
)
394412

395-
run list_existing_partitions "unittest" "public" ${TABLE}
413+
run list_existing_partitions "public" ${TABLE}
396414
assert_output "$expected_1"
397415

398416
# Now advance one week up to the end of March 2025
@@ -412,7 +430,9 @@ public|${TABLE}_2025_w12|2025-03-17 00:00:00+01|2025-03-24 00:00:00+01
412430
public|${TABLE}_2025_w13|2025-03-24 00:00:00+01|2025-03-31 00:00:00+02
413431
EOF
414432
)
415-
run list_existing_partitions "unittest" "public" ${TABLE}
433+
run list_existing_partitions "public" ${TABLE}
416434
assert_output "$expected_2"
417435

436+
rm "$CONFIGURATION_FILE"
437+
418438
}

scripts/bats/40_cleanup.bats

Lines changed: 8 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() {
@@ -63,6 +67,8 @@ EOF
6367
for ((i=NEW_RETENTION +1; i<= INITIAL_PREPROVISIONED; i++));do
6468
assert_table_not_exists public $(generate_daily_partition_name ${TABLE} ${i})
6569
done
70+
71+
rm "$CONFIGURATION_FILE"
6672
}
6773

6874
@test "Test that gaps in the partition set prevent any partition removal" {
@@ -117,7 +123,7 @@ EOF
117123
expected+="public|${PARTS[i]}|${PARTS[i+1]}|${PARTS[i+2]}"
118124
(( i+=3 ))
119125
done
120-
run list_existing_partitions "unittest" "public" "${TABLE}"
126+
run list_existing_partitions "public" "${TABLE}"
121127

122128
assert_output "$expected"
123129

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: 3 additions & 3 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() {
@@ -52,7 +52,7 @@ generate_daily_partition_name() {
5252
}
5353

5454
generate_table_name() {
55-
cat /dev/urandom | head -n 1 | base64 | tr -dc '[:alnum:]' | tr '[:upper:]' '[:lower:]' | cut -c -13 | sed -e 's/^[0-9]/a/g'
55+
echo "tbl_"$(random_suffix)
5656
}
5757

5858
# Generic method to create child partitions with specified ranges
@@ -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: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
1+
# Emit a random string suitable for unquoted database identifiers (lower case, ASCII)
2+
# The result may start with a number.
3+
random_suffix() {
4+
head -c 12 /dev/urandom | base64 | tr -dc '[:alnum:]' | tr '[:upper:]' '[:lower:]'
5+
}
6+
17
init_database() {
2-
QUERY="CREATE DATABASE unittest;"
8+
local dbname="ppm_test_"$(random_suffix)
9+
QUERY="CREATE DATABASE \"$dbname\" ;"
310
execute_sql "${QUERY}" postgres
11+
export PPM_DATABASE="$dbname"
12+
export PGDATABASE="$dbname"
413
}
514

615
drop_database() {
7-
QUERY="set lock_timeout to '5s'; DROP DATABASE IF EXISTS unittest;"
16+
QUERY="set lock_timeout to '5s'; DROP DATABASE IF EXISTS \"$PPM_DATABASE\" ;"
817
execute_sql_commands "${QUERY}" postgres
18+
unset PPM_DATABASE
919
}
1020

1121
reset_database() {
@@ -23,7 +33,7 @@ create_table_from_template() {
2333
created_at DATE NOT NULL
2434
) PARTITION BY RANGE (created_at);
2535
EOQ
26-
execute_sql "${QUERY}"
36+
execute_sql "${QUERY}" "${PPM_DATABASE}"
2737
}
2838

2939
create_table_uuid_range() {
@@ -61,8 +71,8 @@ generate_configuration_file() {
6171

6272
local FILENAME=$(mktemp).yaml
6373
yq '. as $item ireduce ({}; . * $item )' "${CONFIGURATION_TEMPLATE_FILE}" "${TEMPORARY_FILE}" > "${FILENAME}"
64-
65-
echo $FILENAME
74+
rm "${TEMPORARY_FILE}"
75+
echo "$FILENAME"
6676
}
6777

6878
# Return a common configuration

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)