diff --git a/development_notebook/2024-03-20.md b/development_notebook/2024-03-20.md new file mode 100644 index 0000000..d1ca8e0 --- /dev/null +++ b/development_notebook/2024-03-20.md @@ -0,0 +1,48 @@ +# State Of Work + +* Project has been fully configured from cookiecutter-django using MySQL. +* Deployed to production at [negligentoctopus.pythonanywhere.com](negligentoctopus.pythonanywhere.com). + +Core models are being defined and added to the django-admin. +Account and Transactions are implemented with balance as prefix-sum. + +## Update + +Testing for these model is being defined. + +# Today + +Testing required configuration of a db table 'test\_negligent\_octopus. +This was achieved the same way as creating a normal table: +- Go into sudo mysql +- create table +- grant all privileges to user +After this pytest can create and drop the table as needed. + +Also, make sure that your app name is not relative, i.e., 'core' should be 'negligent\_octopus.core'. + +__To Continue__ + +Factories are defined. +Testing is currently defined as methods of a class. +The function can receive arguments with the name of models, which will be created and passed automatically by pytest using the factories (if defined) or using fixtures (use @pytest.fixture). + +## Work Log +__InProgress__ +* Add testing -- Functions are defined in class. Follow the one implemented as example. + * Test business logic + * Test balance is correct + * Test balance on account is safe for one, for many transaction, with or withput inital balance + * Test balance is same on change initial balance and change of a transaction + * Test that changing acc or timestamp fails + +__ToDo__ + +__Done__ +* Configure testing environment +* Factories for testing +* Testing functions + +__Discarded__ + +# To Do diff --git a/negligent_octopus/core/apps.py b/negligent_octopus/core/apps.py index c0ce093..0f7a089 100644 --- a/negligent_octopus/core/apps.py +++ b/negligent_octopus/core/apps.py @@ -2,5 +2,5 @@ class CoreConfig(AppConfig): - default_auto_field = "django.db.models.BigAutoField" - name = "core" + name = "negligent_octopus.core" + verbose_name = "Core" diff --git a/negligent_octopus/core/tests.py b/negligent_octopus/core/tests.py deleted file mode 100644 index 7ce503c..0000000 --- a/negligent_octopus/core/tests.py +++ /dev/null @@ -1,3 +0,0 @@ -from django.test import TestCase - -# Create your tests here. diff --git a/negligent_octopus/core/tests/__init__.py b/negligent_octopus/core/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/negligent_octopus/core/tests/factories.py b/negligent_octopus/core/tests/factories.py new file mode 100644 index 0000000..5f6cb65 --- /dev/null +++ b/negligent_octopus/core/tests/factories.py @@ -0,0 +1,23 @@ +import factory +from factory.django import DjangoModelFactory + +from negligent_octopus.core.models import Account +from negligent_octopus.core.models import Transaction + + +class AccountFactory(DjangoModelFactory): + class Meta: + model = Account + + owner = factory.SubFactory("negligent_octopus.users.tests.factories.UserFactory") + name = factory.Faker("word") + initial_balance = factory.Faker("random_number", digits=2) + + +class TransactionFactory(DjangoModelFactory): + class Meta: + model = Transaction + + account = factory.SubFactory(AccountFactory) + amount = factory.Faker("random_number", digits=2) + title = factory.Faker("word") diff --git a/negligent_octopus/core/tests/fixtures.py b/negligent_octopus/core/tests/fixtures.py new file mode 100644 index 0000000..5b5890a --- /dev/null +++ b/negligent_octopus/core/tests/fixtures.py @@ -0,0 +1,14 @@ +import pytest + +from negligent_octopus.core.tests.factories import AccountFactory +from negligent_octopus.core.tests.factories import TransactionFactory + + +@pytest.fixture() +def account(): + return AccountFactory() + + +@pytest.fixture() +def transaction(): + return TransactionFactory() diff --git a/negligent_octopus/core/tests/test_models.py b/negligent_octopus/core/tests/test_models.py new file mode 100644 index 0000000..c1114bc --- /dev/null +++ b/negligent_octopus/core/tests/test_models.py @@ -0,0 +1,37 @@ +import pytest +from django.utils import timezone +from faker import Faker + +from negligent_octopus.core.models import Account +from negligent_octopus.core.tests.factories import TransactionFactory + +faker = Faker() + + +@pytest.mark.django_db() +class TestAccountTransactionBalance: + def test_account_balance(self, account: Account): + balance = account.initial_balance + for i in range(10): + balance += i + TransactionFactory( + account=account, + amount=i, + timestamp=timezone.now(), # Make sure they are in order + ) + assert account.balance == balance + + def test_account_initial_balance_change(self, account: Account): + raise NotImplementedError + + def test_transaction_added_last(self, account: Account): + raise NotImplementedError + + def test_transaction_added_first(self, account: Account): + raise NotImplementedError + + def test_transaction_added_middle(self, account: Account): + raise NotImplementedError + + def test_transaction_change_amount(self, account: Account): + raise NotImplementedError