Пример #1
0
def test_invoice_generate_add_correction(member, invoice_directory):
    """Tests that generating invoices multiple times for the same month + user
    will add corrections only once.
    """
    from pootle_statistics.models import Submission
    EVENT_COUNT = 5
    WORDCOUNT = 5
    TRANSLATION_RATE = 0.5
    INITIAL_SUBTOTAL = EVENT_COUNT * WORDCOUNT * TRANSLATION_RATE
    MINIMAL_PAYMENT = 20

    month = get_previous_month()
    config = dict({
        'minimal_payment': MINIMAL_PAYMENT,
    }, **FAKE_CONFIG)
    invoice = Invoice(member, config, month=month, add_correction=True)

    # Set some rates
    member.rate = TRANSLATION_RATE
    member.save()

    # Fake some activity that will leave amounts below the minimum bar:
    # EVENT_COUNT * WORDCOUNT * TRANSLATION_RATE < MINIMAL_PAYMENT
    for i in range(EVENT_COUNT):
        scorelog_kwargs = {
            'wordcount': WORDCOUNT,
            'similarity': 0,
            'action_code': TranslationActionCodes.NEW,
            'creation_time': month,
            'user': member,
            'submission': Submission.objects.all()[i],
        }
        ScoreLogFactory(**scorelog_kwargs)

    # Generate an invoice first
    amounts = invoice.get_total_amounts()
    assert amounts['subtotal'] == INITIAL_SUBTOTAL
    assert invoice.should_add_correction(amounts['subtotal'])
    invoice.generate()
    _check_single_paidtask(invoice, INITIAL_SUBTOTAL)

    # Subsequent invoice generations must not add any corrections
    for i in range(5):
        invoice.get_total_amounts.cache_clear()  # clears the LRU cache
        amounts = invoice.get_total_amounts()
        assert amounts['subtotal'] == 0
        assert not invoice.should_add_correction(amounts['subtotal'])
        invoice.generate()
        _check_single_paidtask(invoice, INITIAL_SUBTOTAL)
Пример #2
0
def test_invoice_get_total_amounts_extra_add(monkeypatch):
    """Tests total amounts' correctness when there is an extra amount to be
    added to the accrued total.
    """
    extra_add = 5
    user = UserFactory.build()
    config = dict({
        'extra_add': extra_add,
    }, **FAKE_CONFIG)
    invoice = Invoice(user, config, add_correction=True)

    rates = (0.5, 0.5, 0.5)
    monkeypatch.setattr(invoice, 'get_rates', lambda: rates)
    amounts = (5, 5, 5, 0)
    monkeypatch.setattr(invoice, '_get_full_user_amounts', lambda x: amounts)

    total_amounts = invoice.get_total_amounts()
    assert total_amounts['subtotal'] == 3 * (amounts[0] * rates[0])
    assert total_amounts['balance'] is None
    assert total_amounts['total'] == 3 * (amounts[0] * rates[0]) + extra_add
    assert total_amounts['extra_amount'] == extra_add
Пример #3
0
def test_invoice_get_total_amounts_below_minimal_payment(monkeypatch):
    """Tests total amounts' correctness when the accrued total is below the
    minimal payment bar.
    """
    user = UserFactory.build()
    config = dict({
        'minimal_payment': 10,
        'extra_add': 5,
    }, **FAKE_CONFIG)
    invoice = Invoice(user, config, add_correction=True)

    rates = (0.5, 0.5, 0.5)
    monkeypatch.setattr(invoice, 'get_rates', lambda: rates)
    amounts = (5, 5, 5, 0)
    monkeypatch.setattr(invoice, '_get_full_user_amounts', lambda x: amounts)

    total_amounts = invoice.get_total_amounts()
    assert total_amounts['subtotal'] == 3 * (amounts[0] * rates[0])
    assert total_amounts['balance'] == 3 * (amounts[0] * rates[0])
    assert total_amounts['total'] == 0
    assert total_amounts['extra_amount'] == 0