Last active
October 21, 2017 09:34
-
-
Save guilatrova/2fd1d7fc1563f2823d85f2cce85e6de2 to your computer and use it in GitHub Desktop.
Example of bad Django Rest Framework tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from transactions.models import Transaction | |
from rest_framework.authtoken.models import Token | |
from rest_framework.test import APITestCase, APIClient | |
from rest_framework import status | |
class IntegrationTests(APITestCase): | |
def setUp(self): | |
self.user = User.objects.create_user(username='testuser', email='[email protected]', password='testing') | |
token = Token.objects.create(user=self.user) | |
self.client = APIClient() | |
self.client.credentials(HTTP_AUTHORIZATION='Token ' + token.key) | |
def test_create_transactions(self): | |
dto = { | |
"description": "first", | |
"value": 10 | |
} | |
response = self.client.post(reverse('transactions'), dto, format='json') | |
self.assertEqual(response.status_code, status.HTTP_201_CREATED) | |
self.assertEqual(Transaction.objects.count(), 1) | |
def test_cant_create_transaction_with_value_0(self): | |
dto = { | |
"description": "first", | |
"value": 0 | |
} | |
response = self.client.post(reverse('transactions'), dto, format='json') | |
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | |
self.assertEqual(Transaction.objects.count(), 0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment