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 SomethingFactory_Create_CreatesSomething()
        {
            SomethingFactory somethingFactory = new SomethingFactory();

            Domain.Something actual = somethingFactory.Create();

            Assert.IsType <Domain.Something>(actual);
        }
        public void Something_HasAName()
        {
            var    something1 = new Domain.Something();
            string expected   = null;

            string actual = something1.Name;

            Assert.Equal(expected, actual);
        }
        public void Something_HasAnId()
        {
            var something1 = new Domain.Something();
            int expected   = 0;

            int actual = something1.Id;

            Assert.Equal(expected, actual);
        }
        public void SomethingFactory_Create_CreatesSomethingWithName()
        {
            SomethingFactory somethingFactory = new SomethingFactory();
            string           expected         = "Fred Bloggs";

            Domain.Something actual = somethingFactory.Create(expected);

            Assert.IsType <Domain.Something>(actual);
            Assert.Equal(expected, actual.Name);
        }
        public void Something_SetsId()
        {
            var something1 = new Domain.Something();
            int expected   = 1;

            something1.Id = expected;
            int actual = something1.Id;

            Assert.Equal(expected, actual);
        }
Beispiel #7
0
        public void DbContextFactory_CreateAppDbContext_SavesSomethingToDatabaseAndRetrievesIt()
        {
            var something = new Domain.Something()
            {
                Name = "Fred Bloggs"
            };

            using (var ctx = new DbContextFactory().CreateAppDbContext(nameof(DbContextFactory_CreateAppDbContext_SavesSomethingToDatabaseAndRetrievesIt)))
            {
                ctx.Somethings.Add(something);
                ctx.SaveChanges();
            };
            using (var ctx = new DbContextFactory().CreateAppDbContext(nameof(DbContextFactory_CreateAppDbContext_SavesSomethingToDatabaseAndRetrievesIt)))
            {
                var savedSomething = ctx.Somethings.Single();
                Assert.Equal(something.Name, savedSomething.Name);
            };
        }
        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);
            };
        }
        public void SomethingElsePersistence__UpdateSomethingElseByIdDeleteSomethingById__UpdatesSomethingElseByIdFromDatabaseWithSomethingDeleted()
        {
            int id           = 1;
            int something_id = 1;
            var something1   = new Domain.Something()
            {
                Name = "Bob"
            };

            using (var ctx = new DbContextFactory().CreateAppDbContext(nameof(SomethingElsePersistence__UpdateSomethingElseByIdDeleteSomethingById__UpdatesSomethingElseByIdFromDatabaseWithSomethingDeleted)))
            {
                var persistence = new SomethingElsePersistence(ctx);
                persistence.SaveSomethingElse(somethingElse);
            };

            using (var ctx = new DbContextFactory().CreateAppDbContext(nameof(SomethingElsePersistence__UpdateSomethingElseByIdDeleteSomethingById__UpdatesSomethingElseByIdFromDatabaseWithSomethingDeleted)))
            {
                var persistence          = new SomethingElsePersistence(ctx);
                var updatedSomethingElse = persistence.UpdateSomethingElseByIdDeleteSomethingById(id, something_id);
                Assert.Equal(somethingElse.Name, updatedSomethingElse.Name);
                Assert.Equal(somethingElse.Somethings.Count - 1, updatedSomethingElse.Somethings.Count);
            };
        }