adding some tests

This commit is contained in:
Arthur Barrett
2013-05-08 18:03:41 -04:00
parent 1eb05cfd4e
commit bf21211b25
3 changed files with 125 additions and 13 deletions

View File

@@ -11,7 +11,7 @@ log = logging.getLogger(__name__)
API_SETTINGS = { API_SETTINGS = {
# Version # Version
'META': {'name': 'Notes API', 'version': '1.0'}, 'META': {'name': 'Notes API', 'version': 1},
# Maps resources to HTTP methods # Maps resources to HTTP methods
'RESOURCE_MAP': { 'RESOURCE_MAP': {

View File

@@ -1,16 +1,126 @@
""" """
This file demonstrates writing tests using the unittest module. These will pass Unit tests for the notes API and model.
when you run "manage.py test".
Replace this with more appropriate tests for your application.
""" """
from django.test import TestCase from django.test import TestCase
from django.test.client import Client
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
from collections import namedtuple
from random import random
import json
import logging
class SimpleTest(TestCase): from . import utils, api, models
def test_basic_addition(self):
""" logging.disable(logging.CRITICAL) # remove debugging from the log output
Tests that 1 + 1 always equals 2.
""" class UtilsTest(TestCase):
self.assertEqual(1 + 1, 2) def setUp(self):
'''
Setup a dummy course-like object with a tabs field that can be
accessed via attribute lookup.
'''
self.course = namedtuple('DummyCourse', ['tabs'])
self.course.tabs = []
def test_notes_not_enabled(self):
'''
Tests that notes are disabled when the course tab configuration does NOT
contain a tab with type "notes."
'''
self.assertFalse(utils.notes_enabled_for_course(self.course))
def test_notes_enabled(self):
'''
Tests that notes are enabled when the course tab configuration contains
a tab with type "notes."
'''
self.course.tabs = [
{'type': 'foo'},
{'name': 'My Notes', 'type': 'notes'},
{'type':'bar'}]
self.assertTrue(utils.notes_enabled_for_course(self.course))
class ApiTest(TestCase):
def setUp(self):
self.client = Client()
# Create two accounts
self.password = 'abc'
self.student = User.objects.create_user('student', 'student@test.com', self.password)
self.instructor = User.objects.create_user('instructor', 'instructor@test.com', self.password)
self.course_id = 'HarvardX/CB22x/The_Ancient_Greek_Hero'
self.note = {
'user':self.student,
'course_id':self.course_id,
'uri':'/',
'text':'foo',
'quote':'bar',
'range_start':0,
'range_start_offset':0,
'range_end':100,
'range_end_offset':0,
'tags':'a,b,c'
}
def login(self):
self.client.login(username=self.student.username, password=self.password)
def url(self, name):
return reverse(name, kwargs={'course_id':self.course_id})
def create_notes(self, num_notes):
notes = [ models.Note(**self.note) for n in range(num_notes) ]
models.Note.objects.bulk_create(notes)
return notes
def test_root(self):
self.login()
resp = self.client.get(self.url('notes_api_root'))
self.assertEqual(resp.status_code, 200)
self.assertNotEqual(resp.content, '')
content = json.loads(resp.content)
self.assertEqual(set(('name','version')), set(content.keys()))
self.assertIsInstance(content['version'], int)
self.assertEqual(content['name'], 'Notes API')
def test_index_empty(self):
self.login()
resp = self.client.get(self.url('notes_api_notes'))
self.assertEqual(resp.status_code, 200)
self.assertNotEqual(resp.content, '')
content = json.loads(resp.content)
self.assertEqual(len(content), 0)
def test_index_with_notes(self):
num_notes = 7
self.login()
self.create_notes(num_notes)
resp = self.client.get(self.url('notes_api_notes'))
self.assertEqual(resp.status_code, 200)
self.assertNotEqual(resp.content, '')
content = json.loads(resp.content)
self.assertEqual(len(content), num_notes)
def test_index_max_notes(self):
self.login()
MAX_LIMIT = api.API_SETTINGS.get('MAX_NOTE_LIMIT')
num_notes = MAX_LIMIT + 1
self.create_notes(num_notes)
resp = self.client.get(self.url('notes_api_notes'))
self.assertEqual(resp.status_code, 200)
self.assertNotEqual(resp.content, '')
content = json.loads(resp.content)
self.assertEqual(len(content), MAX_LIMIT)

View File

@@ -3,5 +3,7 @@ def notes_enabled_for_course(course):
Returns True if the notes app is enabled for the course, False otherwise. Returns True if the notes app is enabled for the course, False otherwise.
''' '''
# TODO: create a separate policy setting to enable/disable notes # TODO: create a separate policy setting to enable/disable notes
notes_tab_type = 'notes' tab_type = 'notes'
return next((True for tab in course.tabs if tab['type'] == notes_tab_type), False) tabs = course.tabs
tab_found = next((True for t in tabs if t['type'] == tab_type), False)
return tab_found