Skip to content

Commit

Permalink
Additional tests for Temporal tables (#26682)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaliyaudagedara committed Nov 18, 2021
1 parent 71fa384 commit 6da773f
Showing 1 changed file with 118 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2288,6 +2288,122 @@ public virtual async Task Create_temporal_table_with_default_schema_for_model_ch
) WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = [mySchema].[CustomersHistory]));");
}

[ConditionalFact]
public virtual async Task Create_temporal_table_with_default_schema_for_model_changed_and_explicit_history_table_schema_not_provided()
{
await Test(
builder => { },
builder =>
{
builder.HasDefaultSchema("myDefaultSchema");
builder.Entity(
"Customer", e =>
{
e.Property<int>("Id").ValueGeneratedOnAdd();
e.Property<string>("Name");
e.Property<DateTime>("SystemTimeStart").ValueGeneratedOnAddOrUpdate();
e.Property<DateTime>("SystemTimeEnd").ValueGeneratedOnAddOrUpdate();
e.HasKey("Id");
e.ToTable("Customers", tb => tb.IsTemporal(ttb =>
{
ttb.UseHistoryTable("HistoryTable");
ttb.HasPeriodStart("SystemTimeStart");
ttb.HasPeriodEnd("SystemTimeEnd");
}));
});
},
model =>
{
var table = Assert.Single(model.Tables);
Assert.Equal("Customers", table.Name);
Assert.Equal(true, table[SqlServerAnnotationNames.IsTemporal]);
Assert.Equal("HistoryTable", table[SqlServerAnnotationNames.TemporalHistoryTableName]);
Assert.Equal("myDefaultSchema", table[SqlServerAnnotationNames.TemporalHistoryTableSchema]);
Assert.Equal("SystemTimeStart", table[SqlServerAnnotationNames.TemporalPeriodStartPropertyName]);
Assert.Equal("SystemTimeEnd", table[SqlServerAnnotationNames.TemporalPeriodEndPropertyName]);
Assert.Collection(
table.Columns,
c => Assert.Equal("Id", c.Name),
c => Assert.Equal("Name", c.Name));
Assert.Same(
table.Columns.Single(c => c.Name == "Id"),
Assert.Single(table.PrimaryKey!.Columns));
});

AssertSql(
@"IF SCHEMA_ID(N'myDefaultSchema') IS NULL EXEC(N'CREATE SCHEMA [myDefaultSchema];');",
//
@"CREATE TABLE [myDefaultSchema].[Customers] (
[Id] int NOT NULL,
[Name] nvarchar(max) NULL,
[SystemTimeEnd] datetime2 GENERATED ALWAYS AS ROW END HIDDEN NOT NULL,
[SystemTimeStart] datetime2 GENERATED ALWAYS AS ROW START HIDDEN NOT NULL,
CONSTRAINT [PK_Customers] PRIMARY KEY ([Id]),
PERIOD FOR SYSTEM_TIME([SystemTimeStart], [SystemTimeEnd])
) WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = [myDefaultSchema].[HistoryTable]));");
}

[ConditionalFact]
public virtual async Task Create_temporal_table_with_default_schema_for_model_changed_and_explicit_history_table_schema_provided()
{
await Test(
builder => { },
builder =>
{
builder.HasDefaultSchema("myDefaultSchema");
builder.Entity(
"Customer", e =>
{
e.Property<int>("Id").ValueGeneratedOnAdd();
e.Property<string>("Name");
e.Property<DateTime>("SystemTimeStart").ValueGeneratedOnAddOrUpdate();
e.Property<DateTime>("SystemTimeEnd").ValueGeneratedOnAddOrUpdate();
e.HasKey("Id");
e.ToTable("Customers", tb => tb.IsTemporal(ttb =>
{
ttb.UseHistoryTable("HistoryTable", "historySchema");
ttb.HasPeriodStart("SystemTimeStart");
ttb.HasPeriodEnd("SystemTimeEnd");
}));
});
},
model =>
{
var table = Assert.Single(model.Tables);
Assert.Equal("Customers", table.Name);
Assert.Equal(true, table[SqlServerAnnotationNames.IsTemporal]);
Assert.Equal("HistoryTable", table[SqlServerAnnotationNames.TemporalHistoryTableName]);
Assert.Equal("historySchema", table[SqlServerAnnotationNames.TemporalHistoryTableSchema]);
Assert.Equal("SystemTimeStart", table[SqlServerAnnotationNames.TemporalPeriodStartPropertyName]);
Assert.Equal("SystemTimeEnd", table[SqlServerAnnotationNames.TemporalPeriodEndPropertyName]);
Assert.Collection(
table.Columns,
c => Assert.Equal("Id", c.Name),
c => Assert.Equal("Name", c.Name));
Assert.Same(
table.Columns.Single(c => c.Name == "Id"),
Assert.Single(table.PrimaryKey!.Columns));
});

AssertSql(
@"IF SCHEMA_ID(N'myDefaultSchema') IS NULL EXEC(N'CREATE SCHEMA [myDefaultSchema];');",
//
@"IF SCHEMA_ID(N'historySchema') IS NULL EXEC(N'CREATE SCHEMA [historySchema];');",
//
@"CREATE TABLE [myDefaultSchema].[Customers] (
[Id] int NOT NULL,
[Name] nvarchar(max) NULL,
[SystemTimeEnd] datetime2 GENERATED ALWAYS AS ROW END HIDDEN NOT NULL,
[SystemTimeStart] datetime2 GENERATED ALWAYS AS ROW START HIDDEN NOT NULL,
CONSTRAINT [PK_Customers] PRIMARY KEY ([Id]),
PERIOD FOR SYSTEM_TIME([SystemTimeStart], [SystemTimeEnd])
) WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = [historySchema].[HistoryTable]));");
}

[ConditionalFact]
public virtual async Task Create_temporal_table_with_default_schema_for_table_and_explicit_history_table_schema_provided()
{
Expand Down Expand Up @@ -2783,8 +2899,8 @@ public virtual async Task Remove_columns_from_temporal_table()
e.Property<int>("Number");
}),
builder =>
{
},
{
},
model =>
{
var table = Assert.Single(model.Tables);
Expand Down

0 comments on commit 6da773f

Please sign in to comment.