#!/bin/bash # SPDX-FileCopyrightText: 2021-2024 Ole Tange, http://ole.tange.dk and Free Software and Foundation, Inc. # # SPDX-License-Identifier: GPL-3.0-or-later exec 2>&1 ## Setup # mysqlrootpass=${mysqlrootpass:-M-b+Ydjq4ejT4E} # MYSQL_ADMIN_DBURL=mysql://root:$mysqlrootpass@/mysql # sql $MYSQL_ADMIN_DBURL "drop user 'sqlunittest'@'localhost'" # sql $MYSQL_ADMIN_DBURL DROP DATABASE sqlunittest; # sql $MYSQL_ADMIN_DBURL CREATE DATABASE sqlunittest; # sql $MYSQL_ADMIN_DBURL "CREATE USER 'sqlunittest'@'localhost' IDENTIFIED BY 'CB5A1FFFA5A';" # sql $MYSQL_ADMIN_DBURL "GRANT ALL PRIVILEGES ON sqlunittest.* TO 'sqlunittest'@'localhost';" export MYSQL_TEST_DBURL=mysql://tange:tange@/ export DBURL="$MYSQL_TEST_DBURL" par_shebang-Y() { echo '### Test of #! -Y with file as input' shebang=/tmp/shebang-Y cat >"$shebang" <"$shebang" <"$unittest" <> ~/.sql/aliases perl -i -ne '$seen{$_}++ || print' ~/.sql/aliases sql :sqlunittest "SELECT 'Yes it does' as 'Test if .sql/aliases works';" echo "### Test sql:sql::alias" sql sql:sql::sqlunittest "SELECT 'Yes it works' as 'Test sql:sql::alias';" } par__noheaders() { echo "### Test --noheaders --no-headers -n" testtable noheader | sql "$DBURL" sql -n "$DBURL" 'select * from noheader order by id' | parallel -k --colsep '\t' echo {2} {1} sql --noheaders "$DBURL" 'select * from noheader order by id' | parallel -k --colsep '\t' echo {2} {1} sql --no-headers "$DBURL" 'select * from noheader order by id' | parallel -k --colsep '\t' echo {2} {1} } par_sep() { echo "### Test --sep -s"; sql --no-headers -s : pg:/// 'select 1,2' | parallel --colsep ':' echo {2} {1} sql --no-headers --sep : pg:/// 'select 1,2' | parallel --colsep ':' echo {2} {1} } par_passthrough() { echo "### Test --passthrough -p"; testtable passthrough | sql "$DBURL" sql -p -H "$DBURL" 'select * from passthrough' echo sql --passthrough -H "$DBURL" 'select * from passthrough' echo } par_html() { echo "### Test --html"; testtable html | sql "$DBURL" sql --html "$DBURL" 'select * from html' echo } par__listproc() { echo "### Test --show-processlist|proclist|listproc"; # Take the minimum of 3 runs to avoid error counting # if one of the other jobs happens to be running ( sql --show-processlist "$DBURL" | wc -lw sql --show-processlist "$DBURL" | wc -lw sql --show-processlist "$DBURL" | wc -lw ) | sort | head -n1 ( sql --proclist "$DBURL" | wc -lw sql --proclist "$DBURL" | wc -lw sql --proclist "$DBURL" | wc -lw ) | sort | head -n1 ( sql --listproc "$DBURL" | wc -lw sql --listproc "$DBURL" | wc -lw sql --listproc "$DBURL" | wc -lw ) | sort | head -n1 } par__dbsize() { echo "### Test --db-size --dbsize"; sql --dbsize "$DBURL" | wc -w sql --db-size "$DBURL" | wc -w } par__tablesize() { echo "### Test --table-size --tablesize" sql --showtables "$DBURL" | grep TBL | parallel sql "$DBURL" drop table sql --tablesize "$DBURL" | wc -l sql --table-size "$DBURL" | wc -l } par_debug() { echo "### Test --debug" stdout sql --debug "$DBURL" "SELECT 'Yes it does' as 'Test if --debug works';" | replace_tmpdir | perl -pe 's:/...........sql:/tmpfile:g' } par_version() { echo "### Test --version -V" sql --version | wc sql -V | wc } par_retry() { echo "### Test -r - retry 3 times" stdout sql -r --debug pg://nongood@127.0.0.3:2227/ "SELECT 'This should fail 3 times';" echo "### Test --retries=s" stdout sql --retries=4 --debug pg://nongood@127.0.0.3:2227/ "SELECT 'This should fail 4 times';" } par_help() { echo "### Test --help -h" sql --help sql -h } export -f $(compgen -A function | grep par_) compgen -A function | G par_ "$@" | LC_ALL=C sort | parallel --timeout 3000% -j0 --tag -k --joblog /tmp/jl-`basename $0` '{} 2>&1'