Files
Projekcija/tests/test_api.py

144 lines
4.5 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import requests
import time
import json
BASE_URL = 'http://127.0.0.1:5000'
def test_api():
"""Testiraj web API"""
print("=" * 60)
print("Testiranje Web API-ja")
print("=" * 60)
# Testiranje GET /api/state
print("\n1. Test GET /api/state (početno stanje)")
try:
response = requests.get(f'{BASE_URL}/api/state')
data = response.json()
print(f" Status: {response.status_code}")
print(f" Current text: {data.get('current_text', '')[:50]}...")
print(f" Caps mode: {data.get('caps_mode')}")
print(" ✓ PASS")
except Exception as e:
print(f" ✗ FAIL: {e}")
# Testiranje POST /api/load_song
print("\n2. Test POST /api/load_song (broj 1)")
try:
response = requests.post(
f'{BASE_URL}/api/load_song',
json={'song_number': '1'}
)
print(f" Status: {response.status_code}")
data = response.json()
print(f" Response: {data}")
# Pričekaj da se pesma naloži
time.sleep(0.5)
# Provjeri stanje
response = requests.get(f'{BASE_URL}/api/state')
state = response.json()
print(f" Current text: {state.get('current_text', '')[:50]}...")
print(f" Can next: {state.get('can_next')}")
print(f" Can prev: {state.get('can_prev')}")
print(" ✓ PASS")
except Exception as e:
print(f" ✗ FAIL: {e}")
# Testiranje POST /api/next_page
print("\n3. Test POST /api/next_page")
try:
response = requests.post(f'{BASE_URL}/api/next_page')
print(f" Status: {response.status_code}")
# Provjeri stanje
time.sleep(0.3)
response = requests.get(f'{BASE_URL}/api/state')
state = response.json()
print(f" Page info: {state.get('page_info')}")
print(" ✓ PASS")
except Exception as e:
print(f" ✗ FAIL: {e}")
# Testiranje POST /api/prev_page
print("\n4. Test POST /api/prev_page")
try:
response = requests.post(f'{BASE_URL}/api/prev_page')
print(f" Status: {response.status_code}")
# Provjeri stanje
time.sleep(0.3)
response = requests.get(f'{BASE_URL}/api/state')
state = response.json()
print(f" Page info: {state.get('page_info')}")
print(" ✓ PASS")
except Exception as e:
print(f" ✗ FAIL: {e}")
# Testiranje POST /api/toggle_caps
print("\n5. Test POST /api/toggle_caps")
try:
response = requests.post(f'{BASE_URL}/api/toggle_caps')
print(f" Status: {response.status_code}")
time.sleep(0.3)
response = requests.get(f'{BASE_URL}/api/state')
state = response.json()
print(f" Caps mode: {state.get('caps_mode')}")
print(f" Text (caps): {state.get('current_text', '')[:50]}...")
print(" ✓ PASS")
except Exception as e:
print(f" ✗ FAIL: {e}")
# Testiranje POST /api/clear_screen
print("\n6. Test POST /api/clear_screen")
try:
response = requests.post(f'{BASE_URL}/api/clear_screen')
print(f" Status: {response.status_code}")
time.sleep(0.3)
response = requests.get(f'{BASE_URL}/api/state')
state = response.json()
print(f" Current text: {state.get('current_text', '')[:50]}...")
print(" ✓ PASS")
except Exception as e:
print(f" ✗ FAIL: {e}")
# Testiranje POST /api/search_songs
print("\n7. Test POST /api/search_songs")
try:
response = requests.post(
f'{BASE_URL}/api/search_songs',
json={'query': 'pesem'}
)
print(f" Status: {response.status_code}")
data = response.json()
results = data.get('results', [])
print(f" Rezultati: {results}")
print(" ✓ PASS")
except Exception as e:
print(f" ✗ FAIL: {e}")
# Testiranje GET /
print("\n8. Test GET / (HTML stranicu)")
try:
response = requests.get(f'{BASE_URL}/')
print(f" Status: {response.status_code}")
print(f" HTML length: {len(response.text)} bytes")
print(f" Contains 'Flask': {'Flask' in response.text}")
print(" ✓ PASS")
except Exception as e:
print(f" ✗ FAIL: {e}")
print("\n" + "=" * 60)
print("Testiranje završeno!")
print("=" * 60)
if __name__ == "__main__":
test_api()