Skip to content

Commit

Permalink
More test fixes
Browse files Browse the repository at this point in the history
* Switch to PoolableDbContext in various tests
  (dotnet/efcore#11311)
* Test for MinBatchSize
  (dotnet/efcore#10091)
  • Loading branch information
roji committed Aug 12, 2018
1 parent b65d0ff commit 820878e
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 243 deletions.
20 changes: 11 additions & 9 deletions test/EFCore.PG.FunctionalTests/BatchingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,7 @@ public void Inserts_are_batched_only_when_necessary(int minBatchSize)
{
var expectedBlogs = new List<Blog>();
TestHelpers.ExecuteWithStrategyInTransaction(
() =>
{
var optionsBuilder = new DbContextOptionsBuilder(Fixture.CreateOptions());
new NpgsqlDbContextOptionsBuilder(optionsBuilder).MinBatchSize(minBatchSize);
return new BloggingContext(optionsBuilder.Options);
},
() => (BloggingContext)Fixture.CreateContext(minBatchSize),
UseTransaction,
context =>
{
Expand Down Expand Up @@ -223,7 +218,7 @@ private void ExecuteWithStrategyInTransaction(
protected void UseTransaction(DatabaseFacade facade, IDbContextTransaction transaction)
=> facade.UseTransaction(transaction.GetDbTransaction());

private class BloggingContext : DbContext
private class BloggingContext : PoolableDbContext
{
public BloggingContext(DbContextOptions options)
: base(options)
Expand Down Expand Up @@ -266,17 +261,24 @@ private class Owner
public uint Version { get; set; }
}

public class BatchingTestFixture : SharedStoreFixtureBase<DbContext>
public class BatchingTestFixture : SharedStoreFixtureBase<PoolableDbContext>
{
public TestSqlLoggerFactory TestSqlLoggerFactory => (TestSqlLoggerFactory)ServiceProvider.GetRequiredService<ILoggerFactory>();
protected override string StoreName { get; } = "BatchingTest";
protected override ITestStoreFactory TestStoreFactory => NpgsqlTestStoreFactory.Instance;
protected override Type ContextType { get; } = typeof(BloggingContext);

protected override void Seed(DbContext context)
protected override void Seed(PoolableDbContext context)
{
context.Database.EnsureCreated();
}

public DbContext CreateContext(int minBatchSize)
{
var optionsBuilder = new DbContextOptionsBuilder(CreateOptions());
new NpgsqlDbContextOptionsBuilder(optionsBuilder).MinBatchSize(minBatchSize);
return new BloggingContext(optionsBuilder.Options);
}
}
}
}
237 changes: 6 additions & 231 deletions test/EFCore.PG.FunctionalTests/CompositeKeyEndToEndTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,248 +3,23 @@
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.TestUtilities;
using Microsoft.Extensions.DependencyInjection;
using Npgsql.EntityFrameworkCore.PostgreSQL.TestUtilities;
using Xunit;

namespace Npgsql.EntityFrameworkCore.PostgreSQL
{
public class CompositeKeyEndToEndTest : IDisposable
public class CompositeKeyEndToEndNpgsqlTest : CompositeKeyEndToEndTestBase<CompositeKeyEndToEndNpgsqlTest.CompositeKeyEndToEndNpgsqlFixture>
{
[Fact]
public async Task Can_use_two_non_generated_integers_as_composite_key_end_to_end()
public CompositeKeyEndToEndNpgsqlTest(CompositeKeyEndToEndNpgsqlFixture fixture)
: base(fixture)
{
var serviceProvider = new ServiceCollection()
.AddEntityFrameworkNpgsql()
.BuildServiceProvider();

var ticks = DateTime.UtcNow.Ticks;

using (var context = new BronieContext(serviceProvider, TestStore.Name))
{
context.Database.EnsureCreated();

context.Add(new Pegasus { Id1 = ticks, Id2 = ticks + 1, Name = "Rainbow Dash" });
await context.SaveChangesAsync();
}

using (var context = new BronieContext(serviceProvider, TestStore.Name))
{
var pegasus = context.Pegasuses.Single(e => e.Id1 == ticks && e.Id2 == ticks + 1);

pegasus.Name = "Rainbow Crash";

await context.SaveChangesAsync();
}

using (var context = new BronieContext(serviceProvider, TestStore.Name))
{
var pegasus = context.Pegasuses.Single(e => e.Id1 == ticks && e.Id2 == ticks + 1);

Assert.Equal("Rainbow Crash", pegasus.Name);

context.Pegasuses.Remove(pegasus);

await context.SaveChangesAsync();
}

using (var context = new BronieContext(serviceProvider, TestStore.Name))
{
Assert.Equal(0, context.Pegasuses.Count(e => e.Id1 == ticks && e.Id2 == ticks + 1));
}
}

[Fact]
public async Task Can_use_generated_values_in_composite_key_end_to_end()
public class CompositeKeyEndToEndNpgsqlFixture : CompositeKeyEndToEndFixtureBase
{
var serviceProvider = new ServiceCollection()
.AddEntityFrameworkNpgsql()
.BuildServiceProvider();

long id1;
var id2 = DateTime.UtcNow.Ticks.ToString(CultureInfo.InvariantCulture);
Guid id3;

using (var context = new BronieContext(serviceProvider, TestStore.Name))
{
context.Database.EnsureCreated();

var added = context.Add(new Unicorn { Id2 = id2, Name = "Rarity" }).Entity;

Assert.True(added.Id1 < 0);
Assert.NotEqual(Guid.Empty, added.Id3);

await context.SaveChangesAsync();

Assert.True(added.Id1 > 0);

id1 = added.Id1;
id3 = added.Id3;
}

using (var context = new BronieContext(serviceProvider, TestStore.Name))
{
Assert.Equal(1, context.Unicorns.Count(e => e.Id1 == id1 && e.Id2 == id2 && e.Id3 == id3));
}

using (var context = new BronieContext(serviceProvider, TestStore.Name))
{
var unicorn = context.Unicorns.Single(e => e.Id1 == id1 && e.Id2 == id2 && e.Id3 == id3);

unicorn.Name = "Bad Hair Day";

await context.SaveChangesAsync();
}

using (var context = new BronieContext(serviceProvider, TestStore.Name))
{
var unicorn = context.Unicorns.Single(e => e.Id1 == id1 && e.Id2 == id2 && e.Id3 == id3);

Assert.Equal("Bad Hair Day", unicorn.Name);

context.Unicorns.Remove(unicorn);

await context.SaveChangesAsync();
}

using (var context = new BronieContext(serviceProvider, TestStore.Name))
{
Assert.Equal(0, context.Unicorns.Count(e => e.Id1 == id1 && e.Id2 == id2 && e.Id3 == id3));
}
protected override ITestStoreFactory TestStoreFactory => NpgsqlTestStoreFactory.Instance;
}

[Fact]
public async Task Only_one_part_of_a_composite_key_needs_to_vary_for_uniquness()
{
var serviceProvider = new ServiceCollection()
.AddEntityFrameworkNpgsql()
.BuildServiceProvider();

var ids = new int[3];

using (var context = new BronieContext(serviceProvider, TestStore.Name))
{
await context.Database.EnsureCreatedAsync();

var pony1 = context.Add(new EarthPony { Id2 = 7, Name = "Apple Jack 1" }).Entity;
var pony2 = context.Add(new EarthPony { Id2 = 7, Name = "Apple Jack 2" }).Entity;
var pony3 = context.Add(new EarthPony { Id2 = 7, Name = "Apple Jack 3" }).Entity;

await context.SaveChangesAsync();

ids[0] = pony1.Id1;
ids[1] = pony2.Id1;
ids[2] = pony3.Id1;
}

using (var context = new BronieContext(serviceProvider, TestStore.Name))
{
var ponies = context.EarthPonies.ToList();
Assert.Equal(ponies.Count, ponies.Count(e => e.Name == "Apple Jack 1") * 3);

Assert.Equal("Apple Jack 1", ponies.Single(e => e.Id1 == ids[0]).Name);
Assert.Equal("Apple Jack 2", ponies.Single(e => e.Id1 == ids[1]).Name);
Assert.Equal("Apple Jack 3", ponies.Single(e => e.Id1 == ids[2]).Name);

ponies.Single(e => e.Id1 == ids[1]).Name = "Pinky Pie 2";

await context.SaveChangesAsync();
}

using (var context = new BronieContext(serviceProvider, TestStore.Name))
{
var ponies = context.EarthPonies.ToArray();
Assert.Equal(ponies.Length, ponies.Count(e => e.Name == "Apple Jack 1") * 3);

Assert.Equal("Apple Jack 1", ponies.Single(e => e.Id1 == ids[0]).Name);
Assert.Equal("Pinky Pie 2", ponies.Single(e => e.Id1 == ids[1]).Name);
Assert.Equal("Apple Jack 3", ponies.Single(e => e.Id1 == ids[2]).Name);

context.EarthPonies.RemoveRange(ponies);

await context.SaveChangesAsync();
}

using (var context = new BronieContext(serviceProvider, TestStore.Name))
{
Assert.Equal(0, context.EarthPonies.Count());
}
}

private class BronieContext : DbContext
{
private readonly IServiceProvider _serviceProvider;
private readonly string _databaseName;

public BronieContext(IServiceProvider serviceProvider, string databaseName)
{
_serviceProvider = serviceProvider;
_databaseName = databaseName;
}

public DbSet<Pegasus> Pegasuses { get; set; }
public DbSet<Unicorn> Unicorns { get; set; }
public DbSet<EarthPony> EarthPonies { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseNpgsql(NpgsqlTestStore.CreateConnectionString(_databaseName), b => b.ApplyConfiguration())
.UseInternalServiceProvider(_serviceProvider);

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Pegasus>(b =>
{
b.ToTable("Pegasus");
b.HasKey(e => new { e.Id1, e.Id2 });
});

modelBuilder.Entity<Unicorn>(b =>
{
b.ToTable("Unicorn");
b.HasKey(e => new { e.Id1, e.Id2, e.Id3 });
b.Property(e => e.Id1).UseNpgsqlSerialColumn();
b.Property(e => e.Id3).ValueGeneratedOnAdd();
});

modelBuilder.Entity<EarthPony>(b =>
{
b.ToTable("EarthPony");
b.HasKey(e => new { e.Id1, e.Id2 });
b.Property(e => e.Id1).UseNpgsqlSerialColumn();
});
}
}

private class Pegasus
{
public long Id1 { get; set; }
public long Id2 { get; set; }
public string Name { get; set; }
}

private class Unicorn
{
public int Id1 { get; set; }
public string Id2 { get; set; }
public Guid Id3 { get; set; }
public string Name { get; set; }
}

private class EarthPony
{
public int Id1 { get; set; }
public int Id2 { get; set; }
public string Name { get; set; }
}

public CompositeKeyEndToEndTest()
{
TestStore = NpgsqlTestStore.CreateInitialized("CompositeKeyEndToEndTest");
}

protected NpgsqlTestStore TestStore { get; }

public virtual void Dispose() => TestStore.Dispose();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1779,7 +1779,7 @@ void Test(string createSql, IEnumerable<string> tables, IEnumerable<string> sche
}
}

public class NpgsqlDatabaseModelFixture : SharedStoreFixtureBase<DbContext>
public class NpgsqlDatabaseModelFixture : SharedStoreFixtureBase<PoolableDbContext>
{
protected override string StoreName { get; } = nameof(NpgsqlDatabaseModelFactoryTest);
protected override ITestStoreFactory TestStoreFactory => NpgsqlTestStoreFactory.Instance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder build
public TestSqlLoggerFactory TestSqlLoggerFactory => (TestSqlLoggerFactory)ServiceProvider.GetRequiredService<ILoggerFactory>();
}

public class NetTopologySuiteContext : DbContext
public class NetTopologySuiteContext : PoolableDbContext
{
public NetTopologySuiteContext(DbContextOptions<NetTopologySuiteContext> options) : base(options) {}

Expand Down
2 changes: 1 addition & 1 deletion test/EFCore.PG.Plugins.FunctionalTests/NodaTimeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ protected override void Seed(NodaTimeContext context)
public TestSqlLoggerFactory TestSqlLoggerFactory => (TestSqlLoggerFactory)ServiceProvider.GetRequiredService<ILoggerFactory>();
}

public class NodaTimeContext : DbContext
public class NodaTimeContext : PoolableDbContext
{
public NodaTimeContext(DbContextOptions<NodaTimeContext> options) : base(options) {}

Expand Down

0 comments on commit 820878e

Please sign in to comment.