import unittest 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 ObjectiveCase(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_objective_owner(self): g1 = Game(name='TestGame') g2 = Game(name='AnotherGame') u1 = User(name='Henk') g1.players.append(GamePlayer(user=u1, role=Role.owner)) g2.players.append(GamePlayer(user=u1, 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(u1)) self.assertFalse(o2.owned_by(u1)) def test_delete_objectives_recursively(self): g1 = Game(name='TestGame') u1 = User(name = 'Henk') p1 = GamePlayer(user=u1, role=Role.bunny) o1 = Objective(name='o1') o1.set_hash() o2 = Objective(name='o2') o2.set_hash() g1.players.append(p1) g1.objectives.append(o1) g1.objectives.append(o2) p1.found_objectives.append(o2) db.session.add(g1) db.session.commit() self.assertNotEqual(Objective.query.filter_by(name='o1').first(), None) self.assertNotEqual(Objective.query.filter_by(name='o2').first(), None) db.session.delete(g1) db.session.commit() self.assertEqual(Objective.query.filter_by(name='o1').first(), None) self.assertEqual(Objective.query.filter_by(name='o2').first(), None) if __name__ == '__main__': unittest.main(verbosity=2)