class TestBillingTransfer(Harness):
    def setUp(self):
        super(Harness, self).setUp()
        self.payday = Payday(self.postgres)
        self.payday.start()
        self.tipper = self.make_participant('lgtest')
        self.balanced_account_uri = '/v1/marketplaces/M123/accounts/A123'

    def test_transfer(self):
        amount = Decimal('1.00')
        sender = self.make_participant('test_transfer_sender', pending=0,
                                       balance=1)
        recipient = self.make_participant('test_transfer_recipient', pending=0,
                                          balance=1)

        result = self.payday.transfer( sender.username
                                     , recipient.username
                                     , amount
                                      )
        assert_equals(result, True)

        # no balance remaining for a second transfer
        result = self.payday.transfer( sender.username
                                     , recipient.username
                                     , amount
                                      )
        assert_equals(result, False)

    def test_debit_participant(self):
        amount = Decimal('1.00')
        subject = self.make_participant('test_debit_participant', pending=0,
                                        balance=1)

        initial_amount = subject.balance

        with self.postgres.get_connection() as connection:
            cursor = connection.cursor()

            self.payday.debit_participant(cursor, subject.username, amount)
            connection.commit()

        self.session.refresh(subject)

        expected = initial_amount - amount
        actual = subject.balance
        assert_equals(actual, expected)

        # this will fail because not enough balance
        with self.postgres.get_connection() as conn:
            cur = conn.cursor()

            with self.assertRaises(IntegrityError):
                self.payday.debit_participant(cur, subject.username, amount)
            conn.commit()

    def test_skim_credit(self):
        actual = skim_credit(Decimal('10.00'))
        assert actual == (Decimal('10.00'), Decimal('0.00')), actual

    def test_credit_participant(self):
        amount = Decimal('1.00')
        subject = self.make_participant('test_credit_participant', pending=0,
                                        balance=1)

        initial_amount = subject.pending

        with self.postgres.get_connection() as conn:
            cur = conn.cursor()
            self.payday.credit_participant(cur, subject.username, amount)
            conn.commit()

        self.session.refresh(subject)

        expected = initial_amount + amount
        actual = subject.pending
        assert_equals(actual, expected)

    def test_record_transfer(self):
        from gittip.models import Transfer
        amount = Decimal('1.00')
        subjects = ['jim', 'kate', 'bob']

        for subject in subjects:
            self.make_participant(subject, balance=1, pending=0)

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

            # Tip 'jim' twice
            for recipient in ['jim'] + subjects:
                self.payday.record_transfer( cur
                                           , self.tipper.username
                                           , recipient
                                           , amount
                                            )
            conn.commit()

        for subject in subjects:
            # 'jim' is tipped twice
            expected = amount * 2 if subject == 'jim' else amount
            transfers = Transfer.query.filter_by(tippee=subject).all()
            actual = sum(tip.amount for tip in transfers)
            assert_equals(actual, expected)

    def test_record_transfer_invalid_participant(self):
        amount = Decimal('1.00')

        with self.postgres.get_connection() as conn:
            cur = conn.cursor()
            with assert_raises(IntegrityError):
                self.payday.record_transfer(cur, 'idontexist', 'nori', amount)
            conn.commit()

    def test_mark_transfer(self):
        amount = Decimal('1.00')

        # Forces a load with current state in dict
        before_transfer = PaydayModel.query.first().attrs_dict()

        with self.postgres.get_connection() as conn:
            cur = conn.cursor()
            self.payday.mark_transfer(cur, amount)
            conn.commit()

        # Forces a load with current state in dict
        after_transfer = PaydayModel.query.first().attrs_dict()

        expected = before_transfer['ntransfers'] + 1
        actual = after_transfer['ntransfers']
        assert_equals(actual, expected)

        expected = before_transfer['transfer_volume'] + amount
        actual = after_transfer['transfer_volume']
        assert_equals(actual, expected)

    def test_record_credit_updates_balance(self):
        alice = self.make_participant("alice")
        self.payday.record_credit( amount=Decimal("-1.00")
                                 , fee=Decimal("0.41")
                                 , error=""
                                 , username="******"
                                  )
        assert_equals(alice.balance, Decimal("0.59"))

    def test_record_credit_doesnt_update_balance_if_error(self):
        alice = self.make_participant("alice")
        self.payday.record_credit( amount=Decimal("-1.00")
                                 , fee=Decimal("0.41")
                                 , error="SOME ERROR"
                                 , username="******"
                                  )
        assert_equals(alice.balance, Decimal("0.00"))
class TestBillingTransfer(TestPaydayBase):
    def setUp(self):
        super(TestPaydayBase, self).setUp()
        self.payday = Payday(self.db)
        self.payday.start()
        self.tipper = self.make_participant('lgtest')
        self.balanced_account_uri = '/v1/marketplaces/M123/accounts/A123'

    def test_transfer(self):
        amount = Decimal('1.00')
        sender = self.make_participant('test_transfer_sender', pending=0,
                                       balance=1)
        recipient = self.make_participant('test_transfer_recipient', pending=0,
                                          balance=1)

        result = self.payday.transfer( sender.username
                                     , recipient.username
                                     , amount
                                      )
        assert result == True

        # no balance remaining for a second transfer
        result = self.payday.transfer( sender.username
                                     , recipient.username
                                     , amount
                                      )
        assert result == False

    def test_debit_participant(self):
        amount = Decimal('1.00')
        subject = self.make_participant('test_debit_participant', pending=0,
                                        balance=1)

        initial_amount = subject.balance

        with self.db.get_cursor() as cursor:
            self.payday.debit_participant(cursor, subject.username, amount)

        subject = Participant.from_username('test_debit_participant')

        expected = initial_amount - amount
        actual = subject.balance
        assert actual == expected

        # this will fail because not enough balance
        with self.db.get_cursor() as cursor:
            with self.assertRaises(IntegrityError):
                self.payday.debit_participant(cursor, subject.username, amount)

    def test_skim_credit(self):
        actual = skim_credit(Decimal('10.00'))
        assert actual == (Decimal('10.00'), Decimal('0.00'))

    def test_credit_participant(self):
        amount = Decimal('1.00')
        subject = self.make_participant('test_credit_participant', pending=0,
                                        balance=1)

        initial_amount = subject.pending

        with self.db.get_cursor() as cursor:
            self.payday.credit_participant(cursor, subject.username, amount)

        subject = Participant.from_username('test_credit_participant') # reload

        expected = initial_amount + amount
        actual = subject.pending
        assert actual == expected

    def test_record_transfer(self):
        amount = Decimal('1.00')
        subjects = ['jim', 'kate', 'bob']

        for subject in subjects:
            self.make_participant(subject, balance=1, pending=0)

        with self.db.get_cursor() as cursor:
            # Tip 'jim' twice
            for recipient in ['jim'] + subjects:
                self.payday.record_transfer( cursor
                                           , self.tipper.username
                                           , recipient
                                           , amount
                                            )

        for subject in subjects:
            # 'jim' is tipped twice
            expected = amount * 2 if subject == 'jim' else amount
            actual = self.db.one( "SELECT sum(amount) FROM transfers "
                                  "WHERE tippee=%s"
                                , (subject,)
                                 )
            assert actual == expected

    def test_record_transfer_invalid_participant(self):
        amount = Decimal('1.00')

        with self.db.get_cursor() as cursor:
            with self.assertRaises(IntegrityError):
                self.payday.record_transfer( cursor
                                           , 'idontexist'
                                           , 'nori'
                                           , amount
                                            )

    def test_mark_transfer(self):
        amount = Decimal('1.00')

        # Forces a load with current state in dict
        before_transfer = self.fetch_payday()

        with self.db.get_cursor() as cursor:
            self.payday.mark_transfer(cursor, amount)

        # Forces a load with current state in dict
        after_transfer = self.fetch_payday()

        expected = before_transfer['ntransfers'] + 1
        actual = after_transfer['ntransfers']
        assert actual == expected

        expected = before_transfer['transfer_volume'] + amount
        actual = after_transfer['transfer_volume']
        assert actual == expected

    def test_record_credit_updates_balance(self):
        self.payday.record_credit( amount=Decimal("-1.00")
                                 , fee=Decimal("0.41")
                                 , error=""
                                 , username="******"
                                  )
        alice = Participant.from_username('alice')
        assert alice.balance == Decimal("0.59")

    def test_record_credit_doesnt_update_balance_if_error(self):
        self.payday.record_credit( amount=Decimal("-1.00")
                                 , fee=Decimal("0.41")
                                 , error="SOME ERROR"
                                 , username="******"
                                  )
        alice = Participant.from_username('alice')
        assert alice.balance == Decimal("0.00")
class TestBillingTransfer(Harness):
    def setUp(self):
        super(Harness, self).setUp()
        self.payday = Payday(self.postgres)
        self.payday.start()
        self.tipper = self.make_participant('lgtest')
        self.balanced_account_uri = '/v1/marketplaces/M123/accounts/A123'

    def test_transfer(self):
        amount = Decimal('1.00')
        sender = self.make_participant('test_transfer_sender',
                                       pending=0,
                                       balance=1)
        recipient = self.make_participant('test_transfer_recipient',
                                          pending=0,
                                          balance=1)

        result = self.payday.transfer(sender.username, recipient.username,
                                      amount)
        assert_equals(result, True)

        # no balance remaining for a second transfer
        result = self.payday.transfer(sender.username, recipient.username,
                                      amount)
        assert_equals(result, False)

    def test_debit_participant(self):
        amount = Decimal('1.00')
        subject = self.make_participant('test_debit_participant',
                                        pending=0,
                                        balance=1)

        initial_amount = subject.balance

        with self.postgres.get_connection() as connection:
            cursor = connection.cursor()

            self.payday.debit_participant(cursor, subject.username, amount)
            connection.commit()

        self.session.refresh(subject)

        expected = initial_amount - amount
        actual = subject.balance
        assert_equals(actual, expected)

        # this will fail because not enough balance
        with self.postgres.get_connection() as conn:
            cur = conn.cursor()

            with self.assertRaises(IntegrityError):
                self.payday.debit_participant(cur, subject.username, amount)
            conn.commit()

    def test_skim_credit(self):
        actual = skim_credit(Decimal('10.00'))
        assert actual == (Decimal('10.00'), Decimal('0.00')), actual

    def test_credit_participant(self):
        amount = Decimal('1.00')
        subject = self.make_participant('test_credit_participant',
                                        pending=0,
                                        balance=1)

        initial_amount = subject.pending

        with self.postgres.get_connection() as conn:
            cur = conn.cursor()
            self.payday.credit_participant(cur, subject.username, amount)
            conn.commit()

        self.session.refresh(subject)

        expected = initial_amount + amount
        actual = subject.pending
        assert_equals(actual, expected)

    def test_record_transfer(self):
        from gittip.models import Transfer
        amount = Decimal('1.00')
        subjects = ['jim', 'kate', 'bob']

        for subject in subjects:
            self.make_participant(subject, balance=1, pending=0)

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

            # Tip 'jim' twice
            for recipient in ['jim'] + subjects:
                self.payday.record_transfer(cur, self.tipper.username,
                                            recipient, amount)
            conn.commit()

        for subject in subjects:
            # 'jim' is tipped twice
            expected = amount * 2 if subject == 'jim' else amount
            transfers = Transfer.query.filter_by(tippee=subject).all()
            actual = sum(tip.amount for tip in transfers)
            assert_equals(actual, expected)

    def test_record_transfer_invalid_participant(self):
        amount = Decimal('1.00')

        with self.postgres.get_connection() as conn:
            cur = conn.cursor()
            with assert_raises(IntegrityError):
                self.payday.record_transfer(cur, 'idontexist', 'nori', amount)
            conn.commit()

    def test_mark_transfer(self):
        amount = Decimal('1.00')

        # Forces a load with current state in dict
        before_transfer = PaydayModel.query.first().attrs_dict()

        with self.postgres.get_connection() as conn:
            cur = conn.cursor()
            self.payday.mark_transfer(cur, amount)
            conn.commit()

        # Forces a load with current state in dict
        after_transfer = PaydayModel.query.first().attrs_dict()

        expected = before_transfer['ntransfers'] + 1
        actual = after_transfer['ntransfers']
        assert_equals(actual, expected)

        expected = before_transfer['transfer_volume'] + amount
        actual = after_transfer['transfer_volume']
        assert_equals(actual, expected)

    def test_record_credit_updates_balance(self):
        alice = self.make_participant("alice")
        self.payday.record_credit(amount=Decimal("-1.00"),
                                  fee=Decimal("0.41"),
                                  error="",
                                  username="******")
        assert_equals(alice.balance, Decimal("0.59"))

    def test_record_credit_doesnt_update_balance_if_error(self):
        alice = self.make_participant("alice")
        self.payday.record_credit(amount=Decimal("-1.00"),
                                  fee=Decimal("0.41"),
                                  error="SOME ERROR",
                                  username="******")
        assert_equals(alice.balance, Decimal("0.00"))
Ejemplo n.º 4
0
class TestBillingTransfer(TestPaydayBase):
    def setUp(self):
        super(TestPaydayBase, self).setUp()
        self.payday = Payday(self.db)
        self.payday.start()
        self.tipper = self.make_participant('lgtest')
        self.balanced_account_uri = '/v1/marketplaces/M123/accounts/A123'

    def test_transfer(self):
        amount = Decimal('1.00')
        sender = self.make_participant('test_transfer_sender',
                                       pending=0,
                                       balance=1)
        recipient = self.make_participant('test_transfer_recipient',
                                          pending=0,
                                          balance=1)

        result = self.payday.transfer(sender.username, recipient.username,
                                      amount)
        assert_equals(result, True)

        # no balance remaining for a second transfer
        result = self.payday.transfer(sender.username, recipient.username,
                                      amount)
        assert_equals(result, False)

    def test_debit_participant(self):
        amount = Decimal('1.00')
        subject = self.make_participant('test_debit_participant',
                                        pending=0,
                                        balance=1)

        initial_amount = subject.balance

        with self.db.get_cursor() as cursor:
            self.payday.debit_participant(cursor, subject.username, amount)

        subject = Participant.from_username('test_debit_participant')

        expected = initial_amount - amount
        actual = subject.balance
        assert_equals(actual, expected)

        # this will fail because not enough balance
        with self.db.get_cursor() as cursor:
            with self.assertRaises(IntegrityError):
                self.payday.debit_participant(cursor, subject.username, amount)

    def test_skim_credit(self):
        actual = skim_credit(Decimal('10.00'))
        assert actual == (Decimal('10.00'), Decimal('0.00')), actual

    def test_credit_participant(self):
        amount = Decimal('1.00')
        subject = self.make_participant('test_credit_participant',
                                        pending=0,
                                        balance=1)

        initial_amount = subject.pending

        with self.db.get_cursor() as cursor:
            self.payday.credit_participant(cursor, subject.username, amount)

        subject = Participant.from_username(
            'test_credit_participant')  # reload

        expected = initial_amount + amount
        actual = subject.pending
        assert_equals(actual, expected)

    def test_record_transfer(self):
        amount = Decimal('1.00')
        subjects = ['jim', 'kate', 'bob']

        for subject in subjects:
            self.make_participant(subject, balance=1, pending=0)

        with self.db.get_cursor() as cursor:
            # Tip 'jim' twice
            for recipient in ['jim'] + subjects:
                self.payday.record_transfer(cursor, self.tipper.username,
                                            recipient, amount)

        for subject in subjects:
            # 'jim' is tipped twice
            expected = amount * 2 if subject == 'jim' else amount
            actual = self.db.one(
                "SELECT sum(amount) FROM transfers "
                "WHERE tippee=%s", (subject, ))
            assert_equals(actual, expected)

    def test_record_transfer_invalid_participant(self):
        amount = Decimal('1.00')

        with self.db.get_cursor() as cursor:
            with assert_raises(IntegrityError):
                self.payday.record_transfer(cursor, 'idontexist', 'nori',
                                            amount)

    def test_mark_transfer(self):
        amount = Decimal('1.00')

        # Forces a load with current state in dict
        before_transfer = self.fetch_payday()

        with self.db.get_cursor() as cursor:
            self.payday.mark_transfer(cursor, amount)

        # Forces a load with current state in dict
        after_transfer = self.fetch_payday()

        expected = before_transfer['ntransfers'] + 1
        actual = after_transfer['ntransfers']
        assert_equals(actual, expected)

        expected = before_transfer['transfer_volume'] + amount
        actual = after_transfer['transfer_volume']
        assert_equals(actual, expected)

    def test_record_credit_updates_balance(self):
        self.payday.record_credit(amount=Decimal("-1.00"),
                                  fee=Decimal("0.41"),
                                  error="",
                                  username="******")
        alice = Participant.from_username('alice')
        assert_equals(alice.balance, Decimal("0.59"))

    def test_record_credit_doesnt_update_balance_if_error(self):
        self.payday.record_credit(amount=Decimal("-1.00"),
                                  fee=Decimal("0.41"),
                                  error="SOME ERROR",
                                  username="******")
        alice = Participant.from_username('alice')
        assert_equals(alice.balance, Decimal("0.00"))
class TestBillingTransfer(Harness):
    def setUp(self):
        super(Harness, self).setUp()
        self.payday = Payday(self.postgres)
        self.payday.start()
        self.tipper = self.make_participant("lgtest")
        self.balanced_account_uri = "/v1/marketplaces/M123/accounts/A123"

    def test_transfer(self):
        amount = Decimal("1.00")
        sender = self.make_participant("test_transfer_sender", pending=0, balance=1)
        recipient = self.make_participant("test_transfer_recipient", pending=0, balance=1)

        result = self.payday.transfer(sender.id, recipient.id, amount)
        assert_equals(result, True)

        # no balance remaining for a second transfer
        result = self.payday.transfer(sender.id, recipient.id, amount)
        assert_equals(result, False)

    def test_debit_participant(self):
        amount = Decimal("1.00")
        subject = self.make_participant("test_debit_participant", pending=0, balance=1)

        initial_amount = subject.balance

        with self.postgres.get_connection() as connection:
            cursor = connection.cursor()

            self.payday.debit_participant(cursor, subject.id, amount)
            connection.commit()

        self.session.refresh(subject)

        expected = initial_amount - amount
        actual = subject.balance
        assert_equals(actual, expected)

        # this will fail because not enough balance
        with self.postgres.get_connection() as conn:
            cur = conn.cursor()

            with self.assertRaises(IntegrityError):
                self.payday.debit_participant(cur, subject.id, amount)
            conn.commit()

    def test_credit_participant(self):
        amount = Decimal("1.00")
        subject = self.make_participant("test_credit_participant", pending=0, balance=1)

        initial_amount = subject.pending

        with self.postgres.get_connection() as conn:
            cur = conn.cursor()
            self.payday.credit_participant(cur, subject.id, amount)
            conn.commit()

        self.session.refresh(subject)

        expected = initial_amount + amount
        actual = subject.pending
        assert_equals(actual, expected)

    def test_record_transfer(self):
        from gittip.models import Transfer

        amount = Decimal("1.00")
        subjects = ["jim", "kate", "bob"]

        for subject in subjects:
            self.make_participant(subject, balance=1, pending=0)

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

            # Tip 'jim' twice
            for recipient in ["jim"] + subjects:
                self.payday.record_transfer(cur, self.tipper.id, recipient, amount)
            conn.commit()

        for subject in subjects:
            # 'jim' is tipped twice
            expected = amount * 2 if subject == "jim" else amount
            transfers = Transfer.query.filter_by(tippee=subject).all()
            actual = sum(tip.amount for tip in transfers)
            assert_equals(actual, expected)

    def test_record_transfer_invalid_participant(self):
        amount = Decimal("1.00")

        with self.postgres.get_connection() as conn:
            cur = conn.cursor()
            with assert_raises(IntegrityError):
                self.payday.record_transfer(cur, "idontexist", "nori", amount)
            conn.commit()

    def test_mark_transfer(self):
        amount = Decimal("1.00")

        # Forces a load with current state in dict
        before_transfer = PaydayModel.query.first().attrs_dict()

        with self.postgres.get_connection() as conn:
            cur = conn.cursor()
            self.payday.mark_transfer(cur, amount)
            conn.commit()

        # Forces a load with current state in dict
        after_transfer = PaydayModel.query.first().attrs_dict()

        expected = before_transfer["ntransfers"] + 1
        actual = after_transfer["ntransfers"]
        assert_equals(actual, expected)

        expected = before_transfer["transfer_volume"] + amount
        actual = after_transfer["transfer_volume"]
        assert_equals(actual, expected)