From 104126c7bfae5dce0dd58457c2db4e069a8bc724 Mon Sep 17 00:00:00 2001 From: Burathar Date: Wed, 19 Aug 2020 21:49:59 +0200 Subject: [PATCH] add tests and move methods from routes to models --- app/game_settings/routes.py | 10 ++---- app/main/routes_player.py | 11 ++---- app/models/game.py | 18 ++++++++++ app/models/tests/test_game.py | 58 ++++++++++++++++++++++++++++++ app/models/tests/test_objective.py | 11 ++++++ app/models/tests/test_user.py | 3 +- 6 files changed, 95 insertions(+), 16 deletions(-) diff --git a/app/game_settings/routes.py b/app/game_settings/routes.py index d5ae243..7d7c28a 100644 --- a/app/game_settings/routes.py +++ b/app/game_settings/routes.py @@ -54,19 +54,15 @@ def change_game_settings(game_name): db.session.commit() flash(f"'{game.name}' had been updated!") return redirect(url_for('main.game_dashboard', game_name=game.name)) - else: - flash_errors(form) + + flash_errors(form) return render_template('game_settings/edit_game.html', title='Chage Game Settings', form=form, game=game) @bp.route('/game//delete') @login_required def delete_game(game_name): game = get_game_if_owner(game_name) - db.session.delete(game) - for user in game.users: - if not user.last_login: - db.session.delete(user) - db.session.commit() + game.delete() flash(f"Game '{game.name}' has been deleted!") return redirect(url_for('main.index')) diff --git a/app/main/routes_player.py b/app/main/routes_player.py index 40edccf..7a60975 100644 --- a/app/main/routes_player.py +++ b/app/main/routes_player.py @@ -46,14 +46,9 @@ def add_player(game_name): def remove_player(game_name, username): game = get_game_if_owner(game_name) user = User.query.filter(and_(User.name == username, User.games.contains(game))).first_or_404() - if user.role_in_game(game) == Role.owner: - if len([player for player in game.players if player.role == Role.owner]) < 2: - flash("Can't remove only owner from game") - return redirect(url_for('main.game_dashboard', game_name=game.name)) - game.users.remove(user) - if not user.last_login: - db.session.delete(user) - db.session.commit() + warning = game.remove_player(user) + if warning: + flash(warning) return redirect(url_for('main.game_dashboard', game_name=game.name)) @bp.route('/game//user/', methods=['GET', 'POST']) diff --git a/app/models/game.py b/app/models/game.py index 2822301..2a69be8 100644 --- a/app/models/game.py +++ b/app/models/game.py @@ -95,3 +95,21 @@ class Game(db.Model): if self.hidden: return GameState.hidden return GameState.finished + + def delete(self): + db.session.delete(self) + for user in self.users: + if not user.last_login: + db.session.delete(user) + db.session.commit() + + def remove_player(self, user): + # pylint: disable=not-an-iterable + if user.role_in_game(self) == Role.owner: + if len([player for player in self.players if player.role == Role.owner]) < 2: + return "Can't remove only owner from game" + self.users.remove(user) + if not user.last_login: + db.session.delete(user) + db.session.commit() + return False diff --git a/app/models/tests/test_game.py b/app/models/tests/test_game.py index fb5709c..ee441e6 100644 --- a/app/models/tests/test_game.py +++ b/app/models/tests/test_game.py @@ -1,4 +1,5 @@ import unittest +from datetime import datetime from app import create_app, db from app.models import User, Game, Role, GamePlayer from config import Config @@ -36,5 +37,62 @@ class GameCase(unittest.TestCase): self.assertTrue(g1.owned_by(u1)) self.assertFalse(g1.owned_by(u2)) + def test_delete(self): + g1 = Game(name='TestGame') + u1 = User(name='Henk') + u2 = User(name='Karel') + p1 = GamePlayer(user=u1, role=Role.bunny) + p2 = GamePlayer(user=u2, role=Role.bunny) + + g1.players.append(p1) + g1.players.append(p2) + + db.session.add(g1) + db.session.commit() + + u2.last_login = datetime.now() + db.session.commit() + + self.assertNotEqual(User.query.filter_by(name='Henk').first(), None) + self.assertNotEqual(User.query.filter_by(name='Karel').first(), None) + + g1.delete() + + self.assertEqual(User.query.filter_by(name='Henk').first(), None) + self.assertNotEqual(User.query.filter_by(name='Karel').first(), None) + self.assertEqual(Game.query.filter_by(name='TestGame').first(), None) + + def test_remove_player(self): + g1 = Game(name='TestGame') + g2 = Game(name='TestGame2') + u1 = User(name='Henk') + u2 = User(name='Karel') + p1_1 = GamePlayer(user=u1, role=Role.owner) + p1_2 = GamePlayer(user=u2, role=Role.bunny) + p2_1 = GamePlayer(user=u1, role=Role.owner) + p2_2 = GamePlayer(user=u2, role=Role.owner) + + g1.players.append(p1_1) + g1.players.append(p1_2) + + g2.players.append(p2_1) + g2.players.append(p2_2) + + db.session.add(g1) + db.session.add(g2) + db.session.commit() + + self.assertTrue(g1.remove_player(u1)) + self.assertFalse(g1.remove_player(u2)) + self.assertFalse(g2.remove_player(u1)) + self.assertTrue(g2.remove_player(u2)) + + db.session.commit() + + self.assertTrue(p1_1 in g1.players) + self.assertTrue(p1_2 not in g1.players) + self.assertTrue(p2_1 not in g2.players) + self.assertTrue(p2_2 in g2.players) + if __name__ == '__main__': unittest.main(verbosity=2) diff --git a/app/models/tests/test_objective.py b/app/models/tests/test_objective.py index 8167c62..a5e3219 100644 --- a/app/models/tests/test_objective.py +++ b/app/models/tests/test_objective.py @@ -47,19 +47,30 @@ class ObjectiveCase(unittest.TestCase): 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) diff --git a/app/models/tests/test_user.py b/app/models/tests/test_user.py index 55cb240..659c9b2 100644 --- a/app/models/tests/test_user.py +++ b/app/models/tests/test_user.py @@ -1,4 +1,5 @@ import unittest +from datetime import datetime from app import create_app, db from app.models import User, Game, GamePlayer, Role from config import Config @@ -70,6 +71,6 @@ class UserCase(unittest.TestCase): 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)