public async Task NullEntityOnInsertTest() { MongodbConfig mongodbConfig = MongoDbHelper.GetMongodbConfig(); IMongoDatabase database = MongodbProvider.GetDatabase(mongodbConfig); IMongodbSaveRepository <Test> saveRepository = new MongodbSaveRepository <Test>(database); await Assert.ThrowsAsync <ArgumentNullException>(() => saveRepository.InsertAsync(null)); }
public async Task InsertManyVsInsertOne() { MongodbConfig mongodbConfig = MongoDbHelper.GetMongodbConfig(); IMongoDatabase database = MongodbProvider.GetDatabase(mongodbConfig); ISaveRepository <Test> saveRepository = new MongodbSaveRepository <Test>(database); Stopwatch stopwatch = new Stopwatch(); IList <Test> tests = TestHelper.CreateRandomTests(100); // First insert in order establish the connection await saveRepository.InsertAsync(tests.ElementAt(0)); // One-by-one insert stopwatch.Restart(); foreach (Test test in tests) { await saveRepository.InsertAsync(test); } stopwatch.Stop(); TimeSpan oneByOneTime = stopwatch.Elapsed; // Multi-thread insert stopwatch.Restart(); IList <Task> tasks = new List <Task>(); foreach (Test test in tests) { tasks.Add(saveRepository.InsertAsync(test)); } await Task.WhenAll(tasks); stopwatch.Stop(); TimeSpan multiThreadTime = stopwatch.Elapsed; // Insert many (bulk) stopwatch.Restart(); await saveRepository.InsertManyAsync(tests); stopwatch.Stop(); TimeSpan bulkTime = stopwatch.Elapsed; Assert.True( TimeSpan.Compare(oneByOneTime, multiThreadTime) == 1 && TimeSpan.Compare(multiThreadTime, bulkTime) == 1); }
public async Task SingleEntityIntegralTest() { MongodbConfig mongodbConfig = MongoDbHelper.GetMongodbConfig(); IMongoDatabase database = MongodbProvider.GetDatabase(mongodbConfig); IMongodbSaveRepository <Test> saveRepository = new MongodbSaveRepository <Test>(database); IGetRepository <Test> getRepository = new MongodbGetRepository <Test>(database); IDeleteRepository <Test> deleteRepository = new MongodbDeleteRepository <Test>(database); Test newTest = new Test() { BoolField = true, NumberField = 1, ObjectId = Guid.NewGuid().ToString(), StringField = "new-single-object", TimestampField = DateTime.UtcNow }; await saveRepository.InsertAsync(newTest); Test savedTest = await getRepository.GetFirstByConditionsAsync(x => x.ObjectId == newTest.ObjectId && x.BoolField == newTest.BoolField && x.NumberField == newTest.NumberField && x.StringField == newTest.StringField && x.TimestampField == newTest.TimestampField); savedTest.BoolField = false; savedTest.NumberField = 2; savedTest.StringField = "modified-single-object"; savedTest.TimestampField = DateTime.UtcNow.AddDays(1); await saveRepository.ReplaceAsync( x => x.ObjectId == newTest.ObjectId && x.BoolField == newTest.BoolField && x.NumberField == newTest.NumberField && x.StringField == newTest.StringField && x.TimestampField == newTest.TimestampField, savedTest); Test modifiedTest = (await getRepository.GetAllByConditionsAsync(x => x.ObjectId == savedTest.ObjectId && x.BoolField == savedTest.BoolField && x.NumberField == savedTest.NumberField && x.StringField == savedTest.StringField && x.TimestampField == savedTest.TimestampField)).Single(); await deleteRepository.DeleteAllByConditionsAsync(x => x.ObjectId == savedTest.ObjectId && x.BoolField == savedTest.BoolField && x.NumberField == savedTest.NumberField && x.StringField == savedTest.StringField && x.TimestampField == savedTest.TimestampField); Test deletedTest = await getRepository.GetFirstByConditionsAsync(x => x.ObjectId == savedTest.ObjectId && x.BoolField == savedTest.BoolField && x.NumberField == savedTest.NumberField && x.StringField == savedTest.StringField && x.TimestampField == savedTest.TimestampField); Assert.Null(deletedTest); }