def test_direct_refs(self): balances = Balances() self.assertRaises(AssertionError, balances.isReferenced, 'a') balances.add('a', 1) # can only add state changes to direct ref targets self.assertRaises(KeyError, balances.addStateChange, 'a') # must add first ref explicitly self.assertRaises(AssertionError, balances.addRef, 'a') self.assertFalse(balances.isReferenced('a')) balances.addFirstRef('a') self.assertTrue(balances.isReferenced('a')) self.assertEqual(balances.changeCounts['a'], 0) balances.addStateChange('a') self.assertEqual(balances.changeCounts['a'], 1) balances.addRef('a') balances.addRef('a') balances.removeRef('a') self.assertTrue(balances.isReferenced('a')) self.assertEqual(balances.changeCounts['a'], 1) balances.removeRef('a') self.assertTrue(balances.isReferenced('a')) self.assertEqual(balances.changeCounts['a'], 1) balances.removeRef('a') self.assertFalse(balances.isReferenced('a')) self.assertFalse('a' in balances.changeCounts)
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, {})