Created
March 12, 2019 12:53
-
-
Save bl4ck4ndbr0wn/0957c818efa922b2e8c7d3f53c7388f8 to your computer and use it in GitHub Desktop.
Authors Heaven Sample 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 unittest.mock import patch | |
from django.core.management import call_command | |
from django.db.utils import OperationalError | |
from django.test import TestCase | |
class CommandTests(TestCase): | |
def test_await_for_db_ready(self): | |
"""Test waiting for db when db is available""" | |
with patch("django.db.utils.ConnectionHandler.__getitem__") as gi: | |
gi.return_value =True | |
call_command("wait_for_db") | |
self.assertEqual(gi.call_count, 1) | |
@patch('time.sleep', return_value=True) | |
def test_wait_for_db(self, ts): | |
"""Test waiting for Db""" | |
with patch('django.db.utils.ConnectionHandler.__getitem__') as gi: | |
gi.side_effect = [OperationalError] * 5 + [True] | |
call_command('wait_for_db') | |
self.assertEqual(gi.call_count, 6) |
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 django.test import TestCase | |
from django.contrib.auth import get_user_model | |
class ModelTests(TestCase): | |
def test_create_user_with_email_successful(self): | |
"""Test create a new user with an email is successfull""" | |
email = "[email protected]" | |
password = "Testpass123" | |
user = get_user_model().objects.create_user( | |
email=email, | |
password=password | |
) | |
self.assertEqual(user.email, email) | |
self.assertTrue(user.check_password(password)) | |
def test_new_user_email_nomalized(self): | |
"""Test the email for a new user is normalized""" | |
email = "[email protected]" | |
user = get_user_model().objects.create_user(email, "test123") | |
self.assertEqual(user.email, email.lower()) | |
def test_new_user_invalid_email(self): | |
"""Test creating user with no email raises error""" | |
with self.assertRaises(ValueError): | |
get_user_model().objects.create_user(None, "test123") | |
def test_create_new_supperuser(self): | |
"""Test creating a new supperuser:""" | |
user = get_user_model().objects.create_superuser( | |
"[email protected]", | |
"test123" | |
) | |
self.assertTrue(user.is_superuser) | |
self.assertTrue(user.is_staff) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment