Burathar
5 years ago
3 changed files with 172 additions and 28 deletions
@ -0,0 +1,113 @@
@@ -0,0 +1,113 @@
|
||||
import unittest |
||||
from app import create_app, db |
||||
from app.models import Player, Game, Role, GamePlayer, Objective, ObjectiveMinimalEncoder, LocationEncoder |
||||
from config import Config |
||||
|
||||
class TestConfig(Config): |
||||
TESTING = True |
||||
WTF_CSRF_ENABLED = False |
||||
DEBUG = False |
||||
SQLALCHEMY_DATABASE_URI = 'sqlite://' |
||||
|
||||
class ModelsCase(unittest.TestCase): |
||||
# implement this: https://stackoverflow.com/questions/47294304/how-to-mock-current-user-in-flask-templates |
||||
def setUp(self): |
||||
self.app = create_app(TestConfig) |
||||
self.app_context = self.app.app_context() |
||||
self.app_context.push() |
||||
db.create_all() |
||||
|
||||
def tearDown(self): |
||||
db.session.remove() |
||||
db.drop_all() |
||||
self.app_context.pop() |
||||
|
||||
def test_is_game_owner(self): |
||||
g1 = Game(name='TestGame') |
||||
p1 = Player(name='Henk') |
||||
p2 = Player(name='Alfred') |
||||
|
||||
g1.game_players.append(GamePlayer(player=p1, role=Role.owner)) |
||||
g1.game_players.append(GamePlayer(player=p2, role=Role.bunny)) |
||||
|
||||
db.session.add(g1) |
||||
db.session.commit() |
||||
|
||||
self.assertTrue(g1.is_game_owner(p1)) |
||||
self.assertFalse(g1.is_game_owner(p2)) |
||||
|
||||
def test_is_player_game_owner(self): |
||||
g1 = Game(name='TestGame') |
||||
g2 = Game(name='AnotherGame') |
||||
|
||||
p1 = Player(name='Henk') |
||||
p2 = Player(name='Alfred') |
||||
p3 = Player(name='Sasha') |
||||
|
||||
g1.game_players.append(GamePlayer(player=p1, role=Role.owner)) |
||||
g1.game_players.append(GamePlayer(player=p2, role=Role.bunny)) |
||||
|
||||
g2.game_players.append(GamePlayer(player=p1, role=Role.hunter)) |
||||
g2.game_players.append(GamePlayer(player=p3, role=Role.bunny)) |
||||
|
||||
|
||||
db.session.add(g1) |
||||
db.session.add(g2) |
||||
db.session.commit() |
||||
|
||||
self.assertTrue(p1.owns_game_played_by(player=p2), "owner owns subject_player's game") |
||||
self.assertFalse(p1.owns_game_played_by(player=p3), "owner doesn't own subject_player's game") |
||||
self.assertTrue(p1.owns_game_played_by(player=p1), "owner owns it own's game") |
||||
|
||||
|
||||
def test_is_objective_owner(self): |
||||
g1 = Game(name='TestGame') |
||||
g2 = Game(name='AnotherGame') |
||||
p1 = Player(name='Henk') |
||||
|
||||
g1.game_players.append(GamePlayer(player=p1, role=Role.owner)) |
||||
g2.game_players.append(GamePlayer(player=p1, role=Role.bunny)) |
||||
|
||||
o1 = Objective(name='o1') |
||||
o1.set_hash() |
||||
o2 = Objective(name='o2') |
||||
o2.set_hash() |
||||
|
||||
g1.objectives.append(o1) |
||||
g2.objectives.append(o2) |
||||
|
||||
db.session.add(g1) |
||||
db.session.add(g2) |
||||
db.session.commit() |
||||
|
||||
self.assertTrue(o1.owned_by(p1)) |
||||
self.assertFalse(o2.owned_by(p1)) |
||||
|
||||
def test_role_in_game(self): |
||||
g1 = Game(name='TestGame') |
||||
|
||||
p1 = Player(name='Henk') |
||||
p2 = Player(name='Alfred') |
||||
p3 = Player(name='Sasha') |
||||
p4 = Player(name='Demian') |
||||
p5 = Player(name='Karl') |
||||
|
||||
g1.game_players.append(GamePlayer(player=p1, role=Role.owner)) |
||||
g1.game_players.append(GamePlayer(player=p2, role=Role.bunny)) |
||||
g1.game_players.append(GamePlayer(player=p3, role=Role.hunter)) |
||||
g1.game_players.append(GamePlayer(player=p4, role=Role.none)) |
||||
|
||||
db.session.add(g1) |
||||
db.session.add(p5) |
||||
db.session.commit() |
||||
|
||||
self.assertEqual(p1.role_in_game(g1), Role.owner) |
||||
self.assertEqual(p2.role_in_game(g1), Role.bunny) |
||||
self.assertEqual(p3.role_in_game(g1), Role.hunter) |
||||
self.assertEqual(p4.role_in_game(g1), Role.none) |
||||
self.assertEqual(p5.role_in_game(g1), None) |
||||
with self.assertRaises(AttributeError): |
||||
g1.get_role_for_game(None) |
||||
|
||||
if __name__ == '__main__': |
||||
unittest.main(verbosity=2) |
Loading…
Reference in new issue