Python Forum
How to write test cases by Unit test for database configuration file?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to write test cases by Unit test for database configuration file?
#1
I have a yaml file containing database configuration information and a python file to check connect to database via that configuration file. Then I writed some test cases in python file. There's a Function
def test_db_type_postgres(self):
    database = DB({'database': 'postgres'});
    self.assertEqual(database.get_db_type(), 'postgres')
I hope the above function is that if the database is Postgres then it will pass, but it failed. I checked Test Log. It seems that it is affected by method
#checking db option
        dbopt = self.__get_dbopt()
        if dbopt is None:
            raise InvalidConfigError(
                'E01002', 'Invalid database options.')
        else:
            self.dbopt = dbopt
Where am I wrong? Please show me. Any help is appreciated.
[Image: uYiW9.png]

My python file (include test cases):
import yaml
import unittest

class InvalidConfigError(Exception):
    pass

class DB():
    def __init__(self, dbconf):
        self._dbconf = dict(dbconf)

        # checking for database type
        dbtype = self.get_db_type()
        if dbtype != 'sqlite' and dbtype != 'postgres':
            raise InvalidConfigError()
        else:
            self.dbtype = dbtype
    
        #checking db option
        dbopt = self.__get_dbopt()
        if dbopt is None:
            raise InvalidConfigError(
                'E01002', 'Invalid database options.')
        else:
            self.dbopt = dbopt



    def get_db_type(self):
        return self._dbconf.get('database')

    def __get_dbopt(self):
        return self._dbconf.get('dbopt')

class TestDBtype(unittest.TestCase):
    #setUpClass gets calls only once where as setUp gets called before every test
    @classmethod
    def setUpClass(cls):
        cls.init_db()

    @classmethod
    def init_db(cls):

        with open('DatabaseConfig.yml') as f:
            data = yaml.full_load(f)

            for item, doc in data.items():
                print(item, ":", doc)

            cls.database = DB(data)

    def test_missing_db(self):
        database = DB({'database': None});
        self.assertRaises(InvalidConfigError)

    def test_db_type_postgres(self):
        database = DB({'database': 'postgres'});
        self.assertEqual(database.get_db_type(), 'postgres')

    def test_db_type_invalid(self):
        database = DB({'database': 'mysql'});
        self.assertRaises(InvalidConfigError)

class TestOpt(unittest.TestCase):
    #setUpClass gets calls only once where as setUp gets called before every test
    @classmethod
    def setUpClass(cls):
        cls.init_db()

    @classmethod
    def init_db(cls):

        with open('DatabaseConfig.yml') as f:
            data = yaml.full_load(f)

            for item, doc in data.items():
                print(item, ":", doc)

            cls.database = DB(data)
    
    def test_db_opt(self):
        database = DB({'dbopt': None});
        self.assertRaises(InvalidConfigError)

if __name__ == '__main__':
    unittest.main()
Test log ouput:
Error:
database : postgres dbopt : {'host': 'localhost', 'port': 6311, 'dbname': 'spam_eggs', 'user': 'hamburger', 'password': 'example_password', 'client_encoding': 'utf-8', 'connect_timeout': 60, 'sslmode': 'none'} query : select * from manufacturing_product test_db_type_invalid (test_db_type.TestDBtype) ... ERROR NoneType: None test_db_type_postgres (test_db_type.TestDBtype) ... ERROR NoneType: None test_missing_db (test_db_type.TestDBtype) ... ERROR NoneType: None ====================================================================== ERROR: test_db_type_invalid (test_db_type.TestDBtype) ---------------------------------------------------------------------- Traceback (most recent call last): File "d:\Python\TestDataSource\test_db_type.py", line 60, in test_db_type_invalid database = DB({'database': 'mysql'}); File "d:\Python\TestDataSource\test_db_type.py", line 14, in __init__ raise InvalidConfigError() test_db_type.InvalidConfigError ====================================================================== ERROR: test_db_type_postgres (test_db_type.TestDBtype) ---------------------------------------------------------------------- Traceback (most recent call last): File "d:\Python\TestDataSource\test_db_type.py", line 56, in test_db_type_postgres database = DB({'database': 'postgres'}); File "d:\Python\TestDataSource\test_db_type.py", line 22, in __init__ 'E01002', 'Invalid database options.') test_db_type.InvalidConfigError: ('E01002', 'Invalid database options.') ====================================================================== ERROR: test_missing_db (test_db_type.TestDBtype) ---------------------------------------------------------------------- Traceback (most recent call last): File "d:\Python\TestDataSource\test_db_type.py", line 52, in test_missing_db database = DB({'database': None}); File "d:\Python\TestDataSource\test_db_type.py", line 14, in __init__ raise InvalidConfigError() test_db_type.InvalidConfigError ---------------------------------------------------------------------- Ran 3 tests in 0.003s FAILED (errors=3)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Class test : good way to split methods into several files paul18fr 4 403 Jan-30-2024, 11:46 AM
Last Post: Pedroski55
  Last record in file doesn't write to newline gonksoup 3 364 Jan-22-2024, 12:56 PM
Last Post: deanhystad
  Unit Testing Set Up and Use RockBlok 2 380 Jan-08-2024, 07:43 PM
Last Post: deanhystad
  write to csv file problem jacksfrustration 11 1,373 Nov-09-2023, 01:56 PM
Last Post: deanhystad
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,309 Nov-09-2023, 10:56 AM
Last Post: mg24
  How to automate loop test check on Network device jpc230 1 544 Oct-09-2023, 09:54 PM
Last Post: Larz60+
  How to test the high-score in my Python-game Pluviometer 2 547 Oct-02-2023, 06:55 PM
Last Post: deanhystad
  How do I read and write a binary file in Python? blackears 6 6,016 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,046 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Read text file, modify it then write back Pavel_47 5 1,501 Feb-18-2023, 02:49 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020