Example #1
0
        public IActionResult addUser([FromBody] User _user)
        {
            string JsonData;
            string id        = _user.UserId;
            User   checkUser = _context.User.Find(_user.UserId);

            if (checkUser == null)
            {
                _context.User.Add(_user);
                _context.SaveChanges();
                var JsonResult = new
                {
                    result = "Success"
                };
                JsonData = JsonConvert.SerializeObject(JsonResult);
                return(Ok(JsonData));
            }
            else
            {
                var JsonResult = new
                {
                    result = "Failed"
                };
                JsonData = JsonConvert.SerializeObject(JsonResult);
                return(Ok(JsonData));
            }
        }
Example #2
0
        public void Update_UpdatesUser_UserIsUpdated()
        {
            //Arrange
            DbContextOptions <TestDatabaseContext> options = new DbContextOptionsBuilder <TestDatabaseContext>()
                                                             .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                                                             .Options;
            TestDatabaseContext context = new TestDatabaseContext(options);

            context.Users.Add(new User()
            {
                FirstName = "Jan",
                LastName  = "Kowalski",
            });
            context.SaveChanges();

            UserLogic userLogic = new UserLogic(context);

            //Act
            User user = userLogic.Read(1);

            user.FirstName = "Janusz";
            userLogic.Update(user);

            //Assert
            userLogic.Read(1).FirstName.Should().Be("Janusz");
        }
Example #3
0
        public void Create_CreatesUser_UserIsCreated()
        {
            //Arrange
            DbContextOptions <TestDatabaseContext> options = new DbContextOptionsBuilder <TestDatabaseContext>()
                                                             .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                                                             .Options;
            TestDatabaseContext context = new TestDatabaseContext(options);

            UserLogic userLogic = new UserLogic(context);

            User user1 = new User()
            {
                FirstName = "Jan",
                LastName  = "Kowalski",
            };
            User user2 = new User()
            {
                FirstName = "John",
                LastName  = "Smith",
            };

            //Act
            userLogic.Create(user1);
            userLogic.Create(user2);
            context.SaveChanges();

            //Assert
            context.Users.Where(user => user.ID == 1).First().Should().Be(user1);
            context.Users.Where(user => user.ID == 2).First().Should().Be(user2);
        }
Example #4
0
        public void Read_ReadsUser_UserIsLoaded()
        {
            //Arrange
            DbContextOptions <TestDatabaseContext> options = new DbContextOptionsBuilder <TestDatabaseContext>()
                                                             .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                                                             .Options;
            TestDatabaseContext context = new TestDatabaseContext(options);

            UserLogic userLogic = new UserLogic(context);

            User user1 = new User()
            {
                FirstName = "Jan",
                LastName  = "Kowalski",
            };
            User user2 = new User()
            {
                FirstName = "John",
                LastName  = "Smith",
            };

            context.Users.AddRange(user1, user2);
            context.SaveChanges();

            //Act, Assert
            userLogic.Read(1).Should().Be(user1);
            userLogic.Read(2).Should().Be(user2);
        }
Example #5
0
        public void DeleteUser_OnNonExistingUser_DoesNothing()
        {
            // ARRANGE
            DbContextOptions <TestDatabaseContext> options = new DbContextOptionsBuilder <TestDatabaseContext>()
                                                             .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                                                             .Options;
            IDatabaseContext dbContext  = new TestDatabaseContext(options);
            UserRepository   repository = new UserRepository(dbContext);
            User             user       = new User()
            {
                FirstName = "Jan",
                LastName  = "Kowalski",
                OAuthID   = "12345"
            };

            dbContext.Users.Add(user);

            dbContext.SaveChanges();

            // ACT
            repository.DeleteUser(22);

            // ASSERT
            dbContext.Users.Count().Should().Be(1);
        }
Example #6
0
        public void GetUserOAuth_OnExistingUser_ReturnsUser()
        {
            // ARRANGE
            DbContextOptions <TestDatabaseContext> options = new DbContextOptionsBuilder <TestDatabaseContext>()
                                                             .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                                                             .Options;
            IDatabaseContext dbContext  = new TestDatabaseContext(options);
            UserRepository   repository = new UserRepository(dbContext);
            User             user       = new User()
            {
                FirstName = "Jan",
                LastName  = "Kowalski",
                OAuthID   = "12345"
            };

            dbContext.Users.Add(user);

            dbContext.SaveChanges();

            // ACT
            UserPublic userPublic = repository.GetUserOAuth("12345");

            // ASSERT
            userPublic.Should().BeEquivalentTo(new UserPublic()
            {
                ID        = 1,
                FirstName = "Jan",
                LastName  = "Kowalski",
                OAuthID   = "12345"
            });
        }
Example #7
0
        public void Convert_ConvertsExistingServiceModelToEntity_ReturnsConvertedEntity()
        {
            // ARRANGE
            DbContextOptions <TestDatabaseContext> options = new DbContextOptionsBuilder <TestDatabaseContext>()
                                                             .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                                                             .Options;
            IDatabaseContext dbContext = new TestDatabaseContext(options);

            User user = new User()
            {
                ID        = 1,
                FirstName = "Johhny",
                LastName  = "Test",
                OAuthID   = "12345"
            };

            dbContext.Users.Add(user);
            dbContext.SaveChanges();

            UserConverter converter  = new UserConverter(dbContext);
            UserPublic    userPublic = new UserPublic()
            {
                ID        = 1,
                FirstName = "Johhny",
                LastName  = "Test",
                OAuthID   = "12345"
            };

            // ACT
            User user2 = converter.Convert(userPublic);

            // ASSERT
            user2.Should().BeEquivalentTo <User>(user);
        }
Example #8
0
        public void GetWorkItem_OnValidRequest_ReturnsWorkItem()
        {
            // ARRANGE
            DbContextOptions <TestDatabaseContext> options = new DbContextOptionsBuilder <TestDatabaseContext>()
                                                             .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                                                             .Options;
            IDatabaseContext   dbContext  = new TestDatabaseContext(options);
            WorkItemRepository repository = new WorkItemRepository(dbContext);
            WorkItemConverter  converter  = new WorkItemConverter(dbContext);

            WorkItem workItem = new WorkItem()
            {
                Description          = Guid.NewGuid().ToString(),
                Name                 = Guid.NewGuid().ToString(),
                WorkItemEnd          = DateTime.Now,
                WorkItemStart        = DateTime.Now,
                WorkItemCollectionId = 1
            };

            dbContext.WorkItems.Add(workItem);

            dbContext.SaveChanges();

            // ACT
            WorkItemPublic result = repository.GetWorkItem(1);

            // ASSERT
            result.Should().BeEquivalentTo <WorkItemPublic>(converter.Convert(workItem));
        }
Example #9
0
        public void CreateWorkItem_OnWalidWorkItem_CreatesEntryInDB()
        {
            // ARRANGE
            DbContextOptions <TestDatabaseContext> options = new DbContextOptionsBuilder <TestDatabaseContext>()
                                                             .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                                                             .Options;
            IDatabaseContext context = new TestDatabaseContext(options);
            WorkItemLogic    logic   = new WorkItemLogic(context);
            WorkItem         request = new WorkItem()
            {
                Name                 = Guid.NewGuid().ToString(),
                Color                = Guid.NewGuid().ToString(),
                Description          = Guid.NewGuid().ToString(),
                WorkItemCollectionId = 1,
                WorkItemStart        = DateTime.Now.AddDays(-1),
                WorkItemEnd          = DateTime.Now
            };

            context.WorkItemCollections.Add(new WorkItemCollection()
            {
                ID          = 1,
                TaskBoardId = 1
            });

            context.TaskBoards.Add(new TaskBoard()
            {
                ID     = 1,
                UserID = 1
            });

            context.Users.Add(new User()
            {
                ID = 1
            });

            context.SaveChanges();

            // ACT
            WorkItem result = logic.CreateWorkItem(request);

            context.SaveChanges();

            request.ID = result.ID;

            // ASSERT
            result.Should().BeEquivalentTo <WorkItem>(request);
        }
Example #10
0
        public ActionResult Create([Bind(Include = "TodoTitle,DueDate,Priority")] Todo todo)
        {
            if (ModelState.IsValid)
            {
                db.Todoes.Add(todo);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(todo));
        }
Example #11
0
        public Customer Save(Customer entity)
        {
            var customerDb = CustomerMapper.Map(entity);

            _testDatabaseContext.Customer.Add(customerDb);

            _testDatabaseContext.SaveChanges();

            entity = CustomerMapper.Map(customerDb);

            return(entity);
        }
Example #12
0
        public void DeleteWorkItem_OnValidRequest_DeletesEntry()
        {
            // ARRANGE
            DbContextOptions <TestDatabaseContext> options = new DbContextOptionsBuilder <TestDatabaseContext>()
                                                             .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                                                             .Options;
            IDatabaseContext context = new TestDatabaseContext(options);
            WorkItemLogic    logic   = new WorkItemLogic(context);

            WorkItem request = new WorkItem()
            {
                ID = 1,
            };

            context.WorkItems.Add(request);
            context.SaveChanges();

            // ACT
            logic.DeleteWorkItem(1);
            context.SaveChanges();

            // ASSERT
            context.WorkItems.Any(wi => wi.ID == 1).Should().BeFalse();
        }
Example #13
0
        public void UpdateWorkItem_OnValidRequest_UpdatesWorkItem()
        {
            // ARRANGE
            DbContextOptions <TestDatabaseContext> options = new DbContextOptionsBuilder <TestDatabaseContext>()
                                                             .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                                                             .Options;
            IDatabaseContext   dbContext  = new TestDatabaseContext(options);
            WorkItemRepository repository = new WorkItemRepository(dbContext);
            WorkItemPublic     workItem   = new WorkItemPublic()
            {
                ID                   = 1,
                Description          = Guid.NewGuid().ToString(),
                Name                 = Guid.NewGuid().ToString(),
                WorkItemEnd          = DateTime.Now,
                WorkItemStart        = DateTime.Now,
                WorkItemCollectionID = 1
            };

            dbContext.WorkItems.Add(new WorkItem()
            {
                ID = 1
            });

            dbContext.WorkItemCollections.Add(new WorkItemCollection()
            {
                ID          = 1,
                TaskBoardId = 1
            });

            dbContext.TaskBoards.Add(new TaskBoard()
            {
                ID     = 1,
                UserID = 1
            });

            dbContext.Users.Add(new User()
            {
                ID = 1
            });

            dbContext.SaveChanges();

            // ACT
            WorkItemPublic result = repository.UpdateWorkItem(workItem);

            // ASSERT
            result.Should().BeEquivalentTo <WorkItemPublic>(workItem);
        }
Example #14
0
        private static void CorrectLength(TestDatabaseContext ctxBabies)
        {
            ctxBabies.Babies.ToList().ForEach(b =>
            {
                if (b.Length < 10)
                {
                    b.Length = 10;
                }

                if (b.Length > 60)
                {
                    b.Length = 60;
                }
            });
            ctxBabies.SaveChanges();
        }
Example #15
0
        public void GetUserTaskBoards_OnRequestingUsersTaskBoard_ReturnsOnlyUsersTaskBoards()
        {
            // ARRANGE
            DbContextOptions <TestDatabaseContext> options = new DbContextOptionsBuilder <TestDatabaseContext>()
                                                             .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                                                             .Options;
            IDatabaseContext    dbContext  = new TestDatabaseContext(options);
            TaskBoardRepository repository = new TaskBoardRepository(dbContext);

            User user1 = dbContext.Users.Add(new User()
            {
                FirstName = Guid.NewGuid().ToString(),
                LastName  = Guid.NewGuid().ToString()
            }).Entity;

            User user2 = dbContext.Users.Add(new User()
            {
                FirstName = Guid.NewGuid().ToString(),
                LastName  = Guid.NewGuid().ToString()
            }).Entity;

            dbContext.TaskBoards.Add(new TaskBoard()
            {
                Name = Guid.NewGuid().ToString(),
                User = user1
            });

            dbContext.TaskBoards.Add(new TaskBoard()
            {
                Name = Guid.NewGuid().ToString(),
                User = user1
            });

            dbContext.TaskBoards.Add(new TaskBoard()
            {
                Name = Guid.NewGuid().ToString(),
                User = user2
            });

            dbContext.SaveChanges();

            // ACT
            IEnumerable <TaskBoardPublic> boards = repository.GetUserTaskBoards(1);

            // ASSERT
            boards.Should().HaveCount(2);
        }
Example #16
0
        public void CreateWorkItem_OnNonExistingCollection_ThrowsArgumentException()
        {
            // ARRANGE
            DbContextOptions <TestDatabaseContext> options = new DbContextOptionsBuilder <TestDatabaseContext>()
                                                             .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                                                             .Options;
            IDatabaseContext context = new TestDatabaseContext(options);
            WorkItemLogic    logic   = new WorkItemLogic(context);
            WorkItem         request = new WorkItem()
            {
                WorkItemCollectionId = 1
            };

            context.SaveChanges();

            // ACT
            Action act = () => logic.CreateWorkItem(request);

            // ASSERT
            act.Should().Throw <ArgumentException>().Where(e => e.Message.Contains("doesn't exist"));
        }
Example #17
0
        public void CreateWorkItem_OnInvalidID_ThrowsArgumentException()
        {
            // ARRANGE
            DbContextOptions <TestDatabaseContext> options = new DbContextOptionsBuilder <TestDatabaseContext>()
                                                             .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                                                             .Options;
            IDatabaseContext context = new TestDatabaseContext(options);
            WorkItemLogic    logic   = new WorkItemLogic(context);
            WorkItem         request = new WorkItem()
            {
                ID = 123
            };

            context.SaveChanges();

            // ACT
            Action act = () => logic.CreateWorkItem(request);

            // ASSERT
            act.Should().Throw <ArgumentException>().Where(e => e.Message.Contains("ID can't be set on entity"));
        }
Example #18
0
        public void Convert_ConvertsExistingServiceModelToEntity_ReturnsConvertedEntity()
        {
            // ARRANGE
            DbContextOptions <TestDatabaseContext> options = new DbContextOptionsBuilder <TestDatabaseContext>()
                                                             .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                                                             .Options;
            IDatabaseContext dbContext = new TestDatabaseContext(options);

            User user = new User()
            {
                ID        = 1,
                FirstName = Guid.NewGuid().ToString(),
                LastName  = Guid.NewGuid().ToString()
            };

            dbContext.TaskBoards.Add(new TaskBoard()
            {
                ID = 1
            });
            dbContext.Users.Add(user);
            dbContext.SaveChanges();

            TaskBoardConverter converter       = new TaskBoardConverter(dbContext);
            TaskBoardPublic    taskBoardPublic = new TaskBoardPublic()
            {
                ID     = 1,
                Name   = "Test",
                UserID = 1
            };

            // ACT
            TaskBoard taskBoard = converter.Convert(taskBoardPublic);

            // ASSERT
            taskBoard.Should().BeEquivalentTo <TaskBoard>(new TaskBoard()
            {
                ID = 1, Name = "Test", UserID = 1, User = user
            });
            taskBoard.User.Should().NotBeNull();
        }
Example #19
0
        public void DeleteWorkItem_OnValidRequest_DeletesWorkItem()
        {
            // ARRANGE
            DbContextOptions <TestDatabaseContext> options = new DbContextOptionsBuilder <TestDatabaseContext>()
                                                             .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                                                             .Options;
            IDatabaseContext   dbContext  = new TestDatabaseContext(options);
            WorkItemRepository repository = new WorkItemRepository(dbContext);

            dbContext.WorkItems.Add(new WorkItem()
            {
                ID = 1
            });

            dbContext.SaveChanges();

            // ACT
            repository.DeleteWorkItem(1);

            // ASSERT
            dbContext.WorkItems.Any(wi => wi.ID == 1).Should().BeFalse();
        }
Example #20
0
        public void GetTaskBoard_OnExistingTaskBoard_ReturnsTaskBoard()
        {
            // ARRANGE
            DbContextOptions <TestDatabaseContext> options = new DbContextOptionsBuilder <TestDatabaseContext>()
                                                             .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                                                             .Options;
            IDatabaseContext    dbContext  = new TestDatabaseContext(options);
            TaskBoardRepository repository = new TaskBoardRepository(dbContext);
            TaskBoardConverter  converter  = new TaskBoardConverter(dbContext);
            TaskBoard           taskBoard  = new TaskBoard()
            {
                Name = Guid.NewGuid().ToString(),
            };

            dbContext.TaskBoards.Add(taskBoard);

            dbContext.SaveChanges();

            // ACT
            TaskBoardPublic taskBoardPublic = repository.GetTaskBoard(1);

            // ASSERT
            taskBoardPublic.Should().BeEquivalentTo <TaskBoardPublic>(converter.Convert(taskBoard));
        }