forked from PearlAngeline/Fake-Drug-Detection
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_web_app.py
More file actions
96 lines (84 loc) · 3.34 KB
/
test_web_app.py
File metadata and controls
96 lines (84 loc) · 3.34 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
#!/usr/bin/env python3
"""
Test script for the Drug Barcode Lookup Web Application
"""
import requests
import json
import time
import os
def test_web_app():
"""Test the web application functionality"""
base_url = "http://localhost:5000"
print("Testing Drug Barcode Lookup Web Application")
print("=" * 50)
# Test 1: Check if server is running
print("1. Testing server availability...")
try:
response = requests.get(base_url, timeout=5)
if response.status_code == 200:
print("✓ Server is running")
else:
print(f"✗ Server returned status code: {response.status_code}")
return False
except requests.exceptions.RequestException as e:
print(f"✗ Server is not running: {e}")
print("Please start the server first: python app.py")
return False
# Test 2: Test API endpoint with known barcode
print("\n2. Testing API endpoint with known barcode...")
known_barcode = "8901571007363" # From our database
try:
response = requests.get(f"{base_url}/api/lookup/{known_barcode}", timeout=10)
if response.status_code == 200:
data = response.json()
if data.get('found'):
print(f"✓ Found barcode: {data.get('drug_name', 'Unknown')}")
else:
print(f"✗ Barcode not found in database")
else:
print(f"✗ API returned status code: {response.status_code}")
except requests.exceptions.RequestException as e:
print(f"✗ API test failed: {e}")
# Test 3: Test API endpoint with unknown barcode
print("\n3. Testing API endpoint with unknown barcode...")
unknown_barcode = "9999999999999"
try:
response = requests.get(f"{base_url}/api/lookup/{unknown_barcode}", timeout=10)
if response.status_code == 200:
data = response.json()
if not data.get('found'):
print("✓ Correctly identified unknown barcode")
else:
print("✗ Should have identified as unknown")
else:
print(f"✗ API returned status code: {response.status_code}")
except requests.exceptions.RequestException as e:
print(f"✗ API test failed: {e}")
# Test 4: Test database endpoint
print("\n4. Testing database endpoint...")
try:
response = requests.get(f"{base_url}/database", timeout=10)
if response.status_code == 200:
print("✓ Database endpoint is accessible")
else:
print(f"✗ Database endpoint returned status code: {response.status_code}")
except requests.exceptions.RequestException as e:
print(f"✗ Database test failed: {e}")
print("\n" + "=" * 50)
print("Web application test completed!")
print("You can now:")
print("1. Open http://localhost:5000 in your browser")
print("2. Upload an image with a barcode")
print("3. View the results and drug information")
print("4. Check the database for all processed images")
def main():
"""Main test function"""
# Check if database exists
if not os.path.exists('barcode_text_results.json'):
print("✗ Database file not found!")
print("Please run: python barcode_scanner.py first")
return
# Run tests
test_web_app()
if __name__ == "__main__":
main()