forked from WasinTh/tutorial-django-rest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.py
More file actions
48 lines (41 loc) · 1.79 KB
/
Copy pathtests.py
File metadata and controls
48 lines (41 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import datetime
from django.test import TestCase
from django.urls import reverse
from rest_framework.test import APIClient
from rest_framework import status
from account.models import Transaction, Customer
from account.factories import CustomerFactory, TransactionFactory
class TestCustomerCurrentAmount(TestCase):
def setUp(self):
self.customer = CustomerFactory()
self.client = APIClient()
self.client.force_authenticate(user=self.customer.user)
def test_transactions(self):
expected_amount = 0
for _ in range(0, 10):
transaction = TransactionFactory(customer=self.customer)
if transaction.type == Transaction.Type.INCOME:
expected_amount += transaction.amount
else:
expected_amount -= transaction.amount
self.customer.refresh_from_db()
self.assertEqual(self.customer.current_amount, expected_amount)
class TestTransactionAPI(TestCase):
def setUp(self):
self.customer = CustomerFactory()
self.client = APIClient()
self.client.force_authenticate(user=self.customer.user)
def test_create_transaction_api(self):
data = {
'created': datetime.datetime.now(),
'amount': 1000,
'note': 'test note',
'type': Transaction.Type.INCOME,
'customer': self.customer.id
}
response = self.client.post(reverse('transaction-list'), data=data, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.data)
self.assertEqual(Customer.objects.all().count(), 1)
self.assertEqual(Transaction.objects.filter(customer=self.customer).count(), 1)
self.customer.refresh_from_db()
self.assertEqual(self.customer.current_amount, 1000)