Exemplo n.º 1
0
 public void DatabaseName_IsTestName()
 {
     using (var dbContext = new EntityDbContext())
     {
         dbContext.Database.Connection.Database.ShouldEqual("TripodIocIntegrationTestDb");
     }
 }
Exemplo n.º 2
0
        public void EagerLoad_IncludesRelatedData()
        {
            using (var dbContext = new EntityDbContext())
            {
                var userName       = Guid.NewGuid().ToString();
                var permissionName = Guid.NewGuid().ToString();
                var user           = new User {
                    Name = userName,
                };
                user.Permissions.Add(new Permission {
                    Name = permissionName
                });
                dbContext.Create(user);
                var affectedRows = dbContext.SaveChangesAsync().Result;
                affectedRows.ShouldEqual(3);

                var entity = dbContext.Query <User>()
                             .EagerLoad(new Expression <Func <User, object> >[]
                {
                    x => x.Permissions,
                }).Single(x => x.Name.Equals(userName));
                entity.Permissions.Count.ShouldEqual(1);
                entity.Permissions.Single().Name.ShouldEqual(permissionName);
            }
        }
Exemplo n.º 3
0
        public void Ctor_SetsInitializer_ToBrownfield()
        {
            var dbContext = new EntityDbContext();

            dbContext.Initializer.ShouldNotBeNull();
            dbContext.Initializer.GetType().ShouldEqual(typeof(BrownfieldDbInitializer));
        }
 void IDisposable.Dispose()
 {
     using (var dbContext = new EntityDbContext())
     {
         dbContext.Database.Delete();
     }
 }
Exemplo n.º 5
0
 public void GetAsync_ReturnsNull_WhenPrimaryKeyDoesNotMatchRow()
 {
     using (var dbContext = new EntityDbContext())
     {
         var entity = dbContext.GetAsync <User>(int.MaxValue).Result;
         entity.ShouldBeNull();
     }
 }
Exemplo n.º 6
0
        public void InitializeDatabase_HasNoSideEffects()
        {
            var dbInitializer = new BrownfieldDbInitializer();
            var dbContext     = new EntityDbContext();

            dbInitializer.InitializeDatabase(dbContext);
            dbContext.ShouldNotBeNull();
        }
Exemplo n.º 7
0
 public void EagerLoad_ReturnsNull_WhenQueryIsNull()
 {
     using (var dbContext = new EntityDbContext())
     {
         var result = dbContext.EagerLoad <Permission>(null, x => x.Users);
         result.ShouldBeNull();
     }
 }
Exemplo n.º 8
0
 public void Get_ThrowsArgumentNullException_WhenFirstKeyValueArgumentIsNull()
 {
     using (var dbContext = new EntityDbContext())
     {
         var exception = Assert.Throws <ArgumentNullException>(() => dbContext.Get <User>(null));
         exception.ShouldNotBeNull();
         exception.ParamName.ShouldEqual("firstKeyValue");
     }
 }
Exemplo n.º 9
0
        public void NoArgGet_ReturnsDataFromStore()
        {
            using (var dbContext = new EntityDbContext())
            {
                var entities = dbContext.Get <User>().Take(2).ToArray();

                entities.ShouldNotBeNull();
                entities.Length.ShouldBeInRange(0, 2);
            }
        }
Exemplo n.º 10
0
        public void ModelCreator_HasPublicSetter()
        {
            var modelCreator = new Mock <ICreateDbModel>(MockBehavior.Loose);
            var dbContext    = new EntityDbContext
            {
                ModelCreator = modelCreator.Object
            };

            dbContext.ModelCreator.ShouldEqual(modelCreator.Object);
        }
Exemplo n.º 11
0
        public void Initializer_HasPublicSetter()
        {
            var initializer = new Mock <IDatabaseInitializer <EntityDbContext> >(MockBehavior.Loose);
            var dbContext   = new EntityDbContext
            {
                Initializer = initializer.Object
            };

            dbContext.Initializer.ShouldEqual(initializer.Object);
        }
Exemplo n.º 12
0
 public void Create_SetsEntityState_ToAdded()
 {
     using (var dbContext = new EntityDbContext())
     {
         var entity = new User {
             Name = Guid.NewGuid().ToString()
         };
         dbContext.Entry(entity).State.ShouldEqual(EntityState.Detached);
         dbContext.Create(entity);
         dbContext.Entry(entity).State.ShouldEqual(EntityState.Added);
     }
 }
        public EntityDbContextDatabaseInitializer()
        {
            var dbContext = new EntityDbContext
            {
                Initializer = new DropCreateDatabaseIfModelChanges <EntityDbContext>()
            };

            dbContext.Initializer.InitializeDatabase(dbContext);
            var users = dbContext.Set <User>().ToArray();

            Assert.NotNull(users);
            users.Count().ShouldBeInRange(0, int.MaxValue);
            dbContext.Dispose();
        }
Exemplo n.º 14
0
        public void DiscardChangesAsync_ChangesAddedEntityState_ToDetached()
        {
            using (var dbContext = new EntityDbContext())
            {
                var entity = new User {
                    Name = Guid.NewGuid().ToString()
                };
                dbContext.Create(entity);

                dbContext.Entry(entity).State.ShouldEqual(EntityState.Added);
                dbContext.DiscardChangesAsync().Wait();
                dbContext.Entry(entity).State.ShouldEqual(EntityState.Detached);
            }
        }
Exemplo n.º 15
0
 public void Create_AddsEntityToDb_WhenChangesAreSaved()
 {
     using (var dbContext = new EntityDbContext())
     {
         var entity = new User {
             Name = Guid.NewGuid().ToString()
         };
         dbContext.Create(entity);
         entity.Id.ShouldEqual(0);
         var affectedRows = dbContext.SaveChangesAsync().Result;
         affectedRows.ShouldEqual(1);
         entity.Id.ShouldNotEqual(0);
     }
 }
Exemplo n.º 16
0
        public void InitializeDatabase_CustomizesDuringSeed_WhenDatabaseDoesNotExist()
        {
            using (var dbContext = new EntityDbContext())
            {
                dbContext.Database.Delete(); // force initializer to seed
                var dbCustomizer = new Mock <ICustomizeDb>(MockBehavior.Strict);
                dbCustomizer.Setup(x => x.Customize(It.IsAny <EntityDbContext>()));
                var dbInitializer = new GreenfieldDbInitializer(dbCustomizer.Object);
                dbContext.Initializer = dbInitializer;
                dbContext.Initializer.InitializeDatabase(dbContext);

                dbCustomizer.Verify(x => x.Customize(It.IsAny <EntityDbContext>()), Times.Once);
                dbContext.Dispose();
            }
        }
Exemplo n.º 17
0
        public void Delete_SetsEntityState_ToDeleted()
        {
            using (var dbContext = new EntityDbContext())
            {
                var entity = new User
                {
                    Name = Guid.NewGuid().ToString()
                };
                dbContext.Create(entity);
                var affectedRows = dbContext.SaveChangesAsync().Result;

                affectedRows.ShouldEqual(1);
                dbContext.Entry(entity).State.ShouldEqual(EntityState.Unchanged);
                dbContext.Delete(entity);
                dbContext.Entry(entity).State.ShouldEqual(EntityState.Deleted);
            }
        }
Exemplo n.º 18
0
        public void Query_ReturnsData()
        {
            using (var dbContext = new EntityDbContext())
            {
                var createdEntity = new User {
                    Name = Guid.NewGuid().ToString()
                };
                dbContext.Create(createdEntity);
                var affectedRows = dbContext.SaveChangesAsync().Result;
                affectedRows.ShouldEqual(1);

                var queriedEntity = dbContext.Query <User>().SingleOrDefaultAsync(x => x.Id == createdEntity.Id).Result;

                Assert.NotNull(queriedEntity);
                createdEntity.Id.ShouldEqual(queriedEntity.Id);
            }
        }
Exemplo n.º 19
0
        public void Update_SetsEntityState_ToModified()
        {
            using (var dbContext = new EntityDbContext())
            {
                var permissionName = Guid.NewGuid().ToString();
                var entity         = new Permission {
                    Name = permissionName, Description = "d1"
                };
                dbContext.Create(entity);
                var affectedRows = dbContext.SaveChangesAsync().Result;

                affectedRows.ShouldEqual(1);
                dbContext.Entry(entity).State.ShouldEqual(EntityState.Unchanged);
                dbContext.Update(entity);
                dbContext.Entry(entity).State.ShouldEqual(EntityState.Modified);
            }
        }
Exemplo n.º 20
0
        public void DiscardChangesAsync_ChangesModifiedEntityState_ToUnchanged()
        {
            using (var dbContext = new EntityDbContext())
            {
                var userName = Guid.NewGuid().ToString();
                var entity   = new User {
                    Name = userName
                };
                dbContext.Create(entity);
                var affectedRows = dbContext.SaveChangesAsync().Result;

                affectedRows.ShouldEqual(1);
                dbContext.Entry(entity).State.ShouldEqual(EntityState.Unchanged);
                entity.Name = Guid.NewGuid().ToString();
                dbContext.Entry(entity).State.ShouldEqual(EntityState.Modified);
                dbContext.DiscardChangesAsync().Wait();
                dbContext.Entry(entity).State.ShouldEqual(EntityState.Unchanged);
                entity.Name.ShouldEqual(userName);
            }
        }
Exemplo n.º 21
0
        public void ReloadAsync_ChangesModifiedEntityState_ToUnchanged()
        {
            using (var dbContext = new EntityDbContext())
            {
                var description = Guid.NewGuid().ToString();
                var entity      = new Permission {
                    Name = Guid.NewGuid().ToString(), Description = description
                };
                dbContext.Create(entity);
                var affectedRows = dbContext.SaveChangesAsync().Result;

                affectedRows.ShouldEqual(1);
                dbContext.Entry(entity).State.ShouldEqual(EntityState.Unchanged);
                entity.Description = Guid.NewGuid().ToString();
                dbContext.Entry(entity).State.ShouldEqual(EntityState.Modified);
                dbContext.ReloadAsync(entity).Wait();
                dbContext.Entry(entity).State.ShouldEqual(EntityState.Unchanged);
                entity.Description.ShouldEqual(description);
            }
        }