|
| 1 | +using Microsoft.EntityFrameworkCore.Metadata.Internal; |
| 2 | +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata.Internal; |
| 3 | +using Npgsql.EntityFrameworkCore.PostgreSQL.Migrations.Internal; |
| 4 | + |
| 5 | +namespace Npgsql.EntityFrameworkCore.PostgreSQL.Migrations; |
| 6 | + |
| 7 | +public class NpgsqlMigrationsAnnotationProviderTest |
| 8 | +{ |
| 9 | + private readonly NpgsqlMigrationsAnnotationProvider _migrationsAnnotationProvider = new(new MigrationsAnnotationProviderDependencies()); |
| 10 | + |
| 11 | + [Fact] |
| 12 | + public virtual void ForRemove_returns_relational_model_annotations() |
| 13 | + { |
| 14 | + var modelBuilder = new ModelBuilder() |
| 15 | + .HasPostgresExtension("test_extension") |
| 16 | + .HasPostgresExtension("test_schema2", "test_extension2") |
| 17 | + .HasPostgresEnum("test_enum", ["A", "B", "C"]) |
| 18 | + .HasPostgresEnum("test_schema2", "test_enum2", ["A", "B", "C"]) |
| 19 | + .HasPostgresRange("test_range", "test_subtype") |
| 20 | + .HasPostgresRange("test_schema2", "test_range2", "test_subtype2") |
| 21 | + .HasCollation("test_collation", "test_locale") |
| 22 | + .HasCollation("test_schema2", "test_collation2", "test_locale2", "test_provider2", false); |
| 23 | + |
| 24 | + // Define a sequence, so we can verify that the annotation it creates is excluded from the ForRemove() result. |
| 25 | + modelBuilder.HasSequence("test_sequence"); |
| 26 | + |
| 27 | + var model = new RelationalModel(modelBuilder.FinalizeModel()); |
| 28 | + var allAnnotations = model.Model.GetAnnotations().ToList(); |
| 29 | + |
| 30 | + var annotations = _migrationsAnnotationProvider.ForRemove(model).ToList(); |
| 31 | + |
| 32 | + Assert.Equal(9, allAnnotations.Count); |
| 33 | + Assert.Equal(8, annotations.Count); |
| 34 | + Assert.Contains(annotations, a => a.Name == $"{NpgsqlAnnotationNames.PostgresExtensionPrefix}test_extension"); |
| 35 | + Assert.Contains(annotations, a => a.Name == $"{NpgsqlAnnotationNames.PostgresExtensionPrefix}test_schema2.test_extension2"); |
| 36 | + Assert.Contains(annotations, a => a.Name == $"{NpgsqlAnnotationNames.EnumPrefix}test_enum"); |
| 37 | + Assert.Contains(annotations, a => a.Name == $"{NpgsqlAnnotationNames.EnumPrefix}test_schema2.test_enum2"); |
| 38 | + Assert.Contains(annotations, a => a.Name == $"{NpgsqlAnnotationNames.RangePrefix}test_range"); |
| 39 | + Assert.Contains(annotations, a => a.Name == $"{NpgsqlAnnotationNames.RangePrefix}test_schema2.test_range2"); |
| 40 | + Assert.Contains(annotations, a => a.Name == $"{NpgsqlAnnotationNames.CollationDefinitionPrefix}test_collation"); |
| 41 | + Assert.Contains(annotations, a => a.Name == $"{NpgsqlAnnotationNames.CollationDefinitionPrefix}test_schema2.test_collation2"); |
| 42 | + } |
| 43 | + |
| 44 | + [Fact] |
| 45 | + public virtual void ForRemove_handles_no_annotations() |
| 46 | + { |
| 47 | + var model = new RelationalModel(new Model()); |
| 48 | + Assert.Empty(_migrationsAnnotationProvider.ForRemove(model)); |
| 49 | + } |
| 50 | +} |
0 commit comments