-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathtest_flask.py
More file actions
53 lines (41 loc) · 1.85 KB
/
test_flask.py
File metadata and controls
53 lines (41 loc) · 1.85 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
import os
import random
import subprocess
import sys
import time
import pytest
import requests
HOST = 'http://127.0.0.1:5000'
@pytest.fixture(scope='session', autouse=True)
def pytest_sessionstart():
server = subprocess.Popen([sys.executable, '-m', 'flask', 'run'])
time.sleep(1)
os.chdir('maze_generators/kruskal')
mg = subprocess.Popen([sys.executable, '-m', 'flask', 'run'])
time.sleep(1)
yield
server.terminate()
mg.terminate()
def test_2_requests_for_origin():
'''Test that 2 requests for the same segment give the same value, and that the second one doesn't change the maze.'''
segment1 = requests.get(f'{HOST}/generateSegment', params={'row': 0, 'col': 0}).json()
segment2 = requests.get(f'{HOST}/generateSegment', params={'row': 0, 'col': 0}).json()
assert segment1 == segment2, 'Segment at origin changed between requests.'
requests.delete(f'{HOST}/resetMaze')
def test_reset_maze():
'''Test that maze state can be reset'''
segment1 = requests.get(f'{HOST}/generateSegment', params={'row': 0, 'col': 0}).json()
requests.delete(f'{HOST}/resetMaze')
segment2 = requests.get(f'{HOST}/generateSegment', params={'row': 0, 'col': 0}).json()
assert segment1 != segment2, 'Segment at origin is the same after reset and regeneration.'
requests.delete(f'{HOST}/resetMaze')
def test_many_segments():
states = {}
# generate lots of segments
for _ in range(30):
coords = (random.randint(-100, 100), random.randint(-100, 100))
segment = requests.get(f'{HOST}/generateSegment', params={'row': coords[0], 'col': coords[1]}).json()
states[coords] = segment
for coords, val in states.items():
r = requests.get(f'{HOST}/generateSegment', params={'row': coords[0], 'col': coords[1]})
assert r.json() == val, 'Segment changed between requests.'