1

I'm new in Python. I have a class DatabaseConfig contains some information of database config. I writed a test case by using Unittest for it with the script is if dbopt is invalid (say 'dbopt' : 'abc') then expected output is Throw exception: Invalid value {} of parameter 'dbopt'.

But it fail, I got Error : 'str' object has no attribute 'get'. I do not know why. It should have passed. Would someone please tell me how to fix it? Thank you.

My database config :

class DatabaseConfig:
    DEFAULT_HOST = "localhost"
    DEFAULT_PORT = 5432
    DEFAULT_CONNECT_TIMEOUT = 60
    DEFAULT_CLIENT_ENCODING = "utf-8"
    DEFAULT_SSLMODE = "disable"

    def __init__(self, dbconf):
        self.logger = setup_logger()
        _dbconf = dict(dbconf)

        # checking for database type
        dbtype = _dbconf.get('db')
        if dbtype != 'sqlite' and dbtype != 'postgres':
            msg = f"Invalid value {dbtype} of parameter 'db'. Should be 'sqlite' or 'postgres'"
            self.logger.error(msg)
            raise Exception(msg)
        else:
            self.db_type = dbtype

        dbopt = _dbconf.get('dbopt')
        if dbopt is None:
            msg = f"Invalid value {dbopt} of parameter 'dbopt'."
            self.logger.error(msg)
            raise Exception(msg)

My test case:

import unittest
from analyzer.configmanager import DatabaseConfig
from analyzer.analyzerlogging import setup_logger

# Invalid dbopt (for sqlite)

class TestDatabaseConfigInit(unittest.TestCase):
  def test_UT_DATABASE_CONFIG_INIT_006(self):
      try:
          DatabaseConfig({'db' : 'sqlite', 'dbopt' : 'abc'})
      except Exception as e:
          expectedOutput = "Invalid value abc of parameter 'dbopt'."
          self.assertEqual(str(e), expectedOutput)

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

Error:

test_UT_DATABASE_CONFIG_INIT_006 (ut.test_database_config_init.TestDatabaseConfigInit) ... FAIL
NoneType: None

======================================================================
FAIL: test_UT_DATABASE_CONFIG_INIT_006 (ut.test_database_config_init.TestDatabaseConfigInit)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "d:\Python\yieldnews\ut\test_database_config_init.py", line 60, in test_UT_DATABASE_CONFIG_INIT_006
    DatabaseConfig({'db' : 'sqlite', 'dbopt' : 'abc'})
AttributeError: 'str' object has no attribute 'get'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "d:\Python\yieldnews\ut\test_database_config_init.py", line 63, in test_UT_DATABASE_CONFIG_INIT_006
    self.assertEqual(str(e), expectedOutput)
AssertionError: "'str' object has no attribute 'get'" != "Invalid value abc of parameter 'dbopt'."
- 'str' object has no attribute 'get'
+ Invalid value abc of parameter 'dbopt'.


----------------------------------------------------------------------
Ran 1 test in 0.003s

FAILED (failures=1)

BinhDuong
  • 79
  • 2
  • 15
  • Better use `with self.asssertRaises(SpecificException, msg=''):` - this is less error-prone – MrBean Bremen Feb 27 '20 at 07:36
  • Your code runs without any errors for me and the only essential thing I have changed was replacing your logger by a standard logger. Try creating an object of your class DatabaseConfig outside of a unit test (maybe even without the logger) and see whether the error remains. – ctenar Feb 27 '20 at 07:43

0 Answers0