public void SuccessfullyBlocksRemoveRange() { var connection = TestConfiguration.GetConnection(); var tenantId = TestConfiguration.GetTenantId(); var context = new MongoDbTenantContext(connection, tenantId); var dbSet = new MongoDbTenantSet <TestModel>(context); var entities = new[] { new TestModel { Description = "SuccessfullyBlocksRemoveRange.1" }, new TestModel { Description = "SuccessfullyBlocksRemoveRange.2" } }; dbSet.AddRange(entities); context.SaveChanges(); dbSet = new MongoDbTenantSet <TestModel>(context); entities[0].TenantId = "qweasd"; entities[1].TenantId = "qweasd"; Assert.ThrowsException <MultiTenantException>(() => dbSet.RemoveRange(entities)); }
public void AttachRejectsMismatchedEntity() { var connection = TestConfiguration.GetConnection(); var tenantId = TestConfiguration.GetTenantId(); var context = new MongoDbTenantContext(connection, tenantId); var dbSet = new MongoDbTenantSet <DbSetModel>(context); var model = new DbSetModel { Id = "abcd" }; dbSet.Add(model); context.SaveChanges(); ResetMongoDb(); context = new MongoDbTenantContext(connection, tenantId); dbSet = new MongoDbTenantSet <DbSetModel>(context); var result = dbSet.AsNoTracking().FirstOrDefault(); result.TenantId = tenantId + "a"; Assert.ThrowsException <MultiTenantException>(() => context.Attach(result)); }
public void SuccessfulNullFind() { var connection = TestConfiguration.GetConnection(); var tenantId = TestConfiguration.GetTenantId(); var context = new MongoDbTenantContext(connection, tenantId); var dbSet = new MongoDbTenantSet <TestModel>(context); var context2 = new MongoDbTenantContext(connection, tenantId + "-alt"); var dbSet2 = new MongoDbTenantSet <TestModel>(context2); var entity1 = new TestModel { Description = "SuccessfulInsertAndFind.1" }; var entity2 = new TestModel { Description = "SuccessfulInsertAndFind.2" }; dbSet.Add(entity1); dbSet2.Add(entity2); context.SaveChanges(); context2.SaveChanges(); Assert.IsNull(dbSet.Find("abcd")); }
public void BlocksDuplicatesByTenant() { var connection = TestConfiguration.GetConnection(); var tenantId = TestConfiguration.GetTenantId(); var context = new MongoDbTenantContext(connection, tenantId); var dbSet = new MongoDbTenantSet <TestUniqueModel>(context); var context2 = new MongoDbTenantContext(connection, tenantId + "-alt"); var dbSet2 = new MongoDbTenantSet <TestUniqueModel>(context2); dbSet.Add(new TestUniqueModel { UserName = "******" }); dbSet2.Add(new TestUniqueModel { UserName = "******" }); context.SaveChanges(); context2.SaveChanges(); dbSet.Add(new TestUniqueModel { UserName = "******" }); Assert.ThrowsException <MongoBulkWriteException <TestUniqueModel> >(() => context.SaveChanges()); }
public async Task SuccessfulInsertAndFindAsync() { var connection = TestConfiguration.GetConnection(); var tenantId = TestConfiguration.GetTenantId(); var context = new MongoDbTenantContext(connection, tenantId); var dbSet = new MongoDbTenantSet <TestModel>(context); var context2 = new MongoDbTenantContext(connection, tenantId + "-alt"); var dbSet2 = new MongoDbTenantSet <TestModel>(context2); var entity1 = new TestModel { Description = "SuccessfulInsertAndFind.1" }; var entity2 = new TestModel { Description = "SuccessfulInsertAndFind.2" }; dbSet.Add(entity1); dbSet2.Add(entity2); context.SaveChanges(); context2.SaveChanges(); context = new MongoDbTenantContext(connection, tenantId); dbSet = new MongoDbTenantSet <TestModel>(context); Assert.AreEqual("SuccessfulInsertAndFind.1", (await dbSet.FindAsync(entity1.Id)).Description); Assert.AreEqual(MongoFramework.Infrastructure.EntityEntryState.NoChanges, context.ChangeTracker.GetEntry(entity1).State); }
public async Task SuccessfullyLinqFindTrackedAsync() { var connection = TestConfiguration.GetConnection(); var tenantId = TestConfiguration.GetTenantId(); var context = new MongoDbTenantContext(connection, tenantId); var dbSet = new MongoDbTenantSet <TestModel>(context); var model = new TestModel { Id = "abcd", Description = "SuccessfullyFindTracked.1" }; dbSet.Add(model); context.SaveChanges(); ResetMongoDb(); var result = await dbSet.FirstOrDefaultAsync(); result.Description = "changed"; context.ChangeTracker.DetectChanges(); Assert.AreEqual(MongoFramework.Infrastructure.EntityEntryState.Updated, context.ChangeTracker.GetEntry(result).State); }
public void SuccessfullyRemoveRange() { var connection = TestConfiguration.GetConnection(); var tenantId = TestConfiguration.GetTenantId(); var context = new MongoDbTenantContext(connection, tenantId); var dbSet = new MongoDbTenantSet <TestModel>(context); var entities = new[] { new TestModel { Description = "SuccessfullyRemoveRange.1" }, new TestModel { Description = "SuccessfullyRemoveRange.2" } }; dbSet.AddRange(entities); context.SaveChanges(); dbSet = new MongoDbTenantSet <TestModel>(context); dbSet.RemoveRange(entities); Assert.IsTrue(dbSet.Any(m => m.Description == "SuccessfullyRemoveRange.1")); Assert.IsTrue(dbSet.Any(m => m.Description == "SuccessfullyRemoveRange.2")); context.SaveChanges(); Assert.IsFalse(dbSet.Any(m => m.Description == "SuccessfullyRemoveRange.1")); Assert.IsFalse(dbSet.Any(m => m.Description == "SuccessfullyRemoveRange.2")); }
public void AttachRejectsMismatchedEntities() { var connection = TestConfiguration.GetConnection(); var tenantId = TestConfiguration.GetTenantId(); var context = new MongoDbTenantContext(connection, tenantId); var dbSet = new MongoDbTenantSet <DbSetModel>(context); var entities = new[] { new DbSetModel { Id = "abcd" }, new DbSetModel { Id = "efgh" } }; dbSet.AddRange(entities); context.SaveChanges(); ResetMongoDb(); context = new MongoDbTenantContext(connection, tenantId); dbSet = new MongoDbTenantSet <DbSetModel>(context); var result = dbSet.AsNoTracking().ToList(); result[0].TenantId = tenantId + "a"; Assert.ThrowsException <MultiTenantException>(() => context.AttachRange(result)); }
public void SuccessfullyAttachUntrackedEntity() { var connection = TestConfiguration.GetConnection(); var tenantId = TestConfiguration.GetTenantId(); var context = new MongoDbTenantContext(connection, tenantId); var dbSet = new MongoDbTenantSet <DbSetModel>(context); var model = new DbSetModel { Id = "abcd" }; dbSet.Add(model); context.SaveChanges(); ResetMongoDb(); context = new MongoDbTenantContext(connection, tenantId); dbSet = new MongoDbTenantSet <DbSetModel>(context); var result = dbSet.AsNoTracking().FirstOrDefault(); context.Attach(result); Assert.AreEqual(MongoFramework.Infrastructure.EntityEntryState.NoChanges, context.ChangeTracker.GetEntry(result).State); }
public async Task SuccessfullyLinqFindNoTrackingAsync() { var connection = TestConfiguration.GetConnection(); var tenantId = TestConfiguration.GetTenantId(); var context = new MongoDbTenantContext(connection, tenantId); var dbSet = new MongoDbTenantSet <TestModel>(context); var model = new TestModel { Id = "abcd", Description = "SuccessfullyFindTracked.1" }; dbSet.Add(model); context.SaveChanges(); ResetMongoDb(); context = new MongoDbTenantContext(connection, tenantId); dbSet = new MongoDbTenantSet <TestModel>(context); var result = await dbSet.AsNoTracking().FirstOrDefaultAsync(); Assert.IsNull(context.ChangeTracker.GetEntry(result)); }
public void SuccessfullyUpdateEntity() { var connection = TestConfiguration.GetConnection(); var tenantId = TestConfiguration.GetTenantId(); var context = new MongoDbTenantContext(connection, tenantId); var dbSet = new MongoDbTenantSet <TestModel>(context); var entity = new TestModel { Description = "SuccessfullyUpdateEntity" }; dbSet.Add(entity); context.SaveChanges(); dbSet = new MongoDbTenantSet <TestModel>(context); entity.Description = "SuccessfullyUpdateEntity-Updated"; dbSet.Update(entity); Assert.IsFalse(dbSet.Any(m => m.Description == "SuccessfullyUpdateEntity-Updated")); context.SaveChanges(); Assert.IsTrue(dbSet.Any(m => m.Description == "SuccessfullyUpdateEntity-Updated")); Assert.IsTrue(dbSet.First(m => m.Description == "SuccessfullyUpdateEntity-Updated").TenantId == tenantId); }
public void SuccessfullyAttachUntrackedEntities() { var connection = TestConfiguration.GetConnection(); var tenantId = TestConfiguration.GetTenantId(); var context = new MongoDbTenantContext(connection, tenantId); var dbSet = new MongoDbTenantSet <DbSetModel>(context); var entities = new[] { new DbSetModel { Id = "abcd" }, new DbSetModel { Id = "efgh" } }; dbSet.AddRange(entities); context.SaveChanges(); ResetMongoDb(); context = new MongoDbTenantContext(connection, tenantId); dbSet = new MongoDbTenantSet <DbSetModel>(context); var result = dbSet.AsNoTracking().ToList(); context.AttachRange(result); Assert.AreEqual(MongoFramework.Infrastructure.EntityEntryState.NoChanges, context.ChangeTracker.GetEntry(result[0]).State); Assert.AreEqual(MongoFramework.Infrastructure.EntityEntryState.NoChanges, context.ChangeTracker.GetEntry(result[1]).State); }
public async Task SuccessfullyFindAsyncTracked() { var connection = TestConfiguration.GetConnection(); var tenantId = TestConfiguration.GetTenantId(); var context = new MongoDbTenantContext(connection, tenantId); var dbSet = new MongoDbTenantSet <TestModel>(context); var context2 = new MongoDbTenantContext(connection, tenantId + "-alt"); var dbSet2 = new MongoDbTenantSet <TestModel>(context2); var model = new TestModel { Id = "abcd", Description = "SuccessfullyFindTracked.1" }; var model2 = new TestModel { Id = "abcd", Description = "SuccessfullyFindTracked.2" }; dbSet.Add(model); dbSet2.Add(model2); //Note: not saving, but still should be found as tracked Assert.AreEqual("SuccessfullyFindTracked.1", (await dbSet.FindAsync(model.Id)).Description); Assert.AreEqual("SuccessfullyFindTracked.2", (await dbSet2.FindAsync(model2.Id)).Description); Assert.AreEqual(MongoFramework.Infrastructure.EntityEntryState.Added, context.ChangeTracker.GetEntry(model).State); Assert.AreEqual(MongoFramework.Infrastructure.EntityEntryState.Added, context.ChangeTracker.GetEntry(model2).State); }
public async Task BlocksWrongTenantFindAsync() { var connection = TestConfiguration.GetConnection(); var tenantId = TestConfiguration.GetTenantId(); var context = new MongoDbTenantContext(connection, tenantId); var dbSet = new MongoDbTenantSet <TestModel>(context); var context2 = new MongoDbTenantContext(connection, tenantId + "-alt"); var dbSet2 = new MongoDbTenantSet <TestModel>(context2); var entity1 = new TestModel { Description = "SuccessfulInsertAndFind.1" }; var entity2 = new TestModel { Description = "SuccessfulInsertAndFind.1" }; dbSet.Add(entity1); dbSet2.Add(entity2); context.SaveChanges(); context2.SaveChanges(); Assert.IsNull(await dbSet.FindAsync(entity2.Id)); }
public void AllowsUniquesByTenant() { var connection = TestConfiguration.GetConnection(); var tenantId = TestConfiguration.GetTenantId(); var context = new MongoDbTenantContext(connection, tenantId); var dbSet = new MongoDbTenantSet <TestUniqueModel>(context); var context2 = new MongoDbTenantContext(connection, tenantId + "-alt"); var dbSet2 = new MongoDbTenantSet <TestUniqueModel>(context2); dbSet.Add(new TestUniqueModel { UserName = "******" }); dbSet.Add(new TestUniqueModel { UserName = "******" }); dbSet2.Add(new TestUniqueModel { UserName = "******" }); dbSet2.Add(new TestUniqueModel { UserName = "******" }); context.SaveChanges(); context2.SaveChanges(); Assert.AreEqual(2, dbSet.Count()); Assert.AreEqual(2, dbSet2.Count()); }
public async Task FindAsyncRequiresId() { var connection = TestConfiguration.GetConnection(); var tenantId = TestConfiguration.GetTenantId(); var context = new MongoDbTenantContext(connection, tenantId); var dbSet = new MongoDbTenantSet <TestModel>(context); await Assert.ThrowsExceptionAsync <ArgumentNullException>(async() => await dbSet.FindAsync(null)); }
public void SuccessfullyReturnsBaseContext() { var connection = TestConfiguration.GetConnection(); var tenantId = TestConfiguration.GetTenantId(); var context = new MongoDbTenantContext(connection, tenantId); var dbSet = new MongoDbTenantSet <TestModel>(context); Assert.IsInstanceOfType((dbSet as IMongoDbSet <TestModel>).Context, typeof(IMongoDbContext)); }
public void FindRequiresId() { var connection = TestConfiguration.GetConnection(); var tenantId = TestConfiguration.GetTenantId(); var context = new MongoDbTenantContext(connection, tenantId); var dbSet = new MongoDbTenantSet <TestModel>(context); Assert.ThrowsException <ArgumentNullException>(() => dbSet.Find(null)); }
public void SuccessfulCreateTenantId() { var connection = TestConfiguration.GetConnection(); var tenantId = TestConfiguration.GetTenantId(); var context = new MongoDbTenantContext(connection, tenantId); var dbSet = new MongoDbTenantSet <TestModel>(context); var model = dbSet.Create(); Assert.AreEqual(tenantId, model.TenantId); }
public void SuccessfullyBlocksNulls() { var connection = TestConfiguration.GetConnection(); var tenantId = TestConfiguration.GetTenantId(); var context = new MongoDbTenantContext(connection, tenantId); var dbSet = new MongoDbTenantSet <TestModel>(context); Assert.ThrowsException <ArgumentNullException>(() => dbSet.Add(null)); Assert.ThrowsException <ArgumentNullException>(() => dbSet.AddRange(null)); Assert.ThrowsException <ArgumentNullException>(() => dbSet.Update(null)); Assert.ThrowsException <ArgumentNullException>(() => dbSet.UpdateRange(null)); }
public void SuccessfullyRemoveRangeByPredicate() { var connection = TestConfiguration.GetConnection(); var tenantId = TestConfiguration.GetTenantId(); var context = new MongoDbTenantContext(connection, tenantId); var dbSet = new MongoDbTenantSet <TestModel>(context); var context2 = new MongoDbTenantContext(connection, tenantId + "-alt"); var dbSet2 = new MongoDbTenantSet <TestModel>(context2); var entities = new[] { new TestModel { Description = "SuccessfullyRemoveRangeByPredicate" }, new TestModel { Description = "SuccessfullyRemoveRangeByPredicate", BooleanField = true } }; var entities2 = new[] { new TestModel { Description = "SuccessfullyRemoveRangeByPredicate" }, new TestModel { Description = "SuccessfullyRemoveRangeByPredicate", BooleanField = true } }; dbSet.AddRange(entities); dbSet2.AddRange(entities2); context.SaveChanges(); context2.SaveChanges(); dbSet = new MongoDbTenantSet <TestModel>(context); dbSet.RemoveRange(e => e.BooleanField); Assert.AreEqual(2, dbSet.Count(m => m.Description == "SuccessfullyRemoveRangeByPredicate")); Assert.AreEqual(2, dbSet2.Count(m => m.Description == "SuccessfullyRemoveRangeByPredicate")); context.SaveChanges(); Assert.AreEqual(1, dbSet.Count(m => m.Description == "SuccessfullyRemoveRangeByPredicate")); Assert.AreEqual(2, dbSet2.Count(m => m.Description == "SuccessfullyRemoveRangeByPredicate")); Assert.IsNotNull(dbSet.FirstOrDefault(m => m.Id == entities[0].Id)); }
public async Task SingleOrDefaultAsync_NoValue() { var tenantId = TestConfiguration.GetTenantId(); var connection = TestConfiguration.GetConnection(); var context = new MongoDbTenantContext(connection, tenantId); var dbSet = new MongoDbTenantSet <QueryableAsyncModel>(context); dbSet.AddRange(GetModels()); context.SaveChanges(); var context2 = new MongoDbTenantContext(connection, tenantId + "-2"); var dbSet2 = new MongoDbTenantSet <QueryableAsyncModel>(context2); Assert.IsNull(await dbSet2.SingleOrDefaultAsync()); }
public async Task MinAsync_NoValues() { var tenantId = TestConfiguration.GetTenantId(); var connection = TestConfiguration.GetConnection(); var context = new MongoDbTenantContext(connection, tenantId); var dbSet = new MongoDbTenantSet <QueryableAsyncModel>(context); dbSet.AddRange(GetModels()); context.SaveChanges(); var context2 = new MongoDbTenantContext(connection, tenantId + "-2"); var dbSet2 = new MongoDbTenantSet <QueryableAsyncModel>(context2); await Assert.ThrowsExceptionAsync <InvalidOperationException>(async() => await dbSet2.Select(e => e.IntNumber).MinAsync()); }
public void SuccessfulLimitsQueryToTenant() { var connection = TestConfiguration.GetConnection(); var tenantId = TestConfiguration.GetTenantId(); var context = new MongoDbTenantContext(connection, tenantId); var dbSet = new MongoDbTenantSet <TestModel>(context); var context2 = new MongoDbTenantContext(connection, tenantId + "-alt"); var dbSet2 = new MongoDbTenantSet <TestModel>(context2); var entities = new[] { new TestModel { Description = "SuccessfulLimitsQueryToTenant.1" }, new TestModel { Description = "SuccessfulLimitsQueryToTenant.2", BooleanField = true } }; var entities2 = new[] { new TestModel { Description = "SuccessfulLimitsQueryToTenant.1" }, new TestModel { Description = "SuccessfulLimitsQueryToTenant.2", BooleanField = true } }; dbSet.AddRange(entities); dbSet2.AddRange(entities2); context.SaveChanges(); context2.SaveChanges(); Assert.AreEqual(2, dbSet.Count()); Assert.AreEqual(2, dbSet2.Count()); Assert.AreEqual(1, dbSet.Where(e => e.BooleanField).Count()); Assert.AreEqual(1, dbSet2.Where(e => e.BooleanField).Count()); }
public void SuccessfulInsertAndQueryBack() { var connection = TestConfiguration.GetConnection(); var tenantId = TestConfiguration.GetTenantId(); var context = new MongoDbTenantContext(connection, tenantId); var dbSet = new MongoDbTenantSet <TestModel>(context); dbSet.Add(new TestModel { Description = "SuccessfulInsertAndQueryBack" }); Assert.IsFalse(dbSet.Any(m => m.Description == "SuccessfulInsertAndQueryBack")); context.SaveChanges(); Assert.IsTrue(dbSet.Any(m => m.Description == "SuccessfulInsertAndQueryBack")); Assert.IsTrue(dbSet.Any(m => m.TenantId == tenantId)); }
private MongoDbTenantSet <QueryableAsyncModel> SetupTwoTenantsData(string tenantId) { var connection = TestConfiguration.GetConnection(); var context = new MongoDbTenantContext(connection, tenantId); var dbSet = new MongoDbTenantSet <QueryableAsyncModel>(context); var context2 = new MongoDbTenantContext(connection, tenantId + "-2"); var dbSet2 = new MongoDbTenantSet <QueryableAsyncModel>(context2); dbSet.AddRange(GetModels()); context.SaveChanges(); dbSet2.AddRange(GetModels()); context2.SaveChanges(); return(dbSet); }
public async Task AnyAsync_NoValues() { var tenantId = TestConfiguration.GetTenantId(); var connection = TestConfiguration.GetConnection(); var context = new MongoDbTenantContext(connection, tenantId); var dbSet = new MongoDbTenantSet <QueryableAsyncModel>(context); dbSet.AddRange(GetModels()); context.SaveChanges(); var context2 = new MongoDbTenantContext(connection, tenantId + "-2"); var dbSet2 = new MongoDbTenantSet <QueryableAsyncModel>(context2); var result = await dbSet2.AnyAsync(); Assert.IsFalse(result); }
public void SuccessfullyBlocksUpdateEntity() { var connection = TestConfiguration.GetConnection(); var tenantId = TestConfiguration.GetTenantId(); var context = new MongoDbTenantContext(connection, tenantId); var dbSet = new MongoDbTenantSet <TestModel>(context); var entity = new TestModel { Description = "SuccessfullyBlocksUpdateEntity" }; dbSet.Add(entity); context.SaveChanges(); dbSet = new MongoDbTenantSet <TestModel>(context); entity.TenantId = "qweasd"; entity.Description = "SuccessfullyBlocksUpdateEntity-Updated"; Assert.ThrowsException <MultiTenantException>(() => dbSet.Update(entity)); }
public async Task SuccessfullyRemoveEntityByIdAsync() { var connection = TestConfiguration.GetConnection(); var tenantId = TestConfiguration.GetTenantId(); var context = new MongoDbTenantContext(connection, tenantId); var dbSet = new MongoDbTenantSet <TestModel>(context); var context2 = new MongoDbTenantContext(connection, tenantId + "-alt"); var dbSet2 = new MongoDbTenantSet <TestModel>(context2); var entity = new TestModel { Description = "SuccessfullyRemoveEntityByIdAsync" }; dbSet.Add(entity); await context.SaveChangesAsync(); var entity2 = new TestModel { Description = "SuccessfullyRemoveEntityByIdAsync" }; dbSet2.Add(entity2); await context2.SaveChangesAsync(); dbSet = new MongoDbTenantSet <TestModel>(context); dbSet.RemoveById(entity.Id); //mismatched tenant, should not delete anything dbSet.RemoveById(entity2.Id); Assert.IsTrue(dbSet.Any(m => m.Description == "SuccessfullyRemoveEntityByIdAsync")); Assert.IsTrue(dbSet2.Any(m => m.Description == "SuccessfullyRemoveEntityByIdAsync")); await context.SaveChangesAsync(); Assert.IsFalse(dbSet.Any(m => m.Description == "SuccessfullyRemoveEntityByIdAsync")); Assert.IsTrue(dbSet2.Any(m => m.Description == "SuccessfullyRemoveEntityByIdAsync")); }
public void SearchTextTenant() { var connection = TestConfiguration.GetConnection(); var context = new MongoDbTenantContext(connection, TestConfiguration.GetTenantId()); var dbSet = new MongoDbTenantSet <SearchTextTenantModel>(context); var models = new SearchTextTenantModel[] { new SearchTextTenantModel { MiscField = 1, Text = "The quick brown fox jumps over the lazy dog." }, new SearchTextTenantModel { MiscField = 2, Text = "The five boxing wizards jump quickly." }, new SearchTextTenantModel { MiscField = 3, Text = "The quick brown fox jumps over the lazy dog." }, new SearchTextTenantModel { MiscField = 4, Text = "Jived fox nymph grabs quick waltz." }, }; dbSet.AddRange(models); context.SaveChanges(); //add some extras to ensure only our tenant's results are coming back. var context2 = new MongoDbTenantContext(connection, "second"); var dbSet2 = new MongoDbTenantSet <SearchTextTenantModel>(context); dbSet2.AddRange(models); context2.SaveChanges(); Assert.AreEqual(4, dbSet.SearchText("quick").Count()); Assert.AreEqual(0, dbSet.SearchText("the").Count()); //Stop words aren't used in text indexes: https://docs.mongodb.com/manual/core/index-text/#supported-languages-and-stop-words Assert.AreEqual(2, dbSet.SearchText("dog").Count()); Assert.AreEqual(1, dbSet.SearchText("jived").Count()); Assert.AreEqual(1, dbSet.SearchText("quick").Where(e => e.MiscField == 3).Count()); }