def test_retrieve_mongodb_records_with_desired_fields_when_multiple_documents_in_db( self): self.mongo_create_db() self.mongo_inser_data(data1) self.mongo_inser_data(data4) field = 'address.postalCode, address.city' a = MongoDBLibrary() a.connect_to_mongodb(dbHost=test_mongo_connection_host, dbPort=test_mongo_connection_port) data = a.retrieve_mongodb_records_with_desired_fields( dbName=test_database_name, dbCollName=test_collection_name, recordJSON='{}', fields=field, return__id=False) a.disconnect_from_mongodb() data = self.cleanup_and_sort(data) expected_1 = [(u'address', [(u'city', u'New York'), (u'postalCode', 10021)])] expected_2 = [(u'address', [(u'city', u'Metropolis'), (u'postalCode', 10021)])] self.assertIn(expected_1, data) self.assertIn(expected_2, data)
def test_drop_mongodb_database(self): self.mongo_create_db() self.mongo_inser_data(data1) a = MongoDBLibrary() a.connect_to_mongodb(dbHost=test_mongo_connection_host, dbPort=test_mongo_connection_port) a.drop_mongodb_database(test_database_name) a.disconnect_from_mongodb() database_names = self.mongo_database_names() expected = ['admin', 'local'] self.assertEqual(set(database_names), set(expected))
def test_retrieve_all_mongodb_records_when_zero_documents(self): self.mongo_create_db() a = MongoDBLibrary() a.connect_to_mongodb(dbHost=test_mongo_connection_host, dbPort=test_mongo_connection_port) data = a.retrieve_all_mongodb_records(dbName=test_database_name, dbCollName=test_collection_name) a.disconnect_from_mongodb() expected = '' self.assertEqual(data, expected)
def test_retrieve_all_mongodb_records_when_database_does_not_exist(self): self.mongo_create_db() self.mongo_inser_data(data2) a = MongoDBLibrary() a.connect_to_mongodb(dbHost=test_mongo_connection_host, dbPort=test_mongo_connection_port) data = a.retrieve_all_mongodb_records(dbName='no-database', dbCollName='not_exist') a.disconnect_from_mongodb() expected = '' self.assertEqual(data, expected)
def test_drop_mongodb_database(self): self.mongo_create_db() self.mongo_inser_data(data1) a = MongoDBLibrary() a.connect_to_mongodb(dbHost=test_mongo_connection_host, dbPort=test_mongo_connection_port) a.drop_mongodb_database(test_database_name) a.disconnect_from_mongodb() database_names = self.mongo_database_names() expected = ['local'] self.assertEqual(database_names, expected)
def test_retrieve_mongodb_records_with_desired_fields_when_fields_is_empty(self): self.mongo_create_db() self.mongo_inser_data(data1) self.mongo_inser_data(data4) a = MongoDBLibrary() a.connect_to_mongodb(dbHost=test_mongo_connection_host, dbPort=test_mongo_connection_port) data = a.retrieve_mongodb_records_with_desired_fields(dbName=test_database_name, dbCollName=test_collection_name, recordJSON='{}', fields='', return__id=False) a.disconnect_from_mongodb() expected = "[(u'phoneNumbers', [{u'type': u'home', u'number': u'212 555-1234'}, {u'type': u'fax', u'number': u'646 555-4567'}]), (u'firstName', u'John'), (u'lastName', u'Smith')" self.assertIn(expected, data)
def test_retrieve_mongodb_records_with_desired_fields_when_one_document_in_db(self): self.mongo_create_db() self.mongo_inser_data(data1) field = 'address.postalCode, address.city' a = MongoDBLibrary() a.connect_to_mongodb(dbHost=test_mongo_connection_host, dbPort=test_mongo_connection_port) data = a.retrieve_mongodb_records_with_desired_fields(dbName=test_database_name, dbCollName=test_collection_name, recordJSON='{}', fields=field, return__id=False) a.disconnect_from_mongodb() expected = str([(u'address', {u'postalCode': 10021, u'city': u'New York'})]) self.assertEqual(data, expected)
def test_retrieve_some_mongodb_records_when_multiple_documents(self): self.mongo_create_db() self.mongo_inser_data(data2) self.mongo_inser_data(data3) a = MongoDBLibrary() a.connect_to_mongodb(dbHost=test_mongo_connection_host, dbPort=test_mongo_connection_port) data = a.retrieve_some_mongodb_records(dbName=test_database_name, dbCollName=test_collection_name, recordJSON=selection1) a.disconnect_from_mongodb() expected = self.mongo_find_from_collection(record=selection1) self.assertEqual(data, expected)
def test_retrieve_mongodb_records_with_desired_fields_when_return__id_is_not_boolean(self): self.mongo_create_db() self.mongo_inser_data(data1) self.mongo_inser_data(data4) field = 'address.postalCode, address.city' a = MongoDBLibrary() a.connect_to_mongodb(dbHost=test_mongo_connection_host, dbPort=test_mongo_connection_port) data = a.retrieve_mongodb_records_with_desired_fields(dbName=test_database_name, dbCollName=test_collection_name, recordJSON=selection1, fields=field, return__id='foobar') a.disconnect_from_mongodb() expected = "(u'address', {u'postalCode': 10021, u'city': u'New York'})]" self.assertIn(expected, data)
def test_retrieve_mongodb_records_with_desired_fields__id_is_returned(self): self.mongo_create_db() self.mongo_inser_data(data1) self.mongo_inser_data(data4) field = 'address.postalCode, address.city' a = MongoDBLibrary() a.connect_to_mongodb(dbHost=test_mongo_connection_host, dbPort=test_mongo_connection_port) data = a.retrieve_mongodb_records_with_desired_fields(dbName=test_database_name, dbCollName=test_collection_name, recordJSON='{}', fields=field, return__id=True) a.disconnect_from_mongodb() expected = "u'_id', ObjectId('" self.assertIn(expected, data)
def test_get_mongodb_collections(self): self.mongo_create_db() self.mongo_inser_data(data1) a = MongoDBLibrary() a.connect_to_mongodb(dbHost=test_mongo_connection_host, dbPort=test_mongo_connection_port) collection_names = a.get_mongodb_collections(test_database_name) a.disconnect_from_mongodb() self.mongo_drop_database() expected = ['test_collection'] self.assertEqual(collection_names, expected)
def test_get_mongodb_collections(self): self.mongo_create_db() self.mongo_inser_data(data1) a = MongoDBLibrary() a.connect_to_mongodb(dbHost=test_mongo_connection_host, dbPort=test_mongo_connection_port) collection_names = a.get_mongodb_collections(test_database_name) a.disconnect_from_mongodb() self.mongo_drop_database() expected = ['system.indexes', 'test_collection'] self.assertEqual(collection_names, expected)
def test_get_mongodb_databases(self): self.mongo_create_db() self.mongo_inser_data(data1) a = MongoDBLibrary() a.connect_to_mongodb(dbHost=test_mongo_connection_host, dbPort=test_mongo_connection_port) db = a.get_mongodb_databases() a.disconnect_from_mongodb() self.mongo_drop_database() expected = ['test_database', 'local'] self.assertEqual(db, expected)
def test_retrieve_mongodb_records_with_desired_fields_when_fields_is_empty( self): self.mongo_create_db() self.mongo_inser_data(data1) self.mongo_inser_data(data4) a = MongoDBLibrary() a.connect_to_mongodb(dbHost=test_mongo_connection_host, dbPort=test_mongo_connection_port) data = a.retrieve_mongodb_records_with_desired_fields( dbName=test_database_name, dbCollName=test_collection_name, recordJSON='{}', fields='', return__id=False) a.disconnect_from_mongodb() data = self.cleanup_and_sort(data) expected1 = [(u'address', [(u'city', u'New York'), (u'postalCode', 10021), (u'state', u'NY'), (u'streetAddress', u'21 2nd Street')]), (u'age', 25), (u'firstName', u'John'), (u'lastName', u'Smith'), (u'phoneNumbers', [[(u'number', u'212 555-1234'), (u'type', u'home')], [(u'number', u'646 555-4567'), (u'type', u'fax')]])] expected2 = [(u'address', [(u'city', u'Metropolis'), (u'postalCode', 10021), (u'state', u'NA'), (u'streetAddress', u'21 2nd Street')]), (u'age', 81), (u'firstName', u'Clark'), (u'lastName', u'Kent'), (u'phoneNumbers', [[(u'number', u'919 555-1234'), (u'type', u'home')], [(u'number', u'919 555-4567'), (u'type', u'fax')]])] self.assertIn(expected1, data) self.assertIn(expected2, data)