public void SavingExistingObject_ShouldPreserveNumberOfRecordsInPrimaryAndCache()
        {
            // Arrange
            var sqlConnectionString   = ConfigurationManager.ConnectionStrings["test"].ToString();
            var redisConnectionString = ConfigurationManager.ConnectionStrings["redis-test"].ToString();
            var sqlAdapter            = new SqlServerAdapter(sqlConnectionString);
            var redisAdapter          = new RedisAdapter(ConnectionMultiplexer.Connect(redisConnectionString));
            var cachedAdapter         = new SqlServerCachedAdapter(sqlAdapter, redisAdapter);

            // create new game and insert it, so we have something to update
            var gameToUpdate = new TicTacToeData();

            cachedAdapter.Save(gameToUpdate);

            int numPreExistingGamesSql   = AdapterTestHelpers.CountSqlGames(sqlConnectionString);
            int numPreExistingGamesRedis = AdapterTestHelpers.CountRedisGames(redisConnectionString);

            // Act
            gameToUpdate.CurrentPlayer = Player.O;
            cachedAdapter.Save(gameToUpdate);

            // Assert
            int numPostExistingGamesSql   = AdapterTestHelpers.CountSqlGames(sqlConnectionString);
            int numPostExistingGamesRedis = AdapterTestHelpers.CountRedisGames(redisConnectionString);

            Assert.AreEqual(numPreExistingGamesSql, numPostExistingGamesSql);
            Assert.AreEqual(numPreExistingGamesRedis, numPostExistingGamesRedis);
        }
        public void DeletingGame_ShouldRemoveOneRecordInPrimaryAndCache()
        {
            // Arrange
            var sqlConnectionString   = ConfigurationManager.ConnectionStrings["test"].ToString();
            var redisConnectionString = ConfigurationManager.ConnectionStrings["redis-test"].ToString();
            var sqlAdapter            = new SqlServerAdapter(sqlConnectionString);
            var redisAdapter          = new RedisAdapter(ConnectionMultiplexer.Connect(redisConnectionString));
            var cachedAdapter         = new SqlServerCachedAdapter(sqlAdapter, redisAdapter);

            // create new game and insert it, so we have something to delete
            var gameToUpdate = new TicTacToeData();

            cachedAdapter.Save(gameToUpdate);

            int numPreExistingGamesSql   = AdapterTestHelpers.CountSqlGames(sqlConnectionString);
            int numPreExistingGamesRedis = AdapterTestHelpers.CountRedisGames(redisConnectionString);

            // Act
            cachedAdapter.Delete(gameToUpdate.Id);

            // Assert
            int numPostExistingGamesSql   = AdapterTestHelpers.CountSqlGames(sqlConnectionString);
            int numPostExistingGamesRedis = AdapterTestHelpers.CountRedisGames(redisConnectionString);

            Assert.AreEqual(numPreExistingGamesSql - 1, numPostExistingGamesSql);
            Assert.AreEqual(numPreExistingGamesRedis - 1, numPostExistingGamesRedis);
        }
        public void ReadingAllGames_ShouldReturnAllGames()
        {
            // Arrange
            var sqlConnectionString   = ConfigurationManager.ConnectionStrings["test"].ToString();
            var redisConnectionString = ConfigurationManager.ConnectionStrings["redis-test"].ToString();
            var sqlAdapter            = new SqlServerAdapter(sqlConnectionString);
            var redisAdapter          = new RedisAdapter(ConnectionMultiplexer.Connect(redisConnectionString));
            var cachedAdapter         = new SqlServerCachedAdapter(sqlAdapter, redisAdapter);

            var insertedGames = new List <TicTacToeData>()
            {
                new TicTacToeData(),
                new TicTacToeData()
            };

            foreach (var game in insertedGames)
            {
                cachedAdapter.Save(game);
            }

            // Act
            var allGames = cachedAdapter.ReadAll();

            // Assert
            foreach (var game in insertedGames)
            {
                Assert.IsTrue(allGames.Contains(game));
            }
        }
        public void SaveThenRead_ShouldRoundTrip()
        {
            // Arrange
            var sqlConnectionString   = ConfigurationManager.ConnectionStrings["test"].ToString();
            var redisConnectionString = ConfigurationManager.ConnectionStrings["redis-test"].ToString();
            var sqlAdapter            = new SqlServerAdapter(sqlConnectionString);
            var redisAdapter          = new RedisAdapter(ConnectionMultiplexer.Connect(redisConnectionString));
            var cachedAdapter         = new SqlServerCachedAdapter(sqlAdapter, redisAdapter);
            var gameData = new TicTacToeData();

            // change gameData to make sure Read() isn't just constructing new TicTacToeData()
            gameData.CurrentPlayer = Player.O;

            // Act
            cachedAdapter.Save(gameData);
            var(readSuccessfully, readData) = cachedAdapter.Read(gameData.Id);

            // Assert
            Assert.IsTrue(readSuccessfully);
            Assert.AreEqual(gameData, readData);
        }
        public void SavingNewObject_ShouldInsertOneRecordInPrimaryAndCache()
        {
            // Arrange
            var sqlConnectionString   = ConfigurationManager.ConnectionStrings["test"].ToString();
            var redisConnectionString = ConfigurationManager.ConnectionStrings["redis-test"].ToString();
            var sqlAdapter            = new SqlServerAdapter(sqlConnectionString);
            var redisAdapter          = new RedisAdapter(ConnectionMultiplexer.Connect(redisConnectionString));
            var cachedAdapter         = new SqlServerCachedAdapter(sqlAdapter, redisAdapter);

            var newGame = new TicTacToeData();
            int numPreExistingGamesSql   = AdapterTestHelpers.CountSqlGames(sqlConnectionString);
            int numPreExistingGamesRedis = AdapterTestHelpers.CountRedisGames(redisConnectionString);

            // Act
            cachedAdapter.Save(newGame);

            // Assert
            int numPostExistingGamesSql   = AdapterTestHelpers.CountSqlGames(sqlConnectionString);
            int numPostExistingGamesRedis = AdapterTestHelpers.CountRedisGames(redisConnectionString);

            Assert.AreEqual(numPreExistingGamesSql + 1, numPostExistingGamesSql);
            Assert.AreEqual(numPreExistingGamesRedis + 1, numPostExistingGamesRedis);
        }