Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional tests for Temporal tables #26682

Merged
merged 1 commit into from
Nov 18, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2288,6 +2288,122 @@ PERIOD FOR SYSTEM_TIME([SystemTimeStart], [SystemTimeEnd])
) 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 @@ await Test(
e.Property<int>("Number");
}),
builder =>
{
},
{
},
model =>
{
var table = Assert.Single(model.Tables);
Expand Down