Skip to content

Commit

Permalink
Mazes loaded only once per session
Browse files Browse the repository at this point in the history
  • Loading branch information
MarekSuchanek committed Nov 29, 2016
1 parent 1af1e24 commit 7136f19
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
29 changes: 28 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
import sys, os
import pytest
import numpy as np
import sys
import os
myPath = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, myPath + '/../')


mazes_root = "tests/fixtures/mazes/"


def load_mazes(folder):
result = {}
for name in os.listdir(folder):
path = os.path.join(folder, name)
if os.path.isfile(path) and name.endswith('.csv'):
maze_num = int(name[:-4])
result[maze_num] = np.loadtxt(path, delimiter=',')
return result


@pytest.fixture(scope='session')
def mazes():
maze_db = {}
for name in os.listdir(mazes_root):
path = os.path.join(mazes_root, name)
if os.path.isdir(path) and not name.startswith('.'):
maze_db[name] = load_mazes(path)
return maze_db

22 changes: 6 additions & 16 deletions tests/test_analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
from maze import analyze, NoPathExistsException


mazes_path = "tests/fixtures/mazes/{0}/{1:02d}.csv"


def inside(coords, matrix):
return 0 <= coords[0] < matrix.shape[0] and \
0 <= coords[1] < matrix.shape[1]
Expand Down Expand Up @@ -71,19 +68,14 @@ def verify_path(maze, analysis, row, column):
assert maze[last] == 1, "Does not lead to goal"


def load_maze(mtype, number):
fname = mazes_path.format(mtype, number)
return np.loadtxt(fname, delimiter=',')


@pytest.mark.parametrize('mtype,number', [
('simple', 1), ('simple', 2), ('simple', 3), ('simple', 4),
('bounds', 1), ('bounds', 2), ('bounds', 3), ('bounds', 4),
('multigoal', 1), ('multigoal', 2), ('multigoal', 3), ('multigoal', 4),
('unreachable', 1), ('unreachable', 2)
])
def test_analysis(mtype, number):
maze = load_maze(mtype, number)
def test_analysis(mazes, mtype, number):
maze = mazes[mtype][number]
analysis = analyze(maze)
verify_matrices(maze, analysis)

Expand All @@ -92,8 +84,8 @@ def test_analysis(mtype, number):
('simple', 1, 1, 0), ('simple', 1, 1, 4), ('simple', 1, 3, 1),
('multigoal', 4, 0, 0), ('multigoal', 4, 3, 0)
])
def test_paths(mtype, number, row, column):
maze = load_maze(mtype, number)
def test_paths(mazes, mtype, number, row, column):
maze = mazes[mtype][number]
analysis = analyze(maze)
verify_path(maze, analysis, row, column)

Expand All @@ -102,10 +94,8 @@ def test_paths(mtype, number, row, column):
('simple', 1, 0, 0), ('multigoal', 4, 4, 0),
('unreachable', 1, 0, 0), ('unreachable', 2, 1, 1),
])
def test_paths_exception(mtype, number, row, column):
maze = load_maze(mtype, number)
def test_paths_exception(mazes, mtype, number, row, column):
maze = mazes[mtype][number]
analysis = analyze(maze)
with pytest.raises(NoPathExistsException):
analysis.path(row, column)

# TODO: better loading of mazes from files (parametric fixture?)

0 comments on commit 7136f19

Please sign in to comment.