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