testSQLOperations.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Wed Sep 19 14:34:22 2018
  5. @author: tanya
  6. """
  7. import os
  8. import unittest
  9. import pandas as pd
  10. from libraries.database_operations_library import SQLOperations
  11. class TestSQLOperations(unittest.TestCase):
  12. '''
  13. '''
  14. def __init__(self, test_df = None):
  15. print('\n', '='*5, 'Testing class : SQLOperations', '='*5)
  16. self.inst = SQLOperations(db_url = None)
  17. print('Connected to', self.inst.db_url)
  18. if test_df is None:
  19. self.test_df = pd.DataFrame({'a' : [1,2,3,4,5], 'b' : ['A', 'B', 'C', 'A', 'V'], 'c' : [0.1, 0.2, 0.3, 0.4, 0.5]})
  20. else:
  21. self.test_df = test_df
  22. def _create_test_table(self, test_tablename, create_table_query = None):
  23. '''
  24. '''
  25. self.inst.drop_table_if_exists(test_tablename)
  26. if create_table_query is None:
  27. if 'ibm_db' in self.inst.db_url:
  28. create_table_query = """CREATE TABLE {} (
  29. a INT,
  30. b CHAR,
  31. c DECIMAL(10 , 2 )
  32. );""".format(test_tablename)
  33. else:
  34. create_table_query = """CREATE TABLE test (
  35. a INT,
  36. b TEXT,
  37. c DECIMAL(10 , 2 )
  38. );"""
  39. self.inst.execute(create_table_query)
  40. class TestExecute(TestSQLOperations):
  41. '''
  42. '''
  43. def __init__(self):
  44. super(TestExecute, self).__init__()
  45. print('\n', '-'*2, 'Testing method : execute')
  46. def test_create_table(self, test_tablename, create_table_query = None):
  47. '''
  48. '''
  49. print('-'*4, 'Testing create table operation')
  50. self._create_test_table(test_tablename = test_tablename, create_table_query = create_table_query)
  51. self.assertTrue(self.inst.check_if_table_exists(test_tablename))
  52. self.inst.drop_table_if_exists(test_tablename)
  53. print('-'*4, 'Test ran successfully!')
  54. class TestLoad_csv_to_db(TestSQLOperations):
  55. '''
  56. '''
  57. def __init__(self):
  58. super(TestLoad_csv_to_db, self).__init__()
  59. print('\n', '-'*2, 'Testing method : load_csv_to_db')
  60. def test_correct_content(self, test_csv_path, test_tablename, create_table_query = None):
  61. '''
  62. '''
  63. print('-'*4, 'Testig that load operation gives correct result')
  64. os.makedirs(os.path.dirname(test_csv_path), exist_ok = True)
  65. if not self.inst.drop_table_if_exists(test_tablename):
  66. self._create_test_table(test_tablename)
  67. self.test_df.to_csv(test_csv_path, index = False)
  68. self.inst.load_csv_to_db(csv_path = test_csv_path, tablename = test_tablename)
  69. try:
  70. connection = self.inst.engine.connect()
  71. test_df_from_sql = pd.read_sql(sql = "SELECT * FROM test", con = connection)
  72. connection.close()
  73. except Exception as e:
  74. raise Exception('ERROR: test csv file has not been load to sql at all, \n, exit with {}'.format(e))
  75. print('-'*4, 'Testing data has correct shape')
  76. self.assertTupleEqual(self.test_df.shape, test_df_from_sql.shape)
  77. print('-'*4,'Testing data has correct columns')
  78. self.assertSetEqual(set(self.test_df.columns), set(test_df_from_sql.columns))
  79. print('-'*4,'Testing data has correct content')
  80. for col in self.test_df.columns:
  81. test_df_from_sql[col] = test_df_from_sql[col].astype(self.test_df[col].dtype)
  82. pd.testing.assert_frame_equal(self.test_df, test_df_from_sql)
  83. print('-'*4, 'Test ran successfully!')
  84. if __name__ == '__main__':
  85. test_tablename = 'test10'
  86. test_csv_path = '/home/tanya/acdp/data_samples/test.csv'
  87. TestExecute().test_create_table(test_tablename = test_tablename)
  88. TestLoad_csv_to_db().test_correct_content(test_csv_path = test_csv_path, test_tablename = test_tablename)
  89. print('Done!', '\n')