You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
2.5 KiB
76 lines
2.5 KiB
import unittest |
|
from datetime import datetime |
|
from app import create_app, db |
|
from app.models import User, Game, GamePlayer, Role |
|
from config import Config |
|
|
|
class TestConfig(Config): |
|
TESTING = True |
|
WTF_CSRF_ENABLED = False |
|
DEBUG = False |
|
SQLALCHEMY_DATABASE_URI = 'sqlite://' |
|
|
|
class UserCase(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_player_game_owner(self): |
|
g1 = Game(name='TestGame') |
|
g2 = Game(name='AnotherGame') |
|
|
|
u1 = User(name='Henk') |
|
u2 = User(name='Alfred') |
|
u3 = User(name='Sasha') |
|
|
|
g1.players.append(GamePlayer(user=u1, role=Role.owner)) |
|
g1.players.append(GamePlayer(user=u2, role=Role.bunny)) |
|
|
|
g2.players.append(GamePlayer(user=u1, role=Role.hunter)) |
|
g2.players.append(GamePlayer(user=u3, role=Role.bunny)) |
|
|
|
|
|
db.session.add(g1) |
|
db.session.add(g2) |
|
db.session.commit() |
|
|
|
self.assertTrue(u1.owns_game_played_by(user=u2), "owner owns subject_player's game") |
|
self.assertFalse(u1.owns_game_played_by(user=u3), "owner doesn't own subject_player's game") |
|
self.assertTrue(u1.owns_game_played_by(user=u1), "owner owns it own's game") |
|
|
|
def test_role_in_game(self): |
|
g1 = Game(name='TestGame') |
|
|
|
u1 = User(name='Henk') |
|
u2 = User(name='Alfred') |
|
u3 = User(name='Sasha') |
|
u4 = User(name='Demian') |
|
u5 = User(name='Karl') |
|
|
|
g1.players.append(GamePlayer(user=u1, role=Role.owner)) |
|
g1.players.append(GamePlayer(user=u2, role=Role.bunny)) |
|
g1.players.append(GamePlayer(user=u3, role=Role.hunter)) |
|
g1.players.append(GamePlayer(user=u4, role=Role.none)) |
|
|
|
db.session.add(g1) |
|
db.session.add(u5) |
|
db.session.commit() |
|
|
|
self.assertEqual(u1.role_in_game(g1), Role.owner) |
|
self.assertEqual(u2.role_in_game(g1), Role.bunny) |
|
self.assertEqual(u3.role_in_game(g1), Role.hunter) |
|
self.assertEqual(u4.role_in_game(g1), Role.none) |
|
self.assertEqual(u5.role_in_game(g1), None) |
|
with self.assertRaises(AttributeError): |
|
g1.get_role_for_game(None) |
|
|
|
if __name__ == '__main__': |
|
unittest.main(verbosity=2)
|
|
|