From e6cd7e66cb1202857b0b31d5c750196cab4cc120 Mon Sep 17 00:00:00 2001 From: Smit Patel Date: Mon, 26 Jun 2017 14:56:04 -0700 Subject: [PATCH] Test: Use different database for FiltersInheritanceTests Since InheritanceTests modify the data causing flaky errors Also add InheritanceTests for Sqlite Execute test modifying data in a transaction --- .../DataAnnotationTestBase.cs | 1 + .../Query/FiltersInheritanceTestBase.cs | 136 +++++++++++------- .../Query/InheritanceFixtureBase.cs | 79 +--------- .../Query/InheritanceTestBase.cs | 93 +++++++----- .../Inheritance/InheritanceContext.cs | 2 - .../InheritanceModelInitializer.cs | 63 ++++++++ .../FiltersInheritanceInMemoryFixture.cs | 1 + .../Query/GearsOfWarQueryInMemoryFixture.cs | 7 +- .../Query/InheritanceInMemoryFixture.cs | 47 +++--- .../Query/InheritanceInMemoryTest.cs | 10 +- .../FiltersInheritanceSqlServerFixture.cs | 1 + .../Query/GearsOfWarQuerySqlServerFixture.cs | 5 +- .../Query/InheritanceSqlServerFixture.cs | 67 ++++++--- .../Query/InheritanceSqlServerTest.cs | 5 + .../Query/FiltersInheritanceSqliteFixture.cs | 11 ++ .../Query/FiltersInheritanceSqliteTest.cs | 13 ++ .../Query/GearsOfWarQuerySqliteFixture.cs | 19 ++- .../Query/InheritanceSqliteFixture.cs | 62 +++++--- .../Query/InheritanceSqliteTest.cs | 11 +- 19 files changed, 379 insertions(+), 254 deletions(-) create mode 100644 src/EFCore.Specification.Tests/TestModels/Inheritance/InheritanceModelInitializer.cs create mode 100644 test/EFCore.Sqlite.FunctionalTests/Query/FiltersInheritanceSqliteFixture.cs create mode 100644 test/EFCore.Sqlite.FunctionalTests/Query/FiltersInheritanceSqliteTest.cs diff --git a/src/EFCore.Specification.Tests/DataAnnotationTestBase.cs b/src/EFCore.Specification.Tests/DataAnnotationTestBase.cs index e4b9c89625f..e894af76ea4 100644 --- a/src/EFCore.Specification.Tests/DataAnnotationTestBase.cs +++ b/src/EFCore.Specification.Tests/DataAnnotationTestBase.cs @@ -16,6 +16,7 @@ using Microsoft.EntityFrameworkCore.Storage; using Microsoft.EntityFrameworkCore.TestUtilities; using Xunit; +// ReSharper disable InconsistentNaming namespace Microsoft.EntityFrameworkCore { diff --git a/src/EFCore.Specification.Tests/Query/FiltersInheritanceTestBase.cs b/src/EFCore.Specification.Tests/Query/FiltersInheritanceTestBase.cs index 595ece404a6..fa22536943f 100644 --- a/src/EFCore.Specification.Tests/Query/FiltersInheritanceTestBase.cs +++ b/src/EFCore.Specification.Tests/Query/FiltersInheritanceTestBase.cs @@ -19,107 +19,141 @@ public abstract class FiltersInheritanceTestBase : IClassF [Fact] public virtual void Can_use_of_type_animal() { - var animals = _context.Set().OfType().OrderBy(a => a.Species).ToList(); - - Assert.Equal(1, animals.Count); - Assert.IsType(animals[0]); - Assert.Equal(1, _context.ChangeTracker.Entries().Count()); + using (var context = CreateContext()) + { + var animals = context.Set().OfType().OrderBy(a => a.Species).ToList(); + + Assert.Equal(1, animals.Count); + Assert.IsType(animals[0]); + Assert.Equal(1, context.ChangeTracker.Entries().Count()); + } } [Fact] public virtual void Can_use_is_kiwi() { - var kiwis = _context.Set().Where(a => a is Kiwi).ToList(); + using (var context = CreateContext()) + { + var kiwis = context.Set().Where(a => a is Kiwi).ToList(); - Assert.Equal(1, kiwis.Count); + Assert.Equal(1, kiwis.Count); + } } [Fact] public virtual void Can_use_is_kiwi_with_other_predicate() { - var animals = _context.Set().Where(a => a is Kiwi && a.CountryId == 1).ToList(); + using (var context = CreateContext()) + { + var animals = context.Set().Where(a => a is Kiwi && a.CountryId == 1).ToList(); - Assert.Equal(1, animals.Count); + Assert.Equal(1, animals.Count); + } } [Fact] public virtual void Can_use_is_kiwi_in_projection() { - var animals = _context.Set().Select(a => a is Kiwi).ToList(); - - Assert.Equal(1, animals.Count); - Assert.Equal(1, animals.Count(a => a)); - Assert.Equal(0, animals.Count(a => !a)); + using (var context = CreateContext()) + { + var animals = context.Set().Select(a => a is Kiwi).ToList(); + + Assert.Equal(1, animals.Count); + Assert.Equal(1, animals.Count(a => a)); + Assert.Equal(0, animals.Count(a => !a)); + } } [Fact] public virtual void Can_use_of_type_bird() { - var animals = _context.Set().OfType().OrderBy(a => a.Species).ToList(); - - Assert.Equal(1, animals.Count); - Assert.IsType(animals[0]); - Assert.Equal(1, _context.ChangeTracker.Entries().Count()); + using (var context = CreateContext()) + { + var animals = context.Set().OfType().OrderBy(a => a.Species).ToList(); + + Assert.Equal(1, animals.Count); + Assert.IsType(animals[0]); + Assert.Equal(1, context.ChangeTracker.Entries().Count()); + } } [Fact] public virtual void Can_use_of_type_bird_predicate() { - var animals - = _context.Set() - .Where(a => a.CountryId == 1) - .OfType() - .OrderBy(a => a.Species) - .ToList(); - - Assert.Equal(1, animals.Count); - Assert.IsType(animals[0]); - Assert.Equal(1, _context.ChangeTracker.Entries().Count()); + using (var context = CreateContext()) + { + var animals + = context.Set() + .Where(a => a.CountryId == 1) + .OfType() + .OrderBy(a => a.Species) + .ToList(); + + Assert.Equal(1, animals.Count); + Assert.IsType(animals[0]); + Assert.Equal(1, context.ChangeTracker.Entries().Count()); + } } [Fact] public virtual void Can_use_of_type_bird_with_projection() { - var animals - = _context.Set() - .OfType() - .Select(b => new { b.EagleId }) - .ToList(); - - Assert.Equal(1, animals.Count); - Assert.Equal(0, _context.ChangeTracker.Entries().Count()); + using (var context = CreateContext()) + { + var animals + = context.Set() + .OfType() + .Select(b => new { b.EagleId }) + .ToList(); + + Assert.Equal(1, animals.Count); + Assert.Equal(0, context.ChangeTracker.Entries().Count()); + } } [Fact] public virtual void Can_use_of_type_bird_first() { - var bird = _context.Set().OfType().OrderBy(a => a.Species).First(); - - Assert.NotNull(bird); - Assert.IsType(bird); - Assert.Equal(1, _context.ChangeTracker.Entries().Count()); + using (var context = CreateContext()) + { + var bird = context.Set().OfType().OrderBy(a => a.Species).First(); + + Assert.NotNull(bird); + Assert.IsType(bird); + Assert.Equal(1, context.ChangeTracker.Entries().Count()); + } } [Fact] public virtual void Can_use_of_type_kiwi() { - var animals = _context.Set().OfType().ToList(); + using (var context = CreateContext()) + { + var animals = context.Set().OfType().ToList(); + + Assert.Equal(1, animals.Count); + Assert.IsType(animals[0]); + Assert.Equal(1, context.ChangeTracker.Entries().Count()); + } + } + + protected InheritanceContext CreateContext() => Fixture.CreateContext(TestStore); + + protected FiltersInheritanceTestBase(TFixture fixture) + { + Fixture = fixture; - Assert.Equal(1, animals.Count); - Assert.IsType(animals[0]); - Assert.Equal(1, _context.ChangeTracker.Entries().Count()); + TestStore = Fixture.CreateTestStore(); } protected TFixture Fixture { get; } - private readonly InheritanceContext _context; + protected TTestStore TestStore { get; } - protected FiltersInheritanceTestBase(TFixture fixture) + protected virtual void ClearLog() { - Fixture = fixture; - _context = fixture.CreateContext(); } - public void Dispose() => _context.Dispose(); + public void Dispose() => TestStore.Dispose(); } } diff --git a/src/EFCore.Specification.Tests/Query/InheritanceFixtureBase.cs b/src/EFCore.Specification.Tests/Query/InheritanceFixtureBase.cs index a231b06d001..34accbf34d5 100644 --- a/src/EFCore.Specification.Tests/Query/InheritanceFixtureBase.cs +++ b/src/EFCore.Specification.Tests/Query/InheritanceFixtureBase.cs @@ -1,32 +1,16 @@ // 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 Microsoft.EntityFrameworkCore.TestModels.Inheritance; namespace Microsoft.EntityFrameworkCore.Query { - public abstract class InheritanceFixtureBase : IDisposable + public abstract class InheritanceFixtureBase where TTestStore : TestStore { - private DbContextOptions _options; - protected TTestStore TestStore { get; } - - protected InheritanceFixtureBase() - { - TestStore = CreateTestStore(); - } - - public abstract DbContextOptions BuildOptions(); - public abstract TTestStore CreateTestStore(); - public virtual InheritanceContext CreateContext() - => new InheritanceContext(_options ?? (_options = BuildOptions())); - - protected virtual void ClearLog() - { - } + public abstract InheritanceContext CreateContext(TTestStore testStore); protected virtual bool EnableFilters => false; @@ -51,64 +35,5 @@ public virtual void OnModelCreating(ModelBuilder modelBuilder) modelBuilder.Entity().HasQueryFilter(a => a.CountryId == 1); } } - - protected void SeedData(InheritanceContext context) - { - var kiwi = new Kiwi - { - Species = "Apteryx haastii", - Name = "Great spotted kiwi", - IsFlightless = true, - FoundOn = Island.South - }; - - var eagle = new Eagle - { - Species = "Aquila chrysaetos canadensis", - Name = "American golden eagle", - Group = EagleGroup.Booted - }; - - eagle.Prey.Add(kiwi); - - var rose = new Rose - { - Species = "Rosa canina", - Name = "Dog-rose", - HasThorns = true - }; - - var daisy = new Daisy - { - Species = "Bellis perennis", - Name = "Common daisy" - }; - - var nz = new Country { Id = 1, Name = "New Zealand" }; - - nz.Animals.Add(kiwi); - - var usa = new Country { Id = 2, Name = "USA" }; - - usa.Animals.Add(eagle); - - context.Set().Add(kiwi); - context.Set().Add(eagle); - context.Set().Add(nz); - context.Set().Add(usa); - context.Set().Add(rose); - context.Set().Add(daisy); - - context.AddRange( - new Tea { HasMilk = true, CaffeineGrams = 1 }, - new Lilt { SugarGrams = 4, Carbination = 7 }, - new Coke { SugarGrams = 6, CaffeineGrams = 4, Carbination = 5 }); - - context.SaveChanges(); - - ClearLog(); - } - - public void Dispose() => TestStore.Dispose(); } } diff --git a/src/EFCore.Specification.Tests/Query/InheritanceTestBase.cs b/src/EFCore.Specification.Tests/Query/InheritanceTestBase.cs index 6cea64645f2..aa704d1e226 100644 --- a/src/EFCore.Specification.Tests/Query/InheritanceTestBase.cs +++ b/src/EFCore.Specification.Tests/Query/InheritanceTestBase.cs @@ -1,16 +1,21 @@ // 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.Linq; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage; using Microsoft.EntityFrameworkCore.TestModels.Inheritance; +using Microsoft.EntityFrameworkCore.TestUtilities; using Xunit; + // ReSharper disable InconsistentNaming // ReSharper disable ReturnValueOfPureMethodIsNotUsed // ReSharper disable StringEndsWithIsCultureSpecific namespace Microsoft.EntityFrameworkCore.Query { - public abstract class InheritanceTestBase : IClassFixture + public abstract class InheritanceTestBase : IClassFixture, IDisposable where TTestStore : TestStore where TFixture : InheritanceFixtureBase, new() { @@ -396,49 +401,53 @@ var birds [Fact] public virtual void Can_insert_update_delete() { - using (var context = CreateContext()) - { - var kiwi = new Kiwi - { - Species = "Apteryx owenii", - Name = "Little spotted kiwi", - IsFlightless = true, - FoundOn = Island.North - }; - - var nz = context.Set().Single(c => c.Id == 1); + DbContextHelpers.ExecuteWithStrategyInTransaction( + CreateContext, + UseTransaction, + context => + { + var kiwi = new Kiwi + { + Species = "Apteryx owenii", + Name = "Little spotted kiwi", + IsFlightless = true, + FoundOn = Island.North + }; - nz.Animals.Add(kiwi); + var nz = context.Set().Single(c => c.Id == 1); - context.SaveChanges(); - } - - using (var context = CreateContext()) - { - var kiwi = context.Set().Single(k => k.Species.EndsWith("owenii")); + nz.Animals.Add(kiwi); - kiwi.EagleId = "Aquila chrysaetos canadensis"; + context.SaveChanges(); + }, + context => + { + var kiwi = context.Set().Single(k => k.Species.EndsWith("owenii")); - context.SaveChanges(); - } + kiwi.EagleId = "Aquila chrysaetos canadensis"; - using (var context = CreateContext()) - { - var kiwi = context.Set().Single(k => k.Species.EndsWith("owenii")); + context.SaveChanges(); + }, + context => + { + var kiwi = context.Set().Single(k => k.Species.EndsWith("owenii")); - Assert.Equal("Aquila chrysaetos canadensis", kiwi.EagleId); + Assert.Equal("Aquila chrysaetos canadensis", kiwi.EagleId); - context.Set().Remove(kiwi); + context.Set().Remove(kiwi); - context.SaveChanges(); - } + context.SaveChanges(); + }, + context => + { + var count = context.Set().Count(k => k.Species.EndsWith("owenii")); - using (var context = CreateContext()) - { - var count = context.Set().Count(k => k.Species.EndsWith("owenii")); + Assert.Equal(0, count); + }); + } - Assert.Equal(0, count); - } + protected virtual void UseTransaction(DatabaseFacade facade, IDbContextTransaction transaction) + { } [Fact] @@ -501,13 +510,23 @@ public virtual void Can_union_kiwis_and_eagles_as_birds() } } - protected InheritanceContext CreateContext() => Fixture.CreateContext(); - - protected TFixture Fixture { get; } + protected InheritanceContext CreateContext() => Fixture.CreateContext(TestStore); protected InheritanceTestBase(TFixture fixture) { Fixture = fixture; + + TestStore = Fixture.CreateTestStore(); } + + protected TFixture Fixture { get; } + + protected TTestStore TestStore { get; } + + protected virtual void ClearLog() + { + } + + public void Dispose() => TestStore.Dispose(); } } diff --git a/src/EFCore.Specification.Tests/TestModels/Inheritance/InheritanceContext.cs b/src/EFCore.Specification.Tests/TestModels/Inheritance/InheritanceContext.cs index 3af1e6b628f..8c504380742 100644 --- a/src/EFCore.Specification.Tests/TestModels/Inheritance/InheritanceContext.cs +++ b/src/EFCore.Specification.Tests/TestModels/Inheritance/InheritanceContext.cs @@ -5,8 +5,6 @@ namespace Microsoft.EntityFrameworkCore.TestModels.Inheritance { public class InheritanceContext : DbContext { - public static readonly string StoreName = "Inheritance"; - public InheritanceContext(DbContextOptions options) : base(options) { diff --git a/src/EFCore.Specification.Tests/TestModels/Inheritance/InheritanceModelInitializer.cs b/src/EFCore.Specification.Tests/TestModels/Inheritance/InheritanceModelInitializer.cs new file mode 100644 index 00000000000..4b382f93e30 --- /dev/null +++ b/src/EFCore.Specification.Tests/TestModels/Inheritance/InheritanceModelInitializer.cs @@ -0,0 +1,63 @@ +// 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. + +namespace Microsoft.EntityFrameworkCore.TestModels.Inheritance +{ + public class InheritanceModelInitializer + { + public static void SeedData(InheritanceContext context) + { + var kiwi = new Kiwi + { + Species = "Apteryx haastii", + Name = "Great spotted kiwi", + IsFlightless = true, + FoundOn = Island.South + }; + + var eagle = new Eagle + { + Species = "Aquila chrysaetos canadensis", + Name = "American golden eagle", + Group = EagleGroup.Booted + }; + + eagle.Prey.Add(kiwi); + + var rose = new Rose + { + Species = "Rosa canina", + Name = "Dog-rose", + HasThorns = true + }; + + var daisy = new Daisy + { + Species = "Bellis perennis", + Name = "Common daisy" + }; + + var nz = new Country { Id = 1, Name = "New Zealand" }; + + nz.Animals.Add(kiwi); + + var usa = new Country { Id = 2, Name = "USA" }; + + usa.Animals.Add(eagle); + + context.Set().Add(kiwi); + context.Set().Add(eagle); + context.Set().Add(nz); + context.Set().Add(usa); + context.Set().Add(rose); + context.Set().Add(daisy); + + context.AddRange( + new Tea { HasMilk = true, CaffeineGrams = 1 }, + new Lilt { SugarGrams = 4, Carbination = 7 }, + new Coke { SugarGrams = 6, CaffeineGrams = 4, Carbination = 5 }); + + context.SaveChanges(); + } + } +} diff --git a/test/EFCore.InMemory.FunctionalTests/Query/FiltersInheritanceInMemoryFixture.cs b/test/EFCore.InMemory.FunctionalTests/Query/FiltersInheritanceInMemoryFixture.cs index 3afded3bdea..ff518070ae2 100644 --- a/test/EFCore.InMemory.FunctionalTests/Query/FiltersInheritanceInMemoryFixture.cs +++ b/test/EFCore.InMemory.FunctionalTests/Query/FiltersInheritanceInMemoryFixture.cs @@ -6,5 +6,6 @@ namespace Microsoft.EntityFrameworkCore.Query public class FiltersInheritanceInMemoryFixture : InheritanceInMemoryFixture { protected override bool EnableFilters => true; + protected override string DatabaseName => "FiltersInheritanceInMemoryTest"; } } diff --git a/test/EFCore.InMemory.FunctionalTests/Query/GearsOfWarQueryInMemoryFixture.cs b/test/EFCore.InMemory.FunctionalTests/Query/GearsOfWarQueryInMemoryFixture.cs index 016b9ee2077..8dc1831d97c 100644 --- a/test/EFCore.InMemory.FunctionalTests/Query/GearsOfWarQueryInMemoryFixture.cs +++ b/test/EFCore.InMemory.FunctionalTests/Query/GearsOfWarQueryInMemoryFixture.cs @@ -22,12 +22,11 @@ public GearsOfWarQueryInMemoryFixture() .AddSingleton(new TestLoggerFactory()) .BuildServiceProvider(); - var optionsBuilder = new DbContextOptionsBuilder() + _options = new DbContextOptionsBuilder() .UseInMemoryDatabase(DatabaseName) .ConfigureWarnings(w => w.Log(CoreEventId.IncludeIgnoredWarning)) - .UseInternalServiceProvider(serviceProvider); - - _options = optionsBuilder.Options; + .UseInternalServiceProvider(serviceProvider) + .Options; } public override InMemoryTestStore CreateTestStore() diff --git a/test/EFCore.InMemory.FunctionalTests/Query/InheritanceInMemoryFixture.cs b/test/EFCore.InMemory.FunctionalTests/Query/InheritanceInMemoryFixture.cs index 15649295ef4..1e17066dc5d 100644 --- a/test/EFCore.InMemory.FunctionalTests/Query/InheritanceInMemoryFixture.cs +++ b/test/EFCore.InMemory.FunctionalTests/Query/InheritanceInMemoryFixture.cs @@ -1,41 +1,44 @@ // 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 Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Storage.Internal; +using Microsoft.EntityFrameworkCore.Diagnostics; +using Microsoft.EntityFrameworkCore.TestModels.Inheritance; using Microsoft.Extensions.DependencyInjection; namespace Microsoft.EntityFrameworkCore.Query { public class InheritanceInMemoryFixture : InheritanceFixtureBase { - private const string DatabaseName = "InheritanceInMemoryTest"; + protected virtual string DatabaseName => "InheritanceInMemoryTest"; + + private readonly DbContextOptions _options; + + public InheritanceInMemoryFixture() + { + var serviceProvider = new ServiceCollection() + .AddEntityFrameworkInMemoryDatabase() + .AddSingleton(TestModelSource.GetFactory(OnModelCreating)) + .BuildServiceProvider(); + + _options = new DbContextOptionsBuilder() + .UseInMemoryDatabase(DatabaseName) + .ConfigureWarnings(w => w.Ignore(InMemoryEventId.TransactionIgnoredWarning)) + .UseInternalServiceProvider(serviceProvider) + .Options; + } public override InMemoryTestStore CreateTestStore() - => InMemoryTestStore.CreateScratch( + => InMemoryTestStore.GetOrCreateShared( + DatabaseName, () => { - using (var context = CreateContext()) + using (var context = new InheritanceContext(_options)) { - SeedData(context); - } - }, - () => - { - using (var context = CreateContext()) - { - context.GetService().GetStore(DatabaseName).Clear(); + InheritanceModelInitializer.SeedData(context); } }); - public override DbContextOptions BuildOptions() - => new DbContextOptionsBuilder() - .UseInMemoryDatabase(DatabaseName) - .UseInternalServiceProvider( - new ServiceCollection() - .AddEntityFrameworkInMemoryDatabase() - .AddSingleton(TestModelSource.GetFactory(OnModelCreating)) - .BuildServiceProvider()) - .Options; + public override InheritanceContext CreateContext(InMemoryTestStore testStore) + => new InheritanceContext(_options); } } diff --git a/test/EFCore.InMemory.FunctionalTests/Query/InheritanceInMemoryTest.cs b/test/EFCore.InMemory.FunctionalTests/Query/InheritanceInMemoryTest.cs index d559b4862a0..08df31c0651 100644 --- a/test/EFCore.InMemory.FunctionalTests/Query/InheritanceInMemoryTest.cs +++ b/test/EFCore.InMemory.FunctionalTests/Query/InheritanceInMemoryTest.cs @@ -9,6 +9,11 @@ namespace Microsoft.EntityFrameworkCore.Query { public class InheritanceInMemoryTest : InheritanceTestBase { + public InheritanceInMemoryTest(InheritanceInMemoryFixture fixture) + : base(fixture) + { + } + public override void Discriminator_used_when_projection_over_derived_type2() { Assert.Equal( @@ -24,10 +29,5 @@ public override void Discriminator_with_cast_in_shadow_property() Assert.Throws(() => base.Discriminator_with_cast_in_shadow_property()).Message); } - - public InheritanceInMemoryTest(InheritanceInMemoryFixture fixture) - : base(fixture) - { - } } } diff --git a/test/EFCore.SqlServer.FunctionalTests/Query/FiltersInheritanceSqlServerFixture.cs b/test/EFCore.SqlServer.FunctionalTests/Query/FiltersInheritanceSqlServerFixture.cs index 1a310027e15..81ee450042d 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/FiltersInheritanceSqlServerFixture.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/FiltersInheritanceSqlServerFixture.cs @@ -6,5 +6,6 @@ namespace Microsoft.EntityFrameworkCore.Query public class FiltersInheritanceSqlServerFixture : InheritanceSqlServerFixture { protected override bool EnableFilters => true; + protected override string DatabaseName => "FiltersInheritanceSqlServerTest"; } } diff --git a/test/EFCore.SqlServer.FunctionalTests/Query/GearsOfWarQuerySqlServerFixture.cs b/test/EFCore.SqlServer.FunctionalTests/Query/GearsOfWarQuerySqlServerFixture.cs index c568b731c47..a98746dff84 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/GearsOfWarQuerySqlServerFixture.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/GearsOfWarQuerySqlServerFixture.cs @@ -15,8 +15,6 @@ public class GearsOfWarQuerySqlServerFixture : GearsOfWarQueryRelationalFixture< private readonly DbContextOptions _options; - private readonly string _connectionString = SqlServerTestStore.CreateConnectionString(DatabaseName); - public TestSqlLoggerFactory TestSqlLoggerFactory { get; } = new TestSqlLoggerFactory(); public GearsOfWarQuerySqlServerFixture() @@ -40,7 +38,8 @@ public override SqlServerTestStore CreateTestStore() { using (var context = new GearsOfWarContext( new DbContextOptionsBuilder(_options) - .UseSqlServer(_connectionString, b => b.ApplyConfiguration()) + .UseSqlServer(SqlServerTestStore.CreateConnectionString(DatabaseName), + b => b.ApplyConfiguration()) .Options)) { context.Database.EnsureCreated(); diff --git a/test/EFCore.SqlServer.FunctionalTests/Query/InheritanceSqlServerFixture.cs b/test/EFCore.SqlServer.FunctionalTests/Query/InheritanceSqlServerFixture.cs index 887bbe1a338..12d65bcf45a 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/InheritanceSqlServerFixture.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/InheritanceSqlServerFixture.cs @@ -1,6 +1,7 @@ // 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 Microsoft.EntityFrameworkCore.TestModels.Inheritance; using Microsoft.EntityFrameworkCore.Utilities; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; @@ -9,34 +10,54 @@ namespace Microsoft.EntityFrameworkCore.Query { public class InheritanceSqlServerFixture : InheritanceRelationalFixture { + protected virtual string DatabaseName => "InheritanceSqlServerTest"; + + private readonly DbContextOptions _options; + public TestSqlLoggerFactory TestSqlLoggerFactory { get; } = new TestSqlLoggerFactory(); - private const string DatabaseName = "InheritanceSqlServerTest"; - protected override void ClearLog() - => TestSqlLoggerFactory.Clear(); + public InheritanceSqlServerFixture() + { + var serviceProvider = new ServiceCollection() + .AddEntityFrameworkSqlServer() + .AddSingleton(TestModelSource.GetFactory(OnModelCreating)) + .AddSingleton(TestSqlLoggerFactory) + .BuildServiceProvider(); + + _options = new DbContextOptionsBuilder() + .EnableSensitiveDataLogging() + .UseInternalServiceProvider(serviceProvider) + .Options; + } public override SqlServerTestStore CreateTestStore() - => SqlServerTestStore.GetOrCreateShared(DatabaseName, () => - { - using (var context = CreateContext()) + { + return SqlServerTestStore.GetOrCreateShared( + DatabaseName, () => { - context.Database.EnsureCreated(); - SeedData(context); - } - }); + using (var context = new InheritanceContext( + new DbContextOptionsBuilder(_options) + .UseSqlServer( + SqlServerTestStore.CreateConnectionString(DatabaseName), + b => b.ApplyConfiguration()) + .Options)) + { + context.Database.EnsureCreated(); + InheritanceModelInitializer.SeedData(context); + } + }); + } - public override DbContextOptions BuildOptions() - => new DbContextOptionsBuilder() - .EnableSensitiveDataLogging() - .UseSqlServer( - SqlServerTestStore.CreateConnectionString(DatabaseName), - b => b.ApplyConfiguration()) - .UseInternalServiceProvider( - new ServiceCollection() - .AddEntityFrameworkSqlServer() - .AddSingleton(TestModelSource.GetFactory(OnModelCreating)) - .AddSingleton(TestSqlLoggerFactory) - .BuildServiceProvider()) - .Options; + public override InheritanceContext CreateContext(SqlServerTestStore testStore) + { + var context = new InheritanceContext( + new DbContextOptionsBuilder(_options) + .UseSqlServer(testStore.Connection, b => b.ApplyConfiguration()) + .Options); + + context.Database.UseTransaction(testStore.Transaction); + + return context; + } } } diff --git a/test/EFCore.SqlServer.FunctionalTests/Query/InheritanceSqlServerTest.cs b/test/EFCore.SqlServer.FunctionalTests/Query/InheritanceSqlServerTest.cs index 9144b76efeb..65ef711341a 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/InheritanceSqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/InheritanceSqlServerTest.cs @@ -1,6 +1,8 @@ // 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 Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage; using Microsoft.EntityFrameworkCore.TestModels.Inheritance; using Microsoft.EntityFrameworkCore.Utilities; using Xunit; @@ -378,6 +380,9 @@ FROM [Animal] AS [k] WHERE ([k].[Discriminator] = N'Kiwi') AND (RIGHT([k].[Species], LEN(N'owenii')) = N'owenii')"); } + protected override void UseTransaction(DatabaseFacade facade, IDbContextTransaction transaction) + => facade.UseTransaction(transaction.GetDbTransaction()); + private void AssertSql(params string[] expected) => Fixture.TestSqlLoggerFactory.AssertBaseline(expected); } diff --git a/test/EFCore.Sqlite.FunctionalTests/Query/FiltersInheritanceSqliteFixture.cs b/test/EFCore.Sqlite.FunctionalTests/Query/FiltersInheritanceSqliteFixture.cs new file mode 100644 index 00000000000..ed5955451ab --- /dev/null +++ b/test/EFCore.Sqlite.FunctionalTests/Query/FiltersInheritanceSqliteFixture.cs @@ -0,0 +1,11 @@ +// 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. + +namespace Microsoft.EntityFrameworkCore.Query +{ + public class FiltersInheritanceSqliteFixture : InheritanceSqliteFixture + { + protected override bool EnableFilters => true; + protected override string DatabaseName => "FiltersInheritanceSqliteTest"; + } +} diff --git a/test/EFCore.Sqlite.FunctionalTests/Query/FiltersInheritanceSqliteTest.cs b/test/EFCore.Sqlite.FunctionalTests/Query/FiltersInheritanceSqliteTest.cs new file mode 100644 index 00000000000..394d1bb9a81 --- /dev/null +++ b/test/EFCore.Sqlite.FunctionalTests/Query/FiltersInheritanceSqliteTest.cs @@ -0,0 +1,13 @@ +// 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. + +namespace Microsoft.EntityFrameworkCore.Query +{ + public class FiltersInheritanceSqliteTest : FiltersInheritanceTestBase + { + public FiltersInheritanceSqliteTest(FiltersInheritanceSqliteFixture fixture) + : base(fixture) + { + } + } +} diff --git a/test/EFCore.Sqlite.FunctionalTests/Query/GearsOfWarQuerySqliteFixture.cs b/test/EFCore.Sqlite.FunctionalTests/Query/GearsOfWarQuerySqliteFixture.cs index da7b7c564ac..298e5970698 100644 --- a/test/EFCore.Sqlite.FunctionalTests/Query/GearsOfWarQuerySqliteFixture.cs +++ b/test/EFCore.Sqlite.FunctionalTests/Query/GearsOfWarQuerySqliteFixture.cs @@ -14,8 +14,6 @@ public class GearsOfWarQuerySqliteFixture : GearsOfWarQueryRelationalFixture w.Log(CoreEventId.IncludeIgnoredWarning)) .UseInternalServiceProvider(serviceProvider) .Options; @@ -35,14 +33,15 @@ public GearsOfWarQuerySqliteFixture() public override SqliteTestStore CreateTestStore() { - return SqliteTestStore.GetOrCreateShared(DatabaseName, () => - { - using (var context = new GearsOfWarContext(_options)) + return SqliteTestStore.GetOrCreateShared( + DatabaseName, () => { - context.Database.EnsureClean(); - GearsOfWarModelInitializer.Seed(context); - } - }); + using (var context = new GearsOfWarContext(_options)) + { + context.Database.EnsureClean(); + GearsOfWarModelInitializer.Seed(context); + } + }); } public override GearsOfWarContext CreateContext(SqliteTestStore testStore) diff --git a/test/EFCore.Sqlite.FunctionalTests/Query/InheritanceSqliteFixture.cs b/test/EFCore.Sqlite.FunctionalTests/Query/InheritanceSqliteFixture.cs index 1796db0a6f4..b1b534adc43 100644 --- a/test/EFCore.Sqlite.FunctionalTests/Query/InheritanceSqliteFixture.cs +++ b/test/EFCore.Sqlite.FunctionalTests/Query/InheritanceSqliteFixture.cs @@ -1,6 +1,8 @@ // 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 Microsoft.EntityFrameworkCore.TestModels.Inheritance; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; @@ -8,28 +10,50 @@ namespace Microsoft.EntityFrameworkCore.Query { public class InheritanceSqliteFixture : InheritanceRelationalFixture { + protected virtual string DatabaseName => "InheritanceSqliteTest"; + + private readonly IServiceProvider _serviceProvider; + public TestSqlLoggerFactory TestSqlLoggerFactory { get; } = new TestSqlLoggerFactory(); - private readonly string DatabaseName = "InheritanceSqliteTest"; + + public InheritanceSqliteFixture() + { + _serviceProvider = new ServiceCollection() + .AddEntityFrameworkSqlite() + .AddSingleton(TestModelSource.GetFactory(OnModelCreating)) + .AddSingleton(TestSqlLoggerFactory) + .BuildServiceProvider(); + } public override SqliteTestStore CreateTestStore() - => SqliteTestStore.GetOrCreateShared(DatabaseName, () => - { - using (var context = CreateContext()) + { + return SqliteTestStore.GetOrCreateShared( + DatabaseName, () => { - context.Database.EnsureClean(); - SeedData(context); - } - }); - - public override DbContextOptions BuildOptions() - => new DbContextOptionsBuilder() - .UseSqlite(SqliteTestStore.CreateConnectionString(DatabaseName)) - .UseInternalServiceProvider( - new ServiceCollection() - .AddEntityFrameworkSqlite() - .AddSingleton(TestModelSource.GetFactory(OnModelCreating)) - .AddSingleton(TestSqlLoggerFactory) - .BuildServiceProvider()) - .Options; + using (var context = new InheritanceContext( + new DbContextOptionsBuilder() + .UseSqlite(SqliteTestStore.CreateConnectionString(DatabaseName)) + .UseInternalServiceProvider(_serviceProvider) + .Options)) + { + context.Database.EnsureClean(); + InheritanceModelInitializer.SeedData(context); + } + }); + } + + public override InheritanceContext CreateContext(SqliteTestStore testStore) + { + var context = new InheritanceContext( + new DbContextOptionsBuilder() + .UseSqlite( + testStore.Connection, + b => b.SuppressForeignKeyEnforcement()) + .UseInternalServiceProvider(_serviceProvider).Options); + + context.Database.UseTransaction(testStore.Transaction); + + return context; + } } } diff --git a/test/EFCore.Sqlite.FunctionalTests/Query/InheritanceSqliteTest.cs b/test/EFCore.Sqlite.FunctionalTests/Query/InheritanceSqliteTest.cs index 00677a648d7..0d3d1ca8866 100644 --- a/test/EFCore.Sqlite.FunctionalTests/Query/InheritanceSqliteTest.cs +++ b/test/EFCore.Sqlite.FunctionalTests/Query/InheritanceSqliteTest.cs @@ -1,13 +1,22 @@ // 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 Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage; +using Xunit.Abstractions; + namespace Microsoft.EntityFrameworkCore.Query { public class InheritanceSqliteTest : InheritanceTestBase { - public InheritanceSqliteTest(InheritanceSqliteFixture fixture) + public InheritanceSqliteTest(InheritanceSqliteFixture fixture, ITestOutputHelper testOutputHelper) : base(fixture) { + Fixture.TestSqlLoggerFactory.Clear(); + //Fixture.TestSqlLoggerFactory.SetTestOutputHelper(testOutputHelper); } + + protected override void UseTransaction(DatabaseFacade facade, IDbContextTransaction transaction) + => facade.UseTransaction(transaction.GetDbTransaction()); } }