Example #1
0
        public async Task <FigureRecord> SaveAsync(FigureRecord figure)
        {
            _dbContext.Figures.Add(figure);
            await _dbContext.SaveChangesAsync();

            return(figure);
        }
Example #2
0
        public async Task GetTriangleArea()
        {
            //arrange
            var id     = ++_currentId;
            var record = new FigureRecord()
            {
                Id     = id,
                Type   = FigureType.Triangle,
                Params = JsonConvert.SerializeObject(new Triangle(8.8, 12.2, 18.0)),
            };

            _records.Add(record);

            //act
            var actionResult = await _controller.GetFigureAsync(id);

            //assert
            var response = actionResult.Should().BeOfType <OkObjectResult>()
                           .Subject.Value.Should().BeOfType <FigureAreaResponce>()
                           .Subject;

            response.Should().NotBeNull();
            response.Type.Should().Be(record.Type.ToString());
            response.Id.Should().Be(record.Id);
            response.Area.Should().Be(47.79871860207133);
        }
Example #3
0
 private IFigure DeserializeToFigure(FigureRecord record)
 {
     return(record.Type switch
     {
         FigureType.Circle => JsonConvert.DeserializeObject <Circle>(record.Params),
         FigureType.Triangle => JsonConvert.DeserializeObject <Triangle>(record.Params),
         _ => throw new InvalidFigureTypeException(record.Type.ToString()),
     });
Example #4
0
        public async Task GetFigureAreaAsyncWithInvalidType_ShouldThrowInvalidFigureTypeException()
        {
            //arrange
            var id     = ++_currentId;
            var record = new FigureRecord()
            {
                Id   = id,
                Type = FigureType.None
            };

            _records.Add(record);

            //act & assert
            await Assert.ThrowsAsync <InvalidFigureTypeException>(async() => await _controller.GetFigureAsync(id));
        }
Example #5
0
        public async Task GetFigureAreaAsyncWithInvalidParams_ShouldThrowCorruptedFigureException()
        {
            //arrange
            var id     = _currentId;
            var record = new FigureRecord()
            {
                Id     = id,
                Type   = FigureType.Triangle,
                Params = JsonConvert.SerializeObject(new Triangle(1, 4, 5)),
            };

            _records.Add(record);

            //act & assert
            await Assert.ThrowsAsync <CorruptedFigureException>(async() => await _controller.GetFigureAsync(id));
        }