Skip to content

Commit

Permalink
add concurrent update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PetrDlouhy committed Jan 11, 2023
1 parent 39dcdba commit 8192ecc
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ jobs:
run: |
python setup.py develop
pip install -e .
pip install requests-mock mock pytest coveralls django-coverage-plugin
pip install psycopg2==2.8.6
pip install -r requirements_test.txt
pip install Django==${{ matrix.DJANGO_VERSION }}
pip install codecov
Expand Down Expand Up @@ -97,6 +96,7 @@ jobs:

- name: Install
run: |
pip install -r requirements_test.txt
pip install flake8 isort black mypy django-stubs dj_database_url types-six types-requests
pip install "django-stubs<1.13.0" # Remove this line once https://github.com/typeddjango/django-stubs/issues/1227 is fixed
python setup.py develop
Expand Down
1 change: 1 addition & 0 deletions example_project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"django_concurrent_tests",
"mptt",
"django_extensions",
"hordak",
Expand Down
62 changes: 62 additions & 0 deletions hordak/tests/models/test_concurrently.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from django.db import transaction
from django.test import TransactionTestCase
from django_concurrent_tests.helpers import call_concurrently, make_concurrent_calls
from moneyed import Money

from hordak.models import Account, Leg, Transaction


@transaction.atomic
def create_transaction(account1_id, account2_id):
h_trans = Transaction.objects.create()
Leg.objects.create(
transaction=h_trans,
account_id=account1_id,
amount=Money(1, "USD"),
)
Leg.objects.create(
transaction=h_trans,
account_id=account2_id,
amount=Money(-1, "USD"),
)


@transaction.atomic
def delete_one_transaction(transaction_id):
Transaction.objects.get(id=transaction_id).delete()


class ConcurrentRunningTotalsTest(TransactionTestCase):
def test_multiple_concurrent_transactions(self):
"""
Addin money to account multiple times concurrently should result in correct running totals value.
"""
account1 = Account.objects.create(name="account1", currencies=["USD"])
account2 = Account.objects.create(name="account2", currencies=["USD"])
account1.update_running_totals()
account2.update_running_totals()
self.assertEqual(account1.running_totals.all()[0].balance, Money(0, "USD"))
self.assertEqual(account2.running_totals.all()[0].balance, Money(0, "USD"))
call_count = 10
call_concurrently(
call_count,
create_transaction,
account1_id=account1.id,
account2_id=account2.id,
)
self.assertEqual(
account1.running_totals.all()[0].balance, Money(call_count, "USD")
)
self.assertEqual(
account2.running_totals.all()[0].balance, Money(-call_count, "USD")
)

# Not delete the transactions one by one to check if the
# running totals are updated correctly during delete
calls = [
(delete_one_transaction, {"transaction_id": transaction.id})
for transaction in Transaction.objects.all()
]
make_concurrent_calls(*calls)
self.assertEqual(account1.running_totals.all()[0].balance, Money(0, "USD"))
self.assertEqual(account2.running_totals.all()[0].balance, Money(0, "USD"))
7 changes: 7 additions & 0 deletions requirements_test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
requests-mock
mock
pytest
coveralls
django-coverage-plugin
django-concurrent-test-helper
psycopg2==2.8.6

0 comments on commit 8192ecc

Please sign in to comment.