Project

General

Profile

1
#!/bin/bash
2
# Tests map on all input/*.csv
3
# Usage: env [n=<num-rows>] self
4

    
5
selfDir="$(dirname -- "$0")"
6
cd "$selfDir"
7

    
8
shopt -s nullglob
9

    
10
tests_n=2
11
testMode=
12
test -n "$n" || export n="$tests_n" testMode=1
13

    
14
test "$(hostname)" = nimoy && isNimoy=t || isNimoy=
15
test -n "$isNimoy" && . ../util/env_password mysql_password "your MySQL"
16

    
17
bien_password="$(cat ../util/bien_password)"
18

    
19
make --directory=../../mappings
20
mkdir -p output
21

    
22
function trace()
23
{
24
    (
25
        env echo -n "$PS4" # bug in sh's built-in echo prints the -n
26
        for arg in "$@"; do printf "%q " "$arg"; done
27
        echo "${_in+<$_in}" "${_out+>$_out}"
28
    ) >&2
29
}
30

    
31
function map()
32
{
33
    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
        {
39
            if test "$ext" == "sh"; then
40
                trace . "$in"
41
                (
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
            else
51
                (_in="$in"; trace)
52
                "$1" <"$in"
53
            fi
54
        } || exit # abort tester
55
    done
56
}
57

    
58
function toXml()
59
{
60
    local stem="$orig_src.$table.$out_fmt"
61
    local out="output/$stem${method+.$method}.xml"
62
    local accepted="accepted_output/$stem.xml"
63
    (set -x; ../map "$map" >"$out") || exit # abort tester
64
    (set -x; ${testMode:+diff "$accepted" "$out"})
65
    true # ignore last command's exit status
66
}
67

    
68
vegbienDest=../util/vegbien_dest
69

    
70
function toDb()
71
{
72
    trace . "$vegbienDest"
73
    (. "$vegbienDest"; set -x; ../map "$map") || exit # abort tester
74
}
75

    
76
for in in input/*; do
77
    stem="$(basename -- "${in%.*}")" # remove extension and dir
78
    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
    
85
    for out_fmt in VegX VegBIEN; do map toXml; done # source to XML
86
    out_fmt=VegBIEN
87
    # VegX to VegBIEN
88
    (
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
    ) || exit # subshell error also interrupts main shell
96
    map toDb # source to VegBIEN db
97
done
(2-2/2)