Exemplo n.º 1
0
        public void Log_ExistingBot_ShouldReturnTrueAndSaveInDatabase()
        {
            var databaseContext = DatabaseFactory.Create();
            var timeProvider    = TimeProviderFactory.Create();
            var statsService    = new StatsService(databaseContext, timeProvider);

            databaseContext.Bots.Add(new BotModel
            {
                Id   = "1",
                Name = "Bot1"
            });
            databaseContext.SaveChanges();

            var stats = new LogStatsDto
            {
                BotId = "1",
                ExecutedCommandsCount = 2,
                GuildsCount           = 3,
                MembersCount          = 4
            };

            var result      = statsService.Log(stats);
            var loggedStats = databaseContext.Stats.First();

            Assert.True(result);
            Assert.Equal(1, databaseContext.Stats.Count());
            Assert.Equal("1", loggedStats.BotId);
            Assert.Equal(timeProvider.Get(), loggedStats.CreateTime);
            Assert.Equal(2, loggedStats.ExecutedCommandsCount);
            Assert.Equal(3, loggedStats.GuildsCount);
            Assert.Equal(4, loggedStats.MembersCount);
        }
Exemplo n.º 2
0
        public ActionResult Log([FromBody] LogStatsDto logStatsDto)
        {
            if (!_statsService.Log(logStatsDto))
            {
                return(new BadRequestResult());
            }

            return(new NoContentResult());
        }
Exemplo n.º 3
0
        public bool Log(LogStatsDto logStatsDto)
        {
            if (!_databaseContext.Bots.Any(p => p.Id == logStatsDto.BotId))
            {
                return(false);
            }

            var statsModel = Mapper.Map <StatsModel>(logStatsDto);

            statsModel.CreateTime = _timeProvider.Get();

            _databaseContext.Stats.Add(statsModel);
            _databaseContext.SaveChanges();

            return(true);
        }
Exemplo n.º 4
0
        public void Log_NonExistingId_ShouldReturnBadRequest()
        {
            var statsServiceMock = new Mock <IStatsService>();
            var logStatsDto      = new LogStatsDto
            {
                BotId = "1000",
                ExecutedCommandsCount = 1,
                GuildsCount           = 2,
                MembersCount          = 3
            };

            statsServiceMock.Setup(p => p.Log(logStatsDto)).Returns(false);

            var controller = new StatsController(statsServiceMock.Object);
            var result     = controller.Log(logStatsDto);

            Assert.IsAssignableFrom <BadRequestResult>(result);
        }
Exemplo n.º 5
0
        public void Log_NonExistingBot_ShouldReturnFalse()
        {
            var databaseContext = DatabaseFactory.Create();
            var timeProvider    = TimeProviderFactory.Create();
            var statsService    = new StatsService(databaseContext, timeProvider);

            var stats = new LogStatsDto
            {
                BotId = "1",
                ExecutedCommandsCount = 2,
                GuildsCount           = 3,
                MembersCount          = 4
            };

            var result = statsService.Log(stats);

            Assert.False(result);
            Assert.Equal(0, databaseContext.Stats.Count());
        }