-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_states_api.sh
More file actions
executable file
Β·188 lines (157 loc) Β· 5.6 KB
/
Copy pathtest_states_api.sh
File metadata and controls
executable file
Β·188 lines (157 loc) Β· 5.6 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/bin/bash
# Test script for US States API endpoints
# Run this after the server is started with the database
BASE_URL="http://localhost:8080/api/v1"
API_KEY="${API_KEY:-your-api-key-here}"
echo "π§ͺ Testing US States API Endpoints"
echo "=================================="
echo ""
# Color codes for output
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
test_count=0
pass_count=0
fail_count=0
# Function to test an endpoint
test_endpoint() {
local name="$1"
local url="$2"
local expected_key="$3"
test_count=$((test_count + 1))
echo -e "${YELLOW}Test $test_count: $name${NC}"
response=$(curl -s -H "X-API-Key: $API_KEY" "$url")
if echo "$response" | jq -e ".$expected_key" > /dev/null 2>&1; then
echo -e "${GREEN}β PASS${NC}"
echo "$response" | jq -C ".$expected_key" | head -20
pass_count=$((pass_count + 1))
else
echo -e "${RED}β FAIL${NC}"
echo "$response" | jq -C . | head -20
fail_count=$((fail_count + 1))
fi
echo ""
}
# Function to test coordinate lookup
test_coordinates() {
local name="$1"
local lat="$2"
local lng="$3"
local expected_state="$4"
test_count=$((test_count + 1))
echo -e "${YELLOW}Test $test_count: $name${NC}"
echo " Coordinates: ($lat, $lng)"
response=$(curl -s -H "X-API-Key: $API_KEY" "$BASE_URL/states/lookup?lat=$lat&lng=$lng")
state_abbr=$(echo "$response" | jq -r '.state.state_abbr')
if [ "$state_abbr" = "$expected_state" ]; then
echo -e "${GREEN}β PASS - Found $expected_state${NC}"
echo "$response" | jq -C '.state | {state_name, state_abbr, state_fips}'
pass_count=$((pass_count + 1))
else
echo -e "${RED}β FAIL - Expected $expected_state, got $state_abbr${NC}"
echo "$response" | jq -C . | head -10
fail_count=$((fail_count + 1))
fi
echo ""
}
echo "π Testing State Search Endpoints"
echo "-----------------------------------"
# Test 1: Search all states
test_endpoint "Search all states" \
"$BASE_URL/states?limit=5" \
"states"
# Test 2: Search by name
test_endpoint "Search by name - California" \
"$BASE_URL/states?name=california" \
"states"
# Test 3: Search by abbreviation
test_endpoint "Search by abbreviation - TX" \
"$BASE_URL/states?abbr=TX" \
"states"
# Test 4: Search by partial name
test_endpoint "Search by partial name - new" \
"$BASE_URL/states?name=new" \
"states"
echo "π Testing State Detail Endpoints"
echo "-----------------------------------"
# Test 5: Get state by abbreviation
test_endpoint "Get state by abbreviation - CA" \
"$BASE_URL/states/CA" \
"state"
# Test 6: Get state by FIPS code
test_endpoint "Get state by FIPS - 06 (California)" \
"$BASE_URL/states/06" \
"state"
# Test 7: Get state by name
test_endpoint "Get state by name - Texas" \
"$BASE_URL/states/Texas" \
"state"
echo "πΊοΈ Testing State Boundary Endpoints"
echo "--------------------------------------"
# Test 8: Get California boundary
test_count=$((test_count + 1))
echo -e "${YELLOW}Test $test_count: Get California boundary GeoJSON${NC}"
response=$(curl -s -H "X-API-Key: $API_KEY" "$BASE_URL/states/CA/boundary")
if echo "$response" | jq -e '.geometry.type' | grep -q "MultiPolygon"; then
echo -e "${GREEN}β PASS - Got MultiPolygon geometry${NC}"
echo "$response" | jq -C '{type, properties: .properties, geometry_type: .geometry.type}'
pass_count=$((pass_count + 1))
else
echo -e "${RED}β FAIL - Invalid geometry${NC}"
echo "$response" | jq -C . | head -10
fail_count=$((fail_count + 1))
fi
echo ""
echo "π Testing Reverse Geocoding (Coordinates β State)"
echo "---------------------------------------------------"
# Test 9-14: Point-in-polygon lookups
test_coordinates "San Francisco, CA" "37.7749" "-122.4194" "CA"
test_coordinates "New York City, NY" "40.7128" "-74.0060" "NY"
test_coordinates "Austin, TX" "30.2672" "-97.7431" "TX"
test_coordinates "Miami, FL" "25.7617" "-80.1918" "FL"
test_coordinates "Seattle, WA" "47.6062" "-122.3321" "WA"
test_coordinates "Denver, CO" "39.7392" "-104.9903" "CO"
echo "β Testing Error Handling"
echo "-------------------------"
# Test invalid state
test_count=$((test_count + 1))
echo -e "${YELLOW}Test $test_count: Get non-existent state (ZZ)${NC}"
response=$(curl -s -w "\n%{http_code}" -H "X-API-Key: $API_KEY" "$BASE_URL/states/ZZ")
http_code=$(echo "$response" | tail -n1)
if [ "$http_code" = "404" ]; then
echo -e "${GREEN}β PASS - Got 404 as expected${NC}"
pass_count=$((pass_count + 1))
else
echo -e "${RED}β FAIL - Expected 404, got $http_code${NC}"
fail_count=$((fail_count + 1))
fi
echo ""
# Test coordinates in ocean
test_count=$((test_count + 1))
echo -e "${YELLOW}Test $test_count: Coordinates in ocean (no state)${NC}"
response=$(curl -s -w "\n%{http_code}" -H "X-API-Key: $API_KEY" "$BASE_URL/states/lookup?lat=35&lng=-130")
http_code=$(echo "$response" | tail -n1)
if [ "$http_code" = "404" ]; then
echo -e "${GREEN}β PASS - Got 404 as expected${NC}"
pass_count=$((pass_count + 1))
else
echo -e "${RED}β FAIL - Expected 404, got $http_code${NC}"
fail_count=$((fail_count + 1))
fi
echo ""
# Final summary
echo "=================================="
echo "π Test Summary"
echo "=================================="
echo -e "Total Tests: $test_count"
echo -e "${GREEN}Passed: $pass_count${NC}"
echo -e "${RED}Failed: $fail_count${NC}"
echo ""
if [ $fail_count -eq 0 ]; then
echo -e "${GREEN}β All tests passed!${NC}"
exit 0
else
echo -e "${RED}β Some tests failed${NC}"
exit 1
fi