Skip to content

Commit

Permalink
Example tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisortman committed Dec 1, 2016
1 parent c1bf98b commit 330d7f3
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
2 changes: 1 addition & 1 deletion mileage/templates/mileage/index.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% extends 'mileage/base.html' %}

{% block title %}
The most awesome page in the universe
Cool
{% endblock %}

{% block content %}
Expand Down
2 changes: 1 addition & 1 deletion mileage/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def index(request):
context = {
'name' : name,
'tt' : level.enter(),
'birthday': '12/6/1979',
'birthday': '12/6/1980',
'numbers' : [1,2,3]
}

Expand Down
8 changes: 7 additions & 1 deletion polls/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
from django.utils import timezone
import datetime

@python_2_unicode_compatible # only if you need to support Python 2
class Question(models.Model):
Expand All @@ -11,7 +13,11 @@ def __str__(self):

def was_published_recently(self):
yesterday = timezone.now() - datetime.timedelta(days=1)
return self.pub_date >= yesterday
tomorrow = timezone.now() + datetime.timedelta(days=1)
if self.pub_date >= tomorrow:
return False
else:
return self.pub_date >= yesterday

@python_2_unicode_compatible # only if you need to support Python 2
class Choice(models.Model):
Expand Down
40 changes: 40 additions & 0 deletions polls/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,43 @@
from django.test import TestCase
import datetime
from django.utils import timezone
from django.test import TestCase

from .models import Question
# Create your tests here.

class Player(object):
pass

class SparringRoom(object):
def enter(self, player):

if player.rope:
return {'message' : 'You have a rope!'}
else:
return {'message' : 'No rope for you'}

class QuestionMethodTests(TestCase):

def test_was_published_recently_with_future_question(self):
"""
was_published_recently() should return False for questions whose
pub_date is in the future.
"""
time = timezone.now() + datetime.timedelta(days=20)
future_question = Question(pub_date=time)
self.assertEqual(future_question.was_published_recently(), False)

def test_message_user_with_rope(self):
player = Player()
player.rope = True
room = SparringRoom()
output = room.enter(player)
self.assertEqual(output['message'], "You have a rope!")

def test_message_user_with_rope(self):
player = Player()
player.rope = False
room = SparringRoom()
output = room.enter(player)
self.assertEqual(output['message'], "No rope for you")

0 comments on commit 330d7f3

Please sign in to comment.