Fix tests resulting in opposite provider type exceptions.

pull/152/head
Laurents Meyer 2 years ago
parent f55710825c
commit 73b5c0690f

@ -3,9 +3,12 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data; using System.Data;
using System.Data.Odbc;
using System.Data.OleDb;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using EntityFrameworkCore.Jet.Data;
using EntityFrameworkCore.Jet.FunctionalTests.TestUtilities; using EntityFrameworkCore.Jet.FunctionalTests.TestUtilities;
using EntityFrameworkCore.Jet.Infrastructure; using EntityFrameworkCore.Jet.Infrastructure;
using Microsoft.EntityFrameworkCore.Diagnostics; using Microsoft.EntityFrameworkCore.Diagnostics;
@ -104,8 +107,10 @@ namespace EntityFrameworkCore.Jet.FunctionalTests
execute(new TestJetRetryingExecutionStrategy(context), context); execute(new TestJetRetryingExecutionStrategy(context), context);
context.ChangeTracker.AcceptAllChanges(); context.ChangeTracker.AcceptAllChanges();
var retryMessage = var retryMessage = (TestEnvironment.DataAccessProviderType == DataAccessProviderType.OleDb
"System.Data.OleDb.OleDbException : Bang!"; ? typeof(OleDbException)
: typeof(OdbcException)).FullName + " : Bang!";;
if (realFailure) if (realFailure)
{ {
var logEntry = Fixture.TestSqlLoggerFactory.Log.Single(l => l.Id == CoreEventId.ExecutionStrategyRetrying); var logEntry = Fixture.TestSqlLoggerFactory.Log.Single(l => l.Id == CoreEventId.ExecutionStrategyRetrying);

@ -1,3 +1,4 @@
using EntityFrameworkCore.Jet.FunctionalTests.TestUtilities;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
namespace EntityFrameworkCore.Jet.FunctionalTests; namespace EntityFrameworkCore.Jet.FunctionalTests;
@ -5,5 +6,5 @@ namespace EntityFrameworkCore.Jet.FunctionalTests;
public class ModelBuilding101JetTest : ModelBuilding101RelationalTestBase public class ModelBuilding101JetTest : ModelBuilding101RelationalTestBase
{ {
protected override DbContextOptionsBuilder ConfigureContext(DbContextOptionsBuilder optionsBuilder) protected override DbContextOptionsBuilder ConfigureContext(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseJet(); => optionsBuilder.UseJet(TestEnvironment.DefaultConnection); // TODO: Use TestEnvironment.DataAccessProviderFactory/...Type instead.
} }

@ -537,8 +537,12 @@ WHERE `t`.`Species` LIKE 'F%'
{ {
var contextFactory = await InitializeAsync<Context27427>(); var contextFactory = await InitializeAsync<Context27427>();
using var context = contextFactory.CreateContext(); using var context = contextFactory.CreateContext();
var parameter = TestEnvironment.DataAccessProviderFactory.CreateParameter();
parameter.Value = 1;
var query = context.DemoEntities var query = context.DemoEntities
.FromSqlRaw("SELECT * FROM DemoEntities WHERE Id = {0}", new OleDbParameter { Value = 1 }) .FromSqlRaw("SELECT * FROM DemoEntities WHERE Id = {0}", parameter)
.Select(e => e.Id); .Select(e => e.Id);
var query2 = context.DemoEntities var query2 = context.DemoEntities

@ -1,16 +1,16 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using System.Data.Odbc;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using EntityFrameworkCore.Jet.Data;
using System.Data.OleDb; using System.Data.OleDb;
namespace EntityFrameworkCore.Jet.FunctionalTests.TestUtilities namespace EntityFrameworkCore.Jet.FunctionalTests.TestUtilities
{ {
public static class OleDbExceptionFactory public static class OleDbExceptionFactory
{ {
public static OleDbException CreateOleDbException(int number, Guid? connectionId = null) public static OleDbException CreateException(int number, Guid? connectionId = null)
{ {
var exceptionCtors = typeof(OleDbException) var exceptionCtors = typeof(OleDbException)
.GetTypeInfo() .GetTypeInfo()
@ -20,4 +20,17 @@ namespace EntityFrameworkCore.Jet.FunctionalTests.TestUtilities
.Invoke(new object[] { "Bang!", number, null }); .Invoke(new object[] { "Bang!", number, null });
} }
} }
public static class OdbcExceptionFactory
{
public static OdbcException CreateException(int number, Guid? connectionId = null)
{
var exceptionCtors = typeof(OdbcException)
.GetTypeInfo()
.DeclaredConstructors;
return (OdbcException)exceptionCtors.First(c => c.GetParameters().Length == 3)
.Invoke(new object[] { "Bang!", number, null });
}
}
} }

@ -1,11 +1,12 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data; using System.Data;
using System.Data.Common; using System.Data.Common;
using EntityFrameworkCore.Jet.Data;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using EntityFrameworkCore.Jet.Data;
using EntityFrameworkCore.Jet.Storage.Internal; using EntityFrameworkCore.Jet.Storage.Internal;
using Microsoft.EntityFrameworkCore.Storage; using Microsoft.EntityFrameworkCore.Storage;
@ -13,9 +14,14 @@ namespace EntityFrameworkCore.Jet.FunctionalTests.TestUtilities
{ {
public class TestJetConnection : JetRelationalConnection public class TestJetConnection : JetRelationalConnection
{ {
private readonly Func<int, DbException> _createExceptionFunc;
public TestJetConnection(RelationalConnectionDependencies dependencies) public TestJetConnection(RelationalConnectionDependencies dependencies)
: base(dependencies) : base(dependencies)
{ {
_createExceptionFunc = TestEnvironment.DataAccessProviderType == DataAccessProviderType.OleDb
? number => OleDbExceptionFactory.CreateException(number)
: number => OdbcExceptionFactory.CreateException(number);
} }
public int ErrorNumber { get; set; } = -2; public int ErrorNumber { get; set; } = -2;
@ -56,7 +62,7 @@ namespace EntityFrameworkCore.Jet.FunctionalTests.TestUtilities
if (fail.HasValue) if (fail.HasValue)
{ {
throw OleDbExceptionFactory.CreateOleDbException(ErrorNumber); throw _createExceptionFunc(ErrorNumber);
} }
} }
} }

@ -5,6 +5,7 @@ using System.Collections.Generic;
using System.Data.Common; using System.Data.Common;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using EntityFrameworkCore.Jet.Data;
using Microsoft.EntityFrameworkCore.Diagnostics; using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage; using Microsoft.EntityFrameworkCore.Storage;
@ -98,6 +99,7 @@ namespace EntityFrameworkCore.Jet.FunctionalTests.TestUtilities
private class TestRelationalCommand : IRelationalCommand private class TestRelationalCommand : IRelationalCommand
{ {
private readonly RelationalCommand _realRelationalCommand; private readonly RelationalCommand _realRelationalCommand;
private readonly Func<int, DbException> _createExceptionFunc;
public TestRelationalCommand( public TestRelationalCommand(
RelationalCommandBuilderDependencies dependencies, RelationalCommandBuilderDependencies dependencies,
@ -105,6 +107,10 @@ namespace EntityFrameworkCore.Jet.FunctionalTests.TestUtilities
IReadOnlyList<IRelationalParameter> parameters) IReadOnlyList<IRelationalParameter> parameters)
{ {
_realRelationalCommand = new RelationalCommand(dependencies, commandText, parameters); _realRelationalCommand = new RelationalCommand(dependencies, commandText, parameters);
_createExceptionFunc = TestEnvironment.DataAccessProviderType == DataAccessProviderType.OleDb
? number => OleDbExceptionFactory.CreateException(number)
: number => OdbcExceptionFactory.CreateException(number);
} }
public string CommandText => _realRelationalCommand.CommandText; public string CommandText => _realRelationalCommand.CommandText;
@ -120,7 +126,7 @@ namespace EntityFrameworkCore.Jet.FunctionalTests.TestUtilities
if (errorNumber.HasValue) if (errorNumber.HasValue)
{ {
connection.DbConnection.Close(); connection.DbConnection.Close();
throw OleDbExceptionFactory.CreateOleDbException(errorNumber.Value); throw _createExceptionFunc(errorNumber.Value);
} }
return result; return result;
@ -137,7 +143,7 @@ namespace EntityFrameworkCore.Jet.FunctionalTests.TestUtilities
if (errorNumber.HasValue) if (errorNumber.HasValue)
{ {
connection.DbConnection.Close(); connection.DbConnection.Close();
throw OleDbExceptionFactory.CreateOleDbException(errorNumber.Value); throw _createExceptionFunc(errorNumber.Value);
} }
return result; return result;
@ -152,7 +158,7 @@ namespace EntityFrameworkCore.Jet.FunctionalTests.TestUtilities
if (errorNumber.HasValue) if (errorNumber.HasValue)
{ {
connection.DbConnection.Close(); connection.DbConnection.Close();
throw OleDbExceptionFactory.CreateOleDbException(errorNumber.Value); throw _createExceptionFunc(errorNumber.Value);
} }
return result; return result;
@ -169,7 +175,7 @@ namespace EntityFrameworkCore.Jet.FunctionalTests.TestUtilities
if (errorNumber.HasValue) if (errorNumber.HasValue)
{ {
connection.DbConnection.Close(); connection.DbConnection.Close();
throw OleDbExceptionFactory.CreateOleDbException(errorNumber.Value); throw _createExceptionFunc(errorNumber.Value);
} }
return result; return result;
@ -185,7 +191,7 @@ namespace EntityFrameworkCore.Jet.FunctionalTests.TestUtilities
{ {
connection.DbConnection.Close(); connection.DbConnection.Close();
result.Dispose(); // Normally, in non-test case, reader is disposed by using in caller code result.Dispose(); // Normally, in non-test case, reader is disposed by using in caller code
throw OleDbExceptionFactory.CreateOleDbException(errorNumber.Value); throw _createExceptionFunc(errorNumber.Value);
} }
return result; return result;
@ -203,7 +209,7 @@ namespace EntityFrameworkCore.Jet.FunctionalTests.TestUtilities
{ {
connection.DbConnection.Close(); connection.DbConnection.Close();
result.Dispose(); // Normally, in non-test case, reader is disposed by using in caller code result.Dispose(); // Normally, in non-test case, reader is disposed by using in caller code
throw OleDbExceptionFactory.CreateOleDbException(errorNumber.Value); throw _createExceptionFunc(errorNumber.Value);
} }
return result; return result;
@ -232,7 +238,7 @@ namespace EntityFrameworkCore.Jet.FunctionalTests.TestUtilities
if (fail.Value) if (fail.Value)
{ {
testConnection.DbConnection.Close(); testConnection.DbConnection.Close();
throw OleDbExceptionFactory.CreateOleDbException(testConnection.ErrorNumber); throw _createExceptionFunc(testConnection.ErrorNumber);
} }
errorNumber = testConnection.ErrorNumber; errorNumber = testConnection.ErrorNumber;

@ -2,6 +2,7 @@
using System; using System;
using System.Data.Common; using System.Data.Common;
using EntityFrameworkCore.Jet.Data;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics; using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Storage; using Microsoft.EntityFrameworkCore.Storage;
@ -29,6 +30,7 @@ namespace EntityFrameworkCore.Jet.FunctionalTests.TestUtilities
public class TestRelationalTransaction : RelationalTransaction public class TestRelationalTransaction : RelationalTransaction
{ {
private readonly TestJetConnection _testConnection; private readonly TestJetConnection _testConnection;
private readonly Func<int, Guid?, DbException> _createExceptionFunc;
public TestRelationalTransaction( public TestRelationalTransaction(
IRelationalConnection connection, IRelationalConnection connection,
@ -39,6 +41,10 @@ namespace EntityFrameworkCore.Jet.FunctionalTests.TestUtilities
: base(connection, transaction, new Guid(), logger, transactionOwned, sqlGenerationHelper) : base(connection, transaction, new Guid(), logger, transactionOwned, sqlGenerationHelper)
{ {
_testConnection = (TestJetConnection)connection; _testConnection = (TestJetConnection)connection;
_createExceptionFunc = TestEnvironment.DataAccessProviderType == DataAccessProviderType.OleDb
? OleDbExceptionFactory.CreateException
: OdbcExceptionFactory.CreateException;
} }
public override void Commit() public override void Commit()
@ -58,7 +64,7 @@ namespace EntityFrameworkCore.Jet.FunctionalTests.TestUtilities
} }
_testConnection.DbConnection.Close(); _testConnection.DbConnection.Close();
throw OleDbExceptionFactory.CreateOleDbException(_testConnection.ErrorNumber, _testConnection.ConnectionId); throw _createExceptionFunc(_testConnection.ErrorNumber, _testConnection.ConnectionId);
} }
} }

Loading…
Cancel
Save