from email.Message import Message import os import shutil from mcoll import * import unittest SCRATCH = "tests/output/main" TRASH = "tests/output/trash" SRC = "tests/source" class Test(unittest.TestCase): def setUp(self): if os.path.exists(SCRATCH): shutil.rmtree(SCRATCH) if os.path.exists(TRASH): shutil.rmtree(TRASH) def tearDown(self): if os.path.exists(SCRATCH): shutil.rmtree(SCRATCH) if os.path.exists(TRASH): shutil.rmtree(TRASH) def test_init_id(self): dpath = os.path.join(SCRATCH, "data") os.makedirs(dpath) for n in [2, 3, 12]: f = file(os.path.join(dpath, str(n)), "w") f.write("published: 2006-12-28T22:30:%02d\r\nupdated: 2006-12-29T22:30:%02d\r\n\r\n" % (n, n)) f.close() c = Collection(SCRATCH) max_id = int(file(os.path.join(SCRATCH, "id"), "r").read()) self.assertEqual(13, max_id) self.assertTrue("2006-12-28T22:30:02-2" in os.listdir(os.path.join(SCRATCH, "index", "published"))) self.assertTrue("2006-12-29T22:30:12-12" in os.listdir(os.path.join(SCRATCH, "index", "updated"))) def test_create(self): c = Collection(SCRATCH, TRASH) id = c.create({"content": u"This is a \N{COMET} test"}) self.assertEqual('1', id) id = c.create({"content": u"This is a \N{COMET} test"}) self.assertEqual('2', id) t = Collection(TRASH) self.assertEqual(2, len(t.id_list())) c.delete('2') self.assertEqual(3, len(t.id_list())) id = c.create({"content": u"This is a \N{COMET} test"}) try: message = c.get('2') self.fail("Should not get here.") except: pass self.assertEqual('3', id) def test_update(self): c = Collection(SCRATCH) id = c.create({"content": u"
This is a \N{COMET} test
"}) msg = c.get(id) first_update = msg['updated'] self.assertEqual(msg['content'], u"This is a \N{COMET} test
") msg['content'] = u"&foobar; ben & jerrys < && '} id = c.create(member) member = c.get(id) self.assertEqual(member['content'], u"
&foobar ben & jerrys < &&
") def test_illegal_chars(self): c = Collection(SCRATCH) member = {'content': u'thisthat'} id = c.create(member) member = c.get(id) self.assertEqual(member['content'], u'
this\ufffdthat
') def test_all_entities_in_attributes(self): c = Collection(SCRATCH) member = {'content': u'foo bar &foobar;±¡¡ ', 'title': 'foo'} id = c.create(member) member = c.get(id) self.assertEqual(member['content'], u'foo bar &foobar\N{PLUS-MINUS SIGN}\N{INVERTED EXCLAMATION MARK}\N{INVERTED EXCLAMATION MARK} ') def test_title_stripping(self): c = Collection(SCRATCH) member = {'title': 'This is < bold♀.'} id = c.create(member) member = c.get(id) self.assertEqual(member['slug'], 'This-is-b-bold-b-x2640') self.assertEqual(member['title'], u'This is < bold♀.') def test_indexing(self): c = Collection(SCRATCH) member = {'title': 'This is bold.'} id1 = c.create(member) id = c.create(member) updated_start = sorted(os.listdir(os.path.join(SCRATCH, "index", "updated"))) published_start = sorted(os.listdir(os.path.join(SCRATCH, "index", "published"))) member['title'] = "Now changed." c.put(id, member) updated_end = os.listdir(os.path.join(SCRATCH, "index", "updated")) published_end = os.listdir(os.path.join(SCRATCH, "index", "published")) self.assertTrue(updated_start[0] in updated_end) self.assertFalse(updated_start[1] in updated_end) self.assertEqual(published_start, published_end) self.assertEqual(2, len(published_start)) self.assertEqual(2, len(published_end)) self.assertEqual(2, len(updated_start)) self.assertEqual(2, len(updated_end)) c.delete(id1) updated_end = os.listdir(os.path.join(SCRATCH, "index", "updated")) published_end = os.listdir(os.path.join(SCRATCH, "index", "published")) self.assertEqual(1, len(published_end)) self.assertEqual(1, len(updated_end)) def test_index_lists(self): c = Collection(SCRATCH) id1 = c.create({'title': 'This is first.'}) id2 = c.create({'title': 'This is second.'}) published = c.id_list_by_published() self.assertEqual(c.get_by_published_id(published[1])['title'], "This is first.") self.assertEqual(c.get_by_published_id(published[0])['title'], "This is second.") updated = c.id_list_by_updated() self.assertEqual(c.get_by_updated_id(updated[1])['title'], "This is first.") self.assertEqual(c.get_by_updated_id(updated[0])['title'], "This is second.") member = c.get(id1) member['title'] = "Now changed." c.put(id1, member) published = c.id_list_by_published() self.assertEqual(c.get_by_published_id(published[1])['title'], "Now changed.") self.assertEqual(c.get_by_published_id(published[0])['title'], "This is second.") updated = c.id_list_by_updated() self.assertEqual(c.get_by_updated_id(updated[1]).title, "This is second.") self.assertEqual(c.get_by_updated_id(updated[0])['title'], "Now changed.")