import unittest import json from app import create_app, db from app.models import User, Game, Role, GamePlayer, Objective from config import Config class TestConfig(Config): TESTING = True WTF_CSRF_ENABLED = False DEBUG = False SQLALCHEMY_DATABASE_URI = 'sqlite://' class GamePlayerCase(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_default(self): g1 = Game(name='TestGame') u1 = User(name='Henk') p1 = GamePlayer(user=u1, role=Role.bunny) o1 = Objective(name='Objective 1') o2 = Objective(name='Objective 2') o3 = Objective(name='Objective 3') o1.set_hash() o2.set_hash() o3.set_hash() g1.players.append(p1) g1.objectives.append(o1) g1.objectives.append(o2) g1.objectives.append(o3) p1.found_objectives.append(o1) db.session.add(g1) db.session.commit() # Check if test initiaion succeeded self.assertEqual(len(Game.query.first().objectives), 3) self.assertEqual(User.query.first().user_games[0].found_objectives[0], o1) # The actual Test player_objectives = ('[{"name": "Objective 1", "longitude": null, "latitude": null, "found": true},' '{"name": "Objective 2", "longitude": null, "latitude": null, "found": false},' '{"name": "Objective 3", "longitude": null, "latitude": null, "found": false}]') self.assertEqual(p1.encode_objectives(), player_objectives) if __name__ == '__main__': unittest.main(verbosity=2)