public void GameGenreDictionaryTest() { var gameGenre1 = new GameGenre( 1, 2, 3); var dictionary = GameGenre.ToDictionary(gameGenre1); Assert.IsNotNull(dictionary); var gameGenre2 = GameGenre.FromDictionary(dictionary); Assert.AreNotSame(gameGenre1, gameGenre2); Assert.AreEqual(gameGenre1.Id, gameGenre2.Id); Assert.AreEqual(gameGenre1.GameId, gameGenre2.GameId); Assert.AreEqual(gameGenre1.GenreId, gameGenre2.GenreId); }
public void GameGenreDatabaseCommandsTest() { var connection = new SqlServerConnection(); Assert.AreEqual(connection.IsConnected, false); var result = connection.Connect(ConnectionString, 0); Assert.AreEqual(result, true); // Add a Games var game = new Game( 0, "Name", "Description"); var insertCommand = game.GenerateInsertStatement(); Assert.IsFalse(string.IsNullOrEmpty(insertCommand)); var errorMessage = ""; var insertResult = connection.ExecuteCommand(insertCommand, ref errorMessage, out var newId); Assert.IsTrue(insertResult); Assert.IsTrue(newId > 0); Assert.AreEqual(string.IsNullOrEmpty(errorMessage), true); game.Id = newId; var updateGame = new Game( 0, "Name1", "Description1"); insertCommand = updateGame.GenerateInsertStatement(); Assert.IsFalse(string.IsNullOrEmpty(insertCommand)); errorMessage = ""; insertResult = connection.ExecuteCommand(insertCommand, ref errorMessage, out newId); Assert.IsTrue(insertResult); Assert.IsTrue(newId > 0); Assert.AreEqual(string.IsNullOrEmpty(errorMessage), true); updateGame.Id = newId; // Add a Genres var genre = new Genre(0, "Name", "Description"); insertCommand = genre.GenerateInsertStatment(); Assert.IsFalse(string.IsNullOrEmpty(insertCommand)); errorMessage = ""; insertResult = connection.ExecuteCommand(insertCommand, ref errorMessage, out newId); Assert.IsTrue(insertResult); Assert.IsTrue(newId > 0); Assert.AreEqual(string.IsNullOrEmpty(errorMessage), true); genre.Id = newId; var updateGenre = new Genre(0, "Name", "Description"); insertCommand = updateGenre.GenerateInsertStatment(); Assert.IsFalse(string.IsNullOrEmpty(insertCommand)); errorMessage = ""; insertResult = connection.ExecuteCommand(insertCommand, ref errorMessage, out newId); Assert.IsTrue(insertResult); Assert.IsTrue(newId > 0); Assert.AreEqual(string.IsNullOrEmpty(errorMessage), true); updateGenre.Id = newId; // Select All var gameGenre = new GameGenre(); var selectQuery = gameGenre.GenerateSelectQuery(); Assert.IsFalse(string.IsNullOrEmpty(selectQuery)); errorMessage = ""; var selectResult = connection.ExecuteQuery(selectQuery, ref errorMessage); Assert.AreEqual(string.IsNullOrEmpty(errorMessage), true); Assert.IsNotNull(selectResult); var selectResultList = DataSetUtility.ToDictionaryList(selectResult.Tables[0]); if (selectResultList.Count > 0) { Assert.IsTrue(selectResultList.Count > 0); } // Insert gameGenre = new GameGenre( 0, game.Id, genre.Id); insertCommand = gameGenre.GenerateInsertStatement(); Assert.IsFalse(string.IsNullOrEmpty(insertCommand)); errorMessage = ""; insertResult = connection.ExecuteCommand(insertCommand, ref errorMessage, out newId); Assert.IsTrue(insertResult); Assert.IsTrue(newId > 0); Assert.AreEqual(string.IsNullOrEmpty(errorMessage), true); gameGenre.Id = newId; // Exists var existsQuery = gameGenre.GenerateExistsQuery(); Assert.IsFalse(string.IsNullOrEmpty(existsQuery)); errorMessage = ""; var existsResult = connection.ExecuteQuery(existsQuery, ref errorMessage); Assert.AreEqual(string.IsNullOrEmpty(errorMessage), true); Assert.IsNotNull(existsResult); var existsResultList = DataSetUtility.ToDictionaryList(existsResult.Tables[0]); var recordExists = existsResultList.Any(dictionary => (dictionary != null) && (dictionary.Count > 0)); Assert.AreEqual(recordExists, true); // Select selectQuery = gameGenre.GenerateSelectQuery(); Assert.IsFalse(string.IsNullOrEmpty(selectQuery)); errorMessage = ""; selectResult = connection.ExecuteQuery(selectQuery, ref errorMessage); Assert.AreEqual(string.IsNullOrEmpty(errorMessage), true); Assert.IsNotNull(selectResult); selectResultList = DataSetUtility.ToDictionaryList(selectResult.Tables[0]); GameGenre foundGameGenre = null; if (selectResultList.Count > 0) { foreach (var dictionary in selectResultList.Where(dictionary => (dictionary != null) && (dictionary.Count > 0))) { foundGameGenre = GameGenre.FromDictionary(dictionary); break; } } Assert.IsNotNull(foundGameGenre); Assert.AreNotSame(gameGenre, foundGameGenre); Assert.AreEqual(gameGenre.Id, foundGameGenre.Id); Assert.AreEqual(gameGenre.GameId, foundGameGenre.GameId); Assert.AreEqual(gameGenre.GenreId, foundGameGenre.GenreId); // Update var updateGameGenre = new GameGenre( newId, updateGame.Id, updateGenre.Id); var updateCommand = updateGameGenre.GenerateUpdateStatement(); Assert.IsFalse(string.IsNullOrEmpty(updateCommand)); errorMessage = ""; var updateResult = connection.ExecuteCommand(updateCommand, ref errorMessage); Assert.AreEqual(updateResult, true); Assert.AreEqual(string.IsNullOrEmpty(errorMessage), true); // Exists existsQuery = updateGameGenre.GenerateExistsQuery(); Assert.IsFalse(string.IsNullOrEmpty(existsQuery)); errorMessage = ""; existsResult = connection.ExecuteQuery(existsQuery, ref errorMessage); Assert.AreEqual(string.IsNullOrEmpty(errorMessage), true); Assert.IsNotNull(existsResult); existsResultList = DataSetUtility.ToDictionaryList(existsResult.Tables[0]); recordExists = existsResultList.Any(dictionary => (dictionary != null) && (dictionary.Count > 0)); Assert.AreEqual(recordExists, true); // Select selectQuery = updateGameGenre.GenerateSelectQuery(); Assert.IsFalse(string.IsNullOrEmpty(selectQuery)); errorMessage = ""; selectResult = connection.ExecuteQuery(selectQuery, ref errorMessage); Assert.AreEqual(string.IsNullOrEmpty(errorMessage), true); Assert.IsNotNull(selectResult); selectResultList = DataSetUtility.ToDictionaryList(selectResult.Tables[0]); foundGameGenre = null; if (selectResultList.Count > 0) { foreach (var dictionary in selectResultList.Where(dictionary => (dictionary != null) && (dictionary.Count > 0))) { foundGameGenre = GameGenre.FromDictionary(dictionary); break; } } Assert.IsNotNull(foundGameGenre); Assert.AreNotSame(updateGameGenre, foundGameGenre); Assert.AreEqual(updateGameGenre.Id, foundGameGenre.Id); Assert.AreEqual(updateGameGenre.GameId, foundGameGenre.GameId); Assert.AreEqual(updateGameGenre.GenreId, foundGameGenre.GenreId); // Delete var deleteCommand = gameGenre.GenerateDeleteStatement(); var deleteGenre = genre.GenerateDeleteStatement(); Assert.IsFalse(string.IsNullOrEmpty(deleteCommand)); errorMessage = ""; var deleteResult = connection.ExecuteCommand(deleteCommand, ref errorMessage); Assert.AreEqual(deleteResult, true); Assert.AreEqual(string.IsNullOrEmpty(errorMessage), true); // Exists existsQuery = gameGenre.GenerateExistsQuery(); Assert.IsFalse(string.IsNullOrEmpty(existsQuery)); errorMessage = ""; existsResult = connection.ExecuteQuery(existsQuery, ref errorMessage); Assert.AreEqual(string.IsNullOrEmpty(errorMessage), true); Assert.IsNotNull(existsResult); existsResultList = DataSetUtility.ToDictionaryList(existsResult.Tables[0]); recordExists = existsResultList.Any(dictionary => (dictionary != null) && (dictionary.Count > 0)); Assert.IsFalse(recordExists); // Select selectQuery = gameGenre.GenerateSelectQuery(); Assert.IsFalse(string.IsNullOrEmpty(selectQuery)); errorMessage = ""; selectResult = connection.ExecuteQuery(selectQuery, ref errorMessage); Assert.AreEqual(string.IsNullOrEmpty(errorMessage), true); Assert.IsNotNull(selectResult); selectResultList = DataSetUtility.ToDictionaryList(selectResult.Tables[0]); foundGameGenre = null; if (selectResultList.Count > 0) { foreach (var dictionary in selectResultList.Where(dictionary => (dictionary != null) && (dictionary.Count > 0))) { foundGameGenre = GameGenre.FromDictionary(dictionary); break; } } Assert.IsNull(foundGameGenre); // Delete the genres deleteCommand = genre.GenerateDeleteStatement(); Assert.IsFalse(string.IsNullOrEmpty(deleteCommand)); errorMessage = ""; deleteResult = connection.ExecuteCommand(deleteCommand, ref errorMessage); Assert.AreEqual(deleteResult, true); Assert.AreEqual(string.IsNullOrEmpty(errorMessage), true); deleteCommand = updateGenre.GenerateDeleteStatement(); Assert.IsFalse(string.IsNullOrEmpty(deleteCommand)); errorMessage = ""; deleteResult = connection.ExecuteCommand(deleteCommand, ref errorMessage); Assert.AreEqual(deleteResult, true); Assert.AreEqual(string.IsNullOrEmpty(errorMessage), true); // Delete the games deleteCommand = game.GenerateDeleteStatement(); Assert.IsFalse(string.IsNullOrEmpty(deleteCommand)); errorMessage = ""; deleteResult = connection.ExecuteCommand(deleteCommand, ref errorMessage); Assert.AreEqual(deleteResult, true); Assert.AreEqual(string.IsNullOrEmpty(errorMessage), true); deleteCommand = updateGame.GenerateDeleteStatement(); Assert.IsFalse(string.IsNullOrEmpty(deleteCommand)); errorMessage = ""; deleteResult = connection.ExecuteCommand(deleteCommand, ref errorMessage); Assert.AreEqual(deleteResult, true); Assert.AreEqual(string.IsNullOrEmpty(errorMessage), true); }