Skip to content

Commit

Permalink
Merge pull request #21 from EboMike/cleanup
Browse files Browse the repository at this point in the history
First unit test.
  • Loading branch information
spookybear0 authored Mar 22, 2024
2 parents b36a1ca + 6b0d796 commit f39dd7e
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 12 deletions.
9 changes: 3 additions & 6 deletions helpers/statshelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,9 @@ async def get_average_team_score_at_time_sm5(team: Team, time: int) -> int:

async def count_zaps(game: SM5Game, zapping_entity_id: str, zapped_entity_id: str) -> int:
"""Returns the number of times one entity zapped another."""
return await (game.events.filter(
arguments__filter={"0": zapping_entity_id}
).filter(
arguments__filter={"1": " zaps "}
).filter(
arguments__filter={"2": zapped_entity_id}
return await (game.events.filter(entity1=zapping_entity_id,
action=" zaps ",
entity2=zapped_entity_id
).count())


Expand Down
6 changes: 0 additions & 6 deletions helpers/userhelper.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
from typing import List, Optional
import sys

def in_ipynb() -> bool:
return "ipykernel" in sys.modules

if not in_ipynb():
from shared import app
from db.game import EntityEnds, EntityStarts
from db.player import Player
from db.types import IntRole
Expand Down
1 change: 1 addition & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/helpers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

74 changes: 74 additions & 0 deletions tests/helpers/environment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
"""Basic test environment for all laserforce_ranking tests.
Sets up a Tortoise environment with an in-memory SQLite database, creates some basic test games, exposes their IDs, and
has functions to create specific scenarios for whatever is needed for a unit test.
For some unit tests, it's better to create minimal setups that just have the bare minimum necessary for the system under
test. But there are function where every part of the game matters, so we'll have this complete game here as a test
dataset.
"""
import json
from typing import Optional

from tortoise import Tortoise

from db.game import Events
from db.sm5 import SM5Game
from db.types import EventType, Team
from helpers.tdfhelper import create_event_from_data

ENTITY_ID_1 = "Entity#1"
ENTITY_ID_2 = "Entity#2"
ENTITY_ID_3 = "Entity#3"

_TEST_SM5_GAME: Optional[SM5Game] = None


def get_sm5_game_id() -> int:
"""Returns the game ID of the test SM5 game.
May only be called after the database has been initialized with setup_test_database()."""
assert _TEST_SM5_GAME
return _TEST_SM5_GAME.id


async def setup_test_database():
"""Creates a test in-memory database using SQLite, connects Tortoise to it, and generates the schema.
It will also generate the test dataset."""
global _TEST_SM5_GAME

await Tortoise.init(db_url="sqlite://:memory:",
modules={
"models": ["db.game", "db.laserball", "db.legacy", "db.player", "db.sm5", "aerich.models"]})

await Tortoise.generate_schemas()

_TEST_SM5_GAME = await create_sm5_game_1()


async def create_sm5_game_1() -> SM5Game:
events = []
events.extend([await create_zap_event(100, ENTITY_ID_1, ENTITY_ID_2),
await create_zap_event(200, ENTITY_ID_1, ENTITY_ID_2),
await create_zap_event(400, ENTITY_ID_2, ENTITY_ID_1),
await create_zap_event(500, ENTITY_ID_1, ENTITY_ID_2),
await Events.create(time=600, type=EventType.ACTIVATE_NUKE,
arguments=json.dumps([ENTITY_ID_2, " nukes ", ENTITY_ID_1])),
await create_zap_event(700, ENTITY_ID_3, ENTITY_ID_1),
await create_zap_event(800, ENTITY_ID_1, ENTITY_ID_3),
])

game = await SM5Game.create(winner_color=Team.RED.value.color, tdf_name="in_memory_test", file_version="0.test.0",
software_version="12.34.56", arena="Test Arena", mission_name="Space Marines 5",
mission_type=0, ranked=True, ended_early=False, start_time=2222222,
mission_duration=900000)

await game.events.add(*events)
await game.save()
return game


async def create_zap_event(time_millis: int, zapping_entity_id: str, zapped_entity_id: str) -> Events:
return await create_event_from_data(
["4", str(time_millis), EventType.DOWNED_OPPONENT, zapping_entity_id, " zaps ", zapped_entity_id])
19 changes: 19 additions & 0 deletions tests/helpers/statshelper_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import unittest

from db.sm5 import SM5Game
from helpers.statshelper import count_zaps
from tests.helpers.environment import setup_test_database, ENTITY_ID_1, ENTITY_ID_2, get_sm5_game_id


class TestStringMethods(unittest.IsolatedAsyncioTestCase):
async def asyncSetUp(self):
await setup_test_database()

async def test_count_zaps(self):
game = await SM5Game.filter(id=get_sm5_game_id()).first()
zaps = await count_zaps(game, ENTITY_ID_1, ENTITY_ID_2)
self.assertEqual(3, zaps)


if __name__ == '__main__':
unittest.main()

0 comments on commit f39dd7e

Please sign in to comment.