コード例 #1
0
        public async void TestForCreatePlay()
        {
            var options = new DbContextOptionsBuilder <PlaybookContext>()
                          .UseInMemoryDatabase(databaseName: "p3PlaybookService")
                          .Options;

            using (var context = new PlaybookContext(options))
            {
                context.Database.EnsureDeleted();
                context.Database.EnsureCreated();

                Repo               r                  = new Repo(context, new NullLogger <Repo>());
                Mapper             mapper             = new Mapper();
                Logic              logic              = new Logic(r, mapper, new NullLogger <Repo>());
                PlaybookController playbookController = new PlaybookController(logic, new NullLogger <PlaybookController>());
                var playDto = new PlayDto
                {
                    PlayID      = Guid.NewGuid(),
                    PlaybookID  = Guid.NewGuid(),
                    Name        = "Tackle",
                    Description = "Tackle other players",
                    DrawnPlay   = new byte[1],
                    ImageString = "football,football"
                };

                var createPlay = await playbookController.CreatePlay(playDto);

                Assert.NotEmpty(context.Plays);
                Assert.Equal("Tackle", createPlay.Value.Name);
                Assert.Equal("Tackle other players", createPlay.Value.Description);
            }
        }
コード例 #2
0
        public async void TestForCreatePlay()
        {
            var options = new DbContextOptionsBuilder <PlaybookContext>()
                          .UseInMemoryDatabase(databaseName: "p3PlaybookService")
                          .Options;

            using (var context = new PlaybookContext(options))
            {
                context.Database.EnsureDeleted();
                context.Database.EnsureCreated();

                Repo    r      = new Repo(context, new NullLogger <Repo>());
                Mapper  mapper = new Mapper();
                Logic   logic  = new Logic(r, mapper, new NullLogger <Repo>());
                PlayDto play   = new PlayDto()
                {
                    PlaybookID  = Guid.NewGuid(),
                    Name        = "Tackle",
                    Description = "Tackle other players",
                    ImageString = "Football,football,football"
                };
                var createPlay = await logic.CreatePlay(play);

                //Assert.Equal(1, context.Plays.CountAsync().Result);
                Assert.Contains <Play>(createPlay, context.Plays);
            }
        }
コード例 #3
0
ファイル: PlayController.cs プロジェクト: gabrifdez89/ghost
        public IActionResult PostPlay([FromBody] PlayDto play)
        {
            if (!ModelState.IsValid || play == null)
            {
                return(BadRequest(ModelState));
            }

            if (_gameLogic.CheckLosePlay(play.Text))
            {
                return(Ok(new PlayResultDto()
                {
                    Text = play.Text,
                    Win = false,
                    Lose = true
                }));
            }

            string text = _ghostPlayer.Play(play.Text);

            return(Ok(new PlayResultDto()
            {
                Text = text,
                Win = _gameLogic.CheckLosePlay(text),
                Lose = false
            }));
        }
コード例 #4
0
        public void PostPlayShouldCheckIfTheSystemPlayerHasLost()
        {
            var playDtoMock = new PlayDto()
            {
                Text = "a"
            };

            gameLogicMock
            .Setup(gameLogic => gameLogic.CheckLosePlay(playDtoMock.Text))
            .Returns(false);
            ghostPlayerMock
            .Setup(ghostPlayer => ghostPlayer.Play(playDtoMock.Text))
            .Returns("ab");
            gameLogicMock
            .Setup(gameLogic => gameLogic.CheckLosePlay("ab"))
            .Returns(true);

            IActionResult result = controller.PostPlay(playDtoMock);

            var playResult = Assert.IsType <OkObjectResult>(result);
            var model      = Assert.IsAssignableFrom <PlayResultDto>(playResult.Value);

            Assert.False(model.Lose);
            Assert.True(model.Win);
            Assert.Equal("ab", model.Text);
        }
コード例 #5
0
        public void PostPlayShouldReturnOkIfModelStateIsValid()
        {
            var playDtoMock = new PlayDto()
            {
                Text = "a"
            };

            var result = controller.PostPlay(playDtoMock);

            Assert.IsType <OkObjectResult>(result);
        }
コード例 #6
0
        /// <summary>
        /// Get list of Plays
        /// </summary>
        /// <returns>list of Plays</returns>
        public async Task <IEnumerable <PlayDto> > GetPlays()
        {
            IEnumerable <Play> playList = await _repo.GetPlays();

            List <PlayDto> playDtos = new List <PlayDto>();

            foreach (var play in playList)
            {
                PlayDto playDto = _mapper.ConvertToPlayDto(play);
                playDtos.Add(playDto);
            }
            return(playDtos);
        }
コード例 #7
0
        public void PostPlayShouldReturnBadRequestIfModelStateIsNotValid()
        {
            var playDtoMock = new PlayDto()
            {
                Text = "123"
            };

            controller.ModelState.AddModelError("test", "test");

            var result = controller.PostPlay(playDtoMock);

            Assert.IsType <BadRequestObjectResult>(result);
        }
コード例 #8
0
        /// <summary>
        /// take in a Play as a parameter and creates a PlayDto. returns the new dto
        /// </summary>
        /// <param name="play"></param>
        /// <returns></returns>
        public PlayDto ConvertToPlayDto(Play play)
        {
            PlayDto playDto = new PlayDto
            {
                PlayID      = play.PlayID,
                PlaybookID  = play.PlaybookId,
                Name        = play.Name,
                Description = play.Description,
                DrawnPlay   = play.DrawnPlay,
                ImageString = ConvertByteArrayToJpgString(play.DrawnPlay)
            };

            return(playDto);
        }
コード例 #9
0
        /// <summary>
        /// Create new Play and assign it to the current Playbook
        /// </summary>
        /// <param name="playDto">new Play</param>
        /// <returns>Play</returns>
        public async Task <Play> CreatePlay(PlayDto playDto)
        {
            Play newPlay = new Play()
            {
                PlaybookId  = playDto.PlaybookID,
                Name        = playDto.Name,
                Description = playDto.Description,
                DrawnPlay   = _mapper.ConvertImage(playDto.ImageString)
            };

            await _repo.Plays.AddAsync(newPlay);

            await _repo.CommitSave();

            return(newPlay);
        }
コード例 #10
0
        public void ValidatePlayDto()
        {
            var play = new PlayDto()
            {
                PlayID      = Guid.NewGuid(),
                PlaybookID  = Guid.NewGuid(),
                Name        = "tackles",
                Description = "tackle other players",
                DrawnPlay   = new byte[1],
                ImageString = "football, football"
            };

            var errorcount = ValidateModel(play).Count;

            Assert.Equal(0, errorcount);
        }
コード例 #11
0
        public async Task PlayShouldReturnAPlayResponse()
        {
            var play = new PlayDto()
            {
                Text = "capyb"
            };
            var postData = new StringContent(JsonConvert.SerializeObject(play), Encoding.UTF8, "application/json");

            var response = await _client.PostAsync("/api/play", postData);

            var responseString = await response.Content.ReadAsStringAsync();

            var playResponse = JsonConvert.DeserializeObject <PlayResultDto>(responseString);

            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
            Assert.Equal("capyba", playResponse.Text);
        }
コード例 #12
0
        public async void TestForEditPlay()
        {
            var options = new DbContextOptionsBuilder <PlaybookContext>()
                          .UseInMemoryDatabase(databaseName: "p3PlaybookService")
                          .Options;

            using (var context = new PlaybookContext(options))
            {
                context.Database.EnsureDeleted();
                context.Database.EnsureCreated();

                Repo               r                  = new Repo(context, new NullLogger <Repo>());
                Mapper             mapper             = new Mapper();
                Logic              logic              = new Logic(r, mapper, new NullLogger <Repo>());
                PlaybookController playbookController = new PlaybookController(logic, new NullLogger <PlaybookController>());
                var play = new Play
                {
                    PlayID      = Guid.NewGuid(),
                    PlaybookId  = Guid.NewGuid(),
                    Name        = "Tackle",
                    Description = "Tackle other players",
                    DrawnPlay   = new byte[1]
                };

                r.Plays.Add(play);
                await r.CommitSave();

                var playDto = new PlayDto
                {
                    PlayID      = Guid.NewGuid(),
                    PlaybookID  = Guid.NewGuid(),
                    Name        = "Run",
                    Description = "Run with ball",
                    DrawnPlay   = new byte[1],
                };

                var getPlay = await logic.GetPlayById(play.PlayID);

                Assert.Equal("Tackle", getPlay.Name);
                Assert.Equal("Tackle other players", getPlay.Description);
                var editPlay = await playbookController.EditPlay(play.PlayID.ToString(), playDto);

                Assert.Equal("Run", editPlay.Value.Name);
                Assert.Equal("Run with ball", editPlay.Value.Description);
            }
        }
コード例 #13
0
        public void PostPlayShouldReturnTheRightOkResponseIfPlayerHasLost()
        {
            var playDtoMock = new PlayDto()
            {
                Text = "qwerqwer"
            };

            gameLogicMock
            .Setup(gameLogic => gameLogic.CheckLosePlay(playDtoMock.Text))
            .Returns(true);

            IActionResult result = controller.PostPlay(playDtoMock);

            var playResult = Assert.IsType <OkObjectResult>(result);
            var model      = Assert.IsAssignableFrom <PlayResultDto>(playResult.Value);

            Assert.True(model.Lose);
            Assert.False(model.Win);
            Assert.Equal(playDtoMock.Text, model.Text);
        }
コード例 #14
0
        /// <summary>
        /// Edit a Play
        /// </summary>
        /// <param name="playId">Play to edit</param>
        /// <param name="playDto">New Play info</param>
        /// <returns>edited Play</returns>
        public async Task <Play> EditPlay(Guid playId, PlayDto playDto)
        {
            Play editedPlay = await GetPlayById(playId);

            if (editedPlay != null)
            {
                if (editedPlay.Name != playDto.Name)
                {
                    editedPlay.Name = playDto.Name;
                }
                if (editedPlay.Description != playDto.Description)
                {
                    editedPlay.Description = playDto.Description;
                }
                if (editedPlay.DrawnPlay != playDto.DrawnPlay)
                {
                    editedPlay.DrawnPlay = playDto.DrawnPlay;
                }
                await _repo.CommitSave();
            }
            return(editedPlay);
        }
コード例 #15
0
        public async void TestForEditPlay()
        {
            var options = new DbContextOptionsBuilder <PlaybookContext>()
                          .UseInMemoryDatabase(databaseName: "p3PlaybookService")
                          .Options;

            using (var context = new PlaybookContext(options))
            {
                context.Database.EnsureDeleted();
                context.Database.EnsureCreated();

                Repo   r      = new Repo(context, new NullLogger <Repo>());
                Mapper mapper = new Mapper();
                Logic  logic  = new Logic(r, mapper, new NullLogger <Repo>());
                var    play   = new Play()
                {
                    PlayID      = Guid.NewGuid(),
                    PlaybookId  = Guid.NewGuid(),
                    Name        = "Tackle",
                    Description = "Tackle the player",
                    DrawnPlay   = new byte[1]
                };

                r.Plays.Add(play);
                await r.CommitSave();

                var play2 = new PlayDto()
                {
                    PlaybookID  = Guid.NewGuid(),
                    Name        = "Tackle",
                    Description = "Tackle the quarterback",
                    DrawnPlay   = new byte[1]
                };

                var editedPlay = await logic.EditPlay(play.PlayID, play2);

                Assert.Equal(editedPlay.Description, context.Plays.Find(play.PlayID).Description);
            }
        }
コード例 #16
0
 public async Task <ActionResult <Play> > CreatePlay(PlayDto createPlay)
 {
     return(await _logic.CreatePlay(createPlay));
 }
コード例 #17
0
 public async Task <ActionResult <Play> > EditPlay(string PlayID, PlayDto createPlay)
 {
     return(await _logic.EditPlay(Guid.Parse(PlayID), createPlay));
 }