From bf21211b250c2ea164f64fb8f86a8ea2b7e4e9ed Mon Sep 17 00:00:00 2001 From: Arthur Barrett Date: Wed, 8 May 2013 18:03:41 -0400 Subject: [PATCH] adding some tests --- lms/djangoapps/notes/api.py | 2 +- lms/djangoapps/notes/tests.py | 130 +++++++++++++++++++++++++++++++--- lms/djangoapps/notes/utils.py | 6 +- 3 files changed, 125 insertions(+), 13 deletions(-) diff --git a/lms/djangoapps/notes/api.py b/lms/djangoapps/notes/api.py index 884b87ad55..9849c35d33 100644 --- a/lms/djangoapps/notes/api.py +++ b/lms/djangoapps/notes/api.py @@ -11,7 +11,7 @@ log = logging.getLogger(__name__) API_SETTINGS = { # Version - 'META': {'name': 'Notes API', 'version': '1.0'}, + 'META': {'name': 'Notes API', 'version': 1}, # Maps resources to HTTP methods 'RESOURCE_MAP': { diff --git a/lms/djangoapps/notes/tests.py b/lms/djangoapps/notes/tests.py index 501deb776c..9861863561 100644 --- a/lms/djangoapps/notes/tests.py +++ b/lms/djangoapps/notes/tests.py @@ -1,16 +1,126 @@ """ -This file demonstrates writing tests using the unittest module. These will pass -when you run "manage.py test". - -Replace this with more appropriate tests for your application. +Unit tests for the notes API and model. """ 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): - def test_basic_addition(self): - """ - Tests that 1 + 1 always equals 2. - """ - self.assertEqual(1 + 1, 2) +from . import utils, api, models + +logging.disable(logging.CRITICAL) # remove debugging from the log output + +class UtilsTest(TestCase): + 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) diff --git a/lms/djangoapps/notes/utils.py b/lms/djangoapps/notes/utils.py index 5e3c0182fa..80a872da4e 100644 --- a/lms/djangoapps/notes/utils.py +++ b/lms/djangoapps/notes/utils.py @@ -3,5 +3,7 @@ def notes_enabled_for_course(course): Returns True if the notes app is enabled for the course, False otherwise. ''' # TODO: create a separate policy setting to enable/disable notes - notes_tab_type = 'notes' - return next((True for tab in course.tabs if tab['type'] == notes_tab_type), False) + tab_type = 'notes' + tabs = course.tabs + tab_found = next((True for t in tabs if t['type'] == tab_type), False) + return tab_found