From b2c934ae46b7249faa37e13bdb8b1d80812dcdfc Mon Sep 17 00:00:00 2001 From: Scott Torborg Date: Sun, 8 Nov 2009 13:37:45 -0800 Subject: [PATCH] added some tests for settle() --- bluechips/lib/totals.py | 4 +-- bluechips/tests/lib/test_totals.py | 47 ++++++++++++++++++++++++++++++ bluechips/tests/lib/totals.py | 18 ------------ 3 files changed, 49 insertions(+), 20 deletions(-) create mode 100644 bluechips/tests/lib/test_totals.py delete mode 100644 bluechips/tests/lib/totals.py diff --git a/bluechips/lib/totals.py b/bluechips/lib/totals.py index 730ca64..e0c4559 100644 --- a/bluechips/lib/totals.py +++ b/bluechips/lib/totals.py @@ -94,9 +94,9 @@ def settle(debts_dict): settle_list.append((owes['who'], owed['who'], val)) if len(owes_list) > 0: - raise DirtyBooks, ("People still owe money", owes_list) #pragma:nocover + raise DirtyBooks, ("People still owe money", owes_list) if len(owed_list) > 0: - raise DirtyBooks, ("People are still owed money", owed_list) #pragma:nocover + raise DirtyBooks, ("People are still owed money", owed_list) return settle_list diff --git a/bluechips/tests/lib/test_totals.py b/bluechips/tests/lib/test_totals.py new file mode 100644 index 0000000..fb3e6f2 --- /dev/null +++ b/bluechips/tests/lib/test_totals.py @@ -0,0 +1,47 @@ +from unittest import TestCase +from bluechips.lib import totals + +class TestReorderingSettle(TestCase): + def test_transfer_minimized(self): + """ + Test that the number of transfers is actually minimized. + + Taken from a real-world situation, we discovered that failing + to re-order the debt lists after every transfer could lead to + extra, unnecessary transfers. + """ + self.assertEqual(len(totals.settle({'Alice': 100, + 'Bob': -85, + 'Charlie': 35, + 'Dave': -35, + 'Eve': -15})), + 3) + + def test_settle_even(self): + transfers = totals.settle({'Alice': 0, + 'Bob': 0, + 'Charlie': 0}) + assert transfers == [] + + def test_settle_positive(self): + transfers = totals.settle({'Alice': -50, + 'Bob': 100, + 'Charlie': -50}) + assert transfers == [('Bob', 'Charlie', 50), + ('Bob', 'Alice', 50)] + + def test_settle_uneven_positive(self): + try: + transfers = totals.settle({'Alice': -50, + 'Bob': -50, + 'Charlie': -50}) + except totals.DirtyBooks: + pass + + def test_settle_uneven_negative(self): + try: + transfers = totals.settle({'Alice': 50, + 'Bob': 50, + 'Charlie': 50}) + except totals.DirtyBooks: + pass diff --git a/bluechips/tests/lib/totals.py b/bluechips/tests/lib/totals.py deleted file mode 100644 index ac1d38c..0000000 --- a/bluechips/tests/lib/totals.py +++ /dev/null @@ -1,18 +0,0 @@ -from unittest import TestCase -from bluechips.lib import totals - -class TestReorderingSettle(TestCase): - def test(self): - """ - Test that the number of transfers is actually minimized. - - Taken from a real-world situation, we discovered that failing - to re-order the debt lists after every transfer could lead to - extra, unnecessary transfers. - """ - self.assertEqual(len(totals.settle({'Alice': 100, - 'Bob': -85, - 'Charlie': 35, - 'Dave': -35, - 'Eve': -15})), - 3) -- 2.45.2