def test_unicode(self): # blog unicode_blog = Blogs(PROFILE_UNICODE, PROFILE_DIRECTORY) unicode_blog.add_blog(u"Enchanté", PROFILE_UNICODE) unicode_blog.save() # doc unicode_doc = FileDocument(PROFILE_UNICODE, PROFILE_DIRECTORY) # write data unicode_doc.set_title(u"Mr") unicode_doc.set_firstname(u"Zoé") unicode_doc.set_lastname(u"Bréton") unicode_doc.set_photo(QUESTION_MARK()) unicode_doc.set_email(u"*****@*****.**") unicode_doc.load_defaults() unicode_doc.add_custom_attributes((u"été", u"chôô")) unicode_doc.remove_custom_attributes(u'Sport') # save unicode_doc.save() # check content blog = load_blogs(PROFILE_UNICODE, PROFILE_DIRECTORY) self.assertEquals(blog.blogs[0].text, u"Enchanté") doc = FileDocument(PROFILE_UNICODE, PROFILE_DIRECTORY) doc.load() self.assertEquals(u"Mr", doc.get_title()) self.assertEquals(u"Zoé", doc.get_firstname()) self.assertEquals(u"Bréton", doc.get_lastname()) self.assertEquals(QUESTION_MARK(), doc.get_photo()) self.assertEquals(u"*****@*****.**", doc.get_email()) self.assertEquals({'City': u'', 'été': u'chôô', 'Country': u'', 'Favourite Book': u'', 'Favourite Movie': u'', 'Studies': u''}, doc.get_custom_attributes())
def test_save(self): # write bruce blog try: bruce_blog = load_blogs(PROFILE_BRUCE, PROFILE_DIRECTORY) except ValueError: bruce_blog = Blogs(PROFILE_BRUCE, PROFILE_DIRECTORY, pseudo=PROFILE_BRUCE) bruce_blog.add_blog("Hi Buddy", PROFILE_BRUCE) bruce_blog.save() # write data self.document.set_title(u"Mr") self.document.set_firstname(u"manu") self.document.set_lastname(u"breton") self.document.set_photo(QUESTION_MARK()) self.document.set_email(u"*****@*****.**") self.document.add_custom_attributes((u"homepage", u"manu.com")) self.document.add_custom_attributes((u'color', u'blue')) # set files self.document.add(abspath(u"data")) self.document.expand_dir(abspath(u"data")) self.document.share_files((abspath(u"data"), ["routage", u"emptydir", u"subdir1"], True)) self.document.expand_dir(abspath(u"data/subdir1")) self.document.share_files((abspath(u"data/subdir1/subsubdir"), ["null", "dummy.txt"], True)) self.document.tag_files((abspath(u"data"), ["date.txt"], u"tagos")) self.document.tag_files((abspath(u"data/subdir1/subsubdir"), ["null", "dummy.txt"], u"empty")) # set peers bruce_doc = FileDocument(PROFILE_BRUCE, PROFILE_DIRECTORY) bruce_doc.load() self.document.fill_data((u"bruce", bruce_doc)) self.document.fill_blog((u"bruce", load_blogs(PROFILE_BRUCE, PROFILE_DIRECTORY))) self.document.make_friend(u"bruce") # write file self.document.save() # check content self._assertContent(self.document)
def test_blog(self): blog = Blogs('atao') self.assertRaises(AssertionError, blog.add_blog, "no owner yet", "manu") self.assertRaises(AssertionError, blog.remove_blog, 0, "no owner yet") blog.set_owner('manu') blog.add_blog("first blog", "manu") self.assertRaises(AssertionError, blog.add_blog, "forbidden", 'jules') self.assertEquals(blog.count_blogs(), 1) self.assertEquals(blog.get_blog(0).text, "first blog") copied_blog = blog.copy() self.assertRaises(AssertionError, copied_blog.add_blog, "forbidden", 'jules') self.assertEquals(copied_blog.count_blogs(), 1) self.assertEquals(copied_blog.get_blog(0).text, "first blog")
def get_bruce_profile(): """return PeerDescriptor filled with: node_id = PROFILE_BRUCE Blog: ````` 'Hi Buddy' Files: `````` data/ |-- date.txt Shared |-- ... `-- subdir1 |-- date.doc Shared `-- ... """ # write bruce blog bruce_blog = Blogs() bruce_blog.add_blog(u"Hi Buddy", u"bruce") # write bruce profile bruce_doc = FileDocument() bruce_doc.set_pseudo(u"bruce") bruce_doc.set_title(u"Mr") bruce_doc.set_firstname(u"bruce") bruce_doc.set_lastname(u"willis") bruce_doc.set_photo(QUESTION_MARK()) bruce_doc.set_email(u"*****@*****.**") bruce_doc.add_custom_attributes(u"Homepage", u"bruce.com") bruce_doc.add_custom_attributes(u'Color', u'gray') bruce_doc.add_custom_attributes(u'Movie', u'6th Sense') bruce_doc.add_custom_attributes(u'Actor', u'Robin Williams') bruce_doc.add_repository(TEST_DIR) bruce_doc.share_file(os.path.join(DATA_DIR, "date.txt")) bruce_doc.share_file(os.sep.join([DATA_DIR, "subdir1", "date.doc"])) return PeerDescriptor(PROFILE_BRUCE, document=bruce_doc, blog=bruce_blog)
class BlogTest(unittest.TestCase): """Test Blog behaviour""" def setUp(self): """override one in unittest.TestCase""" self.blog = Blogs(PROFILE_TEST, directory=PROFILE_DIRECTORY) self.blog.add_blog("first blog", PROFILE_TEST) self.blog.add_blog("second blog", PROFILE_TEST) self.blog.add_comment(0, "commenting the first", PSEUDO) self.blog.save() def test_blog(self): self.assertEquals(self.blog.count_blogs(), 2) self.assertEquals(self.blog.get_blog(0).text, "first blog") self.assertEquals(self.blog.get_blog(0).count_blogs(), 1) self.assertEquals(self.blog.get_blog(1).text, "second blog") self.assertEquals(self.blog.get_blog(1).count_blogs(), 0) def test_security(self): self.assertRaises(AssertionError, self.blog.add_blog, "forbidden", "jules") self.assertRaises(AssertionError, self.blog.remove_blog, 0, "jules") self.assertRaises(AssertionError, self.blog.get_blog, 2) def test_copy(self): copied_blog = self.blog.copy() # add some blogs in first self.blog.add_blog("more blog", PROFILE_TEST) self.blog.add_comment(0, "commenting (again) the first", PSEUDO) # check result self.assertEquals(self.blog.count_blogs(), 3) self.assertEquals(self.blog.get_blog(0).count_blogs(), 2) # check copy self.assertEquals(copied_blog.count_blogs(), 2) self.assertEquals(copied_blog.get_blog(0).text, "first blog") self.assertEquals(copied_blog.get_blog(0).count_blogs(), 1) self.assertEquals(copied_blog.get_blog(1).text, "second blog") self.assertEquals(copied_blog.get_blog(1).count_blogs(), 0) def test_load(self): loaded_blog = load_blogs(PROFILE_TEST, directory=PROFILE_DIRECTORY) self.assertEquals(loaded_blog.count_blogs(), 2) self.assertEquals(loaded_blog.get_blog(0).text, "first blog") self.assertEquals(loaded_blog.get_blog(0).count_blogs(), 1) self.assertEquals(loaded_blog.get_blog(1).text, "second blog") self.assertEquals(loaded_blog.get_blog(1).count_blogs(), 0) def test_retro_compatibility(self): loaded_blog = load_blogs(PROFILE_010, directory=PROFILE_DIRECTORY) self.assertEquals(loaded_blog.count_blogs(), 4) self.assertEquals(loaded_blog.get_blog(0).text, u"Hi there! Watch my next movie... it's going to be real fun") self.assertEquals(loaded_blog.get_blog(0).count_blogs(), 0) self.assertEquals( loaded_blog.get_blog(1).text, u"I won't tell you much but this I can say: it's going to be terrific!" ) self.assertEquals(loaded_blog.get_blog(2).text, u"Watch here for its name... coming soon") self.assertEquals(loaded_blog.get_blog(3).text, u"A Godess' world. What do you think?")
class BlogTest(unittest.TestCase): """Test Blog behaviour""" def setUp(self): """override one in unittest.TestCase""" write_test_profile() self.blog = Blogs() self.blog.add_blog(u"first blog", PROFILE_TEST) self.blog.add_blog(u"second blog", PROFILE_TEST) self.blog.add_comment(0, u"commenting the first", PSEUDO) def test_blog(self): self.assertEquals(self.blog.count_blogs(), 2) self.assertEquals(self.blog.get_blog(0).text, u"first blog") self.assertEquals(self.blog.get_blog(0).count_blogs(), 1) self.assertEquals(self.blog.get_blog(1).text, u"second blog") self.assertEquals(self.blog.get_blog(1).count_blogs(), 0) def test_copy(self): copied_blog = self.blog.copy() # add some blogs in first self.blog.add_blog(u"more blog", PROFILE_TEST) self.blog.add_comment(0, u"commenting (again) the first", PSEUDO) # check result self.assertEquals(self.blog.count_blogs(), 3) self.assertEquals(self.blog.get_blog(0).count_blogs(), 2) # check copy self.assertEquals(copied_blog.count_blogs(), 2) self.assertEquals(copied_blog.get_blog(0).text, u"first blog") self.assertEquals(copied_blog.get_blog(0).count_blogs(), 1) self.assertEquals(copied_blog.get_blog(1).text, u"second blog") self.assertEquals(copied_blog.get_blog(1).count_blogs(), 0) def test_load(self): loaded_blog = load_blogs(FILE_TEST + BLOG_EXT) self.assertEquals(loaded_blog.count_blogs(), 1) self.assertEquals(loaded_blog.get_blog(0).text, u"This is a test") self.assertEquals(loaded_blog.get_blog(0).count_blogs(), 0) def test_retro_compatibility(self): loaded_blog = load_blogs(os.path.join(PROFILE_DIR, PROFILE_010 + BLOG_EXT)) self.assertEquals(loaded_blog.count_blogs(), 4) self.assertEquals(loaded_blog.get_blog(0).text, u"Hi there! Watch my next movie... it's going to be real fun") self.assertEquals(loaded_blog.get_blog(0).count_blogs(), 0) self.assertEquals( loaded_blog.get_blog(1).text, u"I won't tell you much but this I can say: it's going to be terrific!" ) self.assertEquals(loaded_blog.get_blog(2).text, u"Watch here for its name... coming soon") self.assertEquals(loaded_blog.get_blog(3).text, u"A Godess' world. What do you think?")
def write_test_profile(): """write testing profile & blogs into test.prf & test.blog node_id = PROFILE_BRUCE Blog: ````` 'This is a test' Files: `````` data/ |-- date.txt - tagos |-- emptydir Shared |-- profiles | `-- ... |-- routage Shared `-- subdir1 Shared |-- TOtO.txt |-- date.doc `-- subsubdir |-- dummy.txt Shared - tag2 |-- null Shared - tag1 `-- ...""" # write filter filter_document = FilterDocument() filter_document.set_pseudo(FilterValue(value=".*", activate=True)) filter_document.set_title(FilterValue(value="Mr", activate=True)) filter_document.set_lastname(FilterValue(value="b.*", activate=True)) filter_document.add_custom_attributes('color', FilterValue(value="blue", activate=True)) filter_document.add_repository("MP3", FilterValue(value=".*\.mp3", activate=True)) if os.path.exists(FILE_TEST + FILTER_EXT): os.remove(FILE_TEST + FILTER_EXT) filter_document.save(FILE_TEST + FILTER_EXT) # write blog blog = Blogs() blog.add_blog(u"This is a test", PSEUDO) if os.path.exists(FILE_TEST + BLOG_EXT): os.remove(FILE_TEST + BLOG_EXT) blog.save(FILE_TEST + BLOG_EXT) # write profile document = FileDocument() document.set_pseudo(PSEUDO) document.set_title(u"Mr") document.set_firstname(u"manu") document.set_lastname(u"breton") document.set_photo(QUESTION_MARK()) document.set_email(u"*****@*****.**") document.load_defaults() document.add_custom_attributes(u"homepage", u"manu.com") document.add_custom_attributes(u'color', u'blue') document.remove_custom_attributes(u'Sport') document.add_repository(TEST_DIR) document.expand_dir(DATA_DIR) document.expand_dir(os.path.join(DATA_DIR, "subdir1")) document.share_files(DATA_DIR, ["routage", "emptydir", "subdir1"]) document.share_files(os.sep.join([DATA_DIR, "subdir1", "subsubdir"]), ["null", "dummy.txt"]) document.tag_file(os.path.join(DATA_DIR, "date.txt"), u"tagos") document.tag_file(os.sep.join([DATA_DIR, "subdir1", "subsubdir", "null"]), u"tag1") document.tag_file(os.sep.join([DATA_DIR, "subdir1", "subsubdir", "dummy.txt"]), u"tag2") # set peers bruce = get_bruce_profile() bruce_doc = bruce.document bruce_blog = bruce.blog document.fill_data(PROFILE_BRUCE, bruce_doc) document.fill_blog(PROFILE_BRUCE, bruce_blog) document.make_friend(PROFILE_BRUCE) # write file if os.path.exists(FILE_TEST + PROFILE_EXT): os.remove(FILE_TEST + PROFILE_EXT) document.save(FILE_TEST + PROFILE_EXT)
class Facade: """manages user's actions & connects document and view""" s_facade = None def __init__(self): self.documents = {} self.views = {} self.blogs = Blogs() self.activated = True # views def add_view(self, view): """add a view object to facade""" self.views[view.get_name()] = view def reset_view(self, view=None): """add a view object to facade""" self.views.clear() if view: self.views[view.get_name()] = view # documents def add_document(self, doc): """add a view object to facade""" # syncrhonize existing docs if "cache" in self.documents: doc.import_document(self.documents["cache"]) # add new self.documents[doc.get_name()] = doc def get_document(self, name): """return document named 'name' if available, first otherwise""" if name in self.documents: return self.documents[name] else: return self.documents.values() \ and self.documents.values()[0] or None def reset_document(self, doc=None): """add a view object to facade""" self.documents.clear() if doc: self.documents[doc.get_name()] = doc # blog def add_blog(self, text): """store blog in cache as wx.HtmlListBox is virtual. return blog's index""" pseudo = self.get_document('cache').get_pseudo() self.blogs.add_blog(text, pseudo) self.update_blogs() def remove_blog(self, index): """delete blog""" pseudo = self.get_document('cache').get_pseudo() self.blogs.remove_blog(index, pseudo) self.update_blogs() def add_comment(self, (index, text)): """store blog in cache as wx.HtmlListBox is virtual. return comment's index""" pseudo = self.get_document('cache').get_pseudo() self.blogs.add_comment(index, text, pseudo) self.update_blogs()