Skip to content

Commit

Permalink
Add tests for IdentityServer model and queries
Browse files Browse the repository at this point in the history
Fixes #20547
  • Loading branch information
ajcvickers committed Jan 1, 2021
1 parent 03c1ba5 commit 9774a82
Show file tree
Hide file tree
Showing 15 changed files with 1,516 additions and 176 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Threading.Tasks;
using IdentityServer4.EntityFramework.DbContexts;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.TestUtilities;

namespace Microsoft.EntityFrameworkCore
{
public class ConfigurationDbContextInMemoryTest
: ConfigurationDbContextTestBase<ConfigurationDbContextInMemoryTest.ConfigurationDbContextInMemoryFixture>
{
public ConfigurationDbContextInMemoryTest(ConfigurationDbContextInMemoryFixture fixture)
: base(fixture)
{
}

protected override void UseTransaction(DatabaseFacade facade, IDbContextTransaction transaction)
{
}

protected override async Task ExecuteWithStrategyInTransactionAsync(
Func<ConfigurationDbContext, Task> testOperation,
Func<ConfigurationDbContext, Task> nestedTestOperation1 = null,
Func<ConfigurationDbContext, Task> nestedTestOperation2 = null,
Func<ConfigurationDbContext, Task> nestedTestOperation3 = null)
{
await base.ExecuteWithStrategyInTransactionAsync(
testOperation, nestedTestOperation1, nestedTestOperation2, nestedTestOperation3);
await Fixture.ReseedAsync();
}

public class ConfigurationDbContextInMemoryFixture : ConfigurationDbContextFixtureBase
{
public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder builder)
=> base.AddOptions(builder).ConfigureWarnings(e => e.Ignore(InMemoryEventId.TransactionIgnoredWarning));

protected override ITestStoreFactory TestStoreFactory
=> InMemoryTestStoreFactory.Instance;

protected override string StoreName
=> "ConfigurationDbContext";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Threading.Tasks;
using IdentityServer4.EntityFramework.DbContexts;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.TestUtilities;

namespace Microsoft.EntityFrameworkCore
{
public class PersistedGrantDbContextInMemoryTest
: PersistedGrantDbContextTestBase<PersistedGrantDbContextInMemoryTest.PersistedGrantDbContextInMemoryFixture>
{
public PersistedGrantDbContextInMemoryTest(PersistedGrantDbContextInMemoryFixture fixture)
: base(fixture)
{
}

protected override void UseTransaction(DatabaseFacade facade, IDbContextTransaction transaction)
{
}

protected override async Task ExecuteWithStrategyInTransactionAsync(
Func<PersistedGrantDbContext, Task> testOperation,
Func<PersistedGrantDbContext, Task> nestedTestOperation1 = null,
Func<PersistedGrantDbContext, Task> nestedTestOperation2 = null,
Func<PersistedGrantDbContext, Task> nestedTestOperation3 = null)
{
await base.ExecuteWithStrategyInTransactionAsync(
testOperation, nestedTestOperation1, nestedTestOperation2, nestedTestOperation3);
await Fixture.ReseedAsync();
}

public class PersistedGrantDbContextInMemoryFixture : PersistedGrantDbContextFixtureBase
{
public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder builder)
=> base.AddOptions(builder).ConfigureWarnings(e => e.Ignore(InMemoryEventId.TransactionIgnoredWarning));

protected override ITestStoreFactory TestStoreFactory
=> InMemoryTestStoreFactory.Instance;

protected override string StoreName
=> "PersistedGrantDbContext";
}
}
}
148 changes: 9 additions & 139 deletions test/EFCore.AspNet.Specification.Tests/AspNetIdentityTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.TestUtilities;
using Xunit;
Expand Down Expand Up @@ -461,13 +460,14 @@ protected static async Task CreateUser(TContext context, TUser user)
public abstract class AspNetIdentityFixtureBase
: SharedStoreFixtureBase<TContext>
{
protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext context)
{
}

protected override void Seed(TContext context)
{
}
public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder builder)
=> base.AddOptions(builder)
.EnableDetailedErrors()
.EnableSensitiveDataLogging()
.ConfigureWarnings(
b => b.Default(WarningBehavior.Throw)
.Log(CoreEventId.SensitiveDataLoggingEnabledWarning)
.Log(CoreEventId.PossibleUnintendedReferenceComparisonWarning));
}

protected TContext CreateContext()
Expand All @@ -484,135 +484,5 @@ protected virtual Task ExecuteWithStrategyInTransactionAsync(

protected virtual void UseTransaction(DatabaseFacade facade, IDbContextTransaction transaction)
=> facade.UseTransaction(transaction.GetDbTransaction());

protected class EntityTypeMapping
{
public EntityTypeMapping()
{
}

public EntityTypeMapping(IEntityType entityType)
{
Name = entityType.Name;
TableName = entityType.GetTableName();
PrimaryKey = entityType.FindPrimaryKey()!.ToDebugString(MetadataDebugStringOptions.SingleLineDefault);

Properties.AddRange(
entityType.GetProperties()
.Select(p => p.ToDebugString(MetadataDebugStringOptions.SingleLineDefault)));

Indexes.AddRange(
entityType.GetIndexes().Select(i => $"{i.Properties.Format()} {(i.IsUnique ? "Unique" : "")}"));

FKs.AddRange(
entityType.GetForeignKeys().Select(f => f.ToDebugString(MetadataDebugStringOptions.SingleLineDefault)));

Navigations.AddRange(
entityType.GetNavigations().Select(n => n.ToDebugString(MetadataDebugStringOptions.SingleLineDefault)));

SkipNavigations.AddRange(
entityType.GetSkipNavigations().Select(n => n.ToDebugString(MetadataDebugStringOptions.SingleLineDefault)));
}

public string Name { get; set; }
public string TableName { get; set; }
public string PrimaryKey { get; set; }
public List<string> Properties { get; } = new();
public List<string> Indexes { get; } = new();
public List<string> FKs { get; } = new();
public List<string> Navigations { get; } = new();
public List<string> SkipNavigations { get; } = new();

public override string ToString()
{
var builder = new StringBuilder();

builder.AppendLine("new()");
builder.AppendLine("{");

builder.AppendLine($" Name = \"{Name}\",");
builder.AppendLine($" TableName = \"{TableName}\",");
builder.AppendLine($" PrimaryKey = \"{PrimaryKey}\",");

builder.AppendLine(" Properties =");
builder.AppendLine(" {");
foreach (var property in Properties)
{
builder.AppendLine($" \"{property}\",");
}

builder.AppendLine(" },");

if (Indexes.Any())
{
builder.AppendLine(" Indexes =");
builder.AppendLine(" {");
foreach (var index in Indexes)
{
builder.AppendLine($" \"{index}\",");
}

builder.AppendLine(" },");
}

if (FKs.Any())
{
builder.AppendLine(" FKs =");
builder.AppendLine(" {");
foreach (var fk in FKs)
{
builder.AppendLine($" \"{fk}\",");
}

builder.AppendLine(" },");
}

if (Navigations.Any())
{
builder.AppendLine(" Navigations =");
builder.AppendLine(" {");
foreach (var navigation in Navigations)
{
builder.AppendLine($" \"{navigation}\",");
}

builder.AppendLine(" },");
}

builder.AppendLine("},");

return builder.ToString();
}

public static void AssertEqual(IReadOnlyList<EntityTypeMapping> expected, IReadOnlyList<EntityTypeMapping> actual)
{
Assert.Equal(expected.Count, actual.Count);
for (var i = 0; i < expected.Count; i++)
{
var e = expected[i];
var a = actual[i];

Assert.Equal(e.Name, a.Name);
Assert.Equal(e.TableName, a.TableName);
Assert.Equal(e.PrimaryKey, a.PrimaryKey);
Assert.Equal(e.Properties, a.Properties);
Assert.Equal(e.Indexes, a.Indexes);
Assert.Equal(e.FKs, a.FKs);
Assert.Equal(e.Navigations, a.Navigations);
Assert.Equal(e.SkipNavigations, a.SkipNavigations);
}
}

public static string Serialize(IEnumerable<EntityTypeMapping> mappings)
{
var builder = new StringBuilder();
foreach (var mapping in mappings)
{
builder.Append(mapping);
}

return builder.ToString();
}
}
}
}
Loading

0 comments on commit 9774a82

Please sign in to comment.