From 822058b251814b3ee209d77b487aec46aaa7d288 Mon Sep 17 00:00:00 2001 From: Alexis Metaireau Date: Wed, 10 Aug 2011 17:49:35 +0200 Subject: [PATCH] Add some more tests about membership. (#14) Fixes #15 as invalid: deactivated users are not listed on the bill form or on the balance. --- budget/forms.py | 1 - budget/models.py | 2 +- budget/tests.py | 47 ++++++++++++++++++++++++++++++++++++++++++++++- budget/web.py | 2 +- 4 files changed, 48 insertions(+), 4 deletions(-) diff --git a/budget/forms.py b/budget/forms.py index 0c5394b..fe14288 100644 --- a/budget/forms.py +++ b/budget/forms.py @@ -62,7 +62,6 @@ class BillForm(Form): bill.what=self.what.data bill.date=self.date.data bill.owers = [Person.query.get(ower) for ower in self.payed_for.data] - print self.payed_for.data return bill diff --git a/budget/models.py b/budget/models.py index 66eb4b6..ab93548 100644 --- a/budget/models.py +++ b/budget/models.py @@ -48,7 +48,7 @@ class Person(db.Model): name = db.Column(db.UnicodeText) activated = db.Column(db.Boolean, default=True) - def is_used(self): + def has_bills(self): bills_as_ower_number = db.session.query(Bill).join(billowers, Person)\ .filter("Bill.id == billowers.bill_id")\ .filter("Person.id == billowers.person_id")\ diff --git a/budget/tests.py b/budget/tests.py index 2277301..595d1a1 100644 --- a/budget/tests.py +++ b/budget/tests.py @@ -1,3 +1,4 @@ + # -*- coding: utf-8 -*- import os import tempfile import unittest @@ -119,7 +120,7 @@ class BudgetTestCase(TestCase): # no new project added self.assertEqual(len(models.Project.query.all()), 1) - def test_add_member(self): + def test_membership(self): self.create_project("raclette") self.login("raclette") @@ -132,6 +133,50 @@ class BudgetTestCase(TestCase): # should not accept him self.assertEqual(len(models.Project.query.get("raclette").members), 1) + # add fred + self.app.post("/raclette/members/add", data={'name': 'fred' }) + self.assertEqual(len(models.Project.query.get("raclette").members), 2) + + # check fred is present in the bills page + result = self.app.get("/raclette/") + self.assertIn("fred", result.data) + + # remove fred + self.app.post("/raclette/members/%s/delete" % + models.Project.query.get("raclette").members[-1].id) + + # as fred is not bound to any bill, he is removed + self.assertEqual(len(models.Project.query.get("raclette").members), 1) + + # add fred again + self.app.post("/raclette/members/add", data={'name': 'fred' }) + fred_id = models.Project.query.get("raclette").members[-1].id + + # bound him to a bill + result = self.app.post("/raclette/add", data={ + 'date': '2011-08-10', + 'what': u'fromage à raclette', + 'payer': fred_id, + 'payed_for': [fred_id,], + 'amount': '25', + }) + + # remove fred + self.app.post("/raclette/members/%s/delete" % fred_id) + + # he is still in the database, but is deactivated + self.assertEqual(len(models.Project.query.get("raclette").members), 2) + self.assertEqual( + len(models.Project.query.get("raclette").active_members), 1) + + # as fred is now deactivated, check that he is not listed when adding + # a bill or displaying the balance + result = self.app.get("/raclette/") + self.assertNotIn("/raclette/members/%s/delete" % fred_id, result.data) + + result = self.app.get("/raclette/add") + self.assertNotIn("fred", result.data) + if __name__ == "__main__": unittest.main() diff --git a/budget/web.py b/budget/web.py index d529171..9240e9c 100644 --- a/budget/web.py +++ b/budget/web.py @@ -150,7 +150,7 @@ def add_member(project): def remove_member(project, member_id): person = Person.query.get_or_404(member_id) if person.project == project: - if not person.is_used(): + if not person.has_bills(): db.session.delete(person) db.session.commit() flash("User '%s' has been removed" % person.name)