Project

General

Profile

1 183 aaronmk
#!/bin/bash
2 106 aaronmk
# Tests map on all input/*.csv
3
# Usage: env [n=<num-rows>] self
4 53 aaronmk
5
selfDir="$(dirname -- "$0")"
6
cd "$selfDir"
7
8 183 aaronmk
shopt -s nullglob
9
10 175 aaronmk
tests_n=2
11 181 aaronmk
testMode=
12
test -n "$n" || export n="$tests_n" testMode=1
13 175 aaronmk
14 131 aaronmk
. ../util/env_password in_password
15
16 161 aaronmk
make --directory=../../mappings
17 131 aaronmk
18 185 aaronmk
mkdir -p output
19
20 135 aaronmk
function trace()
21 118 aaronmk
{
22 135 aaronmk
    (
23 181 aaronmk
        env echo -n "$PS4" # bug in sh's built-in echo prints the -n
24 135 aaronmk
        for arg in "$@"; do printf "%q " "$arg"; done
25
        echo "${_in+<$_in}" "${_out+>$_out}"
26
    ) >&2
27 118 aaronmk
}
28
29 161 aaronmk
function map()
30 132 aaronmk
{
31 169 aaronmk
    test -e "$in" || return 1
32
    local ext="${in##*.}" # after last "."
33
    for map in "../../mappings/$src-$out_fmt."$table".csv"; do
34
        table="${map%.*}" # remove extension
35
        table="${table##*.}" # after last "."
36 161 aaronmk
        (
37
            if test "$ext" == "sh"; then
38
                trace . "$in"
39
                (. "$in"; "$1")
40
            else
41
                (_in="$in"; trace)
42
                "$1" <"$in"
43
            fi
44
        ) || exit # abort tester
45 169 aaronmk
    done
46 132 aaronmk
}
47
48 135 aaronmk
function toXml()
49
{
50 169 aaronmk
    local stem="$orig_src.$table.$out_fmt"
51
    local out="output/$stem${method+.$method}.xml"
52
    local accepted="accepted_output/$stem.xml"
53 175 aaronmk
    (set -x; ../map "$map" >"$out") || exit # abort tester
54 181 aaronmk
    (set -x; ${testMode:+diff "$accepted" "$out"})
55 175 aaronmk
    true # ignore last command's exit status
56 118 aaronmk
}
57
58 222 aaronmk
vegbienDest=../util/vegbien_dest.sh
59
60 135 aaronmk
function toDb()
61 118 aaronmk
{
62 222 aaronmk
    trace . "$vegbienDest"
63
    (. "$vegbienDest"; set -x; ../map "$map") || exit # abort tester
64 118 aaronmk
}
65
66 161 aaronmk
for in in input/*; do
67 118 aaronmk
    stem="$(basename -- "${in%.*}")" # remove extension and dir
68 169 aaronmk
    ext="${in##*.}" # after last "."
69
    src="${stem%.*}" # before last ".", if any
70
    orig_src="$src"
71
    if test "$ext" == "sh"; then table="*" # use all tables with a mapping
72
    else table="${stem##*.}" # after last "."
73
    fi
74 106 aaronmk
75 222 aaronmk
    for out_fmt in VegX VegBIEN; do map toXml; done # source to XML
76
    out_fmt=VegBIEN
77
    # VegX to VegBIEN
78 169 aaronmk
    (
79
        src=VegX method=via_$src
80
        for in in "output/$orig_src."$table".$src.xml"; do
81
            table="${in#*.}" # after first "."
82
            table="${table%%.*}" # before second "."
83
            map toXml
84
        done
85 175 aaronmk
    ) || exit # subshell error also interrupts main shell
86 222 aaronmk
    map toDb # source to VegBIEN db
87 106 aaronmk
done