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)