|
| 1 | +#region License |
| 2 | + |
| 3 | +//The contents of this file are subject to the Mozilla Public License |
| 4 | +//Version 1.1 (the "License"); you may not use this file except in |
| 5 | +//compliance with the License. You may obtain a copy of the License at |
| 6 | +//http://www.mozilla.org/MPL/ |
| 7 | +//Software distributed under the License is distributed on an "AS IS" |
| 8 | +//basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
| 9 | +//License for the specific language governing rights and limitations |
| 10 | +//under the License. |
| 11 | + |
| 12 | +#endregion |
| 13 | + |
| 14 | +using System.Linq; |
| 15 | +using DotNetProjects.Migrator.Framework; |
| 16 | +using DotNetProjects.Migrator.Providers.Impl.SQLite; |
| 17 | +using Migrator.Providers.SQLite; |
| 18 | +using Migrator.Tests.Settings; |
| 19 | +using NUnit.Framework; |
| 20 | + |
| 21 | +namespace Migrator.Tests.Providers; |
| 22 | + |
| 23 | +[TestFixture] |
| 24 | +[Category("SQLite")] |
| 25 | +public class SQLiteTransformationProviderAddForeignKeyTests : TransformationProviderBase |
| 26 | +{ |
| 27 | + [SetUp] |
| 28 | + public void SetUp() |
| 29 | + { |
| 30 | + var configReader = new ConfigurationReader(); |
| 31 | + var connectionString = configReader.GetDatabaseConnectionConfigById("SQLiteConnectionString") |
| 32 | + .ConnectionString; |
| 33 | + |
| 34 | + _provider = new SQLiteTransformationProvider(new SQLiteDialect(), connectionString, "default", null); |
| 35 | + _provider.BeginTransaction(); |
| 36 | + |
| 37 | + AddDefaultTable(); |
| 38 | + } |
| 39 | + |
| 40 | + [Test] |
| 41 | + public void AddForeignKey() |
| 42 | + { |
| 43 | + // Arrange |
| 44 | + AddTableWithPrimaryKey(); |
| 45 | + _provider.ExecuteNonQuery("INSERT INTO Test (Id, name) VALUES (1, 'my name')"); |
| 46 | + _provider.ExecuteNonQuery("INSERT INTO TestTwo (TestId) VALUES (1)"); |
| 47 | + |
| 48 | + // Act |
| 49 | + _provider.AddForeignKey("FK name is not supported by SQLite", parentTable: "Test", parentColumn: "Id", childTable: "TestTwo", childColumn: "TestId", ForeignKeyConstraintType.Cascade); |
| 50 | + |
| 51 | + // Assert |
| 52 | + var foreignKeyConstraints = ((SQLiteTransformationProvider)_provider).GetForeignKeyConstraints("TestTwo"); |
| 53 | + var tableSQLCreateScript = ((SQLiteTransformationProvider)_provider).GetSqlCreateTableScript("TestTwo"); |
| 54 | + |
| 55 | + Assert.That(foreignKeyConstraints.Single().Name, Is.Null); |
| 56 | + Assert.That(foreignKeyConstraints.Single().ChildTable, Is.EqualTo("TestTwo")); |
| 57 | + Assert.That(foreignKeyConstraints.Single().ParentTable, Is.EqualTo("Test")); |
| 58 | + Assert.That(foreignKeyConstraints.Single().ChildColumns.Single(), Is.EqualTo("TestId")); |
| 59 | + Assert.That(foreignKeyConstraints.Single().ParentColumns.Single(), Is.EqualTo("Id")); |
| 60 | + // Cascade is not supported |
| 61 | + |
| 62 | + Assert.That(tableSQLCreateScript, Does.Contain("CREATE TABLE \"TestTwo\"")); |
| 63 | + Assert.That(tableSQLCreateScript, Does.Contain(", FOREIGN KEY (TestId) REFERENCES Test(Id))")); |
| 64 | + |
| 65 | + var result = ((SQLiteTransformationProvider)_provider).CheckForeignKeyIntegrity(); |
| 66 | + Assert.That(result, Is.True); |
| 67 | + } |
| 68 | + |
| 69 | + [Test] |
| 70 | + public void AddForeignKey_RenameParentColumWithForeignKeyAndData_ForeignKeyPointsToRenamedColumn() |
| 71 | + { |
| 72 | + // Arrange |
| 73 | + AddTableWithPrimaryKey(); |
| 74 | + _provider.ExecuteNonQuery("INSERT INTO Test (Id, name) VALUES (1, 'my name')"); |
| 75 | + _provider.ExecuteNonQuery("INSERT INTO TestTwo (TestId) VALUES (1)"); |
| 76 | + _provider.AddForeignKey("FK name is not supported by SQLite", parentTable: "Test", parentColumn: "Id", childTable: "TestTwo", childColumn: "TestId", ForeignKeyConstraintType.Cascade); |
| 77 | + |
| 78 | + // Act |
| 79 | + _provider.RenameColumn("Test", "Id", "IdNew"); |
| 80 | + |
| 81 | + // Assert |
| 82 | + var foreignKeyConstraints = ((SQLiteTransformationProvider)_provider).GetForeignKeyConstraints("TestTwo"); |
| 83 | + var tableSQLCreateScript = ((SQLiteTransformationProvider)_provider).GetSqlCreateTableScript("TestTwo"); |
| 84 | + |
| 85 | + Assert.That(tableSQLCreateScript, Does.Contain("CREATE TABLE \"TestTwo\"")); |
| 86 | + Assert.That(tableSQLCreateScript, Does.Contain(", FOREIGN KEY (TestId) REFERENCES Test(Id))")); |
| 87 | + |
| 88 | + var result = ((SQLiteTransformationProvider)_provider).CheckForeignKeyIntegrity(); |
| 89 | + Assert.That(result, Is.True); |
| 90 | + } |
| 91 | +} |
0 commit comments