def test_record_transfer_invalid_participant(self):
        amount = decimal.Decimal(1)

        with self.db.get_connection() as conn:
            cur = conn.cursor()
            with self.assertRaises(IntegrityError):
                billing.record_transfer(cur, 'idontexist', 'nori', amount)
    def test_record_transfer(self):
        amount = decimal.Decimal(1)

        # check with db that amount is what we expect
        def assert_transfer(recipient, amount):
            transfer_sql = '''
                select sum(amount) as sum
                from transfers
                where tippee = %s
            '''
            result = self.db.fetchone(transfer_sql, (recipient,))
            self.assertEqual(result['sum'], amount)

        recipients = [
            'jim', 'jim', 'kate', 'bob',
        ]
        seen = []

        for recipient in recipients:
            if not recipient in seen:
                self._create_participant(recipient)
                seen.append(recipient)

        with self.db.get_connection() as conn:
            cur = conn.cursor()

            for recipient in recipients:
                billing.record_transfer(cur,
                                        self.participant_id,
                                        recipient,
                                        amount)

            conn.commit()

        assert_transfer('jim', amount * 2)
        assert_transfer('kate', amount)
        assert_transfer('bob', amount)