From d46f7cc7e1ba0ed5f51ae6535cd03dfc70f7751e Mon Sep 17 00:00:00 2001 From: Arthur Vickers Date: Sun, 19 Apr 2020 11:06:23 -0700 Subject: [PATCH] Run dependency object tests automatically for all dependency objects Part of #20409 --- .../Internal/StateManagerDependencies.cs | 2 +- .../CosmosApiConsistencyTest.cs | 8 +- .../DesignApiConsistencyTest.cs | 7 + .../InMemoryApiConsistencyTest.cs | 7 +- .../ProxiesApiConsistencyTest.cs | 7 +- .../EFCore.Relational.Tests.csproj | 4 + ...elationalModelValidatorDependenciesTest.cs | 18 --- ...nalConventionSetBuilderDependenciesTest.cs | 19 --- .../HistoryRepositoryDependenciesTest.cs | 18 --- ...tionsAnnotationProviderDependenciesTest.cs | 18 --- .../MigrationsSqlGeneratorDependenciesTest.cs | 17 --- ...tionCommandBatchFactoryDependenciesTest.cs | 18 --- .../QuerySqlGeneratorDependenciesTest.cs | 17 --- ...uatableExpressionFilterDependenciesTest.cs | 17 --- .../RelationalQueryContextDependenciesTest.cs | 17 --- ...ranslationPostprocessorDependenciesTest.cs | 17 --- ...TranslationPreprocessorDependenciesTest.cs | 17 --- ...latingExpressionVisitorDependenciesTest.cs | 17 --- ...pilingExpressionVisitorDependenciesTest.cs | 17 --- .../RelationalApiConsistencyTest.cs | 7 +- ...emberTranslatorProviderDependenciesTest.cs | 18 --- ...dCallTranslatorProviderDependenciesTest.cs | 18 --- ...latingExpressionVisitorDependenciesTest.cs | 18 --- .../SqlExpressionFactoryDependenciesTest.cs | 18 --- .../ParameterNameGeneratorDependenciesTest.cs | 17 --- ...elationalCommandBuilderDependenciesTest.cs | 17 --- .../RelationalConnectionDependenciesTest.cs | 17 --- ...lationalDatabaseCreatorDependenciesTest.cs | 17 --- .../RelationalDatabaseDependenciesTest.cs | 17 --- ...onalSqlGenerationHelperDependenciesTest.cs | 17 --- .../TestUtilities/RelationalTestHelpers.cs | 4 +- .../UpdateSqlGeneratorDependenciesTest.cs | 17 --- .../TestUtilities/TestHelpers.cs | 63 --------- .../SqlServerApiConsistencyTest.cs | 8 +- .../SqlServerNTSApiConsistencyTest.cs | 8 +- .../SqliteApiConsistencyTest.cs | 8 +- .../SqliteNTSApiConsistencyTest.cs | 9 +- test/EFCore.Tests/ApiConsistencyTest.cs | 6 + test/EFCore.Tests/ApiConsistencyTestBase.cs | 129 ++++++++++++++++++ .../ModelCacheKeyFactoryDependenciesTest.cs | 17 --- .../ModelCustomizerDependenciesTest.cs | 17 --- .../ModelValidatorDependenciesTest.cs | 17 --- .../ModelSourceDependenciesTest.cs | 18 --- ...dQueryCacheKeyGeneratorDependenciesTest.cs | 18 --- ...ntityMaterializerSourceDependenciesTest.cs | 17 --- ...uatableExpressionFilterDependenciesTest.cs | 17 --- ...QueryCompilationContextDependenciesTest.cs | 20 --- .../Query/QueryContextDependenciesTest.cs | 19 --- ...stprocessorDependenciesDependenciesTest.cs | 17 --- ...TranslationPreprocessorDependenciesTest.cs | 17 --- ...latingExpressionVisitorDependenciesTest.cs | 17 --- ...pilingExpressionVisitorDependenciesTest.cs | 17 --- .../Storage/DatabaseDependenciesTest.cs | 17 --- .../DatabaseProviderDependenciesTest.cs | 17 --- .../ExecutionStrategyDependenciesTest.cs | 17 --- .../ValueGeneratorCacheDependenciesTest.cs | 17 --- .../ValueGeneratorSelectorDependenciesTest.cs | 17 --- 57 files changed, 201 insertions(+), 807 deletions(-) delete mode 100644 test/EFCore.Relational.Tests/Infrastructure/RelationalModelValidatorDependenciesTest.cs delete mode 100644 test/EFCore.Relational.Tests/Metadata/Conventions/Internal/RelationalConventionSetBuilderDependenciesTest.cs delete mode 100644 test/EFCore.Relational.Tests/Migrations/HistoryRepositoryDependenciesTest.cs delete mode 100644 test/EFCore.Relational.Tests/Migrations/MigrationsAnnotationProviderDependenciesTest.cs delete mode 100644 test/EFCore.Relational.Tests/Migrations/MigrationsSqlGeneratorDependenciesTest.cs delete mode 100644 test/EFCore.Relational.Tests/ModificationCommandBatchFactoryDependenciesTest.cs delete mode 100644 test/EFCore.Relational.Tests/Query/QuerySqlGeneratorDependenciesTest.cs delete mode 100644 test/EFCore.Relational.Tests/Query/RelationalEvaluatableExpressionFilterDependenciesTest.cs delete mode 100644 test/EFCore.Relational.Tests/Query/RelationalQueryContextDependenciesTest.cs delete mode 100644 test/EFCore.Relational.Tests/Query/RelationalQueryTranslationPostprocessorDependenciesTest.cs delete mode 100644 test/EFCore.Relational.Tests/Query/RelationalQueryTranslationPreprocessorDependenciesTest.cs delete mode 100644 test/EFCore.Relational.Tests/Query/RelationalQueryableMethodTranslatingExpressionVisitorDependenciesTest.cs delete mode 100644 test/EFCore.Relational.Tests/Query/RelationalShapedQueryCompilingExpressionVisitorDependenciesTest.cs delete mode 100644 test/EFCore.Relational.Tests/RelationalMemberTranslatorProviderDependenciesTest.cs delete mode 100644 test/EFCore.Relational.Tests/RelationalMethodCallTranslatorProviderDependenciesTest.cs delete mode 100644 test/EFCore.Relational.Tests/RelationalSqlTranslatingExpressionVisitorDependenciesTest.cs delete mode 100644 test/EFCore.Relational.Tests/SqlExpressionFactoryDependenciesTest.cs delete mode 100644 test/EFCore.Relational.Tests/Storage/ParameterNameGeneratorDependenciesTest.cs delete mode 100644 test/EFCore.Relational.Tests/Storage/RelationalCommandBuilderDependenciesTest.cs delete mode 100644 test/EFCore.Relational.Tests/Storage/RelationalConnectionDependenciesTest.cs delete mode 100644 test/EFCore.Relational.Tests/Storage/RelationalDatabaseCreatorDependenciesTest.cs delete mode 100644 test/EFCore.Relational.Tests/Storage/RelationalDatabaseDependenciesTest.cs delete mode 100644 test/EFCore.Relational.Tests/Storage/RelationalSqlGenerationHelperDependenciesTest.cs delete mode 100644 test/EFCore.Relational.Tests/Update/UpdateSqlGeneratorDependenciesTest.cs delete mode 100644 test/EFCore.Tests/Infrastructure/ModelCacheKeyFactoryDependenciesTest.cs delete mode 100644 test/EFCore.Tests/Infrastructure/ModelCustomizerDependenciesTest.cs delete mode 100644 test/EFCore.Tests/Infrastructure/ModelValidatorDependenciesTest.cs delete mode 100644 test/EFCore.Tests/ModelSourceDependenciesTest.cs delete mode 100644 test/EFCore.Tests/Query/CompiledQueryCacheKeyGeneratorDependenciesTest.cs delete mode 100644 test/EFCore.Tests/Query/EntityMaterializerSourceDependenciesTest.cs delete mode 100644 test/EFCore.Tests/Query/EvaluatableExpressionFilterDependenciesTest.cs delete mode 100644 test/EFCore.Tests/Query/QueryCompilationContextDependenciesTest.cs delete mode 100644 test/EFCore.Tests/Query/QueryContextDependenciesTest.cs delete mode 100644 test/EFCore.Tests/Query/QueryTranslationPostprocessorDependenciesDependenciesTest.cs delete mode 100644 test/EFCore.Tests/Query/QueryTranslationPreprocessorDependenciesTest.cs delete mode 100644 test/EFCore.Tests/Query/QueryableMethodTranslatingExpressionVisitorDependenciesTest.cs delete mode 100644 test/EFCore.Tests/Query/ShapedQueryCompilingExpressionVisitorDependenciesTest.cs delete mode 100644 test/EFCore.Tests/Storage/DatabaseDependenciesTest.cs delete mode 100644 test/EFCore.Tests/Storage/DatabaseProviderDependenciesTest.cs delete mode 100644 test/EFCore.Tests/Storage/ExecutionStrategyDependenciesTest.cs delete mode 100644 test/EFCore.Tests/ValueGeneration/ValueGeneratorCacheDependenciesTest.cs delete mode 100644 test/EFCore.Tests/ValueGeneration/ValueGeneratorSelectorDependenciesTest.cs diff --git a/src/EFCore/ChangeTracking/Internal/StateManagerDependencies.cs b/src/EFCore/ChangeTracking/Internal/StateManagerDependencies.cs index b8d58d85fac..fd40b2dd0d7 100644 --- a/src/EFCore/ChangeTracking/Internal/StateManagerDependencies.cs +++ b/src/EFCore/ChangeTracking/Internal/StateManagerDependencies.cs @@ -293,7 +293,7 @@ public StateManagerDependencies With([NotNull] IInternalEntityEntryNotifier inte /// /// A replacement for the current dependency of this type. /// A new parameter object with the given service replaced. - public StateManagerDependencies With([NotNull] ValueGenerationManager valueGenerationManager) + public StateManagerDependencies With([NotNull] IValueGenerationManager valueGenerationManager) => new StateManagerDependencies( InternalEntityEntryFactory, InternalEntityEntrySubscriber, diff --git a/test/EFCore.Cosmos.Tests/CosmosApiConsistencyTest.cs b/test/EFCore.Cosmos.Tests/CosmosApiConsistencyTest.cs index b6092eb2579..e8de53e6023 100644 --- a/test/EFCore.Cosmos.Tests/CosmosApiConsistencyTest.cs +++ b/test/EFCore.Cosmos.Tests/CosmosApiConsistencyTest.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.Reflection; using Microsoft.EntityFrameworkCore.Cosmos.Storage.Internal; +using Microsoft.EntityFrameworkCore.Cosmos.TestUtilities; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Metadata; using Microsoft.Extensions.DependencyInjection; @@ -19,8 +20,13 @@ public CosmosApiConsistencyTest(CosmosApiConsistencyFixture fixture) } protected override void AddServices(ServiceCollection serviceCollection) + => serviceCollection.AddEntityFrameworkCosmos(); + + protected override bool TryGetProviderOptionsDelegate(out Action configureOptions) { - serviceCollection.AddEntityFrameworkCosmos(); + configureOptions = b => b.UseCosmos(TestEnvironment.DefaultConnection, TestEnvironment.AuthToken, "CosmosApiConsistencyTest"); + + return true; } protected override Assembly TargetAssembly => typeof(CosmosDatabaseWrapper).Assembly; diff --git a/test/EFCore.Design.Tests/DesignApiConsistencyTest.cs b/test/EFCore.Design.Tests/DesignApiConsistencyTest.cs index 9bad045f4d0..52d1ded108c 100644 --- a/test/EFCore.Design.Tests/DesignApiConsistencyTest.cs +++ b/test/EFCore.Design.Tests/DesignApiConsistencyTest.cs @@ -20,6 +20,13 @@ protected override void AddServices(ServiceCollection serviceCollection) { } + protected override bool TryGetProviderOptionsDelegate(out Action configureOptions) + { + configureOptions = b => b.UseInMemoryDatabase(nameof(DesignApiConsistencyTest)); + + return true; + } + protected override Assembly TargetAssembly => typeof(OperationExecutor).Assembly; public class DesignApiConsistencyFixture : ApiConsistencyFixtureBase diff --git a/test/EFCore.InMemory.Tests/InMemoryApiConsistencyTest.cs b/test/EFCore.InMemory.Tests/InMemoryApiConsistencyTest.cs index 0b2a0039f71..f5fa684889e 100644 --- a/test/EFCore.InMemory.Tests/InMemoryApiConsistencyTest.cs +++ b/test/EFCore.InMemory.Tests/InMemoryApiConsistencyTest.cs @@ -18,8 +18,13 @@ public InMemoryApiConsistencyTest(InMemoryApiConsistencyFixture fixture) } protected override void AddServices(ServiceCollection serviceCollection) + => serviceCollection.AddEntityFrameworkInMemoryDatabase(); + + protected override bool TryGetProviderOptionsDelegate(out Action configureOptions) { - serviceCollection.AddEntityFrameworkInMemoryDatabase(); + configureOptions = b => b.UseInMemoryDatabase(nameof(InMemoryApiConsistencyTest)); + + return true; } protected override Assembly TargetAssembly => typeof(InMemoryDatabase).Assembly; diff --git a/test/EFCore.Proxies.Tests/ProxiesApiConsistencyTest.cs b/test/EFCore.Proxies.Tests/ProxiesApiConsistencyTest.cs index eb633169900..cd10790c313 100644 --- a/test/EFCore.Proxies.Tests/ProxiesApiConsistencyTest.cs +++ b/test/EFCore.Proxies.Tests/ProxiesApiConsistencyTest.cs @@ -16,8 +16,13 @@ public ProxiesApiConsistencyTest(ProxiesApiConsistencyFixture fixture) } protected override void AddServices(ServiceCollection serviceCollection) + => serviceCollection.AddEntityFrameworkProxies(); + + protected override bool TryGetProviderOptionsDelegate(out Action configureOptions) { - serviceCollection.AddEntityFrameworkProxies(); + configureOptions = b => b.UseInMemoryDatabase(nameof(ProxiesApiConsistencyTest)); + + return true; } protected override Assembly TargetAssembly => typeof(ProxiesExtensions).Assembly; diff --git a/test/EFCore.Relational.Tests/EFCore.Relational.Tests.csproj b/test/EFCore.Relational.Tests/EFCore.Relational.Tests.csproj index 21e8f9f434e..69bd330eaa3 100644 --- a/test/EFCore.Relational.Tests/EFCore.Relational.Tests.csproj +++ b/test/EFCore.Relational.Tests/EFCore.Relational.Tests.csproj @@ -27,4 +27,8 @@ + + + + diff --git a/test/EFCore.Relational.Tests/Infrastructure/RelationalModelValidatorDependenciesTest.cs b/test/EFCore.Relational.Tests/Infrastructure/RelationalModelValidatorDependenciesTest.cs deleted file mode 100644 index ed4aa864c91..00000000000 --- a/test/EFCore.Relational.Tests/Infrastructure/RelationalModelValidatorDependenciesTest.cs +++ /dev/null @@ -1,18 +0,0 @@ -// 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.TestUtilities; -using Xunit; - -// ReSharper disable InconsistentNaming -namespace Microsoft.EntityFrameworkCore.Infrastructure -{ - public class RelationalModelValidatorDependenciesTest - { - [ConditionalFact] - public void Can_use_With_methods_to_clone_and_replace_service() - { - RelationalTestHelpers.Instance.TestDependenciesClone(); - } - } -} diff --git a/test/EFCore.Relational.Tests/Metadata/Conventions/Internal/RelationalConventionSetBuilderDependenciesTest.cs b/test/EFCore.Relational.Tests/Metadata/Conventions/Internal/RelationalConventionSetBuilderDependenciesTest.cs deleted file mode 100644 index 62eaf5c2340..00000000000 --- a/test/EFCore.Relational.Tests/Metadata/Conventions/Internal/RelationalConventionSetBuilderDependenciesTest.cs +++ /dev/null @@ -1,19 +0,0 @@ -// 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.Metadata.Conventions.Infrastructure; -using Microsoft.EntityFrameworkCore.TestUtilities; -using Xunit; - -// ReSharper disable InconsistentNaming -namespace Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal -{ - public class RelationalConventionSetBuilderDependenciesTest - { - [ConditionalFact] - public void Can_use_With_methods_to_clone_and_replace_service() - { - RelationalTestHelpers.Instance.TestDependenciesClone(); - } - } -} diff --git a/test/EFCore.Relational.Tests/Migrations/HistoryRepositoryDependenciesTest.cs b/test/EFCore.Relational.Tests/Migrations/HistoryRepositoryDependenciesTest.cs deleted file mode 100644 index 664a38316c4..00000000000 --- a/test/EFCore.Relational.Tests/Migrations/HistoryRepositoryDependenciesTest.cs +++ /dev/null @@ -1,18 +0,0 @@ -// 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.TestUtilities; -using Xunit; - -// ReSharper disable InconsistentNaming -namespace Microsoft.EntityFrameworkCore.Migrations -{ - public class HistoryRepositoryDependenciesTest - { - [ConditionalFact] - public void Can_use_With_methods_to_clone_and_replace_service() - { - RelationalTestHelpers.Instance.TestDependenciesClone(); - } - } -} diff --git a/test/EFCore.Relational.Tests/Migrations/MigrationsAnnotationProviderDependenciesTest.cs b/test/EFCore.Relational.Tests/Migrations/MigrationsAnnotationProviderDependenciesTest.cs deleted file mode 100644 index 07ff1c06804..00000000000 --- a/test/EFCore.Relational.Tests/Migrations/MigrationsAnnotationProviderDependenciesTest.cs +++ /dev/null @@ -1,18 +0,0 @@ -// 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.Metadata; -using Microsoft.EntityFrameworkCore.TestUtilities; -using Xunit; - -namespace Microsoft.EntityFrameworkCore.Migrations -{ - public class MigrationsAnnotationProviderDependenciesTest - { - [ConditionalFact] - public void Can_use_With_methods_to_clone_and_replace_service() - { - RelationalTestHelpers.Instance.TestDependenciesClone(); - } - } -} diff --git a/test/EFCore.Relational.Tests/Migrations/MigrationsSqlGeneratorDependenciesTest.cs b/test/EFCore.Relational.Tests/Migrations/MigrationsSqlGeneratorDependenciesTest.cs deleted file mode 100644 index e638acc54e5..00000000000 --- a/test/EFCore.Relational.Tests/Migrations/MigrationsSqlGeneratorDependenciesTest.cs +++ /dev/null @@ -1,17 +0,0 @@ -// 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.TestUtilities; -using Xunit; - -namespace Microsoft.EntityFrameworkCore.Migrations -{ - public class MigrationsSqlGeneratorDependenciesTest - { - [ConditionalFact] - public void Can_use_With_methods_to_clone_and_replace_service() - { - RelationalTestHelpers.Instance.TestDependenciesClone(); - } - } -} diff --git a/test/EFCore.Relational.Tests/ModificationCommandBatchFactoryDependenciesTest.cs b/test/EFCore.Relational.Tests/ModificationCommandBatchFactoryDependenciesTest.cs deleted file mode 100644 index 54837bef921..00000000000 --- a/test/EFCore.Relational.Tests/ModificationCommandBatchFactoryDependenciesTest.cs +++ /dev/null @@ -1,18 +0,0 @@ -// 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.TestUtilities; -using Microsoft.EntityFrameworkCore.Update; -using Xunit; - -namespace Microsoft.EntityFrameworkCore -{ - public class ModificationCommandBatchFactoryDependenciesTest - { - [ConditionalFact] - public void Can_use_With_methods_to_clone_and_replace_service() - { - RelationalTestHelpers.Instance.TestDependenciesClone(); - } - } -} diff --git a/test/EFCore.Relational.Tests/Query/QuerySqlGeneratorDependenciesTest.cs b/test/EFCore.Relational.Tests/Query/QuerySqlGeneratorDependenciesTest.cs deleted file mode 100644 index 9ad38a0db1c..00000000000 --- a/test/EFCore.Relational.Tests/Query/QuerySqlGeneratorDependenciesTest.cs +++ /dev/null @@ -1,17 +0,0 @@ -// 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.TestUtilities; -using Xunit; - -namespace Microsoft.EntityFrameworkCore.Query -{ - public class QuerySqlGeneratorDependenciesTest - { - [ConditionalFact] - public void Can_use_With_methods_to_clone_and_replace_service() - { - RelationalTestHelpers.Instance.TestDependenciesClone(); - } - } -} diff --git a/test/EFCore.Relational.Tests/Query/RelationalEvaluatableExpressionFilterDependenciesTest.cs b/test/EFCore.Relational.Tests/Query/RelationalEvaluatableExpressionFilterDependenciesTest.cs deleted file mode 100644 index bcf295391ad..00000000000 --- a/test/EFCore.Relational.Tests/Query/RelationalEvaluatableExpressionFilterDependenciesTest.cs +++ /dev/null @@ -1,17 +0,0 @@ -// 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.TestUtilities; -using Xunit; - -namespace Microsoft.EntityFrameworkCore.Query -{ - public class RelationalEvaluatableExpressionFilterDependenciesTest - { - [ConditionalFact] - public void Can_use_With_methods_to_clone_and_replace_service() - { - RelationalTestHelpers.Instance.TestDependenciesClone(); - } - } -} diff --git a/test/EFCore.Relational.Tests/Query/RelationalQueryContextDependenciesTest.cs b/test/EFCore.Relational.Tests/Query/RelationalQueryContextDependenciesTest.cs deleted file mode 100644 index d52da4020fc..00000000000 --- a/test/EFCore.Relational.Tests/Query/RelationalQueryContextDependenciesTest.cs +++ /dev/null @@ -1,17 +0,0 @@ -// 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.TestUtilities; -using Xunit; - -namespace Microsoft.EntityFrameworkCore.Query -{ - public class RelationalQueryContextDependenciesTest - { - [ConditionalFact] - public void Can_use_With_methods_to_clone_and_replace_service() - { - RelationalTestHelpers.Instance.TestDependenciesClone(); - } - } -} diff --git a/test/EFCore.Relational.Tests/Query/RelationalQueryTranslationPostprocessorDependenciesTest.cs b/test/EFCore.Relational.Tests/Query/RelationalQueryTranslationPostprocessorDependenciesTest.cs deleted file mode 100644 index 9b6320a8812..00000000000 --- a/test/EFCore.Relational.Tests/Query/RelationalQueryTranslationPostprocessorDependenciesTest.cs +++ /dev/null @@ -1,17 +0,0 @@ -// 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.TestUtilities; -using Xunit; - -namespace Microsoft.EntityFrameworkCore.Query -{ - public class RelationalQueryTranslationPostprocessorDependenciesTest - { - [ConditionalFact] - public void Can_use_With_methods_to_clone_and_replace_service() - { - RelationalTestHelpers.Instance.TestDependenciesClone(); - } - } -} diff --git a/test/EFCore.Relational.Tests/Query/RelationalQueryTranslationPreprocessorDependenciesTest.cs b/test/EFCore.Relational.Tests/Query/RelationalQueryTranslationPreprocessorDependenciesTest.cs deleted file mode 100644 index e3fa8fe0d12..00000000000 --- a/test/EFCore.Relational.Tests/Query/RelationalQueryTranslationPreprocessorDependenciesTest.cs +++ /dev/null @@ -1,17 +0,0 @@ -// 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.TestUtilities; -using Xunit; - -namespace Microsoft.EntityFrameworkCore.Query -{ - public class RelationalQueryTranslationPreprocessorDependenciesTest - { - [ConditionalFact] - public void Can_use_With_methods_to_clone_and_replace_service() - { - RelationalTestHelpers.Instance.TestDependenciesClone(); - } - } -} diff --git a/test/EFCore.Relational.Tests/Query/RelationalQueryableMethodTranslatingExpressionVisitorDependenciesTest.cs b/test/EFCore.Relational.Tests/Query/RelationalQueryableMethodTranslatingExpressionVisitorDependenciesTest.cs deleted file mode 100644 index 208df1b1cb0..00000000000 --- a/test/EFCore.Relational.Tests/Query/RelationalQueryableMethodTranslatingExpressionVisitorDependenciesTest.cs +++ /dev/null @@ -1,17 +0,0 @@ -// 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.TestUtilities; -using Xunit; - -namespace Microsoft.EntityFrameworkCore.Query -{ - public class RelationalQueryableMethodTranslatingExpressionVisitorDependenciesTest - { - [ConditionalFact] - public void Can_use_With_methods_to_clone_and_replace_service() - { - RelationalTestHelpers.Instance.TestDependenciesClone(); - } - } -} diff --git a/test/EFCore.Relational.Tests/Query/RelationalShapedQueryCompilingExpressionVisitorDependenciesTest.cs b/test/EFCore.Relational.Tests/Query/RelationalShapedQueryCompilingExpressionVisitorDependenciesTest.cs deleted file mode 100644 index 6ee4e6d9fb5..00000000000 --- a/test/EFCore.Relational.Tests/Query/RelationalShapedQueryCompilingExpressionVisitorDependenciesTest.cs +++ /dev/null @@ -1,17 +0,0 @@ -// 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.TestUtilities; -using Xunit; - -namespace Microsoft.EntityFrameworkCore.Query -{ - public class RelationalShapedQueryCompilingExpressionVisitorDependenciesTest - { - [ConditionalFact] - public void Can_use_With_methods_to_clone_and_replace_service() - { - RelationalTestHelpers.Instance.TestDependenciesClone(); - } - } -} diff --git a/test/EFCore.Relational.Tests/RelationalApiConsistencyTest.cs b/test/EFCore.Relational.Tests/RelationalApiConsistencyTest.cs index e289486ffa4..88608448557 100644 --- a/test/EFCore.Relational.Tests/RelationalApiConsistencyTest.cs +++ b/test/EFCore.Relational.Tests/RelationalApiConsistencyTest.cs @@ -24,10 +24,13 @@ public RelationalApiConsistencyTest(RelationalApiConsistencyFixture fixture) { } - protected override void AddServices(ServiceCollection serviceCollection) + => new EntityFrameworkRelationalServicesBuilder(serviceCollection).TryAddCoreServices(); + + protected override bool TryGetProviderOptionsDelegate(out Action configureOptions) { - new EntityFrameworkRelationalServicesBuilder(serviceCollection).TryAddCoreServices(); + configureOptions = null; + return false; } protected override Assembly TargetAssembly => typeof(RelationalDatabase).Assembly; diff --git a/test/EFCore.Relational.Tests/RelationalMemberTranslatorProviderDependenciesTest.cs b/test/EFCore.Relational.Tests/RelationalMemberTranslatorProviderDependenciesTest.cs deleted file mode 100644 index a869e28845e..00000000000 --- a/test/EFCore.Relational.Tests/RelationalMemberTranslatorProviderDependenciesTest.cs +++ /dev/null @@ -1,18 +0,0 @@ -// 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.Query; -using Microsoft.EntityFrameworkCore.TestUtilities; -using Xunit; - -namespace Microsoft.EntityFrameworkCore -{ - public class RelationalMemberTranslatorProviderDependenciesTest - { - [ConditionalFact] - public void Can_use_With_methods_to_clone_and_replace_service() - { - RelationalTestHelpers.Instance.TestDependenciesClone(); - } - } -} diff --git a/test/EFCore.Relational.Tests/RelationalMethodCallTranslatorProviderDependenciesTest.cs b/test/EFCore.Relational.Tests/RelationalMethodCallTranslatorProviderDependenciesTest.cs deleted file mode 100644 index a93b095fc94..00000000000 --- a/test/EFCore.Relational.Tests/RelationalMethodCallTranslatorProviderDependenciesTest.cs +++ /dev/null @@ -1,18 +0,0 @@ -// 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.Query; -using Microsoft.EntityFrameworkCore.TestUtilities; -using Xunit; - -namespace Microsoft.EntityFrameworkCore -{ - public class RelationalMethodCallTranslatorProviderDependenciesTest - { - [ConditionalFact] - public void Can_use_With_methods_to_clone_and_replace_service() - { - RelationalTestHelpers.Instance.TestDependenciesClone(); - } - } -} diff --git a/test/EFCore.Relational.Tests/RelationalSqlTranslatingExpressionVisitorDependenciesTest.cs b/test/EFCore.Relational.Tests/RelationalSqlTranslatingExpressionVisitorDependenciesTest.cs deleted file mode 100644 index 776b7b39288..00000000000 --- a/test/EFCore.Relational.Tests/RelationalSqlTranslatingExpressionVisitorDependenciesTest.cs +++ /dev/null @@ -1,18 +0,0 @@ -// 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.Query; -using Microsoft.EntityFrameworkCore.TestUtilities; -using Xunit; - -namespace Microsoft.EntityFrameworkCore -{ - public class RelationalSqlTranslatingExpressionVisitorDependenciesTest - { - [ConditionalFact] - public void Can_use_With_methods_to_clone_and_replace_service() - { - RelationalTestHelpers.Instance.TestDependenciesClone(); - } - } -} diff --git a/test/EFCore.Relational.Tests/SqlExpressionFactoryDependenciesTest.cs b/test/EFCore.Relational.Tests/SqlExpressionFactoryDependenciesTest.cs deleted file mode 100644 index cb0cc1a3fe9..00000000000 --- a/test/EFCore.Relational.Tests/SqlExpressionFactoryDependenciesTest.cs +++ /dev/null @@ -1,18 +0,0 @@ -// 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.Query; -using Microsoft.EntityFrameworkCore.TestUtilities; -using Xunit; - -namespace Microsoft.EntityFrameworkCore -{ - public class SqlExpressionFactoryDependenciesTest - { - [ConditionalFact] - public void Can_use_With_methods_to_clone_and_replace_service() - { - RelationalTestHelpers.Instance.TestDependenciesClone(); - } - } -} diff --git a/test/EFCore.Relational.Tests/Storage/ParameterNameGeneratorDependenciesTest.cs b/test/EFCore.Relational.Tests/Storage/ParameterNameGeneratorDependenciesTest.cs deleted file mode 100644 index ef07c549764..00000000000 --- a/test/EFCore.Relational.Tests/Storage/ParameterNameGeneratorDependenciesTest.cs +++ /dev/null @@ -1,17 +0,0 @@ -// 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.TestUtilities; -using Xunit; - -namespace Microsoft.EntityFrameworkCore.Storage -{ - public class ParameterNameGeneratorDependenciesTest - { - [ConditionalFact] - public void Can_use_With_methods_to_clone_and_replace_service() - { - RelationalTestHelpers.Instance.TestDependenciesClone(); - } - } -} diff --git a/test/EFCore.Relational.Tests/Storage/RelationalCommandBuilderDependenciesTest.cs b/test/EFCore.Relational.Tests/Storage/RelationalCommandBuilderDependenciesTest.cs deleted file mode 100644 index 1d1a8f1d591..00000000000 --- a/test/EFCore.Relational.Tests/Storage/RelationalCommandBuilderDependenciesTest.cs +++ /dev/null @@ -1,17 +0,0 @@ -// 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.TestUtilities; -using Xunit; - -namespace Microsoft.EntityFrameworkCore.Storage -{ - public class RelationalCommandBuilderDependenciesTest - { - [ConditionalFact] - public void Can_use_With_methods_to_clone_and_replace_service() - { - RelationalTestHelpers.Instance.TestDependenciesClone(); - } - } -} diff --git a/test/EFCore.Relational.Tests/Storage/RelationalConnectionDependenciesTest.cs b/test/EFCore.Relational.Tests/Storage/RelationalConnectionDependenciesTest.cs deleted file mode 100644 index 2ef5191ce7d..00000000000 --- a/test/EFCore.Relational.Tests/Storage/RelationalConnectionDependenciesTest.cs +++ /dev/null @@ -1,17 +0,0 @@ -// 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.TestUtilities; -using Xunit; - -namespace Microsoft.EntityFrameworkCore.Storage -{ - public class RelationalConnectionDependenciesTest - { - [ConditionalFact] - public void Can_use_With_methods_to_clone_and_replace_service() - { - RelationalTestHelpers.Instance.TestDependenciesClone(); - } - } -} diff --git a/test/EFCore.Relational.Tests/Storage/RelationalDatabaseCreatorDependenciesTest.cs b/test/EFCore.Relational.Tests/Storage/RelationalDatabaseCreatorDependenciesTest.cs deleted file mode 100644 index f9829d37b9e..00000000000 --- a/test/EFCore.Relational.Tests/Storage/RelationalDatabaseCreatorDependenciesTest.cs +++ /dev/null @@ -1,17 +0,0 @@ -// 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.TestUtilities; -using Xunit; - -namespace Microsoft.EntityFrameworkCore.Storage -{ - public class RelationalDatabaseCreatorDependenciesTest - { - [ConditionalFact] - public void Can_use_With_methods_to_clone_and_replace_service() - { - RelationalTestHelpers.Instance.TestDependenciesClone(); - } - } -} diff --git a/test/EFCore.Relational.Tests/Storage/RelationalDatabaseDependenciesTest.cs b/test/EFCore.Relational.Tests/Storage/RelationalDatabaseDependenciesTest.cs deleted file mode 100644 index 7c68fb584b6..00000000000 --- a/test/EFCore.Relational.Tests/Storage/RelationalDatabaseDependenciesTest.cs +++ /dev/null @@ -1,17 +0,0 @@ -// 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.TestUtilities; -using Xunit; - -namespace Microsoft.EntityFrameworkCore.Storage -{ - public class RelationalDatabaseDependenciesTest - { - [ConditionalFact] - public void Can_use_With_methods_to_clone_and_replace_service() - { - RelationalTestHelpers.Instance.TestDependenciesClone(); - } - } -} diff --git a/test/EFCore.Relational.Tests/Storage/RelationalSqlGenerationHelperDependenciesTest.cs b/test/EFCore.Relational.Tests/Storage/RelationalSqlGenerationHelperDependenciesTest.cs deleted file mode 100644 index 5de244e1dea..00000000000 --- a/test/EFCore.Relational.Tests/Storage/RelationalSqlGenerationHelperDependenciesTest.cs +++ /dev/null @@ -1,17 +0,0 @@ -// 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.TestUtilities; -using Xunit; - -namespace Microsoft.EntityFrameworkCore.Storage -{ - public class RelationalSqlGenerationHelperDependenciesTest - { - [ConditionalFact] - public void Can_use_With_methods_to_clone_and_replace_service() - { - RelationalTestHelpers.Instance.TestDependenciesClone(); - } - } -} diff --git a/test/EFCore.Relational.Tests/TestUtilities/RelationalTestHelpers.cs b/test/EFCore.Relational.Tests/TestUtilities/RelationalTestHelpers.cs index 18552a9eda0..445572aca64 100644 --- a/test/EFCore.Relational.Tests/TestUtilities/RelationalTestHelpers.cs +++ b/test/EFCore.Relational.Tests/TestUtilities/RelationalTestHelpers.cs @@ -19,9 +19,7 @@ public override IServiceCollection AddProviderServices(IServiceCollection servic => FakeRelationalOptionsExtension.AddEntityFrameworkRelationalDatabase(services); protected override void UseProviderOptions(DbContextOptionsBuilder optionsBuilder) - { - optionsBuilder.UseFakeRelational(); - } + => optionsBuilder.UseFakeRelational(); public override LoggingDefinitions LoggingDefinitions { get; } = new TestRelationalLoggingDefinitions(); } diff --git a/test/EFCore.Relational.Tests/Update/UpdateSqlGeneratorDependenciesTest.cs b/test/EFCore.Relational.Tests/Update/UpdateSqlGeneratorDependenciesTest.cs deleted file mode 100644 index 7c728fb2a64..00000000000 --- a/test/EFCore.Relational.Tests/Update/UpdateSqlGeneratorDependenciesTest.cs +++ /dev/null @@ -1,17 +0,0 @@ -// 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.TestUtilities; -using Xunit; - -namespace Microsoft.EntityFrameworkCore.Update -{ - public class UpdateSqlGeneratorDependenciesTest - { - [ConditionalFact] - public void Can_use_With_methods_to_clone_and_replace_service() - { - RelationalTestHelpers.Instance.TestDependenciesClone(); - } - } -} diff --git a/test/EFCore.Specification.Tests/TestUtilities/TestHelpers.cs b/test/EFCore.Specification.Tests/TestUtilities/TestHelpers.cs index a217f19bb03..740a8e86685 100644 --- a/test/EFCore.Specification.Tests/TestUtilities/TestHelpers.cs +++ b/test/EFCore.Specification.Tests/TestUtilities/TestHelpers.cs @@ -23,69 +23,6 @@ namespace Microsoft.EntityFrameworkCore.TestUtilities { public abstract class TestHelpers { - /// - /// Tests that calling the 'With' method for each constructor-injected service creates a clone - /// of TDependencies with only that service replaced. - /// - public void TestDependenciesClone(params string[] ignoreProperties) - { - var customServices = new ServiceCollection() - .AddScoped(CreateOptions) - .AddScoped() - .AddScoped(); - - var services1 = CreateServiceProvider(customServices).CreateScope().ServiceProvider; - var services2 = CreateServiceProvider(customServices).CreateScope().ServiceProvider; - - var dependencies = services1.GetService(); - - var serviceProperties = typeof(TDependencies).GetTypeInfo() - .DeclaredProperties - .Where(p => !ignoreProperties.Contains(p.Name)) - .ToList(); - - var obsoleteTypes = serviceProperties - .Where(p => p.CustomAttributes.Any(a => a.AttributeType == typeof(ObsoleteAttribute))) - .Select(p => p.PropertyType) - .ToList(); - - serviceProperties = serviceProperties.Where(p => !obsoleteTypes.Contains(p.PropertyType)).ToList(); - - var constructor = typeof(TDependencies).GetTypeInfo().DeclaredConstructors.OrderByDescending(c => c.GetParameters().Length) - .First(); - var constructorParameters = constructor.GetParameters().Where(p => !obsoleteTypes.Contains(p.ParameterType)).ToList(); - - foreach (var serviceType in constructorParameters.Select(p => p.ParameterType)) - { - var withMethod = typeof(TDependencies).GetTypeInfo().DeclaredMethods - .Single( - m => m.CustomAttributes.All(a => a.AttributeType != typeof(ObsoleteAttribute)) - && m.Name == "With" - && m.GetParameters()[0].ParameterType == serviceType); - - var clone = withMethod.Invoke(dependencies, new[] { services2.GetService(serviceType) }); - - foreach (var property in serviceProperties) - { - if (property.PropertyType == serviceType) - { - Assert.NotSame(property.GetValue(clone), property.GetValue(dependencies)); - } - else - { - Assert.Equal(property.GetValue(clone), property.GetValue(dependencies)); - } - } - } - } - - // ReSharper disable once ClassNeverInstantiated.Local - private class FakeCurrentDbContext : ICurrentDbContext - { - // ReSharper disable once UnassignedGetOnlyAutoProperty - public DbContext Context { get; } - } - public DbContextOptions CreateOptions(IModel model, IServiceProvider serviceProvider = null) { var optionsBuilder = new DbContextOptionsBuilder() diff --git a/test/EFCore.SqlServer.Tests/SqlServerApiConsistencyTest.cs b/test/EFCore.SqlServer.Tests/SqlServerApiConsistencyTest.cs index d08afed1ed1..e16d39473f2 100644 --- a/test/EFCore.SqlServer.Tests/SqlServerApiConsistencyTest.cs +++ b/test/EFCore.SqlServer.Tests/SqlServerApiConsistencyTest.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Reflection; +using Microsoft.Data.SqlClient; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Metadata; using Microsoft.EntityFrameworkCore.Migrations; @@ -20,8 +21,13 @@ public SqlServerApiConsistencyTest(SqlServerApiConsistencyFixture fixture) } protected override void AddServices(ServiceCollection serviceCollection) + => serviceCollection.AddEntityFrameworkSqlServer(); + + protected override bool TryGetProviderOptionsDelegate(out Action configureOptions) { - serviceCollection.AddEntityFrameworkSqlServer(); + configureOptions = b => b.UseSqlServer(new SqlConnection("Database=DummyDatabase")); + + return true; } protected override Assembly TargetAssembly => typeof(SqlServerConnection).Assembly; diff --git a/test/EFCore.SqlServer.Tests/SqlServerNTSApiConsistencyTest.cs b/test/EFCore.SqlServer.Tests/SqlServerNTSApiConsistencyTest.cs index 0036e389675..8830263c514 100644 --- a/test/EFCore.SqlServer.Tests/SqlServerNTSApiConsistencyTest.cs +++ b/test/EFCore.SqlServer.Tests/SqlServerNTSApiConsistencyTest.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Reflection; +using Microsoft.Data.SqlClient; using Microsoft.Extensions.DependencyInjection; namespace Microsoft.EntityFrameworkCore @@ -16,8 +17,13 @@ public SqlServerNTSApiConsistencyTest(SqlServerNTSApiConsistencyFixture fixture) } protected override void AddServices(ServiceCollection serviceCollection) + => serviceCollection.AddEntityFrameworkSqlServerNetTopologySuite(); + + protected override bool TryGetProviderOptionsDelegate(out Action configureOptions) { - serviceCollection.AddEntityFrameworkSqlServerNetTopologySuite(); + configureOptions = b => b.UseSqlServer(new SqlConnection("Database=DummyDatabase")); + + return true; } protected override Assembly TargetAssembly diff --git a/test/EFCore.Sqlite.Tests/SqliteApiConsistencyTest.cs b/test/EFCore.Sqlite.Tests/SqliteApiConsistencyTest.cs index ef4fc7639d5..bc98fdf0c2c 100644 --- a/test/EFCore.Sqlite.Tests/SqliteApiConsistencyTest.cs +++ b/test/EFCore.Sqlite.Tests/SqliteApiConsistencyTest.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Reflection; +using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Sqlite.Storage.Internal; using Microsoft.Extensions.DependencyInjection; @@ -18,8 +19,13 @@ public SqliteApiConsistencyTest(SqliteApiConsistencyFixture fixture) } protected override void AddServices(ServiceCollection serviceCollection) + => serviceCollection.AddEntityFrameworkSqlite(); + + protected override bool TryGetProviderOptionsDelegate(out Action configureOptions) { - serviceCollection.AddEntityFrameworkSqlite(); + configureOptions = b => b.UseSqlite(new SqliteConnection("Data Source=:memory:")); + + return true; } protected override Assembly TargetAssembly => typeof(SqliteRelationalConnection).Assembly; diff --git a/test/EFCore.Sqlite.Tests/SqliteNTSApiConsistencyTest.cs b/test/EFCore.Sqlite.Tests/SqliteNTSApiConsistencyTest.cs index d1c4516c01b..4c1562605ee 100644 --- a/test/EFCore.Sqlite.Tests/SqliteNTSApiConsistencyTest.cs +++ b/test/EFCore.Sqlite.Tests/SqliteNTSApiConsistencyTest.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Reflection; +using Microsoft.Data.Sqlite; using Microsoft.Extensions.DependencyInjection; namespace Microsoft.EntityFrameworkCore @@ -16,8 +17,14 @@ public SqliteNTSApiConsistencyTest(SqliteNTSApiConsistencyFixture fixture) } protected override void AddServices(ServiceCollection serviceCollection) + => serviceCollection.AddEntityFrameworkSqliteNetTopologySuite(); + + + protected override bool TryGetProviderOptionsDelegate(out Action configureOptions) { - serviceCollection.AddEntityFrameworkSqliteNetTopologySuite(); + configureOptions = b => b.UseSqlite(new SqliteConnection("Data Source=:memory:")); + + return true; } protected override Assembly TargetAssembly diff --git a/test/EFCore.Tests/ApiConsistencyTest.cs b/test/EFCore.Tests/ApiConsistencyTest.cs index e17dec6ee8d..70ef7f64987 100644 --- a/test/EFCore.Tests/ApiConsistencyTest.cs +++ b/test/EFCore.Tests/ApiConsistencyTest.cs @@ -27,6 +27,12 @@ public ApiConsistencyTest(ApiConsistencyFixture fixture) protected override void AddServices(ServiceCollection serviceCollection) => new EntityFrameworkServicesBuilder(serviceCollection).TryAddCoreServices(); + protected override bool TryGetProviderOptionsDelegate(out Action configureOptions) + { + configureOptions = null; + return false; + } + public class ApiConsistencyFixture : ApiConsistencyFixtureBase { protected override void Initialize() diff --git a/test/EFCore.Tests/ApiConsistencyTestBase.cs b/test/EFCore.Tests/ApiConsistencyTestBase.cs index f523942ba66..b082f16c9c1 100644 --- a/test/EFCore.Tests/ApiConsistencyTestBase.cs +++ b/test/EFCore.Tests/ApiConsistencyTestBase.cs @@ -12,6 +12,9 @@ using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Metadata; using Microsoft.EntityFrameworkCore.Metadata.Builders; +using Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata.Internal; +using Microsoft.EntityFrameworkCore.Query; using Microsoft.Extensions.DependencyInjection; using Xunit; @@ -503,6 +506,121 @@ private string ValidateConventionMethod(MethodInfo conventionMethod) return null; } + [ConditionalFact] + public virtual void Ensure_dependency_objects_are_consistent() + { + var serviceCollection = new ServiceCollection(); + + AddServices(serviceCollection); + + var dependencyServices = serviceCollection.Where( + sd => sd.ServiceType.Namespace.StartsWith("Microsoft.Entity", StringComparison.Ordinal) + && sd.ServiceType.Name.EndsWith("Dependencies", StringComparison.Ordinal) + && sd.ImplementationType == sd.ServiceType) + .ToList(); + + foreach (var service in dependencyServices) + { + TestDependenciesObject(service.ImplementationType); + } + } + + protected abstract bool TryGetProviderOptionsDelegate (out Action configureOptions); + + // ReSharper disable once ClassNeverInstantiated.Local + private class FakeCurrentDbContext : ICurrentDbContext + { + // ReSharper disable once UnassignedGetOnlyAutoProperty + public DbContext Context { get; } + } + + private void TestDependenciesObject(Type dependenciesType) + { + if (!TryCreateProviderServices(out var serviceCollection1)) + { + return; + } + + AddServices(serviceCollection1); + var services1 = serviceCollection1.BuildServiceProvider(); + + TryCreateProviderServices(out var serviceCollection2); + AddServices(serviceCollection2); + var services2 = serviceCollection2.BuildServiceProvider(); + + var dependencies = services1.GetService(dependenciesType); + + var serviceProperties = dependenciesType.GetTypeInfo() + .DeclaredProperties + .Where(p => !Fixture.ComputedDependencyProperties.Contains(p)) + .ToList(); + + var obsoleteTypes = serviceProperties + .Where(p => p.CustomAttributes.Any(a => a.AttributeType == typeof(ObsoleteAttribute))) + .Select(p => p.PropertyType) + .ToList(); + + serviceProperties = serviceProperties.Where(p => !obsoleteTypes.Contains(p.PropertyType)).ToList(); + + var constructor = dependenciesType.GetTypeInfo().DeclaredConstructors.OrderByDescending(c => c.GetParameters().Length).First(); + var constructorParameters = constructor.GetParameters().Where(p => !obsoleteTypes.Contains(p.ParameterType)).ToList(); + + foreach (var serviceType in constructorParameters.Select(p => p.ParameterType)) + { + var withMethod = dependenciesType.GetTypeInfo().DeclaredMethods + .FirstOrDefault( + m => m.CustomAttributes.All(a => a.AttributeType != typeof(ObsoleteAttribute)) + && m.Name == "With" + && m.GetParameters()[0].ParameterType == serviceType); + + if (withMethod == null) + { + throw new Exception( + $"Expected 'With' method for service type '{serviceType.ShortDisplayName()}' on '{dependenciesType.ShortDisplayName()}'"); + } + + var clone = withMethod.Invoke(dependencies, new[] { services2.GetService(serviceType) }); + + foreach (var property in serviceProperties) + { + if (property.PropertyType == serviceType) + { + Assert.NotSame(property.GetValue(clone), property.GetValue(dependencies)); + } + else + { + Assert.Equal(property.GetValue(clone), property.GetValue(dependencies)); + } + } + } + + bool TryCreateProviderServices(out ServiceCollection services) + { + if (!TryGetProviderOptionsDelegate(out var optionsDelegate)) + { + services = null; + return false; + } + + services = (ServiceCollection)new ServiceCollection() + .AddScoped(p => CreateOptions(p, optionsDelegate)) + .AddScoped() + .AddScoped(); + + return true; + } + + DbContextOptions CreateOptions(IServiceProvider serviceProvider, Action optionsDelegate) + { + var optionsBuilder = new DbContextOptionsBuilder() + .UseInternalServiceProvider(serviceProvider); + + optionsDelegate(optionsBuilder); + + return optionsBuilder.Options; + } + } + [ConditionalFact] public virtual void Service_implementations_should_use_dependencies_parameter_object() { @@ -748,6 +866,17 @@ protected ApiConsistencyFixtureBase() public virtual HashSet UnmatchedMetadataMethods { get; } = new HashSet(); public virtual HashSet MetadataMethodExceptions { get; } = new HashSet(); + public virtual HashSet ComputedDependencyProperties { get; } + = new HashSet + { + typeof(ProviderConventionSetBuilderDependencies).GetProperty(nameof(ProviderConventionSetBuilderDependencies.ContextType)), + typeof(QueryCompilationContextDependencies).GetProperty(nameof(QueryCompilationContextDependencies.ContextType)), + typeof(QueryCompilationContextDependencies).GetProperty(nameof(QueryCompilationContextDependencies.IsTracking)), + typeof(QueryContextDependencies).GetProperty(nameof(QueryContextDependencies.StateManager)), + typeof(QueryContextDependencies).GetProperty(nameof(QueryContextDependencies.QueryProvider)) + }; + + public Dictionary MetadataTypes { get; } = new Dictionary { diff --git a/test/EFCore.Tests/Infrastructure/ModelCacheKeyFactoryDependenciesTest.cs b/test/EFCore.Tests/Infrastructure/ModelCacheKeyFactoryDependenciesTest.cs deleted file mode 100644 index 9c9bb891aea..00000000000 --- a/test/EFCore.Tests/Infrastructure/ModelCacheKeyFactoryDependenciesTest.cs +++ /dev/null @@ -1,17 +0,0 @@ -// 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.TestUtilities; -using Xunit; - -namespace Microsoft.EntityFrameworkCore.Infrastructure -{ - public class ModelCacheKeyFactoryDependenciesTest - { - [ConditionalFact] - public void Can_use_With_methods_to_clone_and_replace_service() - { - InMemoryTestHelpers.Instance.TestDependenciesClone(); - } - } -} diff --git a/test/EFCore.Tests/Infrastructure/ModelCustomizerDependenciesTest.cs b/test/EFCore.Tests/Infrastructure/ModelCustomizerDependenciesTest.cs deleted file mode 100644 index 5232553b701..00000000000 --- a/test/EFCore.Tests/Infrastructure/ModelCustomizerDependenciesTest.cs +++ /dev/null @@ -1,17 +0,0 @@ -// 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.TestUtilities; -using Xunit; - -namespace Microsoft.EntityFrameworkCore.Infrastructure -{ - public class ModelCustomizerDependenciesTest - { - [ConditionalFact] - public void Can_use_With_methods_to_clone_and_replace_service() - { - InMemoryTestHelpers.Instance.TestDependenciesClone(); - } - } -} diff --git a/test/EFCore.Tests/Infrastructure/ModelValidatorDependenciesTest.cs b/test/EFCore.Tests/Infrastructure/ModelValidatorDependenciesTest.cs deleted file mode 100644 index d32229184ea..00000000000 --- a/test/EFCore.Tests/Infrastructure/ModelValidatorDependenciesTest.cs +++ /dev/null @@ -1,17 +0,0 @@ -// 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.TestUtilities; -using Xunit; - -namespace Microsoft.EntityFrameworkCore.Infrastructure -{ - public class ModelValidatorDependenciesTest - { - [ConditionalFact] - public void Can_use_With_methods_to_clone_and_replace_service() - { - InMemoryTestHelpers.Instance.TestDependenciesClone(); - } - } -} diff --git a/test/EFCore.Tests/ModelSourceDependenciesTest.cs b/test/EFCore.Tests/ModelSourceDependenciesTest.cs deleted file mode 100644 index 99a6425af59..00000000000 --- a/test/EFCore.Tests/ModelSourceDependenciesTest.cs +++ /dev/null @@ -1,18 +0,0 @@ -// 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.TestUtilities; -using Xunit; - -namespace Microsoft.EntityFrameworkCore -{ - public class ModelSourceDependenciesTest - { - [ConditionalFact] - public void Can_use_With_methods_to_clone_and_replace_service() - { - InMemoryTestHelpers.Instance.TestDependenciesClone(); - } - } -} diff --git a/test/EFCore.Tests/Query/CompiledQueryCacheKeyGeneratorDependenciesTest.cs b/test/EFCore.Tests/Query/CompiledQueryCacheKeyGeneratorDependenciesTest.cs deleted file mode 100644 index ddd60403e24..00000000000 --- a/test/EFCore.Tests/Query/CompiledQueryCacheKeyGeneratorDependenciesTest.cs +++ /dev/null @@ -1,18 +0,0 @@ -// 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.TestUtilities; -using Xunit; - -// ReSharper disable InconsistentNaming -namespace Microsoft.EntityFrameworkCore.Query -{ - public class CompiledQueryCacheKeyGeneratorDependenciesTest - { - [ConditionalFact] - public void Can_use_With_methods_to_clone_and_replace_service() - { - InMemoryTestHelpers.Instance.TestDependenciesClone(); - } - } -} diff --git a/test/EFCore.Tests/Query/EntityMaterializerSourceDependenciesTest.cs b/test/EFCore.Tests/Query/EntityMaterializerSourceDependenciesTest.cs deleted file mode 100644 index 2ab71c9f696..00000000000 --- a/test/EFCore.Tests/Query/EntityMaterializerSourceDependenciesTest.cs +++ /dev/null @@ -1,17 +0,0 @@ -// 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.TestUtilities; -using Xunit; - -namespace Microsoft.EntityFrameworkCore.Query -{ - public class EntityMaterializerSourceDependenciesTest - { - [ConditionalFact] - public void Can_use_With_methods_to_clone_and_replace_service() - { - InMemoryTestHelpers.Instance.TestDependenciesClone(); - } - } -} diff --git a/test/EFCore.Tests/Query/EvaluatableExpressionFilterDependenciesTest.cs b/test/EFCore.Tests/Query/EvaluatableExpressionFilterDependenciesTest.cs deleted file mode 100644 index 63132f1bb85..00000000000 --- a/test/EFCore.Tests/Query/EvaluatableExpressionFilterDependenciesTest.cs +++ /dev/null @@ -1,17 +0,0 @@ -// 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.TestUtilities; -using Xunit; - -namespace Microsoft.EntityFrameworkCore.Query -{ - public class EvaluatableExpressionFilterDependenciesTest - { - [ConditionalFact] - public void Can_use_With_methods_to_clone_and_replace_service() - { - InMemoryTestHelpers.Instance.TestDependenciesClone(); - } - } -} diff --git a/test/EFCore.Tests/Query/QueryCompilationContextDependenciesTest.cs b/test/EFCore.Tests/Query/QueryCompilationContextDependenciesTest.cs deleted file mode 100644 index a2414682e7f..00000000000 --- a/test/EFCore.Tests/Query/QueryCompilationContextDependenciesTest.cs +++ /dev/null @@ -1,20 +0,0 @@ -// 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.TestUtilities; -using Xunit; - -// ReSharper disable InconsistentNaming -namespace Microsoft.EntityFrameworkCore.Query -{ - public class QueryCompilationContextDependenciesTest - { - [ConditionalFact] - public void Can_use_With_methods_to_clone_and_replace_service() - { - InMemoryTestHelpers.Instance.TestDependenciesClone( - nameof(QueryCompilationContextDependencies.IsTracking), - nameof(QueryCompilationContextDependencies.ContextType)); - } - } -} diff --git a/test/EFCore.Tests/Query/QueryContextDependenciesTest.cs b/test/EFCore.Tests/Query/QueryContextDependenciesTest.cs deleted file mode 100644 index 8b1e75803de..00000000000 --- a/test/EFCore.Tests/Query/QueryContextDependenciesTest.cs +++ /dev/null @@ -1,19 +0,0 @@ -// 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.TestUtilities; -using Xunit; - -namespace Microsoft.EntityFrameworkCore.Query -{ - public class QueryContextDependenciesTest - { - [ConditionalFact] - public void Can_use_With_methods_to_clone_and_replace_service() - { - InMemoryTestHelpers.Instance.TestDependenciesClone( - nameof(QueryContextDependencies.StateManager), - nameof(QueryContextDependencies.QueryProvider)); - } - } -} diff --git a/test/EFCore.Tests/Query/QueryTranslationPostprocessorDependenciesDependenciesTest.cs b/test/EFCore.Tests/Query/QueryTranslationPostprocessorDependenciesDependenciesTest.cs deleted file mode 100644 index 00dd23caaff..00000000000 --- a/test/EFCore.Tests/Query/QueryTranslationPostprocessorDependenciesDependenciesTest.cs +++ /dev/null @@ -1,17 +0,0 @@ -// 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.TestUtilities; -using Xunit; - -namespace Microsoft.EntityFrameworkCore.Query -{ - public class QueryTranslationPostprocessorDependenciesDependenciesTest - { - [ConditionalFact] - public void Can_use_With_methods_to_clone_and_replace_service() - { - InMemoryTestHelpers.Instance.TestDependenciesClone(); - } - } -} diff --git a/test/EFCore.Tests/Query/QueryTranslationPreprocessorDependenciesTest.cs b/test/EFCore.Tests/Query/QueryTranslationPreprocessorDependenciesTest.cs deleted file mode 100644 index d20fd1a30c9..00000000000 --- a/test/EFCore.Tests/Query/QueryTranslationPreprocessorDependenciesTest.cs +++ /dev/null @@ -1,17 +0,0 @@ -// 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.TestUtilities; -using Xunit; - -namespace Microsoft.EntityFrameworkCore.Query -{ - public class QueryTranslationPreprocessorDependenciesTest - { - [ConditionalFact] - public void Can_use_With_methods_to_clone_and_replace_service() - { - InMemoryTestHelpers.Instance.TestDependenciesClone(); - } - } -} diff --git a/test/EFCore.Tests/Query/QueryableMethodTranslatingExpressionVisitorDependenciesTest.cs b/test/EFCore.Tests/Query/QueryableMethodTranslatingExpressionVisitorDependenciesTest.cs deleted file mode 100644 index e383022195f..00000000000 --- a/test/EFCore.Tests/Query/QueryableMethodTranslatingExpressionVisitorDependenciesTest.cs +++ /dev/null @@ -1,17 +0,0 @@ -// 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.TestUtilities; -using Xunit; - -namespace Microsoft.EntityFrameworkCore.Query -{ - public class QueryableMethodTranslatingExpressionVisitorDependenciesTest - { - [ConditionalFact] - public void Can_use_With_methods_to_clone_and_replace_service() - { - InMemoryTestHelpers.Instance.TestDependenciesClone(); - } - } -} diff --git a/test/EFCore.Tests/Query/ShapedQueryCompilingExpressionVisitorDependenciesTest.cs b/test/EFCore.Tests/Query/ShapedQueryCompilingExpressionVisitorDependenciesTest.cs deleted file mode 100644 index 458d00d8cb2..00000000000 --- a/test/EFCore.Tests/Query/ShapedQueryCompilingExpressionVisitorDependenciesTest.cs +++ /dev/null @@ -1,17 +0,0 @@ -// 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.TestUtilities; -using Xunit; - -namespace Microsoft.EntityFrameworkCore.Query -{ - public class ShapedQueryCompilingExpressionVisitorDependenciesTest - { - [ConditionalFact] - public void Can_use_With_methods_to_clone_and_replace_service() - { - InMemoryTestHelpers.Instance.TestDependenciesClone(); - } - } -} diff --git a/test/EFCore.Tests/Storage/DatabaseDependenciesTest.cs b/test/EFCore.Tests/Storage/DatabaseDependenciesTest.cs deleted file mode 100644 index 0460d7f605f..00000000000 --- a/test/EFCore.Tests/Storage/DatabaseDependenciesTest.cs +++ /dev/null @@ -1,17 +0,0 @@ -// 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.TestUtilities; -using Xunit; - -namespace Microsoft.EntityFrameworkCore.Storage -{ - public class DatabaseDependenciesTest - { - [ConditionalFact] - public void Can_use_With_methods_to_clone_and_replace_service() - { - InMemoryTestHelpers.Instance.TestDependenciesClone(); - } - } -} diff --git a/test/EFCore.Tests/Storage/DatabaseProviderDependenciesTest.cs b/test/EFCore.Tests/Storage/DatabaseProviderDependenciesTest.cs deleted file mode 100644 index 31277bee04f..00000000000 --- a/test/EFCore.Tests/Storage/DatabaseProviderDependenciesTest.cs +++ /dev/null @@ -1,17 +0,0 @@ -// 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.TestUtilities; -using Xunit; - -namespace Microsoft.EntityFrameworkCore.Storage -{ - public class DatabaseProviderDependenciesTest - { - [ConditionalFact] - public void Can_use_With_methods_to_clone_and_replace_service() - { - InMemoryTestHelpers.Instance.TestDependenciesClone(); - } - } -} diff --git a/test/EFCore.Tests/Storage/ExecutionStrategyDependenciesTest.cs b/test/EFCore.Tests/Storage/ExecutionStrategyDependenciesTest.cs deleted file mode 100644 index ff8b125665f..00000000000 --- a/test/EFCore.Tests/Storage/ExecutionStrategyDependenciesTest.cs +++ /dev/null @@ -1,17 +0,0 @@ -// 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.TestUtilities; -using Xunit; - -namespace Microsoft.EntityFrameworkCore.Storage -{ - public class ExecutionStrategyDependenciesTest - { - [ConditionalFact] - public void Can_use_With_methods_to_clone_and_replace_service() - { - InMemoryTestHelpers.Instance.TestDependenciesClone(); - } - } -} diff --git a/test/EFCore.Tests/ValueGeneration/ValueGeneratorCacheDependenciesTest.cs b/test/EFCore.Tests/ValueGeneration/ValueGeneratorCacheDependenciesTest.cs deleted file mode 100644 index 5c9ec4c9a8c..00000000000 --- a/test/EFCore.Tests/ValueGeneration/ValueGeneratorCacheDependenciesTest.cs +++ /dev/null @@ -1,17 +0,0 @@ -// 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.TestUtilities; -using Xunit; - -namespace Microsoft.EntityFrameworkCore.ValueGeneration -{ - public class ValueGeneratorCacheDependenciesTest - { - [ConditionalFact] - public void Can_use_With_methods_to_clone_and_replace_service() - { - InMemoryTestHelpers.Instance.TestDependenciesClone(); - } - } -} diff --git a/test/EFCore.Tests/ValueGeneration/ValueGeneratorSelectorDependenciesTest.cs b/test/EFCore.Tests/ValueGeneration/ValueGeneratorSelectorDependenciesTest.cs deleted file mode 100644 index 062e7a8fcfb..00000000000 --- a/test/EFCore.Tests/ValueGeneration/ValueGeneratorSelectorDependenciesTest.cs +++ /dev/null @@ -1,17 +0,0 @@ -// 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.TestUtilities; -using Xunit; - -namespace Microsoft.EntityFrameworkCore.ValueGeneration -{ - public class ValueGeneratorSelectorDependenciesTest - { - [ConditionalFact] - public void Can_use_With_methods_to_clone_and_replace_service() - { - InMemoryTestHelpers.Instance.TestDependenciesClone(); - } - } -}