public void SomethingElsePersistence_DeleteSomethingElseById_DeletionCascades() { int id = 1; List <int> childIds; var something1 = new Domain.Something() { Name = "Bob" }; Domain.SomethingElse somethingElse1 = Domain.SomethingElse.CreateNamedSomethingElse("Fred Bloggs"); using (var ctx = new DbContextFactory().CreateAppDbContext(nameof(SomethingElsePersistence_DeleteSomethingElseById_DeletionCascades))) { var persistence = new SomethingElsePersistence(ctx); persistence.SaveSomethingElse(somethingElse1); var updatedSomethingElse = persistence.UpdateSomethingElseByIdAddSomething(somethingElse1.Id, something1); var somethingElse = ctx.SomethingElses.Include(s => s.Somethings).Where(r => r.Id == id).FirstOrDefault(); childIds = somethingElse.Somethings.Select(c => c.Id).ToList(); ctx.Remove(somethingElse); ctx.SaveChanges(); } using (var ctx = new DbContextFactory().CreateAppDbContext(nameof(SomethingElsePersistence_DeleteSomethingElseById_DeletionCascades))) { Assert.Empty(ctx.Somethings.Where(c => childIds.Contains(c.Id))); }; }
public void SomethingElsePersistence__UpdateSomethingElseByIdAddSomething__ThrowsInvalidOperationExceptionGivenNonexistentSomething() { int id = 5; using (var ctx = new DbContextFactory().CreateAppDbContext(nameof(SomethingElsePersistence__UpdateSomethingElseByIdAddSomething__ThrowsInvalidOperationExceptionGivenNonexistentSomething))) { var persistence = new SomethingElsePersistence(ctx); persistence.SaveSomethingElse(somethingElse); }; Mock <ISomethingFactory> mockSomethingFactory = new Mock <ISomethingFactory>(); mockSomethingFactory.Setup(x => x.Create(something.Name)).Returns((Domain.Something)null); using (var ctx = new DbContextFactory().CreateAppDbContext(nameof(SomethingElsePersistence__UpdateSomethingElseByIdAddSomething__ThrowsInvalidOperationExceptionGivenNonexistentSomething))) { var persistence = new SomethingElsePersistence(ctx); Domain.Something something1 = mockSomethingFactory.Object.Create(something.Name); var exception = Assert.Throws <InvalidOperationException>(() => persistence.UpdateSomethingElseByIdAddSomething(id, something1)); }; }
public void SomethingElsePersistence__UpdateSomethingElseByIdAddSomething__RetrievesSomethingElseByIdFromDatabase() { int id = 1; var something1 = new Domain.Something() { Name = "Bob" }; using (var ctx = new DbContextFactory().CreateAppDbContext(nameof(SomethingElsePersistence__UpdateSomethingElseByIdAddSomething__RetrievesSomethingElseByIdFromDatabase))) { var persistence = new SomethingElsePersistence(ctx); persistence.SaveSomethingElse(somethingElse); }; using (var ctx = new DbContextFactory().CreateAppDbContext(nameof(SomethingElsePersistence__UpdateSomethingElseByIdAddSomething__RetrievesSomethingElseByIdFromDatabase))) { var persistence = new SomethingElsePersistence(ctx); var updatedSomethingElse = persistence.UpdateSomethingElseByIdAddSomething(id, something1); Assert.Equal(somethingElse.Name, updatedSomethingElse.Name); Assert.Equal(somethingElse.Somethings.Count + 1, updatedSomethingElse.Somethings.Count); }; }