Feb-16-2020, 01:06 PM
I have a YAML config file and a Python ConfigManager file used to check connection to database. I need to write some test cases for my ConfigManager file :
1. If the database is none then test case fails.
2. If the database is Sqlite or Postgre, test case pass, otherwise it fails
I have created a test class but I don't know how to implement it. Can anyone suggest to me ?
DatabaseConfig.yml:
1. If the database is none then test case fails.
2. If the database is Sqlite or Postgre, test case pass, otherwise it fails
I have created a test class but I don't know how to implement it. Can anyone suggest to me ?
DatabaseConfig.yml:
database: #if it is empty or not sqlite or not postgre then it fails dbopt: host: port: 5432 dbname: db1ConfigManager.py:
import yaml 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( 'E01001', 'Invalid database type, should be sqlite or postgres.') else: self.dbtype = dbtype def get_db_type(self): return self._dbconf['database'] with open('DatabaseConfig.yml') as file: data = yaml.full_load(file) for item, doc in data.items(): print(item, ":", doc) db = DB(data)My test file test_database.py (help me with this) :
import unittest from database_config import DB class TestDatabase(unittest.TestCase): def test_missing_db(self): # test database is none then it fail def test_db_type_postgres(self): # test if database is postgres then it pass def test_db_type_sqlite(self): # test if database is sqlite then it pass def test_db_type_invalid(self): # test if database is not postgres or sqlite then it fail if __name__ == '__main__': unittest.main()