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