Skip to content

Instantly share code, notes, and snippets.

@guilatrova
Last active October 21, 2017 09:34
Show Gist options
  • Save guilatrova/2fd1d7fc1563f2823d85f2cce85e6de2 to your computer and use it in GitHub Desktop.
Save guilatrova/2fd1d7fc1563f2823d85f2cce85e6de2 to your computer and use it in GitHub Desktop.
Example of bad Django Rest Framework tests
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='testuser@test.com', 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