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 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
                    
44
                    # defaults
45
                    test -n "$host" || host=localhost
46
                    if test -z "$user"; then
47
                        if test -n "$isNimoy" -a "$engine" = MySQL; then
48
                            user="$USER" password="$mysql_password"
49
                        else
50
                            user=bien password="$bien_password"
51
                        fi
52
                    fi
53
                    
54
                    for var in engine host user password database; do
55
                        export in_$var="${!var}"
56
                    done
57
                    "$1"
58
                )
59
            else
60
                (_in="$in"; trace)
61
                "$1" <"$in"
62
            fi
63
        } || exit # abort tester
64
    done
65
}
66

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

    
77
vegbienDest=../bin/vegbien_dest
78

    
79
function toDb()
80
{
81
    trace . "$vegbienDest"
82
    (. "$vegbienDest"; set -x; ../map "$map") || exit # abort tester
83
}
84

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