import fairness
import unittest

class RegexpTests(unittest.TestCase):
    def test_not_piece_line(self):
        line = "Received [BT_BITFIELD] message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .TorrentDLM: 'test_original.iso'; Peer: L: 255.255.255.255:0000 [Vuze 5.7.0.0]"
        self.assertFalse(fairness.is_piece_line(line))

    def test_piece_line(self):
        line = "Received [BT_PIECE data for piece #74:16384->32767] message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . TorrentDLM: 'test_original.iso'; Peer: L: 255.255.255.255:0000 [Vuze 5.7.0.0]"
        self.assertTrue(fairness.is_piece_line(line))

    def test_get_peer(self):
        line = "Received [BT_PIECE data for piece #74:16384->32767] message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . TorrentDLM: 'test_original.iso'; Peer: L: 127.0.0.1:8000 [Vuze 5.7.0.0]"
        peer = "127.0.0.1"
        self.assertEqual(fairness.get_peer(line), peer)

    def test_send_line(self):
        line = "Sent [BT_PIECE data for piece #74:16384->32767] message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . TorrentDLM: 'test_original.iso'; Peer: L: 8.8.8.8:8888 [Vuze 5.7.0.0]"
        self.assertTrue(fairness.is_send_line(line))
        self.assertFalse(fairness.is_recv_line(line))

    def test_recv_line(self):
        line = "Received [BT_PIECE data for piece #74:16384->32767] message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . TorrentDLM: 'test_original.iso'; Peer: L: 127.0.0.1:8000 [Vuze 5.7.0.0]"
        self.assertFalse(fairness.is_send_line(line))
        self.assertTrue(fairness.is_recv_line(line))

if __name__ == '__main__':
    unittest.main()
