Merge pull request #85 from opencultureconsulting/felixlohmeier/functional-tests-7

first draft functional tests
This commit is contained in:
Felix Lohmeier 2022-12-04 00:40:26 +01:00 committed by GitHub
commit d1486c5929
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 1111 additions and 579 deletions

View File

@ -137,3 +137,9 @@ orcli uses [bashly](https://github.com/DannyBen/bashly/) for generating the one-
```sh
bashly generate --upgrade
```
4. Run tests
```sh
./orcli test
```

1409
orcli

File diff suppressed because it is too large Load Diff

View File

@ -116,6 +116,9 @@ commands:
- orcli info "duplicates"
- orcli info 1234567890123
- name: test
help: run functional tests on tmp OpenRefine workspace
- name: transform
help: apply undo/redo JSON file(s) to an OpenRefine project
args:

View File

@ -66,12 +66,16 @@ send_completions() {
echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_orcli_completions_filter "--help -h")" -- "$cur" )'
echo $' ;;'
echo $''
echo $' \'test\'*)'
echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_orcli_completions_filter "--help -h")" -- "$cur" )'
echo $' ;;'
echo $''
echo $' \'run\'*)'
echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_orcli_completions_filter "--help --interactive --memory --port --quiet -h -q")" -- "$cur" )'
echo $' ;;'
echo $''
echo $' *)'
echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_orcli_completions_filter "--help --version -h -v completions export import info list run transform")" -- "$cur" )'
echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_orcli_completions_filter "--help --version -h -v completions export import info list run test transform")" -- "$cur" )'
echo $' ;;'
echo $''
echo $' esac'

73
src/test_command.sh Normal file
View File

@ -0,0 +1,73 @@
# shellcheck shell=bash disable=SC2154
# locate orcli and OpenRefine
scriptpath=$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")
if [[ -x "${scriptpath}/refine" ]]; then
openrefine="${scriptpath}/refine"
else
error "OpenRefine's startup script (refine) not found!" "Did you put orcli in your OpenRefine app dir?"
fi
# create tmp directory
OPENREFINE_TMPDIR="$(mktemp -d)"
trap '{ rm -rf "$OPENREFINE_TMPDIR"; }' 0 2 3 15
# download the test files if needed
if ! [[ -f "tests/help.sh" ]]; then
cd "$OPENREFINE_TMPDIR"
if ! curl -fs -L -o orcli.zip https://github.com/opencultureconsulting/orcli/archive/refs/heads/main.zip; then
error "downloading test files failed!" "Please download the tests dir manually from GitHub."
fi
unzip -q -j orcli.zip "*/tests/*.sh" -d "tests/"
unzip -q -j orcli.zip "*/tests/data/*" -d "tests/data/"
fi
# check if OpenRefine is already running
if curl -fs "${OPENREFINE_URL}" &>/dev/null; then
error "OpenRefine is already running on port 3333." "Please stop the other process."
fi
# start OpenRefine with tmp workspace
$openrefine -d "$OPENREFINE_TMPDIR" -x refine.headless=true -v warn &>"$OPENREFINE_TMPDIR/openrefine.log" &
OPENREFINE_PID="$!"
# update trap to kill OpenRefine on error or exit
trap '{ rm -rf "$OPENREFINE_TMPDIR"; kill -9 "$OPENREFINE_PID"; }' 0 2 3 15
# wait until OpenRefine is running (timeout 20s)
if ! curl -fs --retry 20 --retry-connrefused --retry-delay 1 "${OPENREFINE_URL}/command/core/get-version" &>/dev/null; then
error "starting OpenRefine server failed!"
else
log "started OpenRefine with tmp workspace ${OPENREFINE_TMPDIR}"
fi
# execute tests in subshell
export OPENREFINE_TMPDIR OPENREFINE_URL OPENREFINE_PID
cd "tests"
files=(*.sh)
results=()
for i in "${!files[@]}"; do
set +e # do not exit on failed tests
bash -e <(
if ! command -v orcli &>/dev/null; then
echo "shopt -s expand_aliases"
echo "alias orcli=${scriptpath}/orcli"
fi
awk 1 "${files[$i]}"
) &>"$OPENREFINE_TMPDIR/test.log"
results+=(${?})
set -e
if [[ "${results[$i]}" =~ [1-9] ]]; then
cat "$OPENREFINE_TMPDIR/test.log"
log "FAILED ${files[$i]} with exit code ${results[$i]}!"
else
log "PASSED ${files[$i]}"
fi
done
# print overall result
if [[ "${results[*]}" =~ [1-9] ]]; then
error "failed tests!"
else
log "all tests passed!"
fi

19
tests/completions.sh Normal file
View File

@ -0,0 +1,19 @@
#!/bin/bash
t="completions"
# create tmp directory
tmpdir="$(mktemp -d)"
trap '{ rm -rf "${tmpdir}"; }' 0 2 3 15
# assertion
cat << "DATA" > "${tmpdir}/${t}.assert"
# orcli completion -*- shell-script -*-
DATA
# action
cd "${tmpdir}" || exit 1
orcli completions | head -n1 > "${t}.output"
# test
diff -u "${t}.assert" "${t}.output"

View File

@ -0,0 +1,7 @@
email count name state gender purchase
arthur.duff@example4.com 2 Arthur Duff OR M Dining table
ben.morisson@example6.org 1 Ben Morisson FL M Amplifier
ben.tyler@example3.org 1 Ben Tyler NV M Flashlight
danny.baron@example1.com 3 Danny Baron CA M TV
jean.griffith@example5.org 1 Jean Griffith WA F Power drill
melanie.white@example2.edu 2 Melanie White NC F iPhone
1 email count name state gender purchase
2 arthur.duff@example4.com 2 Arthur Duff OR M Dining table
3 ben.morisson@example6.org 1 Ben Morisson FL M Amplifier
4 ben.tyler@example3.org 1 Ben Tyler NV M Flashlight
5 danny.baron@example1.com 3 Danny Baron CA M TV
6 jean.griffith@example5.org 1 Jean Griffith WA F Power drill
7 melanie.white@example2.edu 2 Melanie White NC F iPhone

11
tests/data/duplicates.csv Normal file
View File

@ -0,0 +1,11 @@
email,name,state,gender,purchase
danny.baron@example1.com,Danny Baron,CA,M,TV
melanie.white@example2.edu,Melanie White,NC,F,iPhone
danny.baron@example1.com,D. Baron,CA,M,Winter jacket
ben.tyler@example3.org,Ben Tyler,NV,M,Flashlight
arthur.duff@example4.com,Arthur Duff,OR,M,Dining table
danny.baron@example1.com,Daniel Baron,CA,M,Bike
jean.griffith@example5.org,Jean Griffith,WA,F,Power drill
melanie.white@example2.edu,Melanie White,NC,F,iPad
ben.morisson@example6.org,Ben Morisson,FL,M,Amplifier
arthur.duff@example4.com,Arthur Duff,OR,M,Night table
1 email name state gender purchase
2 danny.baron@example1.com Danny Baron CA M TV
3 melanie.white@example2.edu Melanie White NC F iPhone
4 danny.baron@example1.com D. Baron CA M Winter jacket
5 ben.tyler@example3.org Ben Tyler NV M Flashlight
6 arthur.duff@example4.com Arthur Duff OR M Dining table
7 danny.baron@example1.com Daniel Baron CA M Bike
8 jean.griffith@example5.org Jean Griffith WA F Power drill
9 melanie.white@example2.edu Melanie White NC F iPad
10 ben.morisson@example6.org Ben Morisson FL M Amplifier
11 arthur.duff@example4.com Arthur Duff OR M Night table

11
tests/data/duplicates.tsv Normal file
View File

@ -0,0 +1,11 @@
email name state gender purchase
danny.baron@example1.com Danny Baron CA M TV
melanie.white@example2.edu Melanie White NC F iPhone
danny.baron@example1.com D. Baron CA M Winter jacket
ben.tyler@example3.org Ben Tyler NV M Flashlight
arthur.duff@example4.com Arthur Duff OR M Dining table
danny.baron@example1.com Daniel Baron CA M Bike
jean.griffith@example5.org Jean Griffith WA F Power drill
melanie.white@example2.edu Melanie White NC F iPad
ben.morisson@example6.org Ben Morisson FL M Amplifier
arthur.duff@example4.com Arthur Duff OR M Night table
1 email name state gender purchase
2 danny.baron@example1.com Danny Baron CA M TV
3 melanie.white@example2.edu Melanie White NC F iPhone
4 danny.baron@example1.com D. Baron CA M Winter jacket
5 ben.tyler@example3.org Ben Tyler NV M Flashlight
6 arthur.duff@example4.com Arthur Duff OR M Dining table
7 danny.baron@example1.com Daniel Baron CA M Bike
8 jean.griffith@example5.org Jean Griffith WA F Power drill
9 melanie.white@example2.edu Melanie White NC F iPad
10 ben.morisson@example6.org Ben Morisson FL M Amplifier
11 arthur.duff@example4.com Arthur Duff OR M Night table

4
tests/data/example.csv Normal file
View File

@ -0,0 +1,4 @@
a,b,c
1,2,3
0,0,0
$,\,'
1 a b c
2 1 2 3
3 0 0 0
4 $ \ '

4
tests/data/example.tsv Normal file
View File

@ -0,0 +1,4 @@
a b c
1 2 3
0 0 0
$ \ '
1 a b c
2 1 2 3
3 0 0 0
4 $ \ '

21
tests/export-tsv.sh Normal file
View File

@ -0,0 +1,21 @@
#!/bin/bash
t="export-tsv"
# create tmp directory
tmpdir="$(mktemp -d)"
trap '{ rm -rf "${tmpdir}"; }' 0 2 3 15
# input
cp data/example.csv "${tmpdir}/${t}.csv"
# assertion
cp data/example.tsv "${tmpdir}/${t}.assert"
# action
cd "${tmpdir}" || exit 1
orcli import csv "${t}.csv"
orcli export tsv "${t} csv" --output "${t}.output"
# test
diff -u "${t}.assert" "${t}.output"

19
tests/help.sh Normal file
View File

@ -0,0 +1,19 @@
#!/bin/bash
t="help"
# create tmp directory
tmpdir="$(mktemp -d)"
trap '{ rm -rf "${tmpdir}"; }' 0 2 3 15
# assertion
cat << "DATA" > "${tmpdir}/${t}.assert"
orcli - OpenRefine command-line interface written in Bash
DATA
# action
cd "${tmpdir}" || exit 1
orcli --help | head -n1 > "${t}.output"
# test
diff -u "${t}.assert" "${t}.output"

21
tests/import-csv.sh Normal file
View File

@ -0,0 +1,21 @@
#!/bin/bash
t="import-csv"
# create tmp directory
tmpdir="$(mktemp -d)"
trap '{ rm -rf "${tmpdir}"; }' 0 2 3 15
# input
cp data/example.csv "${tmpdir}/${t}.csv"
# assertion
cp data/example.tsv "${tmpdir}/${t}.assert"
# action
cd "${tmpdir}" || exit 1
orcli import csv "${t}.csv"
orcli export tsv "${t} csv" > "${t}.output"
# test
diff -u "${t}.assert" "${t}.output"

18
tests/info.sh Normal file
View File

@ -0,0 +1,18 @@
#!/bin/bash
t="info"
# create tmp directory
tmpdir="$(mktemp -d)"
trap '{ rm -rf "${tmpdir}"; }' 0 2 3 15
# input
cp data/example.csv "${tmpdir}/${t}.csv"
# action
cd "${tmpdir}" || exit 1
orcli import csv "${t}.csv"
orcli info "${t} csv"
# test
# grep "${t}" "${t}.output"

18
tests/list.sh Normal file
View File

@ -0,0 +1,18 @@
#!/bin/bash
t="list"
# create tmp directory
tmpdir="$(mktemp -d)"
trap '{ rm -rf "${tmpdir}"; }' 0 2 3 15
# input
cp data/example.csv "${tmpdir}/${t}.csv"
# action
cd "${tmpdir}" || exit 1
orcli import csv "${t}.csv"
orcli list > "${t}.output"
# test
grep "${t}" "${t}.output"

21
tests/run.sh Normal file
View File

@ -0,0 +1,21 @@
#!/bin/bash
t="run"
# create tmp directory
tmpdir="$(mktemp -d)"
trap '{ rm -rf "${tmpdir}"; }' 0 2 3 15
# assertion
cp data/duplicates-transformed.tsv "${tmpdir}/${t}.assert"
# action
cd "${tmpdir}" || exit 1
orcli run --memory "2000M" --port "3334" << EOF
orcli import csv "https://git.io/fj5hF" --projectName "duplicates"
orcli transform "duplicates" "https://git.io/fj5ju"
orcli export tsv "duplicates" --output "${t}.output"
EOF
# test
diff -u "${t}.assert" "${t}.output"

19
tests/transform.sh Normal file
View File

@ -0,0 +1,19 @@
#!/bin/bash
t="transform"
# create tmp directory
tmpdir="$(mktemp -d)"
trap '{ rm -rf "${tmpdir}"; }' 0 2 3 15
# assertion
cp data/duplicates-transformed.tsv "${tmpdir}/${t}.assert"
# action
cd "${tmpdir}" || exit 1
orcli import csv "https://git.io/fj5hF" --projectName "duplicates"
orcli transform "duplicates" "https://git.io/fj5ju"
orcli export tsv "duplicates" --output "${t}.output"
# test
diff -u "${t}.assert" "${t}.output"