-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathtest
executable file
·283 lines (237 loc) · 7.56 KB
/
test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/bin/bash
set -e
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
export SRCDIR=$SCRIPT_DIR/../../../src
export PGSTACDIR=$SRCDIR/pgstac
echo $SCRIPT_DIR
echo $SRCDIR
echo $PGSTACDIR
if [[ "${CI}" ]]; then
set -x
fi
function usage() {
echo -n \
"Usage: $(basename "$0")
Run PgSTAC tests.
This scripts is meant to be run inside the dev container.
"
}
function setuptestdb(){
cd $PGSTACDIR
psql -X -q -v ON_ERROR_STOP=1 <<EOSQL
DROP DATABASE IF EXISTS pgstac_test_db_template;
CREATE DATABASE pgstac_test_db_template;
ALTER DATABASE pgstac_test_db_template SET CLIENT_MIN_MESSAGES TO WARNING;
ALTER DATABASE pgstac_test_db_template SET SEARCH_PATH to pgstac, public;
\connect pgstac_test_db_template;
\i pgstac.sql
DO \$\$
BEGIN
INSERT INTO queryables (name, definition, property_wrapper, property_index_type) VALUES
('eo:cloud_cover','{"$ref": "https://stac-extensions.github.io/eo/v1.0.0/schema.json#/definitions/fieldsproperties/eo:cloud_cover"}','to_int','BTREE')
ON CONFLICT DO NOTHING;
EXCEPTION WHEN unique_violation THEN
RAISE NOTICE '%', SQLERRM USING ERRCODE = SQLSTATE;
END
\$\$;
EOSQL
}
function test_formatting(){
cd $SRCDIR/pypgstac
echo "Running ruff"
ruff -n src tests
echo "Running mypy"
mypy src
echo "Checking if there are any staged migrations."
find $SRCDIR/pgstac/migrations | grep 'staged' && { echo "There are staged migrations in pypgstac/migrations. Please check migrations and remove staged suffix."; exit 1; }
VERSION=$(python -c "from pypgstac.version import __version__; print(__version__)")
echo $VERSION
if echo $VERSION | grep "dev"; then
VERSION="unreleased"
fi
echo "Checking whether base sql migration exists for pypgstac version."
[ -f $SRCDIR/pgstac/migrations/pgstac."${VERSION}".sql ] || { echo "****FAIL No Migration exists pypgstac/migrations/pgstac.${VERSION}.sql"; exit 1; }
echo "Congratulations! All formatting tests pass."
}
function test_pgtap(){
cd $PGSTACDIR
TEMPLATEDB=${1:-pgstac_test_db_template}
psql -X -q -v ON_ERROR_STOP=1 <<EOSQL
DROP DATABASE IF EXISTS pgstac_test_pgtap WITH (force);
CREATE DATABASE pgstac_test_pgtap TEMPLATE $TEMPLATEDB;
ALTER DATABASE pgstac_test_pgtap SET client_min_messages to $CLIENTMESSAGES;
EOSQL
TESTOUTPUT=$(psql -X -q -v ON_ERROR_STOP=1 -f $PGSTACDIR/tests/pgtap.sql pgstac_test_pgtap)
psql -X -q -v ON_ERROR_STOP=1 <<EOSQL
DROP DATABASE IF EXISTS pgstac_test_pgtap WITH (force);
EOSQL
if [[ $(echo "$TESTOUTPUT" | grep -e '^not') ]]; then
echo "PGTap tests failed."
echo "$TESTOUTPUT"
exit 1
else
echo "PGTap Tests Passed!"
fi
}
function test_basicsql(){
TEMPLATEDB=${1:-pgstac_test_db_template}
cd $PGSTACDIR
psql -X -q -v ON_ERROR_STOP=1 <<EOSQL
DROP DATABASE IF EXISTS pgstac_test_basicsql WITH (force);
CREATE DATABASE pgstac_test_basicsql TEMPLATE $TEMPLATEDB;
ALTER DATABASE pgstac_test_basicsql SET search_path to pgstac, public;
ALTER DATABASE pgstac_test_basicsql SET client_min_messages to $CLIENTMESSAGES;
ALTER DATABASE pgstac_test_basicsql SET pgstac.context to 'on';
ALTER DATABASE pgstac_test_basicsql SET pgstac."default_filter_lang" TO 'cql-json';
\connect pgstac_test_basicsql
\copy collections (content) FROM 'tests/testdata/collections.ndjson';
\copy items_staging (content) FROM 'tests/testdata/items.ndjson'
EOSQL
for SQLFILE in tests/basic/*.sql; do
SQLOUTFILE=${SQLFILE}.out
if [[ $CREATEBASICSQLOUT == 1 && ! -f $SQLOUTFILE ]]; then
TMPFILE=$SQLOUTFILE
else
TMPFILE=$(mktemp)
trap 'rm "$TMPFILE"' 0 2 3 15
fi
cd $PGSTACDIR
echo "Running basic tests for $SQLFILE"
psql -X -t -a -v ON_ERROR_STOP=1 pgstac_test_basicsql \
-c "BEGIN;" \
-f $SQLFILE \
-c "ROLLBACK;" \
| sed -e '/^ROLLBACK/d' -e '/^BEGIN/d' >"$TMPFILE"
diff -Z -b -w -B --strip-trailing-cr --suppress-blank-empty -C 1 "$TMPFILE" $SQLOUTFILE && echo "TEST $SQLFILE PASSED" || { echo "***TEST FOR $SQLFILE FAILED***"; exit 1; }
done
psql -X -q -c "DROP DATABASE IF EXISTS pgstac_test_basicsql WITH (force);";
}
function test_pypgstac(){
[[ $MESSAGELOG == 1 ]] && VERBOSE="-vvv"
TEMPLATEDB=${1:-pgstac_test_db_template}
cd $SRCDIR/pypgstac
python -m venv venv
source venv/bin/activate
pip install --cache /tmp/.pipcache --upgrade pip
pip install --cache /tmp/.pipcache -e . --no-deps
psql -X -q -v ON_ERROR_STOP=1 <<EOSQL
DROP DATABASE IF EXISTS pgstac_test_pypgstac WITH (force);
CREATE DATABASE pgstac_test_pypgstac TEMPLATE $TEMPLATEDB;
ALTER DATABASE pgstac_test_pypgstac SET client_min_messages to $CLIENTMESSAGES;
EOSQL
pytest tests $VERBOSE
psql -X -q -c "DROP DATABASE IF EXISTS pgstac_test_pypgstac WITH (force)";
}
function test_migrations(){
psql -X -q -v ON_ERROR_STOP=1 <<EOSQL
DROP DATABASE IF EXISTS pgstac_test_migration WITH (force);
CREATE DATABASE pgstac_test_migration;
ALTER DATABASE pgstac_test_migration SET search_path to pgstac, public;
ALTER DATABASE pgstac_test_migration SET client_min_messages to $CLIENTMESSAGES;
EOSQL
export PGDATABASE=pgstac_test_migration
echo "Migrating from version 0.3.0"
cd $SRCDIR/pypgstac
python -m venv venv
source venv/bin/activate
pip install --cache /tmp/.pipcache --upgrade pip
pip install --cache /tmp/.pipcache -e .[dev,test,psycopg]
pypgstac migrate --toversion 0.3.0
pypgstac --version
pypgstac migrate
pypgstac --version
echo "Running all tests against incrementally migrated database."
test_pgtap pgstac_test_migration
test_basicsql pgstac_test_migration
test_pypgstac pgstac_test_migration
psql -X -q -c "DROP DATABASE IF EXISTS pgstac_test_migration WITH (force);" postgres
}
FORMATTING=0
SETUPDB=0
PGTAP=0
BASICSQL=0
PYPGSTAC=0
MIGRATIONS=0
MESSAGENOTICE=0
MESSAGELOG=0
CREATEBASICSQLOUT=0
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
--help)
usage
exit 0
shift
;;
--v)
MESSAGENOTICE=1
shift
;;
--vv)
MESSAGEDEBUG=1
shift
;;
--formatting)
FORMATTING=1
shift
;;
--pgtap)
SETUPDB=1
PGTAP=1
shift
;;
--basicsql)
SETUPDB=1
BASICSQL=1
shift
;;
--basicsql-createout)
SETUPDB=1
BASICSQL=1
export CREATEBASICSQLOUT=1
shift
;;
--pypgstac)
SETUPDB=1
PYPGSTAC=1
shift
;;
--migrations)
SETUPDB=1
MIGRATIONS=1
shift
;;
--nomigrations)
SETUPDB=1
PGTAP=1
BASICSQL=1
PYPGSTAC=1
shift
;;
*) # unknown option
usage
exit 1;
;;
esac
done
CLIENTMESSAGES='warning'
[[ $MESSAGENOTICE -eq 1 ]] && CLIENTMESSAGES='notice'
[[ $MESSAGEDEBUG -eq 1 ]] && CLIENTMESSAGES='debug1'
echo $CLIENTMESSAGES
if [[ ($FORMATTING -eq 0) && ($SETUPDB -eq 0) && ($PGTAP -eq 0) && ($BASICSQL -eq 0) && ($PYPGSTAC -eq 0) && ($MIGRATIONS -eq 0) ]]
then
FORMATTING=1
SETUPDB=1
PGTAP=1
BASICSQL=1
PYPGSTAC=1
MIGRATIONS=1
fi
[ $FORMATTING -eq 1 ] && test_formatting
[ $SETUPDB -eq 1 ] && setuptestdb
[ $PGTAP -eq 1 ] && test_pgtap
[ $BASICSQL -eq 1 ] && test_basicsql
[ $PYPGSTAC -eq 1 ] && test_pypgstac
[ $MIGRATIONS -eq 1 ] && test_migrations
exit 0