#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Wed Jul 14 13:45:28 2021 @author: geopeter """ import unittest import pickle import ExportToDatabase as cut import configparser import psycopg2 import time cfg = configparser.ConfigParser() cfg.read('config.ini') assert "POSTGRES" in cfg, "missing POSTGRES in config.ini" param_postgres = cfg["POSTGRES"] stationList = None testDB = 'weathertestdb' class TestExportToDatabase(unittest.TestCase): def testDBExists(self): print("__Test DB Exists") #given cut.drop_db(testDB) #test exists = cut.dbexists(testDB) self.assertFalse(exists) #finished print("Test OKAY__") def testCreateDB(self): print("__Test CreateDB") try: # given cut.drop_db(testDB) # test cut.create_db(testDB) self.assertTrue(cut.dbexists(testDB)) # finished cut.drop_db(testDB) print("Test OKAY__") except(Exception, psycopg2.DatabaseError) as error: self.assertRaises(error) print("Test FAILED__") def testCreateTable(self): print("__Test CreateTable") try: # given cut.drop_db(testDB) cut.create_db(testDB) # test with open("./pickle/stationList_with_temperature.pickle", "rb") as pickleFile: stationList = pickle.load(pickleFile) stationList = stationList.loc[stationList['country']=="Germany"] cut.create_table(stationList, testDB) connection = psycopg2.connect(dbname=testDB, user=param_postgres["user"], password=param_postgres["password"], host=param_postgres["host"], port=param_postgres["port"]) cursor = connection.cursor() columns = cursor.execute("SELECT count(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = 'stations';") self.assertTrue(cursor.fetchall()[0][0]>=50) # finished connection.close() cut.drop_db(testDB) print("Test OKAY__") except(Exception, psycopg2.DatabaseError) as error: connection.close() self.assertRaises(error.message) print("Test FAILED__") if __name__ == '__main__': unittest.main()