Add tests for cs_get_host_args YAZ-842
[yaz-moved-to-github.git] / test / test_marc.sh
1 #!/bin/sh
2 # Tests reading of ISO2709 and checks that we get identical MARCXML
3 #
4 # Reads marc?.marc files , Generates marc*.xml files
5 # If Libxml2 is present, also turbomarc*xml.
6 # as well as reverse transformation from *marc*.xml files are parsed again..
7 #
8 srcdir=${srcdir:-.}
9 ecode=0
10
11 ../util/yaz-marcdump -i marcxml >/dev/null 2>&1
12 if test $? = "3"; then
13     noxml=1
14 fi
15
16 ../util/yaz-marcdump -o xml,marcxml >/dev/null 2>&1
17 if test $? = "4"; then
18     noxmlwrite=1
19 fi
20
21 binmarc_convert() {
22     OUTPUT_FORMAT="$1"
23     REVERT_FORMAT="$2"
24     PREFIX="$3"
25     SUFFIX="$4"
26     for f in ${srcdir}/marc[0-9].marc; do
27         fb=`basename ${f} .marc`
28         CHR=`cat ${srcdir}/${fb}.chr`
29         NEW=${PREFIX}${fb}.new.${SUFFIX}
30         OLD=${srcdir}/${PREFIX}${fb}.${SUFFIX}
31         DIFF=`basename ${f}`.diff
32         ../util/yaz-marcdump -f $CHR -t utf-8 -i marc -o ${OUTPUT_FORMAT} $f > $NEW
33         if test $? != "0"; then
34             echo "$f: yaz-marcdump returned error"
35             ecode=1
36         elif test -f $OLD; then
37             if diff $OLD $NEW >$DIFF; then
38                 rm $DIFF
39                 rm $NEW
40             else
41                 echo "$f: $NEW and $OLD differ"
42                 ecode=1
43             fi
44         else
45             echo "$f: Making test result $OLD for the first time"
46             if test "$SUFFIX" = "xml"; then
47                 if test -x /usr/bin/xmllint; then
48                     if xmllint --noout $NEW >out 2>stderr; then
49                         echo "$f: $NEW is well-formed"
50                         mv $NEW $OLD
51                     else
52                         echo "$f: $NEW not well-formed"
53                         ecode=1
54                     fi
55                 else
56                     echo "xmllint not found. install libxml2-utils"
57                     ecode=1
58                 fi
59             elif test "$SUFFIX" = "json"; then
60                 if ../util/yaz-json-parse < $NEW >out 2>stderr; then
61                     echo "$f: $NEW is JSON OK"
62                     mv $NEW $OLD
63                 else
64                     echo "$f: $NEW is NOT JSON OK"
65                     ecode=1
66                 fi
67             else
68                 echo "Bad suffix $SUFFIX"
69                 ecode = 1
70             fi
71         fi
72
73         if test -z "$noxml"; then
74             ORIGINAL=${f}
75             f=$OLD
76             # compare with original (binary) marc record.
77             OLD=${f}.marc
78             NEW=`basename ${f}`.new.marc
79             DIFF=`basename ${f}`.diff
80             # echo "../util/yaz-marcdump -f utf-8 -t utf-8 -i ${REVERT_FORMAT} -o marc $f > $NEW"
81             ../util/yaz-marcdump -f utf-8 -t utf-8 -i ${REVERT_FORMAT} -o marc $f > $NEW
82             if test $? != "0"; then
83                 echo "Failed decode of $f"
84                 ecode=1
85             elif test -f $OLD; then
86                 if diff $OLD $NEW >$DIFF; then
87                     rm $DIFF
88                     rm $NEW
89                 else
90                     echo "$f: $NEW and $OLD Differ"
91                     ecode=1
92                 fi
93             else
94                 echo "$f: Making test result $OLD for the first time"
95                 mv $NEW $OLD
96             fi
97             # check with original
98             REVERT=`basename ${f}`.marc.revert
99             #../util/yaz-marcdump -f utf-8 -t $CHR  -i ${REVERT_FORMAT} -o marc $f > $REVERT
100             #hexdump -cx $REVERT   > $REVERT.hex
101             #hexdump -cx $ORIGINAL > $ORIGINAL.hex
102             #diff $REVERT.hex $ORIGINAL.hex > $REVERT.diff
103         fi
104     done
105     return $ecode
106 }
107
108 for f in ${srcdir}/marc[0-9].marc; do
109     ../util/yaz-marcdump $f > /dev/null
110     if test $? != "0"; then
111         echo "$f: yaz-marcdump returned error"
112         ecode=1
113     fi
114 done
115
116 binmarc_convert "marcxml"  "marcxml" "" xml
117 echo "binmarc -> marcxml: $?"
118
119 if test -z "$noxmlwrite"; then
120     binmarc_convert "xml,marcxml" "marcxml" "xml2" xml
121     echo "binmarc -> marcxml(libxml2): $?"
122 fi
123
124 binmarc_convert "turbomarc"  "turbomarc" "t" xml
125 echo "binmarc -> turbomarc: $?"
126
127 if test -z "$noxmlwrite"; then
128     binmarc_convert "xml,turbomarc"  "turbomarc" "xml2t" xml
129     echo "binmarc -> turbomarc(libxml2): $?"
130 fi
131
132 binmarc_convert "json"  "json" "" json
133 echo "binmarc -> json: $?"
134
135 exit $ecode
136
137 # Local Variables:
138 # mode:shell-script
139 # sh-indentation: 2
140 # sh-basic-offset: 4
141 # End: