def test_set_log_handler_with_non_callables(self, callback):
     '''
     Test whether a non callable may be set as the log function
     '''
     aerospike.set_log_level(aerospike.LOG_LEVEL_DEBUG)
     with pytest.raises(e.ParamError):
         aerospike.set_log_handler(callback)
    def test_set_log_handler_extra_parameter(self):
        """
        Test log handler with extra parameter
        """
        aerospike.set_log_level(aerospike.LOG_LEVEL_DEBUG)

        with pytest.raises(TypeError) as typeError:
            aerospike.set_log_handler(handler, extrahandler)

        assert "setLogHandler() takes at most 1 argument (2 given)" in str(
            typeError.value)
    def test_set_log_handler_correct(self):
        """
        Test log handler with correct parameters
        """

        response = aerospike.set_log_level(aerospike.LOG_LEVEL_DEBUG)
        aerospike.set_log_handler(handler)

        # Forces an event to be logged
        client = TestBaseClass.get_new_connection()

        assert response == 0
        client.close()
    def test_set_log_handler_correct(self):
        """
        Test log handler with correct parameters
        """

        response = aerospike.set_log_level(aerospike.LOG_LEVEL_DEBUG)

        aerospike.set_log_handler(handler)

        config = {
                "hosts": [("127.0.0.1", 3000)]
                }
        client = aerospike.client(config).connect()

        assert response == 0
        client.close()
    def test_set_log_handler_extra_parameter(self):
        """
        Test log handler with extra parameter
        """

        aerospike.set_log_level(aerospike.LOG_LEVEL_DEBUG)

        def handler(level, func, myfile, line):
            print "Level is: %d" % level

        def extrahandler(level, func, myfile, line):
            print "Level is: %d" % level

        with pytest.raises(TypeError) as typeError:
            aerospike.set_log_handler(handler, extrahandler)

        assert "setLogHandler() takes at most 1 argument (2 given)" in typeError.value
    def test_set_log_handler_correct(self):
        """
        Test log handler with correct parameters
        """

        response = aerospike.set_log_level(aerospike.LOG_LEVEL_DEBUG)
        aerospike.set_log_handler(handler)

        hostlist, user, password = TestBaseClass.get_hosts()
        config = {
            "hosts": hostlist
        }
        if user is None and password is None:
            client = aerospike.client(config).connect()
        else:
            client = aerospike.client(config).connect(user, password)

        assert response == 0
        client.close()
    def test_log_handler_raising_error(self):
        '''
        Test for handling of a log handler which raises an error
        '''
        aerospike.set_log_level(aerospike.LOG_LEVEL_DEBUG)
        aerospike.set_log_handler(error_raising_handler)
        with pytest.raises(SystemError):
            hostlist, user, password = TestBaseClass.get_hosts()
            config = {
                "hosts": hostlist
            }
            if user is None and password is None:
                client = aerospike.client(config).connect()
            else:
                client = aerospike.client(config).connect(user, password)

        try:
            client.close()  # Close the client if it got opened
        except:
            pass
    def test_incorrect_prototype_callback(self):
        """
        Test that having a callback which takes the wrong number
        of args, will raise an error on methods which trigger
        logging
        """
        aerospike.set_log_level(aerospike.LOG_LEVEL_DEBUG)
        aerospike.set_log_handler(wrong_args_handler)

        with pytest.raises(SystemError):
            hostlist, user, password = TestBaseClass.get_hosts()
            config = {
                "hosts": hostlist
            }
            if user is None and password is None:
                client = aerospike.client(config).connect()
            else:
                client = aerospike.client(config).connect(user, password)

        try:
            client.close()  # Close the client if it got opened
        except:
            pass
 def teardown_class(cls):
     '''
     Set the class level logger to a no-op to ensure no problems later
     '''
     aerospike.set_log_handler(valid_handler)