Skip to content
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

Issue 137 non fee transactions #138

Merged
merged 4 commits into from
Feb 15, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion v1/bank_transactions/factories/bank_transaction.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from factory import SubFactory
from factory import Iterator, SubFactory
from thenewboston.constants.network import ACCEPTED_FEE_LIST
from thenewboston.factories.network_transaction import NetworkTransactionFactory

from v1.blocks.factories.block import BlockFactory
Expand All @@ -7,6 +8,8 @@

class BankTransactionFactory(NetworkTransactionFactory):
block = SubFactory(BlockFactory)
# Will remove when thenewboston-python lib is updated
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fee = Iterator(ACCEPTED_FEE_LIST + [''])

class Meta:
model = BankTransaction
14 changes: 14 additions & 0 deletions v1/bank_transactions/filters/bank_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@

class BankTransactionFilter(FilterSet):
account_number = CharFilter(method='filter_account_number')
fee = CharFilter(method='filter_fee')

class Meta:
model = BankTransaction
fields = [
'account_number',
'block__sender',
'recipient',
'fee',
]

@staticmethod
Expand All @@ -22,3 +24,15 @@ def filter_account_number(queryset, _, value):
Q(block__sender=value)
| Q(recipient=value)
)

@staticmethod
def filter_fee(queryset, name, value):
"""Filter queryset by fee"""
if value == 'NONE':
return queryset.filter(
fee__exact='',
)

return queryset.filter(
fee__exact=value,
)
3 changes: 2 additions & 1 deletion v1/bank_transactions/models/bank_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ def __str__(self):
return (
f'ID: {self.id} | '
f'Amount: {self.amount} | '
f'Recipient: {self.recipient}'
f'Recipient: {self.recipient} | '
f'Fee: {self.fee or "non-fee"}'
)
19 changes: 19 additions & 0 deletions v1/bank_transactions/tests/bank_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,22 @@ def test_bank_transactions_filter(client, bank_transactions, field, attribute, d
expected=HTTP_200_OK,
)
assert response[0]['id'] == str(target_transaction.id)


@pytest.mark.parametrize('value', ['PRIMARY_VALIDATOR', 'BANK', '', 'FOO'])
def test_bank_transactions_non_fee_filter(client, bank_transactions, value, django_assert_max_num_queries):
expected_response_len = sum(
transaction.fee == value for transaction in bank_transactions
)
with django_assert_max_num_queries(2):
response = client.get_json(
reverse('banktransaction-list'),
{
'limit': 0,
'fee': value or 'NONE',
},
expected=HTTP_200_OK,
)
assert len(response) == expected_response_len
for transaction in response:
assert transaction['fee'] == value