-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_app.py
More file actions
91 lines (71 loc) · 2.82 KB
/
test_app.py
File metadata and controls
91 lines (71 loc) · 2.82 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
import pytest
import json
from app import app, active_games
@pytest.fixture
def client():
"""Create a test client for the Flask app"""
app.config['TESTING'] = True
with app.test_client() as client:
yield client
active_games.clear()
@pytest.fixture
def sample_game(client):
"""Create a sample game and return its data"""
response = client.get('/api/grid')
data = json.loads(response.data)
return data
class TestGetGrid:
"""Tests for GET /api/grid endpoint"""
def test_get_grid_success(self, client):
"""Test that getting a new grid returns valid data"""
response = client.get('/api/grid')
data = json.loads(response.data)
assert response.status_code == 200
assert 'game_id' in data
assert 'grid' in data
assert 'difficulty' in data
assert len(data['grid']) == 9
assert all(len(row) == 9 for row in data['grid'])
class TestValidateMove:
"""Tests for POST /api/validate endpoint"""
def test_validate_correct_move(self, client, sample_game):
"""Test validating a correct move"""
game_id = sample_game['game_id']
grid = sample_game['grid']
game = active_games[game_id]
# Find first empty cell
row, col = None, None
for r in range(9):
for c in range(9):
if grid[r][c] == 0:
row, col = r, c
break
if row is not None:
break
if row is not None:
correct_value = game['solution'][row][col]
response = client.post('/api/validate',
json={'game_id': game_id, 'row': row, 'col': col, 'value': correct_value})
data = json.loads(response.data)
assert response.status_code == 200
assert data['valid'] is True
assert data['is_correct'] is True
def test_validate_missing_parameters(self, client):
"""Test that missing parameters return 400"""
response = client.post('/api/validate', json={'game_id': 'test', 'row': 0})
assert response.status_code == 400
def test_validate_game_not_found(self, client):
"""Test that invalid game_id returns 404"""
response = client.post('/api/validate',
json={'game_id': 'nonexistent', 'row': 0, 'col': 0, 'value': 5})
assert response.status_code == 404
class TestGetSolution:
"""Tests for GET /api/solution/<game_id> endpoint"""
def test_get_solution_success(self, client, sample_game):
"""Test getting solution for an existing game"""
game_id = sample_game['game_id']
response = client.get(f'/api/solution/{game_id}')
data = json.loads(response.data)
assert response.status_code == 200
assert 'solution' in data
assert len(data['solution']) == 9