-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unit tests for equity module #36
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
from decimal import Decimal | ||
import pytest | ||
from unittest.mock import patch | ||
from oldabe.models import Payment, ItemizedPayment, Attribution | ||
from oldabe.money_in.equity import calculate_incoming_investment, calculate_incoming_attribution, dilute_attributions, handle_investment | ||
from .fixtures import normalized_attributions # noqa | ||
|
||
|
||
class TestCalculateIncomingInvestment: | ||
@pytest.mark.parametrize( | ||
"prior_contribution, incoming_amount, price, expected_investment", | ||
[ | ||
(Decimal("0"), Decimal("0"), Decimal("100"), Decimal("0")), | ||
(Decimal("0"), Decimal("20"), Decimal("100"), Decimal("0")), | ||
(Decimal("0"), Decimal("100"), Decimal("100"), Decimal("0")), | ||
(Decimal("0"), Decimal("120"), Decimal("100"), Decimal("20")), | ||
(Decimal("20"), Decimal("0"), Decimal("100"), Decimal("0")), | ||
(Decimal("20"), Decimal("20"), Decimal("100"), Decimal("0")), | ||
(Decimal("20"), Decimal("80"), Decimal("100"), Decimal("0")), | ||
(Decimal("20"), Decimal("100"), Decimal("100"), Decimal("20")), | ||
(Decimal("100"), Decimal("0"), Decimal("100"), Decimal("0")), | ||
(Decimal("100"), Decimal("20"), Decimal("100"), Decimal("20")), | ||
(Decimal("120"), Decimal("0"), Decimal("100"), Decimal("0")), | ||
(Decimal("120"), Decimal("20"), Decimal("100"), Decimal("20")), | ||
], | ||
) | ||
@patch('oldabe.money_in.equity.ItemizedPaymentsRepo') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While discussing, we decided to pass Repos into functions instead of patching them in tests. |
||
def test_matrix( | ||
self, | ||
prior_itemized_payments, | ||
prior_contribution, | ||
incoming_amount, | ||
price, | ||
expected_investment, | ||
): | ||
email = '[email protected]' | ||
payment = Payment(email, email, incoming_amount) | ||
# this is the total amount paid _including_ the incoming amount | ||
prior_itemized_payments.return_value = ( | ||
[ItemizedPayment(email, 0, prior_contribution, True, 'dummy.file')] | ||
) | ||
new_itemized_payments = [ItemizedPayment(email, 0, incoming_amount, True, 'dummy.file')] | ||
result = calculate_incoming_investment(payment, price, new_itemized_payments) | ||
assert result == expected_investment | ||
|
||
|
||
class TestCalculateIncomingAttribution: | ||
@pytest.mark.parametrize( | ||
"incoming_investment, expected_attribution", | ||
[ | ||
(Decimal("0"), Decimal('0')), | ||
(Decimal("50"), Decimal('0.005')), | ||
(Decimal("5000"), Decimal('0.5')), | ||
], | ||
) | ||
def test_matrix( | ||
self, | ||
incoming_investment, | ||
expected_attribution, | ||
): | ||
email = '[email protected]' | ||
posterior_valuation = Decimal('10000') | ||
|
||
attribution = calculate_incoming_attribution( | ||
email, incoming_investment, posterior_valuation | ||
) | ||
assert ( | ||
attribution.share == expected_attribution | ||
) | ||
|
||
|
||
class TestDiluteAttributions: | ||
|
||
@pytest.mark.parametrize( | ||
"incoming_attribution, expected_attribution", | ||
[ | ||
(Decimal("0"), Decimal('0.2')), | ||
(Decimal("0.1"), Decimal('0.28')), | ||
(Decimal("0.5"), Decimal('0.60')), | ||
], | ||
) | ||
def test_matrix( | ||
self, | ||
incoming_attribution, | ||
expected_attribution, | ||
normalized_attributions, | ||
): | ||
email = '[email protected]' | ||
incoming_attribution = Attribution(email, incoming_attribution) | ||
|
||
dilute_attributions( | ||
incoming_attribution, normalized_attributions | ||
) | ||
a_share = normalized_attributions[email] | ||
b_share = normalized_attributions['[email protected]'] | ||
assert ( | ||
a_share == expected_attribution | ||
) | ||
assert ( | ||
b_share == 1 - expected_attribution | ||
) | ||
|
||
|
||
class TestHandleInvestment: | ||
|
||
def test_dilutes_attributions( | ||
self, | ||
normalized_attributions, | ||
): | ||
apriori_share = normalized_attributions['[email protected]'] | ||
email = '[email protected]' | ||
payment = Payment(email, email, Decimal('90')) | ||
itemized = ItemizedPayment( | ||
email, | ||
10, | ||
90, | ||
payment.attributable, | ||
payment.file, | ||
) | ||
new_itemized_payments = [itemized] | ||
price = Decimal('10') | ||
prior_valuation = Decimal('50') | ||
|
||
_valuation = handle_investment(payment, | ||
new_itemized_payments, | ||
normalized_attributions, | ||
price, | ||
prior_valuation) | ||
assert normalized_attributions['[email protected]'] < apriori_share | ||
assert sum(normalized_attributions.values()) == Decimal('1') | ||
|
||
def test_inflates_valuation( | ||
self, | ||
normalized_attributions, | ||
): | ||
email = '[email protected]' | ||
payment = Payment(email, email, Decimal('90')) | ||
itemized = ItemizedPayment( | ||
email, | ||
10, | ||
90, | ||
payment.attributable, | ||
payment.file, | ||
) | ||
new_itemized_payments = [itemized] | ||
price = Decimal('10') | ||
prior_valuation = Decimal('50') | ||
|
||
valuation = handle_investment(payment, | ||
new_itemized_payments, | ||
normalized_attributions, | ||
price, | ||
prior_valuation) | ||
assert valuation == (prior_valuation + 80) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: are all of these cases actually testing different logic / use cases? Do we need them all? Might be helpful to add a comment for each one so that if one fails, we can easily identify what is supposed to happen and what logic is broken.