public void GamePlatformDictionaryTest() { var gamePlatform1 = new GamePlatform( 1, 2, 3); var dictionary = GamePlatform.ToDictionary(gamePlatform1); Assert.IsNotNull(dictionary); var gamePlatform2 = GamePlatform.FromDictionary(dictionary); Assert.AreNotSame(gamePlatform1, gamePlatform2); Assert.AreEqual(gamePlatform1.Id, gamePlatform2.Id); Assert.AreEqual(gamePlatform1.GameId, gamePlatform2.GameId); Assert.AreEqual(gamePlatform1.PlatformId, gamePlatform2.PlatformId); }
public void GamePlatformDatabaseCommandsTest() { var connection = new SqlServerConnection(); Assert.AreEqual(connection.IsConnected, false); var result = connection.Connect(ConnectionString, 0); Assert.AreEqual(result, true); // Add 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 Platforms var platform = new Platform(0, "Name", "Description"); insertCommand = platform.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); platform.Id = newId; var updatePlatform = new Platform(0, "Name", "Description"); insertCommand = updatePlatform.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); updatePlatform.Id = newId; // Select All var gamePlatform = new GamePlatform(); var selectQuery = gamePlatform.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 gamePlatform = new GamePlatform( 0, game.Id, platform.Id); insertCommand = gamePlatform.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); gamePlatform.Id = newId; // Exists var existsQuery = gamePlatform.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 = gamePlatform.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]); GamePlatform foundGamePlatform = null; if (selectResultList.Count > 0) { foreach (var dictionary in selectResultList.Where(dictionary => (dictionary != null) && (dictionary.Count > 0))) { foundGamePlatform = GamePlatform.FromDictionary(dictionary); break; } } Assert.IsNotNull(foundGamePlatform); Assert.AreNotSame(gamePlatform, foundGamePlatform); Assert.AreEqual(gamePlatform.Id, foundGamePlatform.Id); Assert.AreEqual(gamePlatform.GameId, foundGamePlatform.GameId); Assert.AreEqual(gamePlatform.PlatformId, foundGamePlatform.PlatformId); // Update var updateGamePlatform = new GamePlatform( newId, updateGame.Id, updatePlatform.Id); var updateCommand = updateGamePlatform.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 = updateGamePlatform.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 = updateGamePlatform.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]); foundGamePlatform = null; if (selectResultList.Count > 0) { foreach (var dictionary in selectResultList.Where(dictionary => (dictionary != null) && (dictionary.Count > 0))) { foundGamePlatform = GamePlatform.FromDictionary(dictionary); break; } } Assert.IsNotNull(foundGamePlatform); Assert.AreNotSame(updateGamePlatform, foundGamePlatform); Assert.AreEqual(updateGamePlatform.Id, foundGamePlatform.Id); Assert.AreEqual(updateGamePlatform.GameId, foundGamePlatform.GameId); Assert.AreEqual(updateGamePlatform.PlatformId, foundGamePlatform.PlatformId); // Delete var deleteCommand = gamePlatform.GenerateDeleteStatement(); var deleteGenre = platform.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 = gamePlatform.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 = gamePlatform.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]); foundGamePlatform = null; if (selectResultList.Count > 0) { foreach (var dictionary in selectResultList.Where(dictionary => (dictionary != null) && (dictionary.Count > 0))) { foundGamePlatform = GamePlatform.FromDictionary(dictionary); break; } } Assert.IsNull(foundGamePlatform); // Delete the platforms deleteCommand = platform.GenerateDeleteStatement(); Assert.IsFalse(string.IsNullOrEmpty(deleteCommand)); errorMessage = ""; deleteResult = connection.ExecuteCommand(deleteCommand, ref errorMessage); Assert.AreEqual(deleteResult, true); Assert.AreEqual(string.IsNullOrEmpty(errorMessage), true); deleteCommand = updatePlatform.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); }