Skip to content

Commit

Permalink
fix: update unit tests to pytest (#1497)
Browse files Browse the repository at this point in the history
* fix: update unit tests to pytest

* put name and version back

* classifiers and python_requires for pypi info

* package rename and cleanup

* update cypress' visitAGame test
  • Loading branch information
dionizh committed Mar 29, 2021
1 parent d274fc4 commit c6f05ca
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 185 deletions.
5 changes: 5 additions & 0 deletions aimmo-game-worker/setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[aliases]
test=pytest

[tool:pytest]
testpaths=tests
24 changes: 3 additions & 21 deletions aimmo-game-worker/setup.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,12 @@
# -*- coding: utf-8 -*-
from setuptools import find_packages, setup
import importlib.util


def get_version():
try:
version_spec = importlib.util.spec_from_file_location(
"aimmo_version", "../aimmo/__init__.py"
)
aimmo_version_module = importlib.util.module_from_spec(version_spec)
version_spec.loader.exec_module(aimmo_version_module)
return aimmo_version_module.__version__
except Exception:
return "0.0.0"


setup(
name="aimmo-avatar-api",
name="aimmo-game-worker",
packages=find_packages(),
version="0.0.0",
include_package_data=True,
tests_require=["httmock", "mock"],
tests_require=["pytest", "httmock", "mock"],
setup_requires=["pytest-runner"],
test_suite="tests",
classifiers=[
"Programming Language :: Python :: 3",
"Operating System :: OS Independent",
],
python_requires=">=3.6",
)
25 changes: 10 additions & 15 deletions aimmo-game-worker/tests/tests_simulation/test_avatar_state.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
from __future__ import absolute_import

from unittest import TestCase
import pytest

from simulation.location import Location
from simulation.avatar_state import create_avatar_state


class TestAvatarState(TestCase):
AVATAR = {"location": {"x": 0, "y": 0}, "backpack": [{"type": "key"}]}
@pytest.fixture
def avatar_state_json():
return {"location": {"x": 0, "y": 0}, "backpack": [{"type": "key"}]}


def test_create_avatar_state(self):
avatar_state = create_avatar_state(self.AVATAR)
avatar_loc = self.AVATAR["location"]
def test_create_avatar_state(avatar_state_json):
avatar_state = create_avatar_state(avatar_state_json)
avatar_loc = avatar_state_json["location"]

self.assertEqual(
avatar_state.location,
Location(avatar_loc["x"], avatar_loc["y"]),
)
assert avatar_state.location == Location(avatar_loc["x"], avatar_loc["y"])

self.assertEqual(
avatar_state.backpack[0].type, self.AVATAR["backpack"][0]["type"]
)
assert avatar_state.backpack[0].type == avatar_state_json["backpack"][0]["type"]
288 changes: 148 additions & 140 deletions aimmo-game-worker/tests/tests_simulation/test_world_map.py
Original file line number Diff line number Diff line change
@@ -1,147 +1,155 @@
from __future__ import absolute_import

from unittest import TestCase
import pytest

from simulation.location import Location
from simulation.world_map import WorldMap, WorldMapCreator, ARTEFACT_TYPES


class TestWorldMap(TestCase):
AVATAR = {
"location": {"x": 0, "y": 0},
"health": True,
"score": 3,
"backpack": [],
"events": [],
}

def _generate_cells(self, columns=3, rows=3):
cells = [
{
"location": {"x": x, "y": y},
"habitable": True,
"avatar": None,
"interactable": None,
}
for x in range(-columns // 2 + 1, 1 + columns // 2)
for y in range(-rows // 2 + 1, 1 + rows // 2)
]
return cells

def assertGridSize(self, map, expected_rows, expected_columns=None):
if expected_columns is None:
expected_columns = expected_rows
self.assertEqual(len(list(map.all_cells())), expected_rows * expected_columns)

def assertLocationsEqual(self, actual_cells, expected_locations):
actual_cells = list(actual_cells)
actual = frozenset(cell.location for cell in actual_cells)
self.assertEqual(actual, frozenset(expected_locations))
self.assertEqual(len(actual_cells), len(list(expected_locations)))

def test_grid_size(self):
map = WorldMapCreator.generate_world_map_from_cells_data(
self._generate_cells(1, 3)
)
self.assertGridSize(map, 1, 3)

def test_all_cells(self):
map = WorldMapCreator.generate_world_map_from_cells_data(self._generate_cells())
self.assertLocationsEqual(
map.all_cells(),
[Location(x, y) for x in range(-1, 2) for y in range(-1, 2)],
)

def test_score_cells(self):
cells = self._generate_cells()
cells[0]["interactable"] = {"type": "score"}
cells[8]["interactable"] = {"type": "score"}
map = WorldMapCreator.generate_world_map_from_cells_data(cells)
self.assertLocationsEqual(map.score_cells(), (Location(-1, -1), Location(1, 1)))

def test_interactable_cells(self):
cells = self._generate_cells()
cells[0]["interactable"] = {"type": "health"}
cells[8]["interactable"] = {"type": "damage_boost"}
map = WorldMapCreator.generate_world_map_from_cells_data(cells)
self.assertLocationsEqual(
map.interactable_cells(), (Location(-1, -1), Location(1, 1))
)

def test_artefact_cell(self):
cells = self._generate_cells()
cells[0]["interactable"] = {"type": ARTEFACT_TYPES[0]}
map = WorldMapCreator.generate_world_map_from_cells_data(cells)
self.assertEqual(map.get_cell(Location(-1, -1)).has_artefact(), True)

def test_location_is_visible(self):
map = WorldMapCreator.generate_world_map_from_cells_data(self._generate_cells())
for x in (0, 1):
for y in (0, 1):
self.assertTrue(map.is_visible(Location(x, y)))

def test_x_off_map_is_not_visible(self):
map = WorldMapCreator.generate_world_map_from_cells_data(self._generate_cells())
@pytest.fixture
def avatar_state_json():
return {"location": {"x": 0, "y": 0}, "backpack": [{"type": "key"}]}


def _generate_cells(columns=3, rows=3):
cells = [
{
"location": {"x": x, "y": y},
"habitable": True,
"avatar": None,
"interactable": None,
}
for x in range(-columns // 2 + 1, 1 + columns // 2)
for y in range(-rows // 2 + 1, 1 + rows // 2)
]
return cells


def assertGridSize(map, expected_rows, expected_columns=None):
if expected_columns is None:
expected_columns = expected_rows
assert len(list(map.all_cells())) == expected_rows * expected_columns


def assertLocationsEqual(actual_cells, expected_locations):
actual_cells = list(actual_cells)
actual = frozenset(cell.location for cell in actual_cells)
assert actual == frozenset(expected_locations)
assert len(actual_cells) == len(list(expected_locations))


def test_grid_size():
map = WorldMapCreator.generate_world_map_from_cells_data(_generate_cells(1, 3))
assertGridSize(map, 1, 3)


def test_all_cells():
map = WorldMapCreator.generate_world_map_from_cells_data(_generate_cells())
assertLocationsEqual(
map.all_cells(),
[Location(x, y) for x in range(-1, 2) for y in range(-1, 2)],
)


def test_score_cells():
cells = _generate_cells()
cells[0]["interactable"] = {"type": "score"}
cells[8]["interactable"] = {"type": "score"}
map = WorldMapCreator.generate_world_map_from_cells_data(cells)
assertLocationsEqual(map.score_cells(), (Location(-1, -1), Location(1, 1)))


def test_interactable_cells():
cells = _generate_cells()
cells[0]["interactable"] = {"type": "health"}
cells[8]["interactable"] = {"type": "damage_boost"}
map = WorldMapCreator.generate_world_map_from_cells_data(cells)
assertLocationsEqual(map.interactable_cells(), (Location(-1, -1), Location(1, 1)))


def test_artefact_cell():
cells = _generate_cells()
cells[0]["interactable"] = {"type": ARTEFACT_TYPES[0]}
map = WorldMapCreator.generate_world_map_from_cells_data(cells)
assert map.get_cell(Location(-1, -1)).has_artefact() == True


def test_location_is_visible():
map = WorldMapCreator.generate_world_map_from_cells_data(_generate_cells())
for x in (0, 1):
for y in (0, 1):
self.assertFalse(map.is_visible(Location(-2, y)))
self.assertFalse(map.is_visible(Location(2, y)))

def test_y_off_map_is_not_visible(self):
map = WorldMapCreator.generate_world_map_from_cells_data(self._generate_cells())
for x in (0, 1):
self.assertFalse(map.is_visible(Location(x, -2)))
self.assertFalse(map.is_visible(Location(x, 2)))

def test_get_valid_cell(self):
map = WorldMapCreator.generate_world_map_from_cells_data(self._generate_cells())
for x in (0, 1):
for y in (0, 1):
location = Location(x, y)
self.assertEqual(map.get_cell(location).location, location)

def test_get_x_off_map(self):
map = WorldMapCreator.generate_world_map_from_cells_data(self._generate_cells())
assert map.is_visible(Location(x, y)) == True


def test_x_off_map_is_not_visible():
map = WorldMapCreator.generate_world_map_from_cells_data(_generate_cells())
for y in (0, 1):
assert map.is_visible(Location(-2, y)) == False
assert map.is_visible(Location(2, y)) == False


def test_y_off_map_is_not_visible():
map = WorldMapCreator.generate_world_map_from_cells_data(_generate_cells())
for x in (0, 1):
assert map.is_visible(Location(x, -2)) == False
assert map.is_visible(Location(x, 2)) == False


def test_get_valid_cell():
map = WorldMapCreator.generate_world_map_from_cells_data(_generate_cells())
for x in (0, 1):
for y in (0, 1):
with self.assertRaises(KeyError):
map.get_cell(Location(-2, y))
with self.assertRaises(KeyError):
map.get_cell(Location(2, y))

def test_get_y_off_map(self):
map = WorldMapCreator.generate_world_map_from_cells_data(self._generate_cells())
for x in (0, 1):
with self.assertRaises(KeyError):
map.get_cell(Location(x, -2))
with self.assertRaises(KeyError):
map.get_cell(Location(x, 2))

def test_can_move_to(self):
map = WorldMapCreator.generate_world_map_from_cells_data(self._generate_cells())
target = Location(1, 1)
self.assertTrue(map.can_move_to(target))

def test_cannot_move_to_cell_off_grid(self):
map = WorldMapCreator.generate_world_map_from_cells_data(self._generate_cells())
target = Location(4, 1)
self.assertFalse(map.can_move_to(target))

def test_cannot_move_to_uninhabitable_cell(self):
cells = self._generate_cells()
cells[0]["obstacle"] = {"location": {"x": -1, "y": -1}}
map = WorldMapCreator.generate_world_map_from_cells_data(cells)
self.assertFalse(map.can_move_to(Location(-1, -1)))

def test_cannot_move_to_inhabited_cell(self):
cells = self._generate_cells()
cells[1]["avatar"] = self.AVATAR
map = WorldMapCreator.generate_world_map_from_cells_data(cells)
self.assertFalse(map.can_move_to(Location(-1, 0)))

def test_scan_nearby(self):
cells = self._generate_cells(5, 5)
cells[0]["avatar"] = self.AVATAR
cells[2]["obstacle"] = {"location": {"x": 0, "y": 0}}
cells[4]["interactable"] = {"type": ARTEFACT_TYPES[-1]}
map = WorldMapCreator.generate_world_map_from_cells_data(cells)
artefacts = map.scan_nearby(Location(-1, 0))
self.assertEqual(len(artefacts), 1)
location = Location(x, y)
assert map.get_cell(location).location == location


def test_get_x_off_map():
map = WorldMapCreator.generate_world_map_from_cells_data(_generate_cells())
for y in (0, 1):
with pytest.raises(KeyError):
map.get_cell(Location(-2, y))
with pytest.raises(KeyError):
map.get_cell(Location(2, y))


def test_get_y_off_map():
map = WorldMapCreator.generate_world_map_from_cells_data(_generate_cells())
for x in (0, 1):
with pytest.raises(KeyError):
map.get_cell(Location(x, -2))
with pytest.raises(KeyError):
map.get_cell(Location(x, 2))


def test_can_move_to():
map = WorldMapCreator.generate_world_map_from_cells_data(_generate_cells())
target = Location(1, 1)
assert map.can_move_to(target) == True


def test_cannot_move_to_cell_off_grid():
map = WorldMapCreator.generate_world_map_from_cells_data(_generate_cells())
target = Location(4, 1)
assert map.can_move_to(target) == False


def test_cannot_move_to_uninhabitable_cell():
cells = _generate_cells()
cells[0]["obstacle"] = {"location": {"x": -1, "y": -1}}
map = WorldMapCreator.generate_world_map_from_cells_data(cells)
assert map.can_move_to(Location(-1, -1)) == False


def test_cannot_move_to_inhabited_cell(avatar_state_json):
cells = _generate_cells()
cells[1]["avatar"] = avatar_state_json
map = WorldMapCreator.generate_world_map_from_cells_data(cells)
assert map.can_move_to(Location(-1, 0)) == False


def test_scan_nearby(avatar_state_json):
cells = _generate_cells(5, 5)
cells[0]["avatar"] = avatar_state_json
cells[2]["obstacle"] = {"location": {"x": 0, "y": 0}}
cells[4]["interactable"] = {"type": ARTEFACT_TYPES[-1]}
map = WorldMapCreator.generate_world_map_from_cells_data(cells)
artefacts = map.scan_nearby(Location(-1, 0))
assert len(artefacts) == 1
2 changes: 1 addition & 1 deletion aimmo_runner/build_worker_wheel.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ cd ..

# Copy wheel to django's static directory
mkdir -p aimmo/static/worker/
cp aimmo-game-worker/dist/aimmo_avatar_api-0.0.0-py3-none-any.whl aimmo/static/worker/aimmo_avatar_api-0.0.0-py3-none-any.whl
cp aimmo-game-worker/dist/*.whl aimmo/static/worker/
9 changes: 2 additions & 7 deletions game_frontend/cypress/support/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,8 @@ Cypress.Commands.add('addTestGame', () => {
Cypress.Commands.add('visitAGame', () => {
cy.request('/kurono/api/games/').then(response => {
const games = response.body
let testGameId
for (const [gameId, gameData] of Object.entries(games)) {
if (gameData['class_id'] == class_id && gameData['worksheet_id'] == worksheet_id) {
testGameId = gameId
break
}
}
// just get the first game it can find
let testGameId = Object.keys(games)[0]
cy.fixture('initialState.json').then(initialState => {
cy.visit(`/kurono/play/${testGameId}/`, {
onBeforeLoad: win => {
Expand Down
Loading

0 comments on commit c6f05ca

Please sign in to comment.