Skip to content

Commit

Permalink
Add inheritance tests
Browse files Browse the repository at this point in the history
  • Loading branch information
roji committed Aug 11, 2022
1 parent 68e89c4 commit 1e15aa4
Show file tree
Hide file tree
Showing 12 changed files with 382 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ public DbSet<Entity> WithOriginalAndCurrentValueOnNonConcurrencyToken
public DbSet<Entity> WithInputOutputParameterOnNonConcurrencyToken
=> Set<Entity>(nameof(WithInputOutputParameterOnNonConcurrencyToken));

public DbSet<TphParent> TphParent { get; set; }
public DbSet<TphChild> TphChild { get; set; }
public DbSet<TptParent> TptParent { get; set; }
public DbSet<TptChild> TptChild { get; set; }
public DbSet<TptMixedParent> TptMixedParent { get; set; }
public DbSet<TptMixedChild> TptMixedChild { get; set; }
public DbSet<TpcParent> TpcParent { get; set; }
public DbSet<TpcChild> TpcChild { get; set; }

public static void Clean(StoredProcedureUpdateContext context)
{
context.WithOutputParameter.RemoveRange(context.WithOutputParameter);
Expand All @@ -64,6 +73,14 @@ public static void Clean(StoredProcedureUpdateContext context)
context.WithUserManagedConcurrencyToken.RemoveRange(context.WithUserManagedConcurrencyToken);
context.WithOriginalAndCurrentValueOnNonConcurrencyToken.RemoveRange(context.WithOriginalAndCurrentValueOnNonConcurrencyToken);
context.WithInputOutputParameterOnNonConcurrencyToken.RemoveRange(context.WithInputOutputParameterOnNonConcurrencyToken);
context.TphParent.RemoveRange(context.TphParent);
context.TphChild.RemoveRange(context.TphChild);
context.TptParent.RemoveRange(context.TptParent);
context.TptChild.RemoveRange(context.TptChild);
context.TptMixedParent.RemoveRange(context.TptMixedParent);
context.TptMixedChild.RemoveRange(context.TptMixedChild);
context.TpcParent.RemoveRange(context.TpcParent);
context.TpcChild.RemoveRange(context.TpcChild);

context.SaveChanges();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.EntityFrameworkCore.TestModels.StoredProcedureUpdateModel;

public class TpcChild : TpcParent
{
public int ChildProperty { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.EntityFrameworkCore.TestModels.StoredProcedureUpdateModel;

public class TpcParent
{
public int Id { get; set; }
public string Name { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.EntityFrameworkCore.TestModels.StoredProcedureUpdateModel;

public class TphChild : TphParent
{
public int ChildProperty { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.EntityFrameworkCore.TestModels.StoredProcedureUpdateModel;

public class TphParent
{
public int Id { get; set; }
public string Name { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.EntityFrameworkCore.TestModels.StoredProcedureUpdateModel;

public class TptChild : TptParent
{
public int ChildProperty { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.EntityFrameworkCore.TestModels.StoredProcedureUpdateModel;

public class TptMixedChild : TptMixedParent
{
public int ChildProperty { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.EntityFrameworkCore.TestModels.StoredProcedureUpdateModel;

public class TptMixedParent
{
public int Id { get; set; }
public string Name { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.EntityFrameworkCore.TestModels.StoredProcedureUpdateModel;

public class TptParent
{
public int Id { get; set; }
public string Name { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,76 @@ protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext con
b.Property(w => w.AdditionalProperty2).HasComputedColumnSql("9");
b.UpdateUsingStoredProcedure(
"WithOutputParameterAndResultColumnAndResultValue_Update", spb => spb
nameof(StoredProcedureUpdateContext.WithOutputParameterAndResultColumnAndResultValue) + "_Update",
spb => spb
.HasParameter(w => w.Id)
.HasParameter(w => w.Name)
.HasParameter(w => w.AdditionalProperty1, pb => pb.IsOutput())
.HasResultColumn(w => w.AdditionalProperty2)
.HasRowsAffectedReturnValue());
});

modelBuilder.Entity<TphParent>(
b =>
{
b.ToTable("Tph");
b.InsertUsingStoredProcedure(
"Tph_Insert",
spb => spb
.HasParameter(w => w.Id, pb => pb.IsOutput())
.HasParameter("Discriminator")
.HasParameter(w => w.Name)
.HasParameter(nameof(TphChild.ChildProperty)));
});

modelBuilder.Entity<TphChild>().ToTable("Tph");

modelBuilder.Entity<TptParent>(
b =>
{
b.UseTptMappingStrategy();
b.InsertUsingStoredProcedure(
"TptParent_Insert",
spb => spb
.HasParameter(w => w.Id, pb => pb.IsOutput())
.HasParameter(w => w.Name));
});

// TODO: The following fails validation:
// The entity type 'TptChild' is mapped to the stored procedure 'TptChild_Insert', however the store-generated properties {'Id'} are not mapped to any output parameter or result column.
modelBuilder.Entity<TptChild>()
.InsertUsingStoredProcedure(
"TptChild_Insert",
spb => spb
.HasParameter(w => w.Id)
.HasParameter(w => w.ChildProperty));

modelBuilder.Entity<TptMixedParent>(
b =>
{
b.UseTptMappingStrategy();
b.InsertUsingStoredProcedure(
"TptMixedParent_Insert",
spb => spb
.HasParameter(w => w.Id, pb => pb.IsOutput())
.HasParameter(w => w.Name));
});

// No sproc mapping for TptMixedChild, use regular SQL

modelBuilder.Entity<TpcParent>().UseTpcMappingStrategy();

modelBuilder.Entity<TpcChild>()
.UseTpcMappingStrategy()
.InsertUsingStoredProcedure(
"TpcChild_Insert",
spb => spb
.HasParameter(w => w.Id, pb => pb.IsOutput())
.HasParameter(w => w.Name)
.HasParameter(w => w.ChildProperty));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,90 @@ public virtual async Task Update_with_output_parameter_and_result_column_and_ret
}
}

// TODO: Hierarchy tests (TPC/TPT/TPH), mixed sproc+non-sproc in the same hierarchy
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task Tph(bool async)
{
await using var context = CreateContext();

var entity1 = new TphChild { Name = "Child", ChildProperty = 8 };
context.TphChild.Add(entity1);
await SaveChanges(context, async);

context.ChangeTracker.Clear();

using (Fixture.TestSqlLoggerFactory.SuspendRecordingEvents())
{
var entity2 = context.TphChild.Single(b => b.Id == entity1.Id);

Assert.Equal("Child", entity2.Name);
Assert.Equal(8, entity2.ChildProperty);
}
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task Tpt(bool async)
{
await using var context = CreateContext();

var entity1 = new TptChild { Name = "Child", ChildProperty = 8 };
context.TptChild.Add(entity1);
await SaveChanges(context, async);

context.ChangeTracker.Clear();

using (Fixture.TestSqlLoggerFactory.SuspendRecordingEvents())
{
var entity2 = context.TptChild.Single(b => b.Id == entity1.Id);

Assert.Equal("Child", entity2.Name);
Assert.Equal(8, entity2.ChildProperty);
}
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task Tpt_mixed_sproc_and_non_sproc(bool async)
{
await using var context = CreateContext();

var entity1 = new TptMixedChild { Name = "Child", ChildProperty = 8 };
context.TptMixedChild.Add(entity1);
await SaveChanges(context, async);

context.ChangeTracker.Clear();

using (Fixture.TestSqlLoggerFactory.SuspendRecordingEvents())
{
var entity2 = context.TptMixedChild.Single(b => b.Id == entity1.Id);

Assert.Equal("Child", entity2.Name);
Assert.Equal(8, entity2.ChildProperty);
}
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task Tpc(bool async)
{
await using var context = CreateContext();

var entity1 = new TpcChild { Name = "Child", ChildProperty = 8 };
context.TpcChild.Add(entity1);
await SaveChanges(context, async);

context.ChangeTracker.Clear();

using (Fixture.TestSqlLoggerFactory.SuspendRecordingEvents())
{
var entity2 = context.TpcChild.Single(b => b.Id == entity1.Id);

Assert.Equal("Child", entity2.Name);
Assert.Equal(8, entity2.ChildProperty);
}
}

private async Task SaveChanges(StoredProcedureUpdateContext context, bool async)
{
if (async)
Expand Down
Loading

0 comments on commit 1e15aa4

Please sign in to comment.