|
| 1 | +#region Copyright Attributions |
| 2 | + |
| 3 | +// Copyright (c) 2026 Michael Camara Pendon. |
| 4 | +// Licensed under the Apache License, Version 2.0. |
| 5 | +// See the LICENSE file in the project root for full license information. |
| 6 | + |
| 7 | +#endregion |
| 8 | + |
| 9 | +using System; |
| 10 | +using System.Linq; |
| 11 | +using System.Threading.Tasks; |
| 12 | +using RepoDb.Connector.AuroraDb.MySqlConnector; |
| 13 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 14 | +using RepoDb.Data.Enumerations; |
| 15 | +using RepoDb.Data.AuroraDbMySqlConnector.IntegrationTests.Setup; |
| 16 | +using RepoDb.Enumerations; |
| 17 | +using RepoDb.Schema; |
| 18 | +using RepoDb.Schema.Enumerations; |
| 19 | + |
| 20 | +namespace RepoDb.Data.AuroraDbMySqlConnector.IntegrationTests |
| 21 | +{ |
| 22 | + /// <summary> |
| 23 | + /// Copies the rows of a table into a target table of another schema (the schema of the name of the target table, i.e.: <c>Sales.Person</c>). |
| 24 | + /// The tables are created in that schema, and the copy of the schema requires the source and the target tables to have the same name. |
| 25 | + /// </summary> |
| 26 | + [TestClass] |
| 27 | + public class CopyDataToTargetSchemaTest |
| 28 | + { |
| 29 | + [TestInitialize] |
| 30 | + public void Initialize() |
| 31 | + { |
| 32 | + Database.Initialize(); |
| 33 | + Cleanup(); |
| 34 | + } |
| 35 | + |
| 36 | + [TestCleanup] |
| 37 | + public void Cleanup() |
| 38 | + { |
| 39 | + SchemaReaderMapper.Clear(); |
| 40 | + Database.Cleanup(); |
| 41 | + } |
| 42 | + |
| 43 | + #region Sync |
| 44 | + |
| 45 | + [TestMethod] |
| 46 | + public void TestCopyDataToWithATargetTableOfAnotherSchemaCreatesTheTableInThatSchema() |
| 47 | + { |
| 48 | + using (var source = new AuroraDbConnection(Database.ConnectionStringForSource)) |
| 49 | + using (var target = new AuroraDbConnection(Database.ConnectionStringForTarget)) |
| 50 | + { |
| 51 | + // Setup |
| 52 | + Helper.MapSchemaReader(source); |
| 53 | + |
| 54 | + // Act |
| 55 | + var actual = source.CopyDataTo("Currency", "repodb_amy_target_sales.Currency", target); |
| 56 | + |
| 57 | + // Assert |
| 58 | + Assert.AreEqual(3, actual); |
| 59 | + CollectionAssert.AreEqual(new[] { "`Sales`.`Currency`" }, Helper.TargetTables().ToArray()); |
| 60 | + Helper.AssertRowsEqual(Helper.SourceRows("`Currency`", "`Code`"), Helper.TargetRows("`Sales`.`Currency`", "`Code`")); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + [TestMethod] |
| 65 | + public void TestCopyDataToWithAQuotedTargetTableOfAnotherSchema() |
| 66 | + { |
| 67 | + using (var source = new AuroraDbConnection(Database.ConnectionStringForSource)) |
| 68 | + using (var target = new AuroraDbConnection(Database.ConnectionStringForTarget)) |
| 69 | + { |
| 70 | + // Setup |
| 71 | + Helper.MapSchemaReader(source); |
| 72 | + |
| 73 | + // Act |
| 74 | + var actual = source.CopyDataTo("Currency", "`repodb_amy_target_sales`.`Currency`", target); |
| 75 | + |
| 76 | + // Assert |
| 77 | + Assert.AreEqual(3, actual); |
| 78 | + Assert.AreEqual(3, Helper.TargetCount("`Sales`.`Currency`")); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + [TestMethod] |
| 83 | + public void TestCopyDataToWithATargetTableOfTheDefaultSchema() |
| 84 | + { |
| 85 | + using (var source = new AuroraDbConnection(Database.ConnectionStringForSource)) |
| 86 | + using (var target = new AuroraDbConnection(Database.ConnectionStringForTarget)) |
| 87 | + { |
| 88 | + // Setup |
| 89 | + Helper.MapSchemaReader(source); |
| 90 | + |
| 91 | + // Act |
| 92 | + var actual = source.CopyDataTo("Currency", "repodb_amy_target.Currency", target); |
| 93 | + |
| 94 | + // Assert |
| 95 | + Assert.AreEqual(3, actual); |
| 96 | + CollectionAssert.AreEqual(new[] { "`Currency`" }, Helper.TargetTables().ToArray()); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + [TestMethod] |
| 101 | + public void TestCopyDataToFromATableOfAnotherSchemaIntoTheDefaultSchema() |
| 102 | + { |
| 103 | + using (var source = new AuroraDbConnection(Database.ConnectionStringForSource)) |
| 104 | + using (var target = new AuroraDbConnection(Database.ConnectionStringForTarget)) |
| 105 | + { |
| 106 | + // Setup |
| 107 | + Helper.MapSchemaReader(source); |
| 108 | + |
| 109 | + // Act |
| 110 | + var actual = source.CopyDataTo("repodb_amy_source_sales.Invoice", "repodb_amy_target.Invoice", target); |
| 111 | + |
| 112 | + // Assert |
| 113 | + Assert.AreEqual(2, actual); |
| 114 | + Helper.AssertRowsEqual(Helper.SourceRows("`Sales`.`Invoice`", "`Id`"), Helper.TargetRows("`Invoice`", "`Id`")); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + [TestMethod] |
| 119 | + public void TestCopyDataToWithATargetTableOfAnotherSchemaAddsTheRowsToTheTableThatExists() |
| 120 | + { |
| 121 | + using (var source = new AuroraDbConnection(Database.ConnectionStringForSource)) |
| 122 | + using (var target = new AuroraDbConnection(Database.ConnectionStringForTarget)) |
| 123 | + { |
| 124 | + // Setup |
| 125 | + Helper.MapSchemaReader(source); |
| 126 | + Helper.ExecuteOnTarget("CREATE TABLE `Sales`.`Currency` (`Code` CHAR(3) NOT NULL, `Name` VARCHAR(50) NOT NULL, CONSTRAINT `PK_Currency` PRIMARY KEY (`Code`)); INSERT INTO `Sales`.`Currency` (`Code`, `Name`) VALUES ('USD', 'US Dollar');"); |
| 127 | + |
| 128 | + // Act |
| 129 | + var actual = source.CopyDataTo("Currency", "repodb_amy_target_sales.Currency", target); |
| 130 | + |
| 131 | + // Assert |
| 132 | + Assert.AreEqual(3, actual); |
| 133 | + Assert.AreEqual(4, Helper.TargetCount("`Sales`.`Currency`")); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + [TestMethod] |
| 138 | + public void TestCopyDataToWithATargetTableOfAnotherSchemaAndParentsCopiesTheParentsIntoThatSchema() |
| 139 | + { |
| 140 | + using (var source = new AuroraDbConnection(Database.ConnectionStringForSource)) |
| 141 | + using (var target = new AuroraDbConnection(Database.ConnectionStringForTarget)) |
| 142 | + { |
| 143 | + // Setup |
| 144 | + Helper.MapSchemaReader(source); |
| 145 | + |
| 146 | + // Act |
| 147 | + var actual = source.CopyDataTo("Person", "repodb_amy_target_sales.Person", target, relationshipBehavior: CopyDataRelationshipBehavior.Parents); |
| 148 | + |
| 149 | + // Assert |
| 150 | + Assert.AreEqual(3 + 6, actual); |
| 151 | + CollectionAssert.AreEqual(new[] { "`Sales`.`Country`", "`Sales`.`Person`" }, Helper.TargetTables().ToArray()); |
| 152 | + Helper.AssertRowsEqual(Helper.SourceRows("`Country`", "`Id`"), Helper.TargetRows("`Sales`.`Country`", "`Id`")); |
| 153 | + Helper.AssertRowsEqual(Helper.SourceRows("`Person`", "`Id`"), Helper.TargetRows("`Sales`.`Person`", "`Id`")); |
| 154 | + Assert.AreEqual(0, Helper.TargetOrphans("`Sales`.`Person`", "`CountryId`", "`Sales`.`Country`", "`Id`")); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + [TestMethod] |
| 159 | + public void TestCopyDataToWithATargetTableOfAnotherSchemaAndChildrenCopiesTheChildrenIntoThatSchema() |
| 160 | + { |
| 161 | + using (var source = new AuroraDbConnection(Database.ConnectionStringForSource)) |
| 162 | + using (var target = new AuroraDbConnection(Database.ConnectionStringForTarget)) |
| 163 | + { |
| 164 | + // Setup |
| 165 | + Helper.MapSchemaReader(source); |
| 166 | + |
| 167 | + // Act |
| 168 | + var actual = source.CopyDataTo("Country", "repodb_amy_target_sales.Country", target, relationshipBehavior: CopyDataRelationshipBehavior.Children); |
| 169 | + |
| 170 | + // Assert |
| 171 | + Assert.AreEqual(3 + 6 + 5 + 5, actual); |
| 172 | + CollectionAssert.AreEqual(new[] { "`Sales`.`Country`", "`Sales`.`Person`", "`Sales`.`Pet`", "`Sales`.`Toy`" }, Helper.TargetTables().ToArray()); |
| 173 | + Assert.AreEqual(0, Helper.TargetOrphans("`Sales`.`Toy`", "`PetId`", "`Sales`.`Pet`", "`Id`")); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + [TestMethod] |
| 178 | + public void TestCopyDataToWithATargetTableOfAnotherSchemaAndAFilterFiltersTheRequestedTableOnly() |
| 179 | + { |
| 180 | + using (var source = new AuroraDbConnection(Database.ConnectionStringForSource)) |
| 181 | + using (var target = new AuroraDbConnection(Database.ConnectionStringForTarget)) |
| 182 | + { |
| 183 | + // Setup |
| 184 | + Helper.MapSchemaReader(source); |
| 185 | + |
| 186 | + // Act |
| 187 | + var actual = source.CopyDataTo("Person", "repodb_amy_target_sales.Person", target, where: new QueryGroup(new QueryField("Age", Operation.GreaterThan, 40)), relationshipBehavior: CopyDataRelationshipBehavior.Parents); |
| 188 | + |
| 189 | + // Assert |
| 190 | + Assert.AreEqual(3 + 2, actual); |
| 191 | + Assert.AreEqual(2, Helper.TargetCount("`Sales`.`Person`")); |
| 192 | + Assert.AreEqual(3, Helper.TargetCount("`Sales`.`Country`")); |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + [TestMethod] |
| 197 | + public void TestCopyDataToWithATargetTableOfAnotherSchemaAndDropReplacesTheTableThatExists() |
| 198 | + { |
| 199 | + using (var source = new AuroraDbConnection(Database.ConnectionStringForSource)) |
| 200 | + using (var target = new AuroraDbConnection(Database.ConnectionStringForTarget)) |
| 201 | + { |
| 202 | + // Setup |
| 203 | + Helper.MapSchemaReader(source); |
| 204 | + source.CopyDataTo("Currency", "repodb_amy_target_sales.Currency", target); |
| 205 | + |
| 206 | + // Act |
| 207 | + var actual = source.CopyDataTo("Currency", "repodb_amy_target_sales.Currency", target, tableExistenceBehavior: CopySchemaExistsBehavior.Drop); |
| 208 | + |
| 209 | + // Assert |
| 210 | + Assert.AreEqual(3, actual); |
| 211 | + Assert.AreEqual(3, Helper.TargetCount("`Sales`.`Currency`")); |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + [TestMethod] |
| 216 | + public void ThrowExceptionOnCopyDataToIfTheTablesHaveDifferentNamesAndTheTargetTableHasASchema() |
| 217 | + { |
| 218 | + using (var source = new AuroraDbConnection(Database.ConnectionStringForSource)) |
| 219 | + using (var target = new AuroraDbConnection(Database.ConnectionStringForTarget)) |
| 220 | + { |
| 221 | + // Setup |
| 222 | + Helper.MapSchemaReader(source); |
| 223 | + |
| 224 | + // Act/Assert |
| 225 | + Assert.Throws<ArgumentException>(() => source.CopyDataTo("Currency", "repodb_amy_target_sales.Money", target)); |
| 226 | + Assert.AreEqual(0, Helper.TargetTables().Count); |
| 227 | + } |
| 228 | + } |
| 229 | + |
| 230 | + [TestMethod] |
| 231 | + public void ThrowExceptionOnCopyDataToIfTheTargetSchemaDoesNotExist() |
| 232 | + { |
| 233 | + using (var source = new AuroraDbConnection(Database.ConnectionStringForSource)) |
| 234 | + using (var target = new AuroraDbConnection(Database.ConnectionStringForTarget)) |
| 235 | + { |
| 236 | + // Setup |
| 237 | + Helper.MapSchemaReader(source); |
| 238 | + |
| 239 | + // Act/Assert |
| 240 | + Assert.ThrowsExactly<AuroraDbException>(() => source.CopyDataTo("Currency", "Missing.Currency", target)); |
| 241 | + Assert.AreEqual(0, Helper.TargetTables().Count); |
| 242 | + } |
| 243 | + } |
| 244 | + |
| 245 | + [TestMethod] |
| 246 | + public void ThrowExceptionOnCopyDataToIfTheTableReferencesATableThatDoesNotExistInTheTargetSchema() |
| 247 | + { |
| 248 | + using (var source = new AuroraDbConnection(Database.ConnectionStringForSource)) |
| 249 | + using (var target = new AuroraDbConnection(Database.ConnectionStringForTarget)) |
| 250 | + { |
| 251 | + // Setup |
| 252 | + Helper.MapSchemaReader(source); |
| 253 | + |
| 254 | + // Act/Assert |
| 255 | + Assert.ThrowsExactly<AuroraDbException>(() => source.CopyDataTo("Person", "repodb_amy_target_sales.Person", target)); |
| 256 | + Assert.AreEqual(0, Helper.TargetCount("`Sales`.`Person`")); |
| 257 | + } |
| 258 | + } |
| 259 | + |
| 260 | + [TestMethod] |
| 261 | + public void ThrowExceptionOnCopyDataToIfTheTargetTableOfAnotherSchemaIsCopiedTwiceBecauseTheRowsAlreadyExist() |
| 262 | + { |
| 263 | + using (var source = new AuroraDbConnection(Database.ConnectionStringForSource)) |
| 264 | + using (var target = new AuroraDbConnection(Database.ConnectionStringForTarget)) |
| 265 | + { |
| 266 | + // Setup |
| 267 | + Helper.MapSchemaReader(source); |
| 268 | + source.CopyDataTo("Currency", "repodb_amy_target_sales.Currency", target); |
| 269 | + |
| 270 | + // Act/Assert |
| 271 | + Assert.Throws<MySqlConnector.MySqlException>(() => source.CopyDataTo("Currency", "repodb_amy_target_sales.Currency", target)); |
| 272 | + Assert.AreEqual(3, Helper.TargetCount("`Sales`.`Currency`")); |
| 273 | + } |
| 274 | + } |
| 275 | + |
| 276 | + #endregion |
| 277 | + |
| 278 | + #region Async |
| 279 | + |
| 280 | + [TestMethod] |
| 281 | + public async Task TestCopyDataToAsyncWithATargetTableOfAnotherSchemaCreatesTheTableInThatSchema() |
| 282 | + { |
| 283 | + using (var source = new AuroraDbConnection(Database.ConnectionStringForSource)) |
| 284 | + using (var target = new AuroraDbConnection(Database.ConnectionStringForTarget)) |
| 285 | + { |
| 286 | + // Setup |
| 287 | + Helper.MapSchemaReader(source); |
| 288 | + |
| 289 | + // Act |
| 290 | + var actual = await source.CopyDataToAsync("Currency", "repodb_amy_target_sales.Currency", target); |
| 291 | + |
| 292 | + // Assert |
| 293 | + Assert.AreEqual(3, actual); |
| 294 | + Helper.AssertRowsEqual(Helper.SourceRows("`Currency`", "`Code`"), Helper.TargetRows("`Sales`.`Currency`", "`Code`")); |
| 295 | + } |
| 296 | + } |
| 297 | + |
| 298 | + [TestMethod] |
| 299 | + public async Task TestCopyDataToAsyncWithATargetTableOfAnotherSchemaAndParentsCopiesTheParentsIntoThatSchema() |
| 300 | + { |
| 301 | + using (var source = new AuroraDbConnection(Database.ConnectionStringForSource)) |
| 302 | + using (var target = new AuroraDbConnection(Database.ConnectionStringForTarget)) |
| 303 | + { |
| 304 | + // Setup |
| 305 | + Helper.MapSchemaReader(source); |
| 306 | + |
| 307 | + // Act |
| 308 | + var actual = await source.CopyDataToAsync("Person", "repodb_amy_target_sales.Person", target, relationshipBehavior: CopyDataRelationshipBehavior.Parents); |
| 309 | + |
| 310 | + // Assert |
| 311 | + Assert.AreEqual(3 + 6, actual); |
| 312 | + CollectionAssert.AreEqual(new[] { "`Sales`.`Country`", "`Sales`.`Person`" }, Helper.TargetTables().ToArray()); |
| 313 | + } |
| 314 | + } |
| 315 | + |
| 316 | + [TestMethod] |
| 317 | + public async Task ThrowExceptionOnCopyDataToAsyncIfTheTablesHaveDifferentNamesAndTheTargetTableHasASchema() |
| 318 | + { |
| 319 | + using (var source = new AuroraDbConnection(Database.ConnectionStringForSource)) |
| 320 | + using (var target = new AuroraDbConnection(Database.ConnectionStringForTarget)) |
| 321 | + { |
| 322 | + // Setup |
| 323 | + Helper.MapSchemaReader(source); |
| 324 | + |
| 325 | + // Act/Assert |
| 326 | + await Assert.ThrowsAsync<ArgumentException>(() => source.CopyDataToAsync("Currency", "repodb_amy_target_sales.Money", target)); |
| 327 | + } |
| 328 | + } |
| 329 | + |
| 330 | + [TestMethod] |
| 331 | + public async Task ThrowExceptionOnCopyDataToAsyncIfTheTargetSchemaDoesNotExist() |
| 332 | + { |
| 333 | + using (var source = new AuroraDbConnection(Database.ConnectionStringForSource)) |
| 334 | + using (var target = new AuroraDbConnection(Database.ConnectionStringForTarget)) |
| 335 | + { |
| 336 | + // Setup |
| 337 | + Helper.MapSchemaReader(source); |
| 338 | + |
| 339 | + // Act/Assert |
| 340 | + await Assert.ThrowsExactlyAsync<AuroraDbException>(() => source.CopyDataToAsync("Currency", "Missing.Currency", target)); |
| 341 | + } |
| 342 | + } |
| 343 | + |
| 344 | + #endregion |
| 345 | + } |
| 346 | +} |
0 commit comments