Skip to content

Commit

Permalink
Move test cases to WithConstructorsTestBase
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike-Wodarczyk committed Aug 4, 2020
1 parent 610d9cb commit 4bf2eb7
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

<PropertyGroup>
<Description>Shared test suite for Entity Framework Core database providers.</Description>
<TargetFrameworks>$(StandardTestTfms)</TargetFrameworks>
<TargetFrameworks>$(StandardTestTfms)</TargetFrameworks>
<LangVersion>9.0</LangVersion>
<AssemblyName>Microsoft.EntityFrameworkCore.Specification.Tests</AssemblyName>
<RootNamespace>Microsoft.EntityFrameworkCore</RootNamespace>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Expand Down
53 changes: 53 additions & 0 deletions test/EFCore.Specification.Tests/WithConstructorsTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,49 @@ public virtual void Query_with_loader_delegate_injected_into_property_via_constr
Assert.Same(blog, blog.LazyPcsPosts.Skip(1).First().LazyPcsBlog);
}

#if NET5_0
[ConditionalFact]
public virtual async Task Add_immutable_record()
{
using var context = CreateContext();
const string aBlogTitle = "xyzzy";
var immutableBlog = new BlogAsImmutableRecord(aBlogTitle);

context.Add(immutableBlog);
await context.SaveChangesAsync();

Assert.NotEqual(0, immutableBlog.BlogId);
Assert.Equal(aBlogTitle, immutableBlog.Title);
}
#endif


#if NET5_0
protected record BlogAsImmutableRecord
{
public int BlogId { get; init; }
public string Title { get; init; }
public int? MonthlyRevenue { get; init; }

private BlogAsImmutableRecord(
int blogId,
string title,
int? monthlyRevenue)
{
BlogId = blogId;
Title = title;
MonthlyRevenue = monthlyRevenue;
}

public BlogAsImmutableRecord(
string title,
int? monthlyRevenue = null)
: this(0, title, monthlyRevenue)
{
}
}
#endif

protected class Blog
{
private readonly int _blogId;
Expand Down Expand Up @@ -1539,6 +1582,16 @@ public abstract class WithConstructorsFixtureBase : SharedStoreFixtureBase<WithC

protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext context)
{

#if NET5_0
modelBuilder.Entity<BlogAsImmutableRecord>(
b =>
{
b.HasKey("_blogId");
b.Property(e => e.Title);
});
#endif

modelBuilder.Entity<Blog>(
b =>
{
Expand Down

0 comments on commit 4bf2eb7

Please sign in to comment.