예제 #1
0
        public void Update()
        {
            //Arrange
            var userData = new List <User>
            {
                new User {
                    Email = "[email protected]", Name = "IhaveName1", Password = "******", UserId = 1
                },
                new User {
                    Email = "[email protected]", Name = "IhaveName2", Password = "******", UserId = 2
                },
                new User {
                    Email = "[email protected]", Name = "IhaveName3", Password = "******", UserId = 3
                },
                new User {
                    Email = "[email protected]", Name = "IhaveName4", Password = "******", UserId = 4
                }
            }.AsQueryable();

            //svar matchUserData = new List<MatchUser>{ new MatchUser{MatchId = 1, }}

            var matchData = new List <Match>
            {
                new Match {
                    DateEnd = null, DateStart = DateTime.Now, GameData = "", GameTypeId = (int)GameTypes.Chess, MatchId = 1
                }
            }.AsQueryable();

            var matchUserData = new List <MatchUser>
            {
                new MatchUser {
                    MatchId = 1, UserId = 1
                },
                new MatchUser {
                    MatchId = 1, UserId = 2
                }
            }.AsQueryable();


            var mockMatchSet     = new Mock <DbSet <Match> >();
            var mockUserSet      = new Mock <DbSet <User> >();
            var mockMatchUserSet = new Mock <DbSet <MatchUser> >();

            mockUserSet.SetupData(userData);
            mockMatchSet.SetupData(matchData);
            mockMatchUserSet.SetupData(matchUserData);

            var mockContext = new Mock <IBoardGameDbContext>();

            mockContext.Setup(s => s.Matches).Returns(mockMatchSet.Object);
            mockContext.Setup(s => s.Users).Returns(mockUserSet.Object);
            mockContext.Setup(s => s.MatchUsers).Returns(mockMatchUserSet.Object);

            IMatchService service = new MatchService(mockContext.Object, new MatchServiceValidationBulider());


            Match matchOk = new Match()
            {
                MatchId = 1, DateStart = DateTime.Now, GameTypeId = (int)GameTypes.Chess, GameData = "{\"variable3\": 200,\"variable4\": 2}"
            };
            Match matchNoMatchId = new Match()
            {
                DateStart = DateTime.Now, GameTypeId = (int)GameTypes.Chess, GameData = "{\"variable3\": 200,\"variable4\": 2}"
            };
            Match matchIncorectData = new Match()
            {
                MatchId = 1, GameTypeId = (int)GameTypes.Chess, GameData = "{\"variable3\": 200,\"variable4\":"
            };


            //Act
            var respondOk           = service.Update(matchOk);
            var respondNoMatchId    = service.Update(matchNoMatchId);
            var respondIncorectData = service.Update(matchIncorectData);


            //Assert
            Assert.AreEqual(respondOk.Status, ServiceRespondStatus.Ok);
            Assert.AreEqual(respondOk.Messages.Count, 0);

            Assert.AreEqual(respondNoMatchId.Status, ServiceRespondStatus.Error);
            Assert.AreEqual(respondNoMatchId.Messages.Count, 1);

            Assert.AreEqual(respondIncorectData.Status, ServiceRespondStatus.Error);
        }
        public void Update()
        {
            //Arrange
            var mockContext = new Mock <IBoardGameDbContext>();
            IMatchServiceValidation serviceValidation = new MatchServiceValidation(new MatchValidation(), mockContext.Object);

            Match matchOk = new Match()
            {
                MatchId = 1, DateStart = DateTime.Now, GameTypeId = (int)GameTypes.Chess, GameData = "{\"variable3\": 200,\"variable4\": 2}"
            };
            Match matchOkDataEnd = new Match()
            {
                MatchId = 1, DateStart = DateTime.Now, DateEnd = DateTime.Now.AddDays(1), GameTypeId = (int)GameTypes.Chess, GameData = "{\"variable3\": 200,\"variable4\": 2}"
            };

            Match matchNoMatchId = new Match()
            {
                DateStart = DateTime.Now, GameTypeId = (int)GameTypes.Chess, GameData = "{\"variable3\": 200,\"variable4\": 2}"
            };
            Match matchNull       = null;
            Match matchNoGameData = new Match()
            {
                MatchId = 1, DateStart = DateTime.Now, GameTypeId = (int)GameTypes.Chess
            };
            Match matchIncorrectGameData = new Match()
            {
                MatchId = 1, DateStart = DateTime.Now, GameTypeId = (int)GameTypes.Chess, GameData = "{\"variable3\": 200,\"variable4"
            };
            Match matchNoDateStart = new Match()
            {
                MatchId = 1, GameTypeId = (int)GameTypes.Chess, GameData = "{\"variable3\": 200,\"variable4\": 2}"
            };
            Match matchBadEndData = new Match()
            {
                MatchId = 1, DateStart = DateTime.Now, DateEnd = DateTime.Now.AddDays(-1), GameTypeId = (int)GameTypes.Chess, GameData = "{\"variable3\": 200,\"variable4\": 2}"
            };
            Match matchIncorrect = new Match()
            {
                MatchId = 1, DateEnd = DateTime.Now.AddDays(-1), GameTypeId = (int)GameTypes.Chess, GameData = "{\"variable3\": 200,\"variable4\""
            };


            //Act
            var resultOk                = serviceValidation.Update(matchOk);
            var resultOkDataEnd         = serviceValidation.Update(matchOkDataEnd);
            var resultNoMatchId         = serviceValidation.Update(matchNoMatchId);
            var resultNull              = serviceValidation.Update(matchNull);
            var resultNoGameData        = serviceValidation.Update(matchNoGameData);
            var resultIncorrectGameData = serviceValidation.Update(matchIncorrectGameData);
            var resultNoDateStart       = serviceValidation.Update(matchNoDateStart);
            var resultBadEndData        = serviceValidation.Update(matchBadEndData);
            var resultIncorrect         = serviceValidation.Update(matchIncorrect);


            //Assert
            Assert.AreEqual(resultOk.IsSucces, true);
            Assert.AreEqual(resultOk.ErrorList.Count, 0);

            Assert.AreEqual(resultOkDataEnd.IsSucces, true);
            Assert.AreEqual(resultOkDataEnd.ErrorList.Count, 0);

            Assert.AreEqual(resultNoMatchId.IsSucces, false);
            Assert.AreEqual(resultNoMatchId.ErrorList.Any(a => a.Key == ValidationKey.MatchNoId.ToString()), true);
            Assert.AreEqual(resultNoMatchId.ErrorList.Count, 1);

            Assert.AreEqual(resultNull.IsSucces, false);
            Assert.AreEqual(resultNull.ErrorList.Any(a => a.Key == ValidationKey.IsNull.ToString()), true);
            Assert.AreEqual(resultNull.ErrorList.Count, 1);

            /*
             * Assert.AreEqual(resultNoGameData.IsSucces, false);
             * Assert.AreEqual(resultNoGameData.ErrorList.Any(a => a.Key == ValidationKey.MatchNoJsonFormatGameData.ToString()), true);
             * Assert.AreEqual(resultNoGameData.ErrorList.Count, 1);
             */

            Assert.AreEqual(resultIncorrectGameData.IsSucces, false);
            Assert.AreEqual(resultIncorrectGameData.ErrorList.Any(a => a.Key == ValidationKey.MatchNoJsonFormatGameData.ToString()), true);
            Assert.AreEqual(resultIncorrectGameData.ErrorList.Count, 1);

            Assert.AreEqual(resultNoDateStart.IsSucces, false);
            Assert.AreEqual(resultNoDateStart.ErrorList.Any(a => a.Key == ValidationKey.MatchNullDateStart.ToString()), true);
            Assert.AreEqual(resultNoDateStart.ErrorList.Count, 1);

            Assert.AreEqual(resultBadEndData.IsSucces, false);
            Assert.AreEqual(resultBadEndData.ErrorList.Any(a => a.Key == ValidationKey.MatchIncorectDateEnd.ToString()), true);
            Assert.AreEqual(resultBadEndData.ErrorList.Count, 1);

            Assert.AreEqual(resultIncorrect.IsSucces, false);
            Assert.AreEqual(resultIncorrect.ErrorList.Any(a => a.Key == ValidationKey.MatchNoJsonFormatGameData.ToString()), true);
            Assert.AreEqual(resultIncorrect.ErrorList.Any(a => a.Key == ValidationKey.MatchNullDateStart.ToString()), true);
            Assert.AreEqual(resultIncorrect.ErrorList.Count, 2);
        }