示例#1
0
        public void MethodPost_Fail()
        {
            lock (_syncLock)
            {
                // Arrange
                _logger.LogInformation($"test MethodPost_Fail()");
                CalculationsController controller = BuildController(false);

                try
                {
                    int i = 0;
                    foreach (var op in TD_MethodPost_Fail)
                    {
                        _logger.LogInformation($"TD[{i}] = {JsonConvert.SerializeObject(op)}");
                        i++;

                        // Act
                        var actual = controller.Post(op.DtoIn);

                        // Assert
                        AssertActionResult <Dto.CResultDto>(op.expectedData, op.expectedCode, actual);                        // 400 Bad Request
                    }
                }
                finally
                {
                    var actual = controller.Delete();
                }
            }
        }
        public void HistoryReturnsBadRequestWhenListIsNull()
        {
            var controller = new CalculationsController(_mockLogger.Object, _mockRepository.Object, _mockCalculator.Object, _mockMapper.Object, _mockDbContext.Object);

            var result = controller.History().Result;

            Assert.IsType <NotFoundResult>(result);
        }
        public void DeleteReturnsBadRequestResultWhenIdIsNull()
        {
            var controller = new CalculationsController(_mockLogger.Object, _mockRepository.Object, _mockCalculator.Object, _mockMapper.Object, _mockDbContext.Object);

            var result = controller.Delete(null).Result;

            Assert.IsType <BadRequestResult>(result);
        }
        public void GetByIdReturnsBadRequestResultWhenIdIsNull()
        {
            var controller = new CalculationsController(_mockLogger.Object, _mockRepository.Object, _mockCalculator.Object, _mockMapper.Object, _mockDbContext.Object);

            var result = controller.GetById(null).Result;

            Assert.IsType <ActionResult <CalculationModel> >(result);
        }
        public void UpdateReturnsResultWithInvalidCalculationModel()
        {
            var controller = new CalculationsController(_mockLogger.Object, _mockRepository.Object, _mockCalculator.Object, _mockMapper.Object, _mockDbContext.Object);

            controller.ModelState.AddModelError("Type", "Required");
            var modifiedCalculation = new CalculationModel();

            var result = controller.Update(modifiedCalculation.Id, modifiedCalculation).Result;

            Assert.IsType <UnprocessableEntityResult>(result);
        }
 public CalculationsControllerTests()
 {
     _options                = new DbContextOptions <CalculatorDbContext>();
     _mockMapper             = new Mock <IMapper>();
     _mockCalculator         = new Mock <ICalculator>();
     _mockDbContext          = new Mock <CalculatorDbContext>(_options);
     _mockRepository         = new Mock <ICalculationRepository>();
     _mockLogger             = new Mock <ILogger <CalculationsController> >();
     _listOfCalculations     = new List <CalculationModel>();
     _calculationsController = new CalculationsController(_mockLogger.Object, _mockRepository.Object, _mockCalculator.Object, _mockMapper.Object, _mockDbContext.Object);
 }
        public void DeleteReturnsNotFoundResultWhenCalculationNotFound()
        {
            int testCalcId = 8;

            _mockRepository.Setup(rep => rep.DeleteAsync(testCalcId))
            .Returns(null as Task <Calculation>);
            var controller = new CalculationsController(_mockLogger.Object, _mockRepository.Object, _mockCalculator.Object, _mockMapper.Object, _mockDbContext.Object);

            var result = controller.Delete(testCalcId).Result;

            Assert.IsType <NotFoundResult>(result);
        }
        public void GetByIdReturnsNotFoundResultWhenCalculationNotFound()
        {
            int testCalcId = 6555;

            _mockRepository.Setup(rep => rep.FindByIdAsync(testCalcId))
            .Returns(null as Task <Calculation>);
            var controller = new CalculationsController(_mockLogger.Object, _mockRepository.Object, _mockCalculator.Object, _mockMapper.Object, _mockDbContext.Object);

            var result = controller.GetById(testCalcId);

            Assert.IsType <Task <ActionResult <CalculationModel> > >(result);
        }
        public void UpdateCalculationReturnsNotFound()
        {
            int testUserId = 65555;

            _mockRepository.Setup(rep => rep.FindByIdAsync(testUserId))
            .Returns(null as Task <Calculation>);
            var controller          = new CalculationsController(_mockLogger.Object, _mockRepository.Object, _mockCalculator.Object, _mockMapper.Object, _mockDbContext.Object);
            var modifiedCalculation = new CalculationModel();

            var result = controller.Update(testUserId, modifiedCalculation).Result;

            Assert.IsType <NotFoundResult>(result);
        }
        public void HistoryReturnsResultWithListOfCalculations()
        {
            // Arrange
            var controller = new CalculationsController(_mockLogger.Object, _mockRepository.Object, _mockCalculator.Object, _mockMapper.Object, _mockDbContext.Object);

            // Act
            var result = controller.History().Result;

            // Assert
            var actionResult = Assert.IsType <ActionResult <IEnumerable <CalculationModel> > >(result);

            Assert.Equal(GetTestCalculations().Count(), actionResult.Value.Count());
        }
        public void DeleteReturnsResultWithCalculation()
        {
            var testUserId  = 1;
            var calculation = GetTestCalculations().FirstOrDefault(c => c.Id == testUserId);

            _mockRepository.Setup(rep => rep.FindByIdAsync(testUserId).Result)
            .Returns(_mockMapper.Object.Map <Calculation>(calculation));

            var controller = new CalculationsController(_mockLogger.Object, _mockRepository.Object, _mockCalculator.Object, _mockMapper.Object, _mockDbContext.Object);

            var result = controller.Delete(testUserId).Result;

            Assert.IsType <NoContentResult>(result);
        }
示例#12
0
        public void MethodGet_Fail()
        {
            lock (_syncLock)
            {
                // Arrange
                _logger.LogInformation($"test MethodGet_Fail()");
                CalculationsController controller = BuildController(true);

                try
                {
                    /*
                     *                      CPostTestData[] TD_MethodGet_Success_Repo = new CPostTestData[TD_MethodPost_Success.Count()*2];
                     *                      TD_MethodPost_Success.CopyTo(TD_MethodGet_Success_Repo,0);
                     *                      TD_MethodPost_Success.CopyTo(TD_MethodGet_Success_Repo, TD_MethodPost_Success.Count());
                     */
                    int i0 = 0;
                    foreach (var op in TD_MethodPost_Success)
                    {
                        // Arrange
                        var    actual = controller.Post(op.DtoIn);
                        int    iRet   = (actual as ObjectResult).StatusCode.Value;
                        string sCode  = $"HTTP response {iRet} {(System.Net.HttpStatusCode)iRet}";
                        string Mes    = (200 == iRet) ? (sCode) : ("[[ERROR!]] " + sCode + (System.Net.HttpStatusCode)iRet);
                        _logger.LogInformation($"Arrange[{i0}] = {op.expectedData}, {Mes}");
                        i0++;
                    }

                    int i = 0;
                    foreach (var op in TD_MethodGet_Fail)
                    {
                        // Arrange
                        _logger.LogInformation($"TD[{i}] = {JsonConvert.SerializeObject(op)}");
                        i++;

                        // Act
                        var actual = controller.Get(op.DtoIn.actions, op.DtoIn.offset, op.DtoIn.fetch);

                        // Assert
                        AssertActionResult <Dto.COperationOutputDto[]>(op.expectedData, op.expectedCode, actual, arr =>
                        {
                            return(JsonConvert.SerializeObject(arr.Select(item => item.result).ToArray()));
                        });                          // 400
                    }
                }
                finally
                {
                    var actual = controller.Delete();
                }
            }
        }
示例#13
0
        public void MethodDelete_Success()
        {
            lock (_syncLock)
            {
                // Arrange
                _logger.LogInformation($"test MethodDelete_Success()");
                CalculationsController controller = BuildController(false);

                // Act
                var actual = controller.Delete();

                // Assert
                AssertActionResult <IActionResult>(null, 202, actual);                // 202 Accepted
            }
        }
        public void GetByIdReturnsResultWithCalculation()
        {
            var testUserId  = 1;
            var calculation = GetTestCalculations().FirstOrDefault(c => c.Id == testUserId);

            _mockRepository.Setup(rep => rep.FindByIdAsync(testUserId).Result)
            .Returns(_mockMapper.Object.Map <Calculation>(calculation));
            var controller = new CalculationsController(_mockLogger.Object, _mockRepository.Object, _mockCalculator.Object, _mockMapper.Object, _mockDbContext.Object);

            var result = controller.GetById(testUserId);

            var actionResult = Assert.IsType <Task <ActionResult <CalculationModel> > >(result);
            var model        = Assert.IsType <Task <Calculation> >(actionResult.Result.Value);

            Assert.Equal(testUserId, model.Result.Id);
            Assert.Equal("Add", model.Result.Type);
            Assert.Equal("3 + 5", model.Result.Expression);
            Assert.Equal(8, model.Result.Result);
        }
        public void CreateReturnsCreatedAtActionAndAddsCalculation()
        {
            var controller     = new CalculationsController(_mockLogger.Object, _mockRepository.Object, _mockCalculator.Object, _mockMapper.Object, _mockDbContext.Object);
            var newCalculation = new CalculationModel()
            {
                Type       = "Add",
                Expression = "88 + 11",
                CreateDate = DateTime.Now,
                Result     = 99
            };

            var result = controller.Create(newCalculation, newCalculation.Expression).Result;

            var createdAtActionResult = Assert.IsType <CreatedAtActionResult>(result);

            Assert.Null(createdAtActionResult.ControllerName);
            Assert.Equal("GetById", createdAtActionResult.ActionName);
            _mockRepository.Verify(r => r
                                   .CreateAsync(_mockMapper.Object.Map <Calculation>(newCalculation)));
        }
示例#16
0
        private CalculationsController BuildController(bool NeedEmptyDB)
        {
            IOperationRepo repo;

            if (_inmemoryTable)
            {
                repo = new OperationMemRepo("", _loggerFactory.CreateLogger <OperationMemRepo>());
            }
            else
            {
                repo = new OperationBdRepo(_connection, _loggerFactory.CreateLogger <OperationBdRepo>());
            }

            CalculationsController controller = new CalculationsController(repo, _loggerFactory.CreateLogger <CalculationsController>());

            if (NeedEmptyDB)
            {
                controller.Delete();
            }

            return(controller);
        }
示例#17
0
        public void MethodGet_EmptyRepo()
        {
            lock (_syncLock)
            {
                // Arrange
                _logger.LogInformation($"test MethodGet_EmptyRepo()");
                CalculationsController controller = BuildController(true);

                int i = 0;
                foreach (var op in TD_MethodGet_EmptyRepo)
                {
                    _logger.LogInformation($"TD[{i}] = {JsonConvert.SerializeObject(op)}");
                    i++;

                    // Act
                    var actual = controller.Get(op.DtoIn.actions, op.DtoIn.offset, op.DtoIn.fetch);

                    // Assert
                    AssertActionResult <Dto.CResultDto>(op.expectedData, op.expectedCode, actual);                    // 200 OK
                }
            }
        }
 public AdditionCalculationTests()
 {
     _calculator = new CalculationsController();
 }
 public CalculationsControllerUnitTest()
 {
     _controller = new CalculationsController();
 }
示例#20
0
 public MultiplicationCalculationTests()
 {
     _calculator = new CalculationsController();
 }
 public DivisionCalculationTests()
 {
     _calculator = new CalculationsController();
 }