def test_update_invalid(self): body_data = { 'name': 'b', 'duration': 4, 'uploaded_time': 'b', 'date': 'wef' } collection_name = 'collection_name' client = mongomock.MongoClient() persistence_gateway = PersistenceGateway(client) with self.assertRaises(ItemNotFound): persistence_gateway.update(collection_name, 3232, body_data)
def test_add(self): body_data = { 'name': 'b', 'duration': 4, 'uploaded_time': 'b', 'date': 'wef' } collection_name = 'collection_name' client = mongomock.MongoClient() persistence_gateway = PersistenceGateway(client) persistence_gateway.add(collection_name, 3232, body_data) assert client.mydatabase[collection_name].find_one({'_id': 3232}) == body_data
def test_get(self): body_data = { '_id': 56, 'name': 'b', 'duration': 4, 'uploaded_time': 'b', 'date': 'wef' } client = mongomock.MongoClient() collection_name = 'collection_name' persistence_gateway = PersistenceGateway(client) client.mydatabase[collection_name].insert_one(body_data) body_data['id'] = body_data.pop('_id') assert body_data == persistence_gateway.get(collection_name, 56)
def test_delete(self): body_data = { '_id': 56, 'name': 'b', 'duration': 4, 'uploaded_time': 'b', 'date': 'wef' } client = mongomock.MongoClient() persistence_gateway = PersistenceGateway(client) collection_name = 'collection_name' client.mydatabase[collection_name].insert_one(body_data) persistence_gateway.delete(collection_name, 56) find_result = client.mydatabase[collection_name].find_one({'_id': 56}) assert find_result is None
def test_get_all(self): body_data1 = { 'name': 'b', 'duration': 4, 'uploaded_time': 'b', 'date': 'wef' } body_data2 = { 'name': 'bsd', 'duration': 44, 'uploaded_time': 'gfdb', 'date': 'wefgs' } collection_name = 'collection_name' client = mongomock.MongoClient() persistence_gateway = PersistenceGateway(client) persistence_gateway.add(collection_name, 1, body_data1) persistence_gateway.add(collection_name, 2, body_data2) first_entry = persistence_gateway.get(collection_name, 1) second_entry = persistence_gateway.get(collection_name, 2) assert (len(persistence_gateway.get_all(collection_name))) == 2 assert (first_entry in persistence_gateway.get_all(collection_name)) assert (second_entry in persistence_gateway.get_all(collection_name))
def test_add_duplicate(self): body_data = { 'name': 'b', 'duration': 4, 'uploaded_time': 'b', 'date': 'wef' } collection_name = 'collection_name' client = mongomock.MongoClient() persistence_gateway = PersistenceGateway(client) persistence_gateway.add(collection_name, 3232, body_data) with self.assertRaises(UnableToInsertDueToDuplicateKeyError): persistence_gateway.add(collection_name, 3232, body_data)
def test_update(self): body_data = { 'name': 'b', 'duration': 4, 'uploaded_time': 'b', 'date': 'wef' } collection_name = 'collection_name' client = mongomock.MongoClient() persistence_gateway = PersistenceGateway(client) persistence_gateway.add(collection_name, 3232, body_data) new_data = {'name': 'xxx'} persistence_gateway.update(collection_name, 3232, new_data) from_db = client.mydatabase[collection_name].find_one({'_id': 3232}) assert from_db == new_data
def test_delete_invalid(self): collection_name = 'collection_name' client = mongomock.MongoClient() persistence_gateway = PersistenceGateway(client) with self.assertRaises(ItemNotFound): persistence_gateway.delete(collection_name, 3422)
from app import create_app from audio_service import AudioFileService from persistance_gateway import PersistenceGateway from pymongo import MongoClient import os db_environment_variable_name = 'AUDIO_FILE_SERVER_DATABASE_URI' environment_variables = os.environ database_uri = "mongodb://localhost:27017/" if db_environment_variable_name in environment_variables.keys(): database_uri = environment_variables[db_environment_variable_name] mongo_client = MongoClient(database_uri) persistence_gateway = PersistenceGateway(mongo_client) audio_file_service = AudioFileService(persistence_gateway) app = create_app(audio_file_service) if __name__ == '__main__': app.run(debug=True, host='0.0.0.0', port=80)
def create_service_and_gateway(self): database_uri = "mongodb_uri" mongo_client = MongoClient(database_uri) persistence_gateway = PersistenceGateway(mongo_client) audio_file_service = AudioFileService(persistence_gateway) return audio_file_service, persistence_gateway