#!/bin/bash # SPDX-FileCopyrightText: 2021-2023 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"; sql --show-processlist "$DBURL" | wc -lw sql --proclist "$DBURL" | wc -lw sql --listproc "$DBURL" | wc -lw } 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_-r() { echo "### Test -r - retry 3 times" stdout sql -r --debug pg://nongood@127.0.0.3:2227/ "SELECT 'This should fail 3 times';" } par_--retries() { 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 | grep par_ | LC_ALL=C sort | parallel --timeout 3000% -j6 --tag -k --joblog /tmp/jl-`basename $0` '{} 2>&1'