TestMongodbHandler.py 6.8 KB

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