Skip to content

Commit

Permalink
Make QgsServerLogger testable and test it
Browse files Browse the repository at this point in the history
  • Loading branch information
Éric Lemoine committed Sep 13, 2018
1 parent 504657e commit 3443102
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 3 deletions.
77 changes: 77 additions & 0 deletions python/server/auto_generated/qgsserverlogger.sip.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/server/qgsserverlogger.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/






class QgsServerLogger : QgsMessageLogConsole
{
%Docstring
Writes message log into server logfile

.. versionadded:: 2.8
%End

%TypeHeaderCode
#include "qgsserverlogger.h"
%End
public:

static QgsServerLogger *instance();
%Docstring
Gets the singleton instance
%End

Qgis::MessageLevel logLevel() const;
%Docstring
Gets the current log level

:return: the log level

.. versionadded:: 3.0
%End

void setLogLevel( Qgis::MessageLevel level );
%Docstring
Set the current log level

:param level: the log level

.. versionadded:: 3.0
%End

void setLogFile( const QString &f );
%Docstring
Set the current log file
%End

public slots:

void logMessage( const QString &message, const QString &tag, Qgis::MessageLevel level );
%Docstring
Log a message from the server context

:param message: the message
:param tag: tag of the message
:param level: log level of the message
%End

protected:
QgsServerLogger();

};

/************************************************************************
* This file has been generated automatically from *
* *
* src/server/qgsserverlogger.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
1 change: 1 addition & 0 deletions python/server/server_auto.sip
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
%Include auto_generated/qgsmapserviceexception.sip
%Include auto_generated/qgscapabilitiescache.sip
%Include auto_generated/qgsconfigcache.sip
%Include auto_generated/qgsserverlogger.sip
%Include auto_generated/qgsserversettings.sip
%Include auto_generated/qgsserverparameters.sip
%Include auto_generated/qgsbufferserverrequest.sip
Expand Down
5 changes: 2 additions & 3 deletions src/server/qgsserverlogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,21 @@
#ifndef QGSSERVERLOGGER_H
#define QGSSERVERLOGGER_H

#define SIP_NO_FILE


#include "qgsmessagelog.h"

#include <QFile>
#include <QObject>
#include <QString>
#include <QTextStream>
#include "qgis_server.h"

/**
* \ingroup server
* \brief Writes message log into server logfile
* \since QGIS 2.8
*/
class QgsServerLogger: public QgsMessageLogConsole
class SERVER_EXPORT QgsServerLogger : public QgsMessageLogConsole
{
Q_OBJECT
public:
Expand Down
1 change: 1 addition & 0 deletions tests/src/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ ENDIF (ENABLE_ORACLETEST)

IF (WITH_SERVER)
ADD_PYTHON_TEST(PyQgsServer test_qgsserver.py)
ADD_PYTHON_TEST(PyQgsServerLogger test_qgsserverlogger.py)
ADD_PYTHON_TEST(PyQgsServerPlugins test_qgsserver_plugins.py)
ADD_PYTHON_TEST(PyQgsServerWMS test_qgsserver_wms.py)
ADD_PYTHON_TEST(PyQgsServerWMSGetMap test_qgsserver_wms_getmap.py)
Expand Down
52 changes: 52 additions & 0 deletions tests/src/python/test_qgsserverlogger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsServerLogger.
"""
__author__ = 'Eric Lemoine'
__date__ = '11/09/2018'
__copyright__ = 'Copyright 2018, The QGIS Project'
__revision__ = '$Format:%H$'

import os

from qgis.testing import unittest
from qgis.server import QgsServerLogger

from utilities import unitTestDataPath


class TestQgsServerLogger(unittest.TestCase):

log_file = os.path.join(unitTestDataPath('qgis_server'), 'qgis_server_test.log')

@staticmethod
def remove_file(filename):
try:
os.remove(filename)
except FileNotFoundError:
pass

def setUp(self):
self.logger = QgsServerLogger.instance()
self.logger.setLogLevel(0)
self.logger.setLogFile(self.log_file)
exists = os.access(self.log_file, os.R_OK)
self.assertTrue(exists)
self.remove_file(self.log_file)

def tearDown(self):
self.remove_file(self.log_file)

def test_logging_no_log_file(self):
self.logger.setLogFile('')
exists = os.access(self.log_file, os.R_OK)
self.assertFalse(exists)

def test_logging_log_file(self):
self.logger.setLogFile(self.log_file)
exists = os.access(self.log_file, os.R_OK)
self.assertTrue(exists)

def test_logging_stderr(self):
self.logger.setLogFile('stderr')
exists = os.access(self.log_file, os.R_OK)
self.assertFalse(exists)

0 comments on commit 3443102

Please sign in to comment.