Skip to content

Commit

Permalink
Speed up SQL Server tests
Browse files Browse the repository at this point in the history
By cleaning data rather than dropping/recreating
  • Loading branch information
roji committed Mar 2, 2022
1 parent 711c567 commit 445510d
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,22 @@ protected override void Seed(SaveChangesScenariosContext context)
context.SaveChanges();
}

protected override void Clean(DbContext context)
{
var saveChangesScenariosContext = CreateContext();

saveChangesScenariosContext.WithSomeDatabaseGenerated.RemoveRange(saveChangesScenariosContext.WithSomeDatabaseGenerated);
saveChangesScenariosContext.WithSomeDatabaseGenerated2.RemoveRange(saveChangesScenariosContext.WithSomeDatabaseGenerated2);

saveChangesScenariosContext.WithNoDatabaseGenerated.RemoveRange(saveChangesScenariosContext.WithNoDatabaseGenerated);
saveChangesScenariosContext.WithNoDatabaseGenerated2.RemoveRange(saveChangesScenariosContext.WithNoDatabaseGenerated2);

saveChangesScenariosContext.WithAllDatabaseGenerated.RemoveRange(saveChangesScenariosContext.WithAllDatabaseGenerated);
saveChangesScenariosContext.WithAllDatabaseGenerated2.RemoveRange(saveChangesScenariosContext.WithAllDatabaseGenerated2);

saveChangesScenariosContext.SaveChanges();
}

protected override bool ShouldLogCategory(string logCategory)
=> logCategory == DbLoggerCategory.Database.Transaction.Name
|| logCategory == DbLoggerCategory.Database.Command.Name;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.EntityFrameworkCore.TestModels.SaveChangesScenariosModel;

namespace Microsoft.EntityFrameworkCore.Update;

public class SaveChangesScenariosIdentitySqlServerTest : SaveChangesScenariosTestBase<
Expand Down Expand Up @@ -423,9 +425,48 @@ protected override async Task Test(

public class SaveChangesScenariosIdentitySqlServerFixture : SaveChangesScenariosFixtureBase
{
private string _identityResetCommand;

protected override string StoreName { get; } = "SaveChangesScenariosIdentityTest";

protected override ITestStoreFactory TestStoreFactory
=> SqlServerTestStoreFactory.Instance;

public override void Reseed()
{
using var context = CreateContext();
Clean(context);
Seed(context);
}

protected override void Clean(DbContext context)
{
base.Clean(context);

// Reset the IDENTITY values since we assert on them
context.Database.ExecuteSqlRaw(GetIdentityResetCommand());
}

private string GetIdentityResetCommand()
{
if (_identityResetCommand is not null)
{
return _identityResetCommand;
}

var context = CreateContext();
var builder = new StringBuilder();

var tablesWithIdentity = context.Model.GetEntityTypes()
.Where(e => e.GetProperties().Any(p => p.GetValueGenerationStrategy() == SqlServerValueGenerationStrategy.IdentityColumn))
.Select(e => e.GetTableName());

foreach (var table in tablesWithIdentity)
{
builder.AppendLine($"DBCC CHECKIDENT ('{table}', RESEED, 0);");
}

return _identityResetCommand = builder.ToString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -470,5 +470,20 @@ protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext con
.HasDefaultValueSql("NEXT VALUE FOR [Ids]");
}
}

public override void Reseed()
{
using var context = CreateContext();
Clean(context);
Seed(context);
}

protected override void Clean(DbContext context)
{
base.Clean(context);

// Reset the sequence values since we assert on them
context.Database.ExecuteSqlRaw("ALTER SEQUENCE [Ids] RESTART WITH 1");
}
}
}

0 comments on commit 445510d

Please sign in to comment.