def test_double_forwarding(self): balances = Balances() balances.add('a', 1) balances.add('b', 2) balances.add('c', 3) balances.add('d', 4) balances.add('e', 5) balances.add('f', 6) balances.addFirstRef('a') balances.addFirstRef('b') balances.consumeAndForward(['a', 'b', 'c'], 'e') balances.consumeAndForward(['e'], 'f') self.assertEqual(balances.getEndOfForwardingChainFrom('a'), 'f') # can only forward from actual reference targets self.assertRaises(AssertionError, balances.getEndOfForwardingChainFrom, 'e') self.assertEqual(balances.getEndOfForwardingChainFrom('d'), 'd') self.assertEqual(balances.changeCounts['a'], 0) balances.removeRef('a') self.assertFalse('a' in balances.changeCounts) self.assertRaises(AssertionError, balances.getEndOfForwardingChainFrom, 'a') self.assertEqual(balances.getEndOfForwardingChainFrom('b'), 'f') self.assertTrue(balances.isReferenced('f')) self.assertDictEqual(balances.changeCounts, {'b': 0}) balances.removeRef('b') self.assertDictEqual(balances.changeCounts, {}) self.assertFalse('b' in balances.changeCounts) self.assertRaises(AssertionError, balances.getEndOfForwardingChainFrom, 'b') self.assertFalse(balances.isReferenced('f')) self.assertDictEqual(balances.balances, {'d': 4, 'f': 17})
def test_single_forwarding(self): balances = Balances() self.assertRaises(AssertionError, balances.getEndOfForwardingChainFrom, 'a') balances.add('a', 1) balances.add('b', 2) balances.add('c', 3) balances.add('d', 4) balances.add('e', 5) balances.add('f', 6) self.assertRaises(KeyError, balances.consumeAndForward, ['a', 'b', 'c'], 'g') self.assertDictEqual(balances.balances, { 'a': 1, 'b': 2, 'c': 3, 'e': 5, 'd': 4, 'f': 6 }) self.assertEqual(balances.getEndOfForwardingChainFrom('a'), 'a') balances.addFirstRef('a') balances.addFirstRef('b') balances.consumeAndForward(['a', 'b', 'c'], 'e') self.assertEqual(balances.getEndOfForwardingChainFrom('a'), 'e') self.assertRaises(AssertionError, balances.isReferenced, 'a') self.assertEqual(balances.changeCounts['a'], 0) self.assertEqual(balances.changeCounts['b'], 0) self.assertTrue(balances.isReferenced('e')) self.assertFalse(balances.isReferenced('f')) # cannot start adding refs after a balance has been consumed self.assertRaises(AssertionError, balances.addFirstRef, 'c') balances.addRef('a') balances.removeRef('a') self.assertEqual(balances.changeCounts['a'], 0) balances.removeRef('a') self.assertFalse('a' in balances.changeCounts) self.assertTrue(balances.isReferenced('e')) self.assertDictEqual(balances.changeCounts, {'b': 0}) balances.removeRef('b') self.assertFalse(balances.isReferenced('e')) self.assertDictEqual(balances.balances, {'e': 11, 'd': 4, 'f': 6}) self.assertDictEqual(balances.changeCounts, {})