예제 #1
0
        public void Create_ShouldAdd_CorrectEntityToDb()
        {
            var options = new DbContextOptionsBuilder <TasksDbContext>()
                          .UseInMemoryDatabase(databaseName: "Get_create_group")
                          .Options;

            using (var context = new TasksDbContext(options))
            {
                var taskGroupManager = new TaskGroupDbManager(context, _mapper);
                taskGroupManager.Create(new TaskGroupViewModel
                {
                    Name      = "TestGroup",
                    UserTasks = new List <UserTaskViewModel> {
                        new UserTaskViewModel {
                            Name = "TestTask1"
                        }, new UserTaskViewModel {
                            Name = "TestTask2"
                        }
                    }
                });
            }

            using (var context = new TasksDbContext(options))
            {
                Assert.That(context.TaskGroups.Count(), Is.EqualTo(1));
                Assert.That(context.UserTasks.Count(), Is.EqualTo(2));

                var result = context.TaskGroups.Include(g => g.UserTasks).First();
                Assert.That(result.Name, Is.EqualTo("TestGroup"));
                Assert.That(result.UserTasks.Count, Is.EqualTo(2));
                Assert.That(result.UserTasks.ToList().Exists(t => t.Name == "TestTask1" || t.Name == "TestTask2"), Is.True);

                context.Database.EnsureDeleted();
            }
        }
예제 #2
0
        public void GetById_ShouldReturn_CorrectGroupFromDb()
        {
            var options = new DbContextOptionsBuilder <TasksDbContext>()
                          .UseInMemoryDatabase(databaseName: "Get_group_by_id")
                          .Options;

            using (var context = new TasksDbContext(options))
            {
                context.TaskGroups.AddRange(new TaskGroupEntity
                {
                    Name      = "TestGroup1",
                    UserTasks = new List <UserTaskEntity> {
                        new UserTaskEntity {
                            Name = "TestTask"
                        }
                    }
                }, new TaskGroupEntity
                {
                    Name = "TestGroup2"
                });
                context.SaveChanges();
            }

            using (var context = new TasksDbContext(options))
            {
                var taskGroupManager = new TaskGroupDbManager(context, _mapper);
                var result           = taskGroupManager.Get(1);

                Assert.That(result.Id, Is.EqualTo(1));
                Assert.That(result.Name, Is.EqualTo("TestGroup1"));
                Assert.That(result.UserTasks.ToList().Exists(t => t.Name == "TestTask"), Is.True);

                context.Database.EnsureDeleted();
            }
        }
예제 #3
0
        public void Delete_ShouldRemove_EntityFromDb()
        {
            var options = new DbContextOptionsBuilder <TasksDbContext>()
                          .UseInMemoryDatabase(databaseName: "Get_all_groups")
                          .Options;

            using (var context = new TasksDbContext(options))
            {
                context.TaskGroups.AddRange(new TaskGroupEntity
                {
                    Name      = "TestGroup1",
                    UserTasks = new List <UserTaskEntity> {
                        new UserTaskEntity {
                            Name = "TestTask"
                        }
                    }
                });
                context.SaveChanges();

                var groupsDbManger = new TaskGroupDbManager(context, _mapper);
                groupsDbManger.Remove(1);
            }

            using (var context = new TasksDbContext(options))
            {
                Assert.That(context.TaskGroups.Count(), Is.EqualTo(0));
                Assert.That(context.UserTasks.Count(), Is.EqualTo(0));

                context.Database.EnsureDeleted();
            }
        }
예제 #4
0
        public void Get_ShouldReturn_AllGroupsFromDb()
        {
            var options = new DbContextOptionsBuilder <TasksDbContext>()
                          .UseInMemoryDatabase(databaseName: "Get_all_groups")
                          .Options;

            using (var context = new TasksDbContext(options))
            {
                context.TaskGroups.AddRange(new TaskGroupEntity
                {
                    Name      = "TestGroup1",
                    UserTasks = new List <UserTaskEntity> {
                        new UserTaskEntity {
                            Name = "TestTask"
                        }
                    }
                }, new TaskGroupEntity
                {
                    Name = "TestGroup2"
                });
                context.SaveChanges();
            }

            using (var context = new TasksDbContext(options))
            {
                var taskGroupManager = new TaskGroupDbManager(context, _mapper);
                var result           = taskGroupManager.Get();

                Assert.That(result.Count, Is.EqualTo(2));
                Assert.That(result.Exists(group => group.Name == "TestGroup1" && group.Id == 1 && group.UserTasks.Count() == 1), Is.True);
                Assert.That(result.Exists(group => group.Name == "TestGroup2" && group.Id == 2 && !group.UserTasks.Any()), Is.True);

                context.Database.EnsureDeleted();
            }
        }
예제 #5
0
        public void Update_ShouldSet_CorrectValuesOnEntity()
        {
            var options = new DbContextOptionsBuilder <TasksDbContext>()
                          .UseInMemoryDatabase(databaseName: "Update_group")
                          .Options;

            using (var context = new TasksDbContext(options))
            {
                context.TaskGroups.Add(new TaskGroupEntity
                {
                    Name      = "TestGroup1",
                    UserTasks = new List <UserTaskEntity> {
                        new UserTaskEntity {
                            Name = "TestTask"
                        }
                    }
                });
                context.SaveChanges();

                var groupsDbManger = new TaskGroupDbManager(context, _mapper);
                groupsDbManger.Update(1, new TaskGroupViewModel
                {
                    Id        = 1,
                    Name      = "UpdatedGroup",
                    UserTasks = new List <UserTaskViewModel>
                    {
                        new UserTaskViewModel {
                            Name = "TestTask1"
                        }, new UserTaskViewModel {
                            Name = "TestTask2"
                        }
                    }
                });
            }

            using (var context = new TasksDbContext(options))
            {
                Assert.That(context.TaskGroups.Count(), Is.EqualTo(1));
                Assert.That(context.UserTasks.Count(), Is.EqualTo(2));

                var result = context.TaskGroups.Include(g => g.UserTasks).First();
                Assert.That(result.Id, Is.EqualTo(1));
                Assert.That(result.Name, Is.EqualTo("UpdatedGroup"));
                Assert.That(result.UserTasks.ToList().Exists(t => t.Name == "TestTask1" || t.Name == "TestTask2"), Is.True);

                context.Database.EnsureDeleted();
            }
        }