Browse Source

add tests and move methods from routes to models

testing
Burathar 4 years ago
parent
commit
104126c7bf
  1. 8
      app/game_settings/routes.py
  2. 11
      app/main/routes_player.py
  3. 18
      app/models/game.py
  4. 58
      app/models/tests/test_game.py
  5. 11
      app/models/tests/test_objective.py
  6. 1
      app/models/tests/test_user.py

8
app/game_settings/routes.py

@ -54,7 +54,7 @@ def change_game_settings(game_name): @@ -54,7 +54,7 @@ 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)
return render_template('game_settings/edit_game.html', title='Chage Game Settings', form=form, game=game)
@ -62,11 +62,7 @@ def change_game_settings(game_name): @@ -62,11 +62,7 @@ def change_game_settings(game_name):
@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'))

11
app/main/routes_player.py

@ -46,14 +46,9 @@ def add_player(game_name): @@ -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/<game_name>/user/<username>', methods=['GET', 'POST'])

18
app/models/game.py

@ -95,3 +95,21 @@ class Game(db.Model): @@ -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

58
app/models/tests/test_game.py

@ -1,4 +1,5 @@ @@ -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): @@ -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)

11
app/models/tests/test_objective.py

@ -47,19 +47,30 @@ class ObjectiveCase(unittest.TestCase): @@ -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)

1
app/models/tests/test_user.py

@ -1,4 +1,5 @@ @@ -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

Loading…
Cancel
Save