public async Task AllWithNullId()
        {
            ApplicationDbContext dbContext = new ApplicationDbContext(new DbContextOptionsBuilder <ApplicationDbContext>()
                                                                      .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options);
            var service = new EmployeesService(new EfDeletableEntityRepository <Employee>(dbContext));
            var date    = DateTime.UtcNow;

            for (int i = 1; i <= 5; i++)
            {
                await service.AddAsync(
                    "Ivan Ivanov" + i,
                    "*****@*****.**",
                    "08888888888",
                    date,
                    "1");
            }

            var results = service.All <EmployeeViewModel>(null);
            int count   = 0;

            foreach (var result in results)
            {
                count++;
            }

            Assert.Equal(0, count);
            dbContext.Dispose();
        }
        public async Task GetByIdWithVaidId()
        {
            ApplicationDbContext dbContext = new ApplicationDbContext(new DbContextOptionsBuilder <ApplicationDbContext>()
                                                                      .UseInMemoryDatabase(Guid.NewGuid().ToString()).Options);
            var service = new EmployeesService(new EfDeletableEntityRepository <Employee>(dbContext));
            var date    = DateTime.UtcNow;
            await service.AddAsync(
                "Ivan Ivanov",
                "*****@*****.**",
                "08888888888",
                date,
                "1");

            var employee = await dbContext.Employees.FirstOrDefaultAsync();

            var result = service.GetById <EmployeeViewModel>(employee.Id);

            Assert.Equal(employee.FullName, result.FullName);
            Assert.Equal(employee.Email, result.Email);
            Assert.Equal(employee.PhoneNumber, result.PhoneNumber);
            Assert.Equal(employee.StartWorkDate, result.StartWorkDate);
            Assert.Equal(employee.GamingHallId, result.GamingHallId);
            dbContext.Database.EnsureDeleted();
            dbContext.Dispose();
        }
        public async Task AllWithValidId()
        {
            ApplicationDbContext dbContext = new ApplicationDbContext(new DbContextOptionsBuilder <ApplicationDbContext>()
                                                                      .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options);
            var service = new EmployeesService(new EfDeletableEntityRepository <Employee>(dbContext));
            var date    = DateTime.UtcNow;

            for (int i = 1; i <= 5; i++)
            {
                await service.AddAsync(
                    "Ivan Ivanov" + i,
                    "*****@*****.**",
                    "08888888888",
                    date,
                    "1");
            }

            var results = service.All <EmployeeViewModel>("1");
            int count   = 0;

            foreach (var result in results)
            {
                count++;
                Assert.Equal("Ivan Ivanov" + count, result.FullName);
                Assert.Equal("*****@*****.**", result.Email);
                Assert.Equal("08888888888", result.PhoneNumber);
                Assert.Equal(date, result.StartWorkDate);
                Assert.Equal("1", result.GamingHallId);
            }

            Assert.Equal(5, count);
            dbContext.Database.EnsureDeleted();
            dbContext.Dispose();
        }
Beispiel #4
0
        public async Task AddAsync_AddsEntity()
        {
            //Assert

            //Act
            await testedService.AddAsync(newEntity);

            //Assert
            Mock.Verify(c => c.Add(newEntity), Times.Once());
            Mock.Verify(c => c.SaveChangesAsync(It.IsAny <CancellationToken>()), Times.Once());
        }
        public async Task GetByIdWithNullId()
        {
            ApplicationDbContext dbContext = new ApplicationDbContext(new DbContextOptionsBuilder <ApplicationDbContext>()
                                                                      .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options);
            var service = new EmployeesService(new EfDeletableEntityRepository <Employee>(dbContext));
            var date    = DateTime.UtcNow;
            await service.AddAsync(
                "Ivan Ivanov",
                "*****@*****.**",
                "08888888888",
                date,
                "1");

            var result = service.GetById <EmployeeViewModel>(null);

            Assert.True(result == null);
            dbContext.Database.EnsureDeleted();
            dbContext.Dispose();
        }
        public async Task GetHallIdWithValidId()
        {
            ApplicationDbContext dbContext = new ApplicationDbContext(new DbContextOptionsBuilder <ApplicationDbContext>()
                                                                      .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options);
            var service = new EmployeesService(new EfDeletableEntityRepository <Employee>(dbContext));
            var date    = DateTime.UtcNow;
            await service.AddAsync(
                "Ivan Ivanov",
                "*****@*****.**",
                "08888888888",
                date,
                "1");

            var employee = await dbContext.Employees.FirstOrDefaultAsync();

            var result = service.GetHallId(employee.Id);

            Assert.Equal("1", result);
            dbContext.Database.EnsureDeleted();
            dbContext.Dispose();
        }
        public async Task DeleteAsyncWithValidId()
        {
            ApplicationDbContext dbContext = new ApplicationDbContext(new DbContextOptionsBuilder <ApplicationDbContext>()
                                                                      .UseInMemoryDatabase(Guid.NewGuid().ToString()).Options);
            var service = new EmployeesService(new EfDeletableEntityRepository <Employee>(dbContext));
            var date    = DateTime.UtcNow;
            await service.AddAsync(
                "Ivan Ivanov",
                "*****@*****.**",
                "08888888888",
                date,
                "1");

            var employee = await dbContext.Employees.FirstOrDefaultAsync();

            await service.DeleteAsync(employee.Id);

            var result = await dbContext.GamingHalls.Where(x => x.Id == employee.Id).FirstOrDefaultAsync();

            Assert.True(result == null);
            dbContext.Database.EnsureDeleted();
            dbContext.Dispose();
        }
        public async Task ChangePhonenumberWithIncorectData()
        {
            ApplicationDbContext dbContext = new ApplicationDbContext(new DbContextOptionsBuilder <ApplicationDbContext>()
                                                                      .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options);
            var service = new EmployeesService(new EfDeletableEntityRepository <Employee>(dbContext));
            var date    = DateTime.UtcNow;
            await service.AddAsync(
                "Ivan Ivanov",
                "*****@*****.**",
                "08888888888",
                date,
                "1");

            var employee = await dbContext.Employees.FirstOrDefaultAsync();

            await service.ChangePhoneNumberAsync(employee.Id, "abvabv.bg");

            var result = await dbContext.Employees.FirstOrDefaultAsync();

            Assert.Equal(employee.PhoneNumber, result.PhoneNumber);
            dbContext.Database.EnsureDeleted();
            dbContext.Dispose();
        }
Beispiel #9
0
        public async Task ShouldAddEmployee()
        {
            var userId     = Guid.NewGuid();
            var employeeId = Guid.NewGuid();
            var company    = new Company();

            var employee = new Employee
            {
                Id           = employeeId,
                SecurityUser = new SecurityUser
                {
                    User = new User()
                }
            };

            var user = new User
            {
                SecurityUser = new SecurityUser
                {
                    Employee = new Employee()
                }
            };

            var expected = new EmployeeDTO
            {
                Id = employeeId
            };

            var employeeDTO = new EmployeeDTO();

            _mock.Mock <IUserCompanyProvider>()
            .Setup(provider => provider.GetAsync(userId))
            .ReturnsAsync(company);

            _mock.Mock <IPrepareEmployeeForAddingProvider>()
            .Setup(provider => provider.Prepare(It.IsAny <Employee>(), company))
            .Returns(employee);

            _mock.Mock <IEmployeeUserTransformer>()
            .Setup(provider => provider.Transform(employee))
            .Returns(user);

            _mock.Mock <IUserManager>()
            .Setup(provider => provider.CreateAsync(user))
            .ReturnsAsync(IdentityResult.Success)
            .Callback((User u) =>
            {
                u.Id = userId;
                u.SecurityUser.Employee.Id = employeeId;
                employee.Id = employeeId;
                employee.SecurityUser.User.Id = userId;
            });

            _mock.Mock <IEmployeeUserTransformer>()
            .Setup(provider => provider.Transform(user))
            .Returns(employee);

            _mock.Mock <IMapper>()
            .Setup(mapper => mapper.Map <Employee, EmployeeDTO>(employee))
            .Returns(expected);

            var actual = await _service.AddAsync(employeeDTO, userId);

            _mock.Mock <IUserManager>()
            .Verify(manager => manager.CreateAsync(user), Times.Once);

            _mock.Mock <IRolesService>()
            .Verify(service => service.SetIsAdminStateAsync(user, employeeDTO.IsAdmin), Times.Once);

            _mock.Mock <IInventContext>()
            .Verify(context => context.Attach(user), Times.Once);

            ContentAssert.AreEqual(expected, actual);
        }