class FilterTest(unittest.TestCase):
    """test that all fields are correctly validated"""

    def setUp(self):
        """override one in unittest.TestCase"""
        write_test_profile()
        # load profile
        self.document = FilterDocument()
        self.document.load(FILTER_TEST)

    def assert_content(self, document):
        self.assertEquals(3, len(self.document.filters))
        self.assert_(self.document.filters["All"].filter_or)
        self.assert_(self.document.filters["MP3"].filter_or)
        self.assertEquals(False, self.document.filters["Mr_B"].filter_or)
        all_dict = self.document.filters["All"].as_dict()
        peer_dict = self.document.filters["Mr_B"].as_dict()
        file_dict = self.document.filters["MP3"].as_dict()
        self.assertEquals('*, (1)', all_dict['pseudo'])
        self.assertEquals('.mp3, (1)', file_dict['name'])
        self.assertEquals('*, (1)', peer_dict['pseudo'])
        self.assertEquals('Mr, (1)', peer_dict['title'])
        self.assertEquals('b, (1)', peer_dict['lastname'])
        self.assertEquals('blue, (1)', peer_dict['color'])
            
    def test_load(self):
        self.assert_content(self.document)

    def test_import(self):
        doc = FilterDocument()
        doc.import_document(self.document)
        self.assert_content(doc)

    def test_match(self):
        peer_desc = PeerDescriptor(PROFILE_TEST)
        peer_desc.load(directory=PROFILE_DIR)
        self.document.match(peer_desc)
        # check results
        self.assertEquals(3, len(self.document.results))
        self.assert_('All' in self.document.results)
        self.assert_('Mr_B' in self.document.results)
        self.assert_('MP3' in self.document.results)
        results = [(result.get_name(), result.match)
                   for result in self.document.results['Mr_B'][PROFILE_TEST]]
        results.sort()
        self.assertEquals([('color', 'blue'),
                           ('lastname', 'breton'),
                           ('pseudo', 'atao'),
                           ('title', 'Mr')],
                          results)
 def setUp(self):
     """override one in unittest.TestCase"""
     self.document = FilterDocument(PROFILE_TEST, PROFILE_DIRECTORY)
     # first test to call must write test file to get correct
     # execution of the others
     if not os.path.exists(os.path.join(PROFILE_DIRECTORY, PROFILE_TEST + FILTER_EXT)):
         self.test_save()
 def setUp(self):
     # peer
     peer_document = FileDocument(PROFILE_TEST, PROFILE_DIRECTORY)
     peer_document.load()
     self.peer_desc = PeerDescriptor(PROFILE_TEST, document=peer_document)
     self.peer_desc.set_shared_files()
     # filter
     self.document = FilterDocument(PROFILE_TEST, PROFILE_DIRECTORY)
     self.document.load()
 def setUp(self):
     write_test_profile()
     # peer
     self.peer_desc = PeerDescriptor(PROFILE_TEST)
     self.peer_desc.load(directory=PROFILE_DIR)
     self.peer_desc.document.share_files(TEST_DIR,
                                         [os.path.join("data", "date.txt"),
                                          os.path.join("data", "subdir1")],
                                         True)
     # filter
     self.document = FilterDocument()
     self.document.load(FILTER_TEST)
class MatchTest(unittest.TestCase):

    def setUp(self):
        # peer
        peer_document = FileDocument(PROFILE_TEST, PROFILE_DIRECTORY)
        peer_document.load()
        self.peer_desc = PeerDescriptor(PROFILE_TEST, document=peer_document)
        self.peer_desc.set_shared_files()
        # filter
        self.document = FilterDocument(PROFILE_TEST, PROFILE_DIRECTORY)
        self.document.load()

    def test_creation(self):
        match = PeerMatch(self.peer_desc, self.document)
        self.assertEquals(match.title.get_match(), "Mr")
        self.assertEquals(match.firstname, False)
        self.assertEquals(match.lastname.get_match(), "breton")
        self.assertEquals(match.photo, False)
        self.assertEquals(match.email, False)
        self.assertEquals(match.customs['color'].get_match(), u'blue')
        self.assertEquals(match.files, {})

    def test_activated(self):
        # activate
        self.document.get_email().activate()
        match = PeerMatch(self.peer_desc, self.document)
        self.assertEquals(match.email.get_match(), u'*****@*****.**')

    def test_files(self):
        # add filter for dummy.txt
        self.document.add_file((u'Any', FilterValue(value=u'.*\..*', activate=True)))
        match = PeerMatch(self.peer_desc, self.document)
        match_files = [file_container.match for file_container in match.files[u'Any']]
        self.assertEquals(match_files, ['TOtO.txt', 'dummy.txt'])
 def setUp(self):
     """override one in unittest.TestCase"""
     # write test profile
     if os.path.exists(FILTER_TEST):
         os.remove(FILTER_TEST)
     write_test_filter()
     # write test profile
     if os.path.exists(FILE_TEST):
         os.remove(FILE_TEST)
     write_test_profile()
     # load profile
     self.document = FilterDocument()
     self.document.load(FILTER_TEST)
class MatchTest(unittest.TestCase):

    def setUp(self):
        write_test_profile()
        # peer
        self.peer_desc = PeerDescriptor(PROFILE_TEST)
        self.peer_desc.load(directory=PROFILE_DIR)
        self.peer_desc.document.share_files(TEST_DIR,
                                            [os.path.join("data", "date.txt"),
                                             os.path.join("data", "subdir1")],
                                            True)
        # filter
        self.document = FilterDocument()
        self.document.load(FILTER_TEST)

    def test_creation(self):
        match = PeerMatch(self.peer_desc, self.document)
        self.assertEquals(match.title.get_match(), "Mr")
        self.assertEquals(match.firstname, False)
        self.assertEquals(match.lastname.get_match(), "breton")
        self.assertEquals(match.photo, False)
        self.assertEquals(match.email, False)
        self.assertEquals(match.customs['color'].get_match(), u'blue')
        self.assertEquals(match.files, {})

    def test_activated(self):
        # activate
        self.document.get_email().activated = True
        match = PeerMatch(self.peer_desc, self.document)
        self.assertEquals(match.lastname.get_match(), u'breton')

    def test_files(self):
        # add filter for dummy.txt
        self.document.add_repository(u'Any', FilterValue(value=u'.*\..*', activate=True))
        match = PeerMatch(self.peer_desc, self.document)
        match_files = [file_container.match for file_container in match.files[u'Any']]
        match_files.sort()
        self.assertEquals(match_files, ['TOtO.txt', 'date.doc', 'date.txt', "dummy.txt"])
def write_test_filter():
    document = FilterDocument()
    # set personal data
    document.set_title(FilterValue(value=u"Mr", activate=True))
    document.set_firstname(FilterValue(value=u"", activate=True))
    document.set_lastname(FilterValue(value=u"breton", activate=True))
    document.set_photo(FilterValue(value=QUESTION_MARK(), activate=False))
    document.set_email(FilterValue(value=u"*****@*****.**", activate=False))
    # set custom interests, working if IGNORECASE set
    document.add_custom_attributes(u'color', FilterValue(value=u'BLUE', activate=True))
    # set files
    document.add_repository(u'MP3', FilterValue(value=u'.*\.mp3$', activate=True))
    # write file
    document.save(FILTER_TEST)
 def test_import(self):
     doc = FilterDocument()
     doc.import_document(self.document)
     self.assert_content(doc)
class FilterTest(unittest.TestCase):
    """test that all fields are correctly validated"""

    def setUp(self):
        """override one in unittest.TestCase"""
        # write test profile
        if os.path.exists(FILTER_TEST):
            os.remove(FILTER_TEST)
        write_test_filter()
        # write test profile
        if os.path.exists(FILE_TEST):
            os.remove(FILE_TEST)
        write_test_profile()
        # load profile
        self.document = FilterDocument()
        self.document.load(FILTER_TEST)

    def assert_content(self, document):
        self.assertEquals(document.get_title().description, u"Mr")
        self.assertEquals(document.get_title().activated, True)
        self.assertEquals(document.get_firstname().description, u"")
        self.assertEquals(document.get_firstname().activated, False)
        self.assertEquals(document.get_lastname().description, u"b.*")
        self.assertEquals(document.get_lastname().activated, True)
        self.assertEquals(document.get_photo().description, u"")
        self.assertEquals(document.get_photo().activated, False)
        self.assertEquals(document.get_email().description, u"")
        self.assertEquals(document.get_email().activated, False)
        self.assertEquals(document.has_custom_attribute(u'color'), True)
        self.assertEquals(document.has_file(u'MP3'), True)
            
    def test_save(self):
        self.assert_content(self.document)

    def test_import(self):
        doc = FilterDocument()
        doc.import_document(self.document)
        self.assert_content(doc)

    def test_customs(self):
        self.assertEquals(self.document.has_custom_attribute(u'color'), True)
        self.assertEquals(self.document.get_custom_attributes()[u'color']._name, 'color')
        self.assertEquals(self.document.get_custom_attributes()[u'color'].description, 'blue')
        self.assertEquals(self.document.get_custom_attributes()[u'color'].activated, True)
        self.document.add_custom_attributes(u'color', FilterValue(value=u'blue', activate=False))
        self.assertEquals(self.document.get_custom_attributes()[u'color'].activated, False)
        self.document.remove_custom_attributes(u'color')
        self.assertEquals(self.document.has_custom_attribute(u'color'), False)
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)
 def setUp(self):
     """override one in unittest.TestCase"""
     write_test_profile()
     # load profile
     self.document = FilterDocument()
     self.document.load(FILTER_TEST)
class FilterTest(unittest.TestCase):
    """test that all fields are correctly validated"""

    def setUp(self):
        """override one in unittest.TestCase"""
        self.document = FilterDocument(PROFILE_TEST, PROFILE_DIRECTORY)
        # first test to call must write test file to get correct
        # execution of the others
        if not os.path.exists(os.path.join(PROFILE_DIRECTORY, PROFILE_TEST + FILTER_EXT)):
            self.test_save()

    def assert_content(self):
        self.assertEquals(self.document.get_title().description, u"Mr")
        self.assertEquals(self.document.get_title().activated, True)
        self.assertEquals(self.document.get_firstname().description, u"")
        self.assertEquals(self.document.get_firstname().activated, True)
        self.assertEquals(self.document.get_lastname().description, u"breton")
        self.assertEquals(self.document.get_lastname().activated, True)
        self.assertEquals(self.document.get_photo().description, QUESTION_MARK())
        self.assertEquals(self.document.get_photo().activated, False)
        self.assertEquals(self.document.get_email().description, u"*****@*****.**")
        self.assertEquals(self.document.get_email().activated, False)
        self.assertEquals(self.document.has_custom_attribute(u'color'), True)
        self.assertEquals(self.document.has_file(u'MP3'), True)
            
    def test_save(self):
        # set personal data
        self.document.set_title(FilterValue(value=u"Mr", activate=True))
        self.document.set_firstname(FilterValue(value=u"", activate=True))
        self.document.set_lastname(FilterValue(value=u"breton", activate=True))
        self.document.set_photo(FilterValue(value=QUESTION_MARK(), activate=False))
        self.document.set_email(FilterValue(value=u"*****@*****.**", activate=False))
        # set custom interests, working if IGNORECASE set
        self.document.add_custom_attributes((u'color', FilterValue(value=u'BLUE', activate=True)))
        # set files
        self.document.add_file((u'MP3', FilterValue(value=u'.*\.mp3$', activate=True)))
        # write file
        self.document.save()
            
    def test_load(self):
        self.document.load()
        self.assert_content()

    def test_import(self):
        doc = FilterDocument(PROFILE_TEST, PROFILE_DIRECTORY)
        doc.load()
        self.document.import_document(doc)
        self.assert_content()

    def test_customs(self):
        self.document.load()
        customs = self.document.get_custom_attributes()
        self.assertEquals(self.document.has_custom_attribute(u'color'), True)
        self.assertEquals(self.document.get_custom_attributes()[u'color']._name, 'color')
        self.assertEquals(self.document.get_custom_attributes()[u'color'].description, 'BLUE')
        self.assertEquals(self.document.get_custom_attributes()[u'color'].activated, True)
        self.document.add_custom_attributes((u'color', FilterValue(value=u'blue', activate=False)))
        self.assertEquals(self.document.get_custom_attributes()[u'color'].activated, False)
        customs = self.document.remove_custom_attributes(u'color')
        self.assertEquals(self.document.has_custom_attribute(u'color'), False)
 def test_import(self):
     doc = FilterDocument(PROFILE_TEST, PROFILE_DIRECTORY)
     doc.load()
     self.document.import_document(doc)
     self.assert_content()