class TestManipulation(object): """Graph manipulation methods should work properly.""" @classmethod def setUp(self): """Set up a test DebtGraph.""" self.dg = DebtGraph({"alice": {"bob": 20, "charlie": 5}, "bob": {"alice": 10}, "charlie": {}}) # removal def test_remove_normal(self): """Should remove debt correctly.""" self.dg.remove("alice", "bob", 5) assert self.dg.graph == {"alice": {"bob": 15, "charlie": 5}, "bob": {"alice": 10}, "charlie": {}} def test_remove_deletion(self): """Should delete the node if more debt is removed than exists.""" self.dg.remove("alice", "bob", 25) assert self.dg.graph == {"alice": {"charlie": 5}, "bob": {"alice": 10}, "charlie": {}} @raises(KeyError) def test_remove_error(self): """Should throw an error when trying to remove a nonexistant debt.""" self.dg.remove("charlie", "bob", 4) # forgiveness def test_forgive_normal(self): """Forgive method should delete debts.""" self.dg.forgive("bob", "alice") assert self.dg.graph == {"alice": {"bob": 20, "charlie": 5}, "bob": {}, "charlie": {}} @raises(KeyError) def test_forgive_error(self): """Forgive method should throw an error when used on a nonexistant debt.""" self.dg.forgive("bob", "charlie") # addition def test_addition_normal(self): """Adding to an already existing debt should increase it.""" self.dg.add("alice", "bob", 5) assert self.dg.graph == {"alice": {"bob": 25, "charlie": 5}, "bob": {"alice": 10}, "charlie": {}} def test_addition_split(self): """Adding a debt with multiple debtors should split the debt.""" self.dg.add("charlie", ["alice", "bob"], 10) assert self.dg.graph == { "alice": {"bob": 20, "charlie": 5}, "bob": {"alice": 10}, "charlie": {"alice": 5, "bob": 5}, } def test_addition_split_self(self): """Adding a debt with multiple debtors including the creditor should spilt the debt between everyone except the creditor.""" self.dg.add("charlie", ["charlie", "alice", "bob"], 15) assert self.dg.graph == { "alice": {"bob": 20, "charlie": 5}, "bob": {"alice": 10}, "charlie": {"alice": 5, "bob": 5}, } def test_addition_with_ints(self): """Addition should work when the keys are ints.""" self.dg = DebtGraph({1: {2: 20, 3: 5}, 2: {1: 10}, 3: {}}) self.dg.add(1, 2, 5) assert self.dg.graph == {1: {2: 25, 3: 5}, 2: {1: 10}, 3: {}} @raises(TypeError) def test_addition_error1(self): """Addition should throw an error if not called using a list, int, or string.""" self.dg.add("alice", {}, 3) def test_addition_error2(self): """Addition should throw an error if any of the people specified don't exist.""" assert_raises(KeyError, lambda: self.dg.add("alice", "not here", 3)) assert_raises(KeyError, lambda: self.dg.add("not here", "alice", 3)) assert_raises(KeyError, lambda: self.dg.add("alice", ["bob", "NH"], 3)) # cancellation def test_cancel(self): """Canceling useless debts should produce the correct graph.""" self.dg.cancel() print self.dg.graph assert self.dg.graph == {"alice": {"bob": 10, "charlie": 5}, "bob": {}, "charlie": {}}