From: Charles Connell Date: Tue, 31 Dec 2013 04:20:11 +0000 (-0500) Subject: Clean up notes tests, deal with search index X-Git-Tag: release-20150131~346 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=c5af62fe0c2d14f2420f1eef0ab577b95f2e68d9;p=oweals%2Fkarmaworld.git Clean up notes tests, deal with search index --- diff --git a/karmaworld/apps/document_upload/tests.py b/karmaworld/apps/document_upload/tests.py index cb4d13e..29a6128 100644 --- a/karmaworld/apps/document_upload/tests.py +++ b/karmaworld/apps/document_upload/tests.py @@ -11,16 +11,9 @@ from karmaworld.apps.document_upload.forms import RawDocumentForm from karmaworld.apps.notes.gdrive import * from karmaworld.apps.notes.models import Note -CREDENTIALS_PATH = os.path.join(settings.DJANGO_ROOT, - 'secret/oauth_token.json') - -# NOTE: these tests require that you have the file -# secret/oauth_token.json. See docs/source/gdrive.rst -# for how to set this up. class ConversionTest(TestCase): - def setUp(self): self.school = School(name='Northeastern University') self.school.save() diff --git a/karmaworld/apps/notes/models.py b/karmaworld/apps/notes/models.py index 37c2029..db8ca7a 100644 --- a/karmaworld/apps/notes/models.py +++ b/karmaworld/apps/notes/models.py @@ -26,8 +26,8 @@ from karmaworld.apps.courses.models import Course import karmaworld.apps.notes.search as search try: - from karmaworld.secrets.drive import GOOGLE_USER -except: + from karmaworld.secret.drive import GOOGLE_USER +except ImportError: GOOGLE_USER = u'admin@karmanotes.org' fs = FileSystemStorage(location=settings.MEDIA_ROOT) @@ -172,7 +172,6 @@ class Note(Document): tweeted = models.BooleanField(default=False) thanks = models.PositiveIntegerField(default=0) - def __unicode__(self): return u"Note: {0} {1} -- {2}".format(self.file_type, self.name, self.uploaded_at) diff --git a/karmaworld/apps/notes/search.py b/karmaworld/apps/notes/search.py index e47656f..e08c3b9 100644 --- a/karmaworld/apps/notes/search.py +++ b/karmaworld/apps/notes/search.py @@ -1,13 +1,19 @@ #!/usr/bin/env python # -*- coding:utf8 -*- # Copyright (C) 2013 FinalsClub Foundation +import time import indextank.client as itc import karmaworld.secret.indexden as secret api_client = itc.ApiClient(secret.PRIVATE_URL) -index = api_client.get_index('karmanotes') +if not api_client.get_index(secret.INDEX).exists(): + api_client.create_index(secret.INDEX, {'public_search': False}) +index = api_client.get_index(secret.INDEX) + +while not index.has_started(): + time.sleep(0.5) def note_to_dict(note): d = { diff --git a/karmaworld/apps/notes/tests.py b/karmaworld/apps/notes/tests.py index 0425f43..45cb695 100644 --- a/karmaworld/apps/notes/tests.py +++ b/karmaworld/apps/notes/tests.py @@ -3,20 +3,26 @@ # Copyright (C) 2012 FinalsClub Foundation """ """ +import karmaworld.secret.indexden as secret +import uuid -import datetime +# This needs to happen before other things +# are imported to avoid putting test data +# in our production search index +secret.INDEX = uuid.uuid4().hex -from nose.tools import eq_ -from nose.tools import ok_ -from nose.tools import assert_is_none +import datetime +from django.test import TestCase +from karmaworld.apps.notes import search from karmaworld.apps.notes.models import Note from karmaworld.apps.courses.models import Course from karmaworld.apps.courses.models import School +import indextank.client as itc +class TestNoes(TestCase): -class BaseNote(object): - def setup(self): + def setUp(self): # create base values to test db representations self.now = datetime.datetime.utcnow() @@ -41,41 +47,40 @@ class BaseNote(object): #self.note.slug := do not set for test_remake_slug() behavior self.note.file_type = 'doc' self.note.uploaded_at = self.now + self.note.text = "This is the plaintext version of a note. It's pretty cool. Alpaca." self.note.save() - def teardown(self): - """ erase anything we created """ - print "generating a note teardown" - self.note.delete() - - -class TestNoteWithRelation(BaseNote): - """ Test the Note model with fkey to courses.models.Course """ + @classmethod + def tearDownClass(cls): + """Delete the test index that was automatically + created by notes/search.py""" + api = itc.ApiClient(secret.PRIVATE_URL) + api.delete_index(secret.INDEX) def test_unicode(self): """ Ensure that the unicode repl for a Note is as expected """ - expected = u"doc: Lecture notes concerning the use of therefore ∴ -- {0}"\ + expected = u"Note: doc Lecture notes concerning the use of therefore ∴ -- {0}"\ .format(self.now) - eq_(self.note.__unicode__(), expected) + self.assertEqual(self.note.__unicode__(), expected) def test_course_fkey(self): - eq_(self.course, self.note.course) - - -class TestNoteSlug(BaseNote): - """ Test the conditional generation of the Note.slug field """ - - expected = u'lecture-notes-concerning-the-use-of-therefore' + self.assertEqual(self.course, self.note.course) def test_slug_natural(self): """ Test that the slug field is slugifying unicode Note.names """ - eq_(self.note.slug, self.expected) + expected = u"lecture-notes-concerning-the-use-of-therefore" + self.assertEqual(self.note.slug, expected) def test_remake_slug(self): - """ Test the generation of a Note.slug field based on Note.name """ + """ Test the generation of a Note.slug field based on Note. + Name collision is expected, so see if slug handles this.""" + expected = u"lecture-notes-concerning-the-use-of-therefore-{0}-{1}-{2}".format( + self.note.uploaded_at.month, + self.note.uploaded_at.day, self.note.uploaded_at.microsecond) + self.note.slug = None self.note.save() - eq_(self.note.slug, self.expected) + self.assertEqual(self.note.slug, expected) def test_save_no_slug(self): """ Test that Note.save() doesn't make a slug @@ -85,10 +90,8 @@ class TestNoteSlug(BaseNote): self.note.slug = None self.note.save() # re-save the note # check that slug has note been generated - assert_is_none(self.note.slug) - + self.assertIsNone(self.note.slug) -class TestNoteUrl(BaseNote): expected_url_prefix = u'/marshall-college/archaeology-101/' expected_slug = u'lecture-notes-concerning-the-use-of-therefore' expected = expected_url_prefix + expected_slug @@ -96,9 +99,22 @@ class TestNoteUrl(BaseNote): def test_note_get_absolute_url_slug(self): """ Given a note with a slug, test that an expected url is generated """ # check that Note.get_absolute_url() is generating the right url - eq_(self.note.get_absolute_url(), self.expected) + self.assertEqual(self.note.get_absolute_url(), self.expected) def test_note_get_absolute_url_id(self): self.note.slug = None url = self.expected_url_prefix + str(self.note.id) - eq_(self.note.get_absolute_url(), url) + self.assertEqual(self.note.get_absolute_url(), url) + + def test_search_index(self): + """Search for a note within IndexDen""" + + # Search for it + results = search.search('alpaca') + self.assertIn(str(self.note.id), results) + + # Search for it, filtering by course + results = search.search('alpaca', self.note.course.id) + self.assertIn(str(self.note.id), results) + +