Exemplo n.º 1
0
 public ReportServicesTests()
 {
     this.db = DbGenerator.GetDbContext();
     DbSeeder.SeedNormalUsers(this.db);
     DbSeeder.SeedGames(this.db);
     this.reportService = new ReportService(this.db, new GameService(this.db));
 }
Exemplo n.º 2
0
        public async Task TestSearchEmptyStringReturnsAllGames()
        {
            // arrange
            DbSeeder.SeedGames(this.db);
            var initialGames = await this.db.Games.ToArrayAsync();

            // act
            var games = await this.gameService.GetAll().ToArrayAsync();

            // assert
            var result = games.SequenceEqual(initialGames);

            Assert.True(result);
        }
Exemplo n.º 3
0
        public async Task TestGetGameUserWithIncorrectDataReturnsNull()
        {
            // arrange
            DbSeeder.SeedGames(this.db);
            var game = await this.db.Games.FirstOrDefaultAsync();

            var user = await this.db.Users.FirstOrDefaultAsync();

            // act
            var result = this.gameService.GetGameUser(user.Id, game.Id);

            // assert
            Assert.Null(result);
        }
Exemplo n.º 4
0
        public DrawingServiceTests()
        {
            this.db = DbGenerator.GetDbContext();
            DbSeeder.SeedNormalUsers(this.db);
            DbSeeder.SeedGames(this.db);

            var imageService = new Mock <IImageService>();

            imageService
            .Setup(s => s.SaveGameDrawing(It.IsAny <string>(), It.IsAny <int>(), It.IsAny <string>()))
            .Returns(() => Task.FromResult(ImageUrl));

            this.drawingService = new DrawingService(this.db, imageService.Object);
        }
Exemplo n.º 5
0
        public async Task TestJoinGameWithExistingUserExistingGameNonexistingUserGameCreatesUserGame()
        {
            // arrange
            DbSeeder.SeedGames(this.db);
            var user = await this.db.Users.FirstOrDefaultAsync();

            var game = await this.db.Games.FirstOrDefaultAsync();

            // act
            this.gameService.JoinGame(user.Id, game.Id);

            // arrange
            var userGame = game.Players.First();

            Assert.Equal(user.Id, userGame.UserId);
        }
Exemplo n.º 6
0
        public async Task TestSearchWithStringReturnsCorrectGames()
        {
            // arrange
            const string searchKeyword = "Stella";

            DbSeeder.SeedGames(this.db);
            var initialGames = await this.db.Games.Where(g => g.Theme.Contains(searchKeyword)).ToArrayAsync();

            // act
            var games = await this.gameService.GetAll(searchKeyword).ToArrayAsync();

            // assert
            var result = games.SequenceEqual(initialGames);

            Assert.True(result);
        }
Exemplo n.º 7
0
        public async Task TestJoinGameWithInvalidDataCreatesNothing()
        {
            // arrange
            DbSeeder.SeedGames(this.db);
            var game = await this.db.Games.FirstOrDefaultAsync();

            var user = await this.db.Users.FirstOrDefaultAsync();

            game.Players.Add(new GameUser()
            {
                Game = game, GameId = game.Id, User = user, UserId = user.Id
            });
            await this.db.SaveChangesAsync();

            // act
            this.gameService.JoinGame(user.Id, game.Id);

            // arrange
            Assert.Equal(1, game.Players.Count);
        }
Exemplo n.º 8
0
        public async Task TestGetGameUserWithCorrectDataReturnsCorrectData()
        {
            // arrange
            DbSeeder.SeedGames(this.db);
            var game = await this.db.Games.FirstOrDefaultAsync();

            var user = await this.db.Users.FirstOrDefaultAsync();

            var gu = new GameUser()
            {
                Game = game, GameId = game.Id, User = user, UserId = user.Id
            };

            game.Players.Add(gu);
            await this.db.SaveChangesAsync();

            // act
            var result = this.gameService.GetGameUser(user.Id, game.Id);

            // assert
            Assert.Equal(gu, result);
        }
Exemplo n.º 9
0
        public async Task TestLeaveGameWithValidDataLeavesGame()
        {
            // arrange
            DbSeeder.SeedGames(this.db);
            var game = await this.db.Games.FirstOrDefaultAsync();

            var user = await this.db.Users.FirstOrDefaultAsync();

            game.Players.Add(new GameUser()
            {
                Game = game, GameId = game.Id, User = user, UserId = user.Id
            });
            await this.db.SaveChangesAsync();

            // act
            this.gameService.LeaveGame(user.Id, game.Id);

            // assert
            var count = game.Players.Count;

            Assert.Equal(0, count);
        }
Exemplo n.º 10
0
        public async Task TestSelectWinnerSelectsWinnerWithValidDataClosesGameGivesScoreDeductsScore()
        {
            // arrange
            DbSeeder.SeedGames(this.db);
            var game = new Game()
            {
                PlayersCount = 2
            };
            var users = await this.db.Users.Take(2).ToArrayAsync();

            var winner             = users[0];
            var initialWinnerScore = winner.TotalScore;
            var loser             = users[1];
            var initialLoserScore = loser.TotalScore;
            var first             = new GameUser()
            {
                Game = game, GameId = game.Id, User = winner, UserId = winner.Id, HasDrawn = true
            };
            var second = new GameUser()
            {
                Game = game, GameId = game.Id, User = loser, UserId = loser.Id, HasDrawn = true
            };

            game.Players.Add(first);
            game.Players.Add(second);
            this.db.Games.Add(game);
            await this.db.SaveChangesAsync();

            // act
            this.gameService.SelectWinner(winner.Id, game.Id);

            // assert
            Assert.Equal(winner.Id, game.WinnerId);
            Assert.Equal(GameStauts.Finished, game.Status);
            Assert.Equal(game.PlayersCount + initialWinnerScore, winner.TotalScore);
            Assert.Equal(initialLoserScore - 1, loser.TotalScore);
        }