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
. ../util/env_password in_password
15

    
16
make --directory=../../mappings
17

    
18
mkdir -p output
19

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

    
29
function map()
30
{
31
    test -e "$in" || return 1
32
    local ext="${in##*.}" # after last "."
33
    for map in "../../mappings/$src-$out_fmt."$table".csv"; do
34
        table="${map%.*}" # remove extension
35
        table="${table##*.}" # after last "."
36
        (
37
            if test "$ext" == "sh"; then
38
                trace . "$in"
39
                (. "$in"; "$1")
40
            else
41
                (_in="$in"; trace)
42
                "$1" <"$in"
43
            fi
44
        ) || exit # abort tester
45
    done
46
}
47

    
48
function toXml()
49
{
50
    local stem="$orig_src.$table.$out_fmt"
51
    local out="output/$stem${method+.$method}.xml"
52
    local accepted="accepted_output/$stem.xml"
53
    (set -x; ../map "$map" >"$out") || exit # abort tester
54
    (set -x; ${testMode:+diff "$accepted" "$out"})
55
    true # ignore last command's exit status
56
}
57

    
58
vegbienDest=../util/vegbien_dest
59

    
60
function toDb()
61
{
62
    trace . "$vegbienDest"
63
    (. "$vegbienDest"; set -x; ../map "$map") || exit # abort tester
64
}
65

    
66
for in in input/*; do
67
    stem="$(basename -- "${in%.*}")" # remove extension and dir
68
    ext="${in##*.}" # after last "."
69
    src="${stem%.*}" # before last ".", if any
70
    orig_src="$src"
71
    if test "$ext" == "sh"; then table="*" # use all tables with a mapping
72
    else table="${stem##*.}" # after last "."
73
    fi
74
    
75
    for out_fmt in VegX VegBIEN; do map toXml; done # source to XML
76
    out_fmt=VegBIEN
77
    # VegX to VegBIEN
78
    (
79
        src=VegX method=via_$src
80
        for in in "output/$orig_src."$table".$src.xml"; do
81
            table="${in#*.}" # after first "."
82
            table="${table%%.*}" # before second "."
83
            map toXml
84
        done
85
    ) || exit # subshell error also interrupts main shell
86
    map toDb # source to VegBIEN db
87
done
(2-2/2)