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" && . ../bin/env_password mysql_password "your MySQL"
16

    
17
bien_password="$(cat ../config/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 fromFile()
32
{
33
    (_in="$in"; trace)
34
    ("$@" <"$in") || exit # run in a sandbox; abort tester if error
35
}
36

    
37
function map()
38
{
39
    test -e "$in" || return 1
40
    local ext="${in##*.}" # after last "."
41
    (for map in "../mappings/$src-$out_fmt."$table".csv"; do
42
        table="${map%.*}" # remove extension
43
        table="${table##*.}" # after last "."
44
        {
45
            if test "$ext" == "sh"; then
46
                trace . "$in"
47
                (
48
                    . "$in"
49
                    
50
                    # defaults
51
                    test -n "$host" || host=localhost
52
                    if test -z "$user"; then
53
                        if test -n "$isNimoy" -a "$engine" = MySQL; then
54
                            user="$USER" password="$mysql_password"
55
                        else
56
                            user=bien password="$bien_password"
57
                        fi
58
                    fi
59
                    
60
                    for var in engine host user password database; do
61
                        export in_$var="${!var}"
62
                    done
63
                    "$@"
64
                )
65
            else fromFile "$@"
66
            fi
67
        } || exit
68
    done) || exit # run in a sandbox; abort tester if error
69
}
70

    
71
function toXml()
72
{
73
    local stem="$orig_src.$table.$out_fmt"
74
    local out="output/$stem${method+.$method}.xml"
75
    local accepted="accepted_output/$stem.xml"
76
    (set -x; ../map "$map" >"$out") || exit # abort tester
77
    (set -x; ${testMode:+diff "$accepted" "$out"})
78
    true # ignore last command's exit status
79
}
80

    
81
vegbienDest=../bin/vegbien_dest
82

    
83
function toDb()
84
{
85
    trace . "$vegbienDest"
86
    (. "$vegbienDest"; set -x; ../map ${map:+"$map"}) || exit # abort tester
87
}
88

    
89
for in in input/*; do
90
    stem="$(basename -- "${in%.*}")" # remove extension and dir
91
    ext="${in##*.}" # after last "."
92
    src="${stem%.*}" # before last ".", if any
93
    orig_src="$src"
94
    if test "$ext" == "sh"; then table="*" # use all tables with a mapping
95
    else table="${stem##*.}" # after last "."
96
    fi
97
    
98
    for out_fmt in VegX VegBIEN; do map toXml; done # source to XMLs
99
    
100
    out_fmt=VegBIEN
101
    # VegX to VegBIEN XML
102
    (
103
        src=VegX method=via_$src
104
        for in in "output/$orig_src."$table".$src.xml"; do
105
            table="${in#*.}" # after first "."
106
            table="${table%%.*}" # before second "."
107
            map toXml
108
        done
109
    ) || exit
110
        
111
    map toDb # source to VegBIEN db
112
    # VegBIEN XML to VegBIEN db
113
    for in in "output/$orig_src."$table".VegBIEN"*".xml"; do fromFile toDb; done
114
done
(2-2/4)