forked from PyLadiesCZ/roboprojekt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_backend.py
205 lines (174 loc) · 8.51 KB
/
test_backend.py
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
from backend import get_board, get_coordinates, get_data, get_tile_id, get_tile_direction, get_paths, get_starting_coordinates, get_robot_paths, get_robots_to_start, get_start_state, Robot, State, Tile, Direction
from pathlib import Path
from validator import check_squares
import pytest
@pytest.mark.parametrize("map_name", ["test_1", "test_2", "test_3"])
def test_get_coordinates_returns_list(map_name):
"""Test that get_coordinates() returns a list for each map."""
data = get_data("maps/" + map_name + ".json")
coordinates = get_coordinates(data)
assert isinstance(coordinates, list)
# Set of tests checking the structure of read JSON file (supposed to come from Tiled 1.2)
def test_map_returns_correct_data_list():
"""
Take JSON file with test_1 map and assert correct data list.
If the test_1.json map is changed or removed, the test needs to be updated.
"""
data = get_data("maps/test_1.json")
assert data["layers"][0]["data"] == [2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
@pytest.mark.parametrize(("index_number", "expected_value"),
[(0, 0),
(2, 2),
(4, 6),
(6, 9),
(13, 17), ])
def test_map_returns_correct_image_ID(index_number, expected_value):
"""
Take JSON file with test_1 map and assert correct image ID.
index_number: tiles list instance index
expected value: "id" stored in tiles list
If the test_1.json map is changed or removed, the test needs to be updated.
"""
data = get_data("maps/test_1.json")
assert data["tilesets"][0]["tiles"][index_number]["id"] == expected_value
@pytest.mark.parametrize(("index_number", "expected_value"),
[(0, "../img/squares/png/ground.png"),
(2, "../img/squares/png/laser_1_base.png"),
(4, "../img/squares/png/gear_r.png"),
(6, "../img/squares/png/pusher_1_3_5.png"),
(13, "../img/squares/png/conveyor_belt_1.png"), ])
def test_map_returns_correct_image_path(index_number, expected_value):
"""
Take JSON file with test_1 map and assert correct image path.
index_number: tiles list instance index
expected value: "image" stored in tiles list
If the test_1.json map is changed or removed, the test needs to be updated.
"""
data = get_data("maps/test_1.json")
assert data["tilesets"][0]["tiles"][index_number]["image"] == expected_value
def test_board_structure():
"""
Take board (based on JSON test_3 map) and assert correct board structure is returned.
If the test_3.json map is changed or removed, the test needs to be updated.
"""
data = get_data("maps/test_3.json")
board = get_board(data)
example_tile = board[0, 0]
assert example_tile[0].path == "./img/squares/png/ground.png"
assert example_tile[0].direction == Direction.N
def test_starting_coordinates():
"""
Take board (based on JSON test_3 map) and assert correct starting coordinates are returned.
If the test_3.json map is changed or removed, the test needs to be updated.
"""
data = get_data("maps/test_3.json")
board = get_board(data)
assert len(get_starting_coordinates(board)) == 8
assert isinstance(get_starting_coordinates(board), list)
def test_robot_paths():
"""
Get list of robot paths, assert that instance of the list is Path object. The list will change in time, it is not possible to test length or all the paths.
"""
robot_paths = get_robot_paths()
assert isinstance(robot_paths, list)
assert isinstance(robot_paths[0], Path)
@pytest.mark.parametrize(("tile_number", "converted_number"),
[(1, 1),
(2684354573, 13),
(2684354584, 24),
(1610612749, 13)])
def test_convert_tile_id(tile_number, converted_number):
"""
Take number from layer's data (JSON file) and assert it was correctly
transformed to valid tile ID.
"""
assert get_tile_id(tile_number) == converted_number
@pytest.mark.parametrize(("tile_number", "converted_number"),
[(1, Direction.N),
(2684354573, Direction.E),
(2684354584, Direction.E),
(1610612749, Direction.W),
(3221225497, Direction.S)])
def test_convert_tile_direction(tile_number, converted_number):
"""
Take number from layer's data (JSON file) and assert it was correctly
transformed to valid direction in degrees.
"""
assert get_tile_direction(tile_number) == converted_number
def test_dict_paths_is_correct():
"""
Assert that the result of get_paths() is a dictionary.
Assert that the paths structure is valid: integer is tile ID, string is path to the picture.
"""
data = get_data("maps/test_3.json")
paths = get_paths(data)
for key, value in paths.items():
assert isinstance(key, int)
assert isinstance(value, str)
assert isinstance(paths, dict)
def test_robots_on_starting_coordinates():
"""
Assert that the result of get_robots_to_start is a list which contains Robot objects with correct attribute coordinates.
"""
data = get_data("maps/test_3.json")
board = get_board(data)
robots = get_robots_to_start(board)
assert isinstance(robots, list)
assert isinstance(robots[0], Robot)
def test_starting_state():
"""
Assert that created starting state (board and robots) contains the correct instances of objects.
"""
ss = get_start_state("maps/test_3.json")
assert isinstance(ss, State)
assert isinstance(ss.robots, list)
assert isinstance(ss.robots[0], Robot)
assert isinstance(ss.board, dict)
assert isinstance(ss.board[0, 0], list)
assert isinstance(ss.board[0, 0][0], Tile)
@pytest.mark.parametrize(("input_coordinates", "input_direction", "distance", "output_coordinates"),
[((3, 3), Direction.N, 2, (3, 5)),
((3, 3), Direction.E, 2, (3, 3)),
((3, 3), Direction.S, 2, (3, 2)),
((3, 3), Direction.W, 2, (2, 3))])
def test_robot_walk(input_coordinates, input_direction, distance, output_coordinates):
"""
Take robot's coordinates, direction and distance and assert robot walked
to correct coordinates.
"""
state = get_start_state("maps/test_3.json")
robot = Robot(input_direction, None, input_coordinates)
robot.walk(distance, state)
assert robot.coordinates == output_coordinates
@pytest.mark.parametrize(("input_coordinates", "input_direction", "distance", "output_coordinates"),
[((0, 1), Direction.N, 3, (0, 4)),
((8, 1), Direction.N, 3, (8, 3)),
((10, 1), Direction.N, 3, (10, 2)),
((3, 3), Direction.E, 2, (3, 3)),
((3, 3), Direction.S, 2, (3, 2)),
((3, 3), Direction.W, 2, (2, 3))])
def test_robot_move(input_coordinates, input_direction, distance, output_coordinates):
"""
Take robot's coordinates, move's direction and distance and assert robot
was moved to correct coordinates.
"""
state = get_start_state("maps/test_3.json")
robot = Robot(Direction.N, None, input_coordinates)
robot.move(input_direction, distance, state)
assert robot.coordinates == output_coordinates
@pytest.mark.parametrize("map_name", ["test_1", "test_2", "test_3", "test_4"])
def test_tile_size(map_name):
"""
Take size of tiles used in JSON files and assert correct tile size.
This test has to be removed, when width and height of tile image are
no longer constants used for tile drawing.
"""
data = get_data("maps/" + map_name + ".json")
assert data["tilewidth"] == 64
assert data["tileheight"] == 64
@pytest.mark.parametrize("map_name", ["test_3", "test_5"])
def test_map_is_valid(map_name):
assert check_squares(map_name) == True
@pytest.mark.parametrize("map_name", ["test_6"])
def test_map_is_invalid(map_name):
assert check_squares(map_name) != True