1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
# -*- coding: utf-8 -*-
"""
***************************************************************************
connector_test.py
---------------------
Date : May 2017
Copyright : (C) 2017, Sandro Santilli
Email : strk at kbt dot io
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""
__author__ = 'Sandro Santilli'
__date__ = 'May 2017'
__copyright__ = '(C) 2017, Sandro Santilli'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
import os
import qgis
from qgis.testing import start_app, unittest
from qgis.core import QgsDataSourceURI
from qgis.utils import iface
start_app()
from db_manager.db_plugins.postgis.connector import PostGisDBConnector
class TestDBManagerPostgisConnector(unittest.TestCase):
#def setUpClass():
def _getUser(self, connector):
r = connector._execute(None, "SELECT USER")
val = connector._fetchone(r)[0]
connector._close_cursor(r)
return val
def _getDatabase(self, connector):
r = connector._execute(None, "SELECT current_database()")
val = connector._fetchone(r)[0]
connector._close_cursor(r)
return val
# See https://issues.qgis.org/issues/16625
# and https://issues.qgis.org/issues/10600
def test_dbnameLessURI(self):
c = PostGisDBConnector(QgsDataSourceURI())
self.assertIsInstance(c, PostGisDBConnector)
uri = c.uri()
# No username was passed, so we expect it to be taken
# from PGUSER or USER environment variables
expected_user = os.environ.get('PGUSER') or os.environ.get('USER')
actual_user = self._getUser(c)
self.assertEqual(actual_user, expected_user)
# No database was passed, so we expect it to be taken
# from PGDATABASE or expected user
expected_db = os.environ.get('PGDATABASE') or expected_user
actual_db = self._getDatabase(c)
self.assertEqual(actual_db, expected_db)
# TODO: add service-only test (requires a ~/.pg_service.conf file)
if __name__ == '__main__':
unittest.main()
|