Skip to content

Commit

Permalink
Test cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
roji committed Aug 11, 2022
1 parent 6f491e2 commit c17fa25
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,6 @@ public DbSet<Entity> WithOriginalAndCurrentValueOnNonConcurrencyToken
public DbSet<Entity> WithInputOutputParameterOnNonConcurrencyToken
=> Set<Entity>(nameof(WithInputOutputParameterOnNonConcurrencyToken));

public static void Seed(StoredProcedureUpdateContext context)
{
// TODO: Go over these, probably just do in the tests
context.WithOutputParameter.Add(new() { Name = "Pre-seeded" });
context.WithRowsAffectedParameter.Add(new() { Name = "Pre-seeded" });

context.SaveChanges();
}

public static void Clean(StoredProcedureUpdateContext context)
{
context.WithOutputParameter.RemoveRange(context.WithOutputParameter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext con
/// </summary>
protected abstract void ConfigureStoreGeneratedConcurrencyToken(EntityTypeBuilder entityTypeBuilder, string propertyName);

protected override void Seed(StoredProcedureUpdateContext context)
=> StoredProcedureUpdateContext.Seed(context);

protected override void Clean(DbContext context)
=> StoredProcedureUpdateContext.Clean(CreateContext());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,13 @@ public virtual async Task Update(bool async)
{
await using var context = CreateContext();

var entity = await context.WithOutputParameter.SingleAsync(w => w.Name == "Pre-seeded");
entity.Name = "Updated";
var entity = new Entity { Name = "Initial" };
context.WithOutputParameter.Add(entity);
await SaveChanges(context, async);

ClearLog();

entity.Name = "Updated";
await SaveChanges(context, async);

using (Fixture.TestSqlLoggerFactory.SuspendRecordingEvents())
Expand Down Expand Up @@ -137,16 +139,18 @@ public virtual async Task Delete(bool async)
{
await using var context = CreateContext();

var entity = await context.WithOutputParameter.SingleAsync(w => w.Name == "Pre-seeded");
context.WithOutputParameter.Remove(entity);
var entity = new Entity { Name = "Initial" };
context.WithOutputParameter.Add(entity);
await context.SaveChangesAsync();

ClearLog();

context.WithOutputParameter.Remove(entity);
await SaveChanges(context, async);

using (Fixture.TestSqlLoggerFactory.SuspendRecordingEvents())
{
Assert.Equal(0, await context.WithOutputParameter.CountAsync(b => b.Name == "Pre-seeded"));
Assert.Equal(0, await context.WithOutputParameter.CountAsync(b => b.Name == "Initial"));
}
}

Expand All @@ -156,11 +160,14 @@ public virtual async Task Rows_affected_parameter(bool async)
{
await using var context = CreateContext();

var entity = await context.WithRowsAffectedParameter.SingleAsync(w => w.Name == "Pre-seeded");
entity.Name = "Updated";
var entity = new Entity { Name = "Initial" };
context.WithRowsAffectedParameter.Add(entity);
await context.SaveChangesAsync();

ClearLog();

entity.Name = "Updated";

await SaveChanges(context, async);

using (Fixture.TestSqlLoggerFactory.SuspendRecordingEvents())
Expand All @@ -175,18 +182,21 @@ public virtual async Task Rows_affected_parameter_and_concurrency_failure(bool a
{
await using var context1 = CreateContext();

var entity1 = await context1.WithRowsAffectedParameter.SingleAsync(w => w.Name == "Pre-seeded");
entity1.Name = "Updated";
var entity1 = new Entity { Name = "Initial" };
context1.WithRowsAffectedParameter.Add(entity1);
await context1.SaveChangesAsync();

await using (var context2 = CreateContext())
{
var entity2 = await context2.WithRowsAffectedParameter.SingleAsync(w => w.Name == "Pre-seeded");
var entity2 = await context2.WithRowsAffectedParameter.SingleAsync(w => w.Name == "Initial");
context2.WithRowsAffectedParameter.Remove(entity2);
await context2.SaveChangesAsync();
}

ClearLog();

entity1.Name = "Updated";

var exception = await Assert.ThrowsAsync<DbUpdateConcurrencyException>(async () => await SaveChanges(context1, async));
var entry = exception.Entries.Single();
Assert.Same(entity1, entry.Entity);
Expand Down Expand Up @@ -406,11 +416,7 @@ public virtual async Task Input_output_parameter_on_non_concurrency_token(bool a
{
await using var context = CreateContext();

var entity = new Entity
{
Name = "Initial",
};

var entity = new Entity { Name = "Initial", };
context.WithInputOutputParameterOnNonConcurrencyToken.Add(entity);
await context.SaveChangesAsync();

Expand All @@ -425,7 +431,8 @@ public virtual async Task Input_output_parameter_on_non_concurrency_token(bool a

using (Fixture.TestSqlLoggerFactory.SuspendRecordingEvents())
{
Assert.Equal("Pre-seeded", (await context.WithOutputParameter.SingleAsync(w => w.Id == entity.Id)).Name);
Assert.Equal(
"Updated", (await context.WithInputOutputParameterOnNonConcurrencyToken.SingleAsync(w => w.Id == entity.Id)).Name);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1011,9 +1011,8 @@ public ICommandBatchPreparer CreateCommandBatchPreparer(
IUpdateAdapter updateAdapter = null,
bool sensitiveLogging = false)
{
var contextServices = FakeRelationalTestHelpers.Instance.CreateContextServices();

modificationCommandBatchFactory ??= contextServices.GetRequiredService<IModificationCommandBatchFactory>();
modificationCommandBatchFactory ??=
FakeRelationalTestHelpers.Instance.CreateContextServices().GetRequiredService<IModificationCommandBatchFactory>();

var loggingOptions = new LoggingOptions();
if (sensitiveLogging)
Expand All @@ -1023,11 +1022,10 @@ public ICommandBatchPreparer CreateCommandBatchPreparer(

return new CommandBatchPreparer(
new CommandBatchPreparerDependencies(
modificationCommandBatchFactory ?? contextServices.GetRequiredService<IModificationCommandBatchFactory>(),
modificationCommandBatchFactory,
new ParameterNameGeneratorFactory(new ParameterNameGeneratorDependencies()),
new ModificationCommandComparer(),
new ModificationCommandFactory(),
contextServices.GetRequiredService<IRelationalTypeMappingSource>(),
loggingOptions,
new FakeDiagnosticsLogger<DbLoggerCategory.Update>(),
new DbContextOptionsBuilder().Options));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public override async Task Insert_with_output_parameter(bool async)

AssertSql(
@"@p0='New' (Size = 4000)
@p1='2' (Direction = Output)
@p1='1' (Direction = Output)
SET NOCOUNT ON;
EXEC [WithOutputParameter_Insert] @p0, @p1 OUTPUT;");
Expand All @@ -33,9 +33,9 @@ public override async Task Insert_twice_with_output_parameter(bool async)

AssertSql(
@"@p0='New1' (Size = 4000)
@p1='2' (Direction = Output)
@p1='1' (Direction = Output)
@p2='New2' (Size = 4000)
@p3='3' (Direction = Output)
@p3='2' (Direction = Output)
SET NOCOUNT ON;
EXEC [WithOutputParameter_Insert] @p0, @p1 OUTPUT;
Expand Down

0 comments on commit c17fa25

Please sign in to comment.