Skip to content

Instantly share code, notes, and snippets.

@moacirmoda
Created August 10, 2018 01:07
Show Gist options
  • Save moacirmoda/2cf9ddbeeb035bba93f7edad4a8a0d7f to your computer and use it in GitHub Desktop.
Save moacirmoda/2cf9ddbeeb035bba93f7edad4a8a0d7f to your computer and use it in GitHub Desktop.
from django import models
from django.test import TestCase
class Person(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30, null=True)
age = models.PositiveIntegerField()
def __str__(self):
return '{} {}'.format(self.first_name, self.last_name)
def save(self, *args, **kwargs):
if not self.last_name:
self.last_name = "N/A"
super(self).save(*args, **kwargs)
class PersonTestCase(TestCase):
def test_should_return_attributes(self):
self.assertTrue(hasattr(Person, 'first_name'))
self.assertTrue(hasattr(Person, 'last_name'))
self.assertTrue(hasattr(Person, 'age'))
def test_should_create_item(self):
person = Person.objects.create(first_name='Moacir', last_name='Moda', age=27)
self.assertIsNotNone(person)
def test_should_assert_methods(self):
person = Person.objects.create(first_name='Moacir', age=27)
self.assertIsEqual(person.last_name, 'N/A')
self.assertIsEqual(str(person), 'Moacir N/A')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment