Test Driven Development with Django – Part 1

Intro

Here is the first tutorial we are going to go through.   I will be commenting as I progress through it.

Models — Test-Driven Django Development v3.0 documentation

After Completing Models Section:

  •  This is a very gentle walk-through, assumes the reader knows only the most basic concepts of django.
  • I like to use shell_plus to test around with things interactively. Here is a fix you may, need depending on what version of python you are using.
  • Shows you how to hook into the models and testing interfaces, and how to set up the most basic testing logic. 
  • Presents a process, pattern, procedure– for building an automated testing suite.
  • Makes me interested in finding a similar guide for Django-REST.
  • I beefed my model up with a little mezzanine logic to establish a simple pattern for hooking into the django model save() methods.
 

models.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models
from mezzanine.core.models import Displayable, Ownable, RichText, Slugged
from mezzanine.generic.fields import CommentsField, RatingField

# Import mezzanine stuff
from mezzanine.core.models import Displayable
from mezzanine.utils.models import base_concrete_model

# Import urllib to encode strings for urls
import urllib

# utility function to encode strings into urls
def build_steam_url(title):
return 'https://store.steampowered.com/search/?publisher/'+urllib.quote_plus(title)

class Author(models.Model):
DEV = 'DEV'
PUB = 'PUB'
    ROLES = (
        (DEV, 'Developer'),
        (PUB, 'Publisher')
    )

# define some fields
title = models.CharField(max_length=500) # a name for our author
role = models.CharField( # role/relation to game
    max_length=3,
    choices=ROLES,
    default=DEV,
)
steam_url = models.CharField( # a url to their steam page
    max_length=500,
    blank=True,
)

# def some functions
def __str__(self):
    return self.title

def save(self, *args, **kwargs):
if not self.steam_url:
self.steam_url = self.generate_unique_steam_url()
super(Author, self).save(*args, **kwargs)

def generate_unique_steam_url(self):
return build_steam_url(self.title)

 

tests.py

from django.test import TestCase
from .models import Author
from pprint import pprint as p # prettify our ouput


def dump(thing):
# build a string state so we can diff it later
s = '\n\ntitle: '+ str(thing.title)
s += '\nsteam_url: '+ str(thing.steam_url)
s += '\nrole: '+ str(thing.role)

p(thing)
p(s)

return s

class AuthorModelTest(TestCase):
def test_string_representation(self):
author = Author(title="My author title")
self.assertEqual(str(author), author.title)
s0 = dump(author) # print state
author.save() # save objects
s1 = dump(author) # check state again
print '\n\n'
p('Update Success?: {} (s0 != s1)'.format(s0!=s1)) # test if there was a change

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>