Move tests to their own folder
This commit is contained in:
91
tests/test_iplookup.py
Normal file
91
tests/test_iplookup.py
Normal file
@ -0,0 +1,91 @@
|
||||
'''
|
||||
Test ismijnverweg geolookup api
|
||||
'''
|
||||
import logging
|
||||
import random
|
||||
import re
|
||||
from operator import itemgetter
|
||||
|
||||
from faker import Faker
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from main import app
|
||||
|
||||
# Set up logging
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Initialize Faker for generating test data
|
||||
fake = Faker()
|
||||
|
||||
# Create test client
|
||||
fake_ipv6 = fake.ipv6()
|
||||
client = TestClient(app, client=(fake_ipv6, 31337))
|
||||
|
||||
|
||||
def test_no_query():
|
||||
"""Test searching without a query parameter"""
|
||||
|
||||
response = client.get("/")
|
||||
|
||||
assert response.status_code == 200
|
||||
results = response.json()
|
||||
logging.info(results)
|
||||
assert results['ip'] == fake_ipv6
|
||||
assert len(results) > 0
|
||||
|
||||
|
||||
def test_single_query():
|
||||
"""Test searching with an ip address"""
|
||||
fake_ipv4 = fake.ipv4_public()
|
||||
|
||||
response = client.get(f"/{fake_ipv4}")
|
||||
|
||||
assert response.status_code == 200
|
||||
results = response.json()
|
||||
logging.info(results)
|
||||
assert results['ip'] == fake_ipv4
|
||||
assert len(results) > 0
|
||||
|
||||
|
||||
def test_multi_query():
|
||||
"""Test searching with an ip address"""
|
||||
fake_ips = [{'ip': fake.ipv6() if random.random() > 0.5 else fake.ipv4()}
|
||||
for _ in range(16)]
|
||||
|
||||
response = client.post("/", json=fake_ips)
|
||||
|
||||
assert response.status_code == 200
|
||||
results = response.json()
|
||||
logging.info(results)
|
||||
|
||||
for ip in map(itemgetter('ip'), results):
|
||||
assert ip in map(itemgetter('ip'), fake_ips)
|
||||
|
||||
assert len(results) > 0
|
||||
|
||||
|
||||
def test_invalid_query():
|
||||
"""Test searching with an invalid ip address"""
|
||||
invalid_ip = '500.312.77.31337'
|
||||
test_pattern = 'Input is not a valid IPv[46] address'
|
||||
|
||||
response = client.get(f"/{invalid_ip}")
|
||||
|
||||
assert response.status_code == 422
|
||||
results = response.json()
|
||||
logging.info(results)
|
||||
assert all(map(lambda x: x == invalid_ip, (
|
||||
map(itemgetter('input'), results['detail']))))
|
||||
assert all(map(lambda x: re.match(test_pattern, x), (
|
||||
map(itemgetter('msg'), results['detail']))))
|
||||
assert len(results) > 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Run tests
|
||||
test_no_query()
|
||||
test_single_query()
|
||||
test_invalid_query()
|
||||
test_multi_query()
|
||||
print("All tests passed!")
|
||||
Reference in New Issue
Block a user