def test_request_similars(self):
        """
        Check basic operation of DBEndpoint
        """
        mfe = MockFrameEndpoint()
        dbe = DBEndpoint(mfe,'my_db_name')

        # We expect the CHOOSE_DB message to be here:
        self.assertEqual(len(mfe.out_list),1)
        frame = mfe.out_list.pop(0)
        assert 'my_db_name' in frame

        func_data = '029348509238459kldfjaklsdjflkasjdfklasdf'
        num_similars = 8

        # Prepare a response for the REQUEST_SIMILARS message ahead of time:
        # Two results:
        name1 = 'name1'
        comment1 = 'comment1'
        sim_grade1 = 7

        name2 = 'name1'
        comment2 = 'comment1'
        sim_grade2 = 7

        msg = ""
        # Two records:
        msg += struct.pack('I',2)

        # First record:
        msg += struct.pack('I',len(name1))
        msg += name1
        msg += struct.pack('I',len(comment1))
        msg += comment1
        msg += struct.pack('I',sim_grade1)

        # Second record:
        msg += struct.pack('I',len(name2))
        msg += name2
        msg += struct.pack('I',len(comment2))
        msg += comment2
        msg += struct.pack('I',sim_grade2)

        # Add message type:
        frame = dword_pack(MsgTypes.RESPONSE_SIMILARS,msg)
        mfe.in_list.append(frame)

        dbe.request_similars(func_data,num_similars)
        similars = dbe.response_similars()
        self.assertEqual(len(mfe.out_list),1)
        frame = mfe.out_list.pop(0)
        assert func_data in frame

        # We expect two results:
        self.assertEqual(len(similars),2)
        self.assertIsInstance(similars[0],FSimilar)

        dbe.close()
    def test_dword_pack(self):
        """
        Test dword_{pack,unpack} functions.
        """
        msg = 'This is example msg'
        num = 0x1337
        data = dword_pack(num,msg)
        self.assertEqual(len(data),len(msg) + 4)
        num1,msg1 = dword_unpack(data)

        self.assertEqual(num1,num)
        self.assertEqual(msg1,msg)
    def test_build_msg_get_similars(self):
        func_data = 'kalsfdjaslkjfoiweuroiweurioweuriowjsdf'
        num_similars = 52
        # Run build_msg_get_similars with some arguments:
        data = build_msg_get_similars(func_data,num_similars)

        # Build the data myself:
        res = ""
        res += struct.pack('I',len(func_data))
        res += func_data
        res += struct.pack('I',num_similars)

        # Add message type:
        res = dword_pack(MsgTypes.REQUEST_SIMILARS,res)

        # Compare the two results:
        self.assertEqual(data,res)
    def test_build_msg_add_function(self):
        func_name = 'a_function_name'
        func_comment = 'A comment'
        func_data = 'klasdjflkasjdflkjasfkljasdfasdf'
        # Run build_msg_add_function on some strange arguments:
        data = build_msg_add_function(func_name,func_comment,func_data)

        # Build the data myself:
        res = ""
        for d in [func_name,func_comment,func_data]:
            res += struct.pack('I',len(d))
            res += d

        # Add message type:
        res = dword_pack(MsgTypes.ADD_FUNCTION,res)

        # Compare the two results:
        self.assertEqual(data,res)