Skip to content

Commit

Permalink
begin implementing tests for the models
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmurilo75 committed Mar 20, 2024
1 parent aeadc4a commit dc1fa89
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 5 deletions.
48 changes: 48 additions & 0 deletions development_notebook/2024-03-20.md
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions negligent_octopus/core/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@


class CoreConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "core"
name = "negligent_octopus.core"
verbose_name = "Core"
3 changes: 0 additions & 3 deletions negligent_octopus/core/tests.py

This file was deleted.

Empty file.
23 changes: 23 additions & 0 deletions negligent_octopus/core/tests/factories.py
Original file line number Diff line number Diff line change
@@ -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")
14 changes: 14 additions & 0 deletions negligent_octopus/core/tests/fixtures.py
Original file line number Diff line number Diff line change
@@ -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()
37 changes: 37 additions & 0 deletions negligent_octopus/core/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit dc1fa89

Please sign in to comment.