TestMongodbHandler.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import unittest
  2. import sys
  3. import os
  4. import time
  5. from pymongo import MongoClient
  6. sys.path.append(os.getcwd())
  7. from cdplib.log import Log
  8. from cdplib.db_handlers.MongodbHandler import MongodbHandler
  9. class TestMongodbHandler(unittest.TestCase):
  10. def setUp(self):
  11. database_url = "mongodb://{0}:{1}@{2}:{3}"\
  12. .format('root',
  13. 'oebb',
  14. 'localhost',
  15. 27017)
  16. self.database_name = 'test_database'
  17. self.first_collection_name = 'first_test_collection'
  18. self.second_collection_name = 'second_test_collection'
  19. self.mongodb_handler = MongodbHandler(database_name=self.database_name, database_url=database_url)
  20. self.client = MongoClient(database_url)
  21. self.database = self.client[self.database_name]
  22. self.valid_schema_path = os.path.join('.', 'cdplib', 'unit_tests', "valid_test_schema.json")
  23. self.invalid_schema_path = os.path.join('.', 'cdplib', 'unit_tests', "invalid_test_schema.json")
  24. self.valid_input = {
  25. "test_value_string": "test_value",
  26. "test_value_double": 2.4,
  27. "test_value_double_array": [1.4, 1.6, 3.5],
  28. "test_value_date": "2020-01-28T15:45:25.000Z"
  29. }
  30. self.invalid_input = {
  31. "test_value_string": 1,
  32. "test_value_double": "Wrong value",
  33. "test_value_double_array": [1.4, 1.6, 3.5],
  34. "test_value_date": "2019-01-28T15:45:25.000Z"
  35. }
  36. def test_A_set_database(self):
  37. '''
  38. Checks that the database doesnt exist.
  39. Creates the database.
  40. Create a collection in the database.
  41. Check that the database now exists.
  42. Check that an assertionerror is thrown then an integer value is sent to the method.
  43. '''
  44. self.assertFalse(self.database_name in self.client.list_database_names())
  45. #self.test_B_create_collection()
  46. #self.assertTrue(self.database_name in self.client.list_database_names())
  47. self.assertRaises(AssertionError, lambda:self.mongodb_handler.set_database(123))
  48. def test_B_create_collection(self):
  49. '''
  50. Checks that the collection doesnt exist
  51. Creates the collection
  52. Checks that the collection now exists
  53. Checks that an Exception is thrown when an integervalue is given to the method.
  54. '''
  55. self.assertFalse(self.first_collection_name in self.database.list_collection_names())
  56. self.mongodb_handler.create_collection(self.first_collection_name)
  57. self.mongodb_handler.create_collection(self.second_collection_name)
  58. self.assertTrue(self.first_collection_name in self.database.list_collection_names())
  59. self.assertRaises(Exception, lambda:self.mongodb_handler.create_collection(123))
  60. def test_C_set_collection_schema(self):
  61. '''
  62. Sets a schema for the collection
  63. Tries to set a schema which has syntax errors
  64. '''
  65. self.mongodb_handler.set_collection_schema(self.first_collection_name, self.valid_schema_path)
  66. self.assertRaises(Exception, lambda:set_collection_schema(self.first_collection_name, self.invalid_schema_path))
  67. def test_D_insert_data_into_collection(self):
  68. '''
  69. Inserts data into the collection
  70. Tries to insert data which doesnt comform to the schema.
  71. '''
  72. self.mongodb_handler.insert_data_into_collection(self.valid_input, self.first_collection_name)
  73. self.assertRaises(Exception, lambda:set_collection_schemaself.mongodb_handler.insert_data_into_collection(self.invalid_input, self.first_collection_name))
  74. def test_E_query_data_and_generate_dataframe(self):
  75. '''
  76. Fetch data and confirms thats it is the same as was entered into the database
  77. Do the same with more specific query
  78. '''
  79. self.assertEqual(self.mongodb_handler.query_data_and_generate_dataframe(self.first_collection_name).to_dict()['test_value_double'][0], self.valid_input['test_value_double'])
  80. self.assertEqual(self.mongodb_handler.query_data_and_generate_dataframe(self.first_collection_name, 'test_value_string', 'test_value').to_dict()['test_value_double'][0], self.valid_input['test_value_double'])
  81. def test_F_aggregate_data_and_generate_dataframe(self):
  82. '''
  83. Make an aggregation call
  84. Make sure its the same data as was entered into the database
  85. '''
  86. aggregation_pipeline = [
  87. { '$match': {}}
  88. ]
  89. self.assertEqual(self.mongodb_handler.aggregate_data_and_generate_dataframe(self.first_collection_name, aggregation_pipeline).to_dict()['test_value_double'][0], self.valid_input['test_value_double'])
  90. def test_G_update_data_in_collection(self):
  91. '''
  92. Fetch data from database
  93. Make sure its the same as was entered into the database
  94. Update a value in the data
  95. Fetch the new data
  96. Make sure that it is different from the original data.
  97. '''
  98. original_value = self.mongodb_handler.query_data_and_generate_dataframe(self.first_collection_name).to_dict()['test_value_string'][0]
  99. self.assertEqual(original_value, self.valid_input['test_value_string'])
  100. self.mongodb_handler.update_data_in_collection('test_value_string', 'new_test_value', self.first_collection_name, 'test_value_string', 'test_value', create_if_not_exist=False)
  101. new_value = self.mongodb_handler.query_data_and_generate_dataframe(self.first_collection_name).to_dict()['test_value_string'][0]
  102. self.assertNotEqual(original_value, new_value)
  103. def test_H_create_index(self):
  104. '''
  105. Create a index in the collection
  106. Make sure that the index exists in the collection indexes
  107. '''
  108. index = 'test_value_string'
  109. self.mongodb_handler.create_index(self.first_collection_name, index)
  110. self.assertTrue(index in list(self.database[self.first_collection_name].index_information().keys()))
  111. def test_I_query_data_between_dates_and_generate_dataframe(self):
  112. data = self.mongodb_handler.query_data_between_dates_and_generate_dataframe(self.first_collection_name, "test_value_date", "2020-01-27T15:45:25.000Z", "2020-01-29T15:45:25.000Z", index ='test_value_string')
  113. self.assertEqual(data['test_value_double'][0], self.valid_input['test_value_double'])
  114. def test_Y_drop_collection(self):
  115. '''
  116. Checks that the collection exists
  117. Drops the collection
  118. Checks that the collection doesn't exist anymore
  119. '''
  120. self.assertTrue(self.first_collection_name in self.database.list_collection_names())
  121. self.mongodb_handler.drop_collection(self.first_collection_name)
  122. self.assertFalse(self.first_collection_name in self.database.list_collection_names())
  123. def test_Z_drop_database(self):
  124. '''
  125. Checks that the database exists
  126. Drops the database
  127. Checks that the database doesn't exist anymore
  128. '''
  129. self.assertTrue(self.database_name in self.client.list_database_names())
  130. self.mongodb_handler.drop_database()
  131. self.assertFalse(self.database_name in self.client.list_database_names())
  132. if __name__ == '__main__':
  133. unittest.main()