This repository has been archived by the owner on Oct 25, 2022. It is now read-only.
forked from mysociety/mapit
-
Notifications
You must be signed in to change notification settings - Fork 5
/
test-samples.sh
107 lines (83 loc) · 2.88 KB
/
test-samples.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env bash
function main {
precheck_dir="precheck"
samples_dir="$precheck_dir/samples"
mkdir -p $samples_dir
postcodes_file="$precheck_dir/sample_postcodes.txt"
check_sample_postcodes_file $postcodes_file
output_dir="$precheck_dir/output"
postcodes_output_dir="$output_dir/postcodes"
mkdir -p "$postcodes_output_dir"
if [[ "$1" == "sample" ]]; then
echo "Downloading sample outputs"
for postcode in $(grep -E '^\w' $postcodes_file); do
fetch_url "http://localhost:3108/postcode/$postcode.json" "$postcode" $samples_dir
done
elif [[ "$1" == "check" ]]; then
echo "Downloading current state"
for postcode in $(grep -E '^\w' $postcodes_file); do
fetch_url "http://localhost:3108/postcode/$postcode.json" "$postcode" $postcodes_output_dir
done
fi
if [[ "$1" == "check" ]] || [[ "$1" == "verify" ]]; then
if ! command jsondiff &>/dev/null; then
echo "Installing jsondiff to temporary directory"
venv=$(mktemp -d)
virtualenv "$venv"
source "$venv/bin/activate"
pip install jsondiff
fi
echo "Checking current state against sample outputs"
for postcode in $(grep -E '^\w' $postcodes_file); do
check_postcode "$postcode" "$samples_dir" "$postcodes_output_dir"
done
if [[ ! -z "$venv" ]]; then
rm -r "$venv"
fi
fi
}
function check_sample_postcodes_file {
postcodes_file=$1
if [ ! -f "$postcodes_file" ]; then
mysociety_postcodes_file="https://raw.githubusercontent.com/mysociety/uk-postcode-utils/master/ukpostcodeutils/test/sample_postcodes.txt"
echo "Missing sample postcodes file ($postcodes_file), downloading mySociety's copy"
curl "$mysociety_postcodes_file" > "$postcodes_file"
fi
}
function fetch_url {
url=$1
postcode=$2
output_dir=$3
# https://stackoverflow.com/a/37072904
res=$(curl -sw "%{http_code}" "$url")
http_code="${res:${#res}-3}"
if [ ${#res} -eq 3 ]; then
body=""
else
body="${res:0:${#res}-3}"
fi
mkdir "$output_dir/$postcode"
echo -n "$http_code" > "$output_dir/$postcode/code"
echo -n "$body" > "$output_dir/$postcode/body"
}
function check_postcode {
postcode=$1
expected_dir=$2
actual_dir=$3
if [[ ! -e "${expected_dir}/${postcode}" ]]; then
echo "${postcode}: missing expected output"
elif [[ ! -e "${actual_dir}/${postcode}" ]]; then
echo "${postcode}: missing actual output"
else
expected_code=$(cat "${expected_dir}/${postcode}/code")
actual_code=$(cat "${actual_dir}/${postcode}/code")
if [[ "$expected_code" != "$actual_code" ]]; then
echo "${postcode}: response code mismatch (expected ${expected_code} got ${actual_code})"
fi
diff=$(jsondiff "${expected_dir}/${postcode}/body" "${actual_dir}/${postcode}/body")
if [[ "$diff" != "{}" ]] && [[ "$diff" != "[]" ]]; then
echo "${postcode}: response body mismatch (got: ${diff})"
fi
fi
}
main "$@"