Skip to content

Instantly share code, notes, and snippets.

@bl4ck4ndbr0wn
Created March 12, 2019 12:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bl4ck4ndbr0wn/0957c818efa922b2e8c7d3f53c7388f8 to your computer and use it in GitHub Desktop.
Save bl4ck4ndbr0wn/0957c818efa922b2e8c7d3f53c7388f8 to your computer and use it in GitHub Desktop.
Authors Heaven Sample Tests
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)
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 = "test@gmail.com"
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 = "test@GMAIL.COM"
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(
"test@gmail.com",
"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