Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Commit

Permalink
Added the tests for service.py from the master branch adapted to the …
Browse files Browse the repository at this point in the history
…new API. We still need to test the endpoints of the flask application.
  • Loading branch information
danalex97 committed Jul 26, 2017
1 parent 3ffba31 commit 9b0b4fe
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 16 deletions.
25 changes: 14 additions & 11 deletions aimmo-game/simulation/custom_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@ class BaseGenerator(object):
A map generator that exposes a game state and a check for level completion.
API:
- contructor(setting)
- a set of basic settings that the map uses at generation
- contructor(setting)
- a set of basic settings that the map uses at generation
- see DEFAULT_LEVEL_SETTINGS in simulation.world_map
- get_game_state(avatar_manager)
- exposes a game state used by the turn manager daemon
- for details see GameState
- for details see GameState
- check_complete(game_state)
- function to check if a map is "complete"
- the turn manager runs the action for each avatar, then runs check_complete afterwards
@abstract
- get_map
- returns the generated map
Important: for the moment level configurations are found directly by their name by looking into
the module map_generator - look in service.py for:
generator = getattr(map_generator, settings['GENERATOR'])(settings)
Important: for the moment level configurations are found directly by their name by looking into
the module map_generator - look in service.py for:
generator = getattr(map_generator, settings['GENERATOR'])(settings)
"""
__metaclass__ = abc.ABCMeta

Expand Down Expand Up @@ -109,8 +109,8 @@ class Decoder():
A Decorer is a class that receives a Json formatted as in levels/models
and decodes it, altering the state of the world_map.
Decoders are used to translate a Level from the JSON format to the internal
Decoders are used to translate a Level from the JSON format to the internal
state of a map(i.e. a WorldMap).
An example of a JSon format is:
Expand Down Expand Up @@ -160,10 +160,13 @@ class JsonLevelGenerator(BaseLevelGenerator):
- register the json that represents the map
- register the decoders that tranform the jsons into WorldMap objects
- decode the map applying the decoder to each of the jsons
All the levels can be found in json format in levels.LEVELS.
All the levels can be found in json format in levels.LEVELS.
To register a level extend this class.
"""
def __init__(self, *args, **kwargs):
super(JsonLevelGenerator, self).__init__(*args, **kwargs)

def _setup_meta(self):
# Used so that the map dimension does not increase automatically
self.settings["TARGET_NUM_CELLS_PER_AVATAR"] = -1000
Expand Down
57 changes: 54 additions & 3 deletions aimmo-game/tests/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,71 @@
from simulation.turn_manager import state_provider
from simulation.world_map import WorldMap
from simulation.world_state import WorldState
from simulation.avatar.avatar_manager import AvatarManager

import service

from .test_simulation.dummy_avatar import MoveEastDummy
from .test_simulation.maps import MockPickup
from .test_simulation.test_world_map import MockCell

class SimpleAvatarManager(object):
avatars = [MoveEastDummy(1, Location(0, -1))]
class SimpleAvatarManager(AvatarManager):
def __init__(self):
avatar = MoveEastDummy(1, Location(0, -1))
self.avatars_by_id = {
1 : avatar
}

# TODO: Write test for the new API...
class TestService(TestCase):
def setUp(self):
self.user_id = 1

def test_healthy(self):
service.app.config['TESTING'] = True
self.app = service.app.test_client()
response = self.app.get('/')
self.assertEqual(response.data, 'HEALTHY')

def setup_world(self):
avatar_manager = SimpleAvatarManager()
CELLS = [
[
{'pickup': MockPickup('b'), 'avatar': avatar_manager.get_avatar(self.user_id)},
{},
{'generates_score': True},
],
[
{},
{'habitable': False},
{'pickup': MockPickup('a')},
],
]
grid = {Location(x, y-1): MockCell(Location(x, y-1), **CELLS[x][y])
for y in xrange(3) for x in xrange(2)}
state_provider.set_world(GameState(WorldMap(grid, {}), avatar_manager))

world_state = WorldState(state_provider, self.user_id)
world_state.ready_to_update = True

return world_state.get_updates()

def test_player_dict(self):
player_list = self.setup_world()['players']['create']
self.assertEqual(len(player_list), 1)
details = player_list[0]
self.assertEqual(details['id'], 1)
self.assertEqual(details['x'], 0)
self.assertEqual(details['y'], -1)
self.assertEqual(details['health'], 5)
self.assertEqual(details['score'], 0)

def test_score_locations(self):
result = self.setup_world()['map_features']['score_point']['create']
self.assertEqual(result[0]['x'], 0)
self.assertEqual(result[0]['y'], 1)

def test_pickup_list(self):
result = self.setup_world()['map_features']['pickup']['create']
pickup_pos_list = [(pickup['x'], pickup['y']) for pickup in result]
self.assertIn((1, 1), pickup_pos_list)
self.assertIn((0, -1), pickup_pos_list)
2 changes: 0 additions & 2 deletions aimmo-game/tests/test_simulation/levels/test_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,6 @@ def test_map_apply_transforms(self):
self.__with_micro_map()
self.__with_big_model()

pprint(self.parser.map_apply_transforms())

self.assertItemsEqual(self.parser.map_apply_transforms(), [{
"code": "0",
"test" : "zero"
Expand Down
2 changes: 2 additions & 0 deletions aimmo-game/tests/test_simulation/maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ def __init__(self, location=1, habitable=True, generates_score=False,
self.name = name
self.actions = actions
self.partially_fogged = False
self.remove_from_scene = None
self.add_to_scene = None

def __eq__(self, other):
return self is other
Expand Down

0 comments on commit 9b0b4fe

Please sign in to comment.