def test_and_filters(self):
     p_filter = PeerFilter("test peer", filter_or=False, **{
         "title": "Mr",
         "firstname": "Bob",
         "lastname": "b",
         "pseudo": "emb"})
     matches =  p_filter.match("bob", self.peer_desc)
     self.assertEquals([], matches)
     # ok
     p_filter = PeerFilter("test peer", filter_or=False, **{
         "title": "Mr",
         "lastname": "b",
         "pseudo": "emb"})
     matches =  p_filter.match("bob", self.peer_desc)
     self.assertEquals(3, len(matches))
 def test_creation(self):
     name, new_filter = PeerFilter.from_str("peer_bob", [
         ("title", "Mr, (1)"),
         ("lastname", "Bob, (0)")])
     self.assertEquals("bob", name)
     self.assert_(new_filter)
     name, new_filter = FileFilter.from_str("peer_bob", [
         ("title", "Mr, (1)"),
         ("lastname", "Bob, (0)")])
     self.assertEquals(None, new_filter)
     # files
     name, new_filter = FileFilter.from_str("file_mp3", [
         ("name", "mp3, (1)")])
     self.assertEquals("mp3", name)
     self.assert_(new_filter)
     name, new_filter = PeerFilter.from_str("file_mp3", [
         ("name", "mp3, (1)")])
     self.assertEquals(None, new_filter)
 def setUp(self):
     # filters
     self.peer_filter = PeerFilter("test peer", **{
         "title": "Mr",
         "firstname": "Bob",
         "lastname": "b",
         "pseudo": "emb"})
     self.file_filter = FileFilter("test file", **{
         "name": "mp3"})
     # properties
     doc = CacheDocument()
     doc.title = "Mr"
     doc.firstname = "Emmanuel"
     doc.lastname = "Breton"
     doc.pseudo = "emb"
     doc.photo = "none"
     doc.email = "*****@*****.**"
     doc.add_custom_attributes(u"book", u"Harry Potter")
     doc.add_custom_attributes(u"movie", u"Leon")
     doc.add_custom_attributes(u"sport", u"biking")
     self.peer_desc = PeerDescriptor("bob", document=doc)
     self.file_props = ToBeMatch(**{
         "name": "Hero.mp3",
         "size": "3000"})
class FiltersTest(unittest.TestCase):

    def setUp(self):
        # filters
        self.peer_filter = PeerFilter("test peer", **{
            "title": "Mr",
            "firstname": "Bob",
            "lastname": "b",
            "pseudo": "emb"})
        self.file_filter = FileFilter("test file", **{
            "name": "mp3"})
        # properties
        doc = CacheDocument()
        doc.title = "Mr"
        doc.firstname = "Emmanuel"
        doc.lastname = "Breton"
        doc.pseudo = "emb"
        doc.photo = "none"
        doc.email = "*****@*****.**"
        doc.add_custom_attributes(u"book", u"Harry Potter")
        doc.add_custom_attributes(u"movie", u"Leon")
        doc.add_custom_attributes(u"sport", u"biking")
        self.peer_desc = PeerDescriptor("bob", document=doc)
        self.file_props = ToBeMatch(**{
            "name": "Hero.mp3",
            "size": "3000"})

    def test_creation(self):
        name, new_filter = PeerFilter.from_str("peer_bob", [
            ("title", "Mr, (1)"),
            ("lastname", "Bob, (0)")])
        self.assertEquals("bob", name)
        self.assert_(new_filter)
        name, new_filter = FileFilter.from_str("peer_bob", [
            ("title", "Mr, (1)"),
            ("lastname", "Bob, (0)")])
        self.assertEquals(None, new_filter)
        # files
        name, new_filter = FileFilter.from_str("file_mp3", [
            ("name", "mp3, (1)")])
        self.assertEquals("mp3", name)
        self.assert_(new_filter)
        name, new_filter = PeerFilter.from_str("file_mp3", [
            ("name", "mp3, (1)")])
        self.assertEquals(None, new_filter)

    def test_file_filter(self):
        matches = self.file_filter.match("bob", [self.file_props])
        self.assertEquals(1, len(matches))
        self.assertEquals("name", matches[0].get_name())
        self.assertEquals("mp3", matches[0].get_description())
        self.assertEquals("Hero.mp3", matches[0].get_match())

    def test_peer_filter(self):
        matches =  self.peer_filter.match("bob", self.peer_desc)
        self.assertEquals(3, len(matches))
        self.assertEquals("title", matches[0].get_name())
        self.assertEquals("b", matches[1].get_description())
        self.assertEquals("emb", matches[2].get_match())

    def test_and_filters(self):
        p_filter = PeerFilter("test peer", filter_or=False, **{
            "title": "Mr",
            "firstname": "Bob",
            "lastname": "b",
            "pseudo": "emb"})
        matches =  p_filter.match("bob", self.peer_desc)
        self.assertEquals([], matches)
        # ok
        p_filter = PeerFilter("test peer", filter_or=False, **{
            "title": "Mr",
            "lastname": "b",
            "pseudo": "emb"})
        matches =  p_filter.match("bob", self.peer_desc)
        self.assertEquals(3, len(matches))

    def test_update(self):
        self.peer_filter.update_title(FilterValue("title", "Mss", True))
        self.assertEquals(2, len(
            self.peer_filter.match("bob", self.peer_desc)))
        self.peer_filter.update_email(FilterValue("email", "fr", True))
        self.assertEquals(3, len(
            self.peer_filter.match("bob", self.peer_desc)))

    def test_update_properties(self):
        self.assertEquals("Mr", self.peer_filter.title.description)
        self.assertEquals("", self.peer_filter.email.description)
        props = {"title": "Mss",
                 "email": "fr"}
        self.peer_filter.update_properties(**props)
        self.assertEquals("Mss", self.peer_filter.title.description)
        self.assertEquals("fr", self.peer_filter.email.description)
        
    def test_customs(self):
        self.peer_filter.update_dict(FilterValue("book", "potter", True))
        self.peer_filter.update_dict(FilterValue("sport", "bik", True))
        matches =  self.peer_filter.match("bob", self.peer_desc)
        self.assertEquals(5, len(matches))
        self.assertEquals("book", matches[3].get_name())
        self.assertEquals("bik", matches[4].get_description())

    def test_as_dict(self):
        peer_dict = self.peer_filter.as_dict()
        file_dict = self.file_filter.as_dict()
        self.assertEquals({'name': 'mp3, (1)',
                           'size': ', (0)'}, file_dict)
        self.assertEquals({'firstname': 'Bob, (1)',
                           'title': 'Mr, (1)',
                           'lastname': 'b, (1)',
                           'pseudo': 'emb, (1)',
                           'photo': ', (0)',
                           'email': ', (0)'}, peer_dict)
Esempio n. 5
0
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()
    peer_filter = PeerFilter("Mr_B", filter_or=False,**{
        "pseudo" : "*",
        "title": "Mr",
        "lastname": "b"})
    peer_filter.update_dict(FilterValue(name='color',
                                        value="blue",
                                        activate=True))
    file_filter = FileFilter("MP3", **{"name": ".mp3"})
    filter_document.filters[peer_filter.filter_name] = peer_filter
    filter_document.filters[file_filter.filter_name] = file_filter
    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)