Skip to content

Commit

Permalink
Убираем UnityFactory из тестов
Browse files Browse the repository at this point in the history
  • Loading branch information
turbcool committed May 19, 2023
1 parent 42906c1 commit c5b8a4c
Show file tree
Hide file tree
Showing 34 changed files with 168 additions and 166 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ namespace NewPlatform.Flexberry.ORM.ODataService.Tests
using Oracle.ManagedDataAccess.Client;
using Xunit;
using Xunit.Abstractions;
using Unity;
using ICSSoft.Services;

#if NETFRAMEWORK
/// <summary>
Expand All @@ -28,16 +30,15 @@ public abstract class BaseIntegratedTest : IDisposable
#if NETCOREAPP
using Microsoft.AspNetCore.Mvc.Testing;
using ODataServiceSample.AspNetCore;
using ICSSoft.Services;
using Unity;

/// <summary>
/// Base class for integration tests.
/// </summary>
public abstract class BaseIntegratedTest : IClassFixture<CustomWebApplicationFactory<Startup>>, IDisposable

This comment has been minimized.

Copy link
@Anisimova2020

Anisimova2020 May 19, 2023

Contributor

Чем принципиальна важна такая смена?

This comment has been minimized.

Copy link
@turbcool

turbcool May 19, 2023

Author Contributor

Чтобы UnityContainer в тесты передать. Альтернативный способ - через разрешение зависимостей, придётся установить пакет https://github.com/pengweiqhca/Xunit.DependencyInjection - тогда UnityContainer можно теоретически через встроенный DI зарегистрировать и передавать в конструктор теста.

public abstract class BaseIntegratedTest : IClassFixture<TestFixtureData>, IDisposable
{
protected readonly WebApplicationFactory<Startup> _factory;
#endif
protected readonly IUnityContainer _container;
/// <summary>
/// Provider for injection to data services for test purposes.
/// </summary>
Expand Down Expand Up @@ -155,6 +156,7 @@ public void Dispose()
/// <param name="useGisDataService">Use DataService with Gis support.</param>
protected BaseIntegratedTest(string tempDbNamePrefix, bool useGisDataService = false)
{
_container = UnityFactory.GetContainer();
#endif
#if NETCOREAPP
/// <summary>
Expand All @@ -164,15 +166,20 @@ protected BaseIntegratedTest(string tempDbNamePrefix, bool useGisDataService = f
/// <param name="output">Unit tests debug output.</param>
/// <param name="tempDbNamePrefix">Prefix for temp database name.</param>
/// <param name="useGisDataService">Use DataService with Gis support.</param>
protected BaseIntegratedTest(CustomWebApplicationFactory<Startup> factory, ITestOutputHelper output, string tempDbNamePrefix, bool useGisDataService = false)
protected BaseIntegratedTest(TestFixtureData fixtureData, ITestOutputHelper output, string tempDbNamePrefix, bool useGisDataService = false)
{
_factory = factory;
if (fixtureData == null)
{
throw new ArgumentNullException(nameof(fixtureData));
}

_factory = fixtureData.factory;
_container = fixtureData.unityContainer;
_output = output;

if (output != null)
{
IUnityContainer container = UnityFactory.GetContainer();
container.RegisterInstance(_output);
_container.RegisterInstance(_output);
}
#endif
_useGisDataService = useGisDataService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ public BaseODataServiceIntegratedTest(
}
#endif
#if NETCOREAPP
public BaseODataServiceIntegratedTest(CustomWebApplicationFactory<Startup> factory, ITestOutputHelper output = null, bool useNamespaceInEntitySetName = false, bool useGisDataService = false, PseudoDetailDefinitions pseudoDetailDefinitions = null)
: base(factory, output, "ODataDB", useGisDataService)
public BaseODataServiceIntegratedTest(TestFixtureData fixtureData, ITestOutputHelper output = null, bool useNamespaceInEntitySetName = false, bool useGisDataService = false, PseudoDetailDefinitions pseudoDetailDefinitions = null)
: base(fixtureData, output, "ODataDB", useGisDataService)
{
Init(useNamespaceInEntitySetName, pseudoDetailDefinitions);
}
Expand All @@ -98,12 +98,11 @@ private void Init(
/// <param name="e">Исключение, которое возникло внутри ODataService.</param>
/// <param name="code">Возвращаемый код HTTP. По-умолчанияю 500.</param>
/// <returns>Исключение, которое будет отправлено клиенту.</returns>
public static Exception AfterInternalServerError(Exception e, ref HttpStatusCode code)
public Exception AfterInternalServerError(Exception e, ref HttpStatusCode code)
{
IUnityContainer container = UnityFactory.GetContainer();
if (container.IsRegistered<ITestOutputHelper>())
if (_container.IsRegistered<ITestOutputHelper>())
{
ITestOutputHelper output = container.Resolve<ITestOutputHelper>();
ITestOutputHelper output = _container.Resolve<ITestOutputHelper>();
output.WriteLine(e.ToString());
}

Expand Down Expand Up @@ -168,16 +167,14 @@ public virtual void ActODataService(Action<TestArgs> action)
// Add "/odata/" postfix.
client.BaseAddress = new Uri(client.BaseAddress, DataObjectRoutingConventions.DefaultRouteName + "/");

IUnityContainer container = UnityFactory.GetContainer();

ManagementToken token = (ManagementToken)container.Resolve(typeof(ManagementToken));
container.RegisterInstance(dataService);
ManagementToken token = (ManagementToken)_container.Resolve(typeof(ManagementToken));
_container.RegisterInstance(dataService);
token.Events.CallbackAfterInternalServerError = AfterInternalServerError;

var fileAccessor = (IDataObjectFileAccessor)_factory.Services.GetService(typeof(IDataObjectFileAccessor));
container.RegisterInstance(fileAccessor);
_container.RegisterInstance(fileAccessor);

var args = new TestArgs { UnityContainer = container, DataService = dataService, HttpClient = client, Token = token };
var args = new TestArgs { UnityContainer = _container, DataService = dataService, HttpClient = client, Token = token };
action(args);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ public class BusinessServersTest : BaseODataServiceIntegratedTest
/// <summary>
/// Конструктор по-умолчанию.
/// </summary>
/// <param name="factory">Фабрика для приложения.</param>
/// <param name="fixtureData">Данные для теста.</param>
/// <param name="output">Вывод отладочной информации.</param>
public BusinessServersTest(CustomWebApplicationFactory<ODataServiceSample.AspNetCore.Startup> factory, Xunit.Abstractions.ITestOutputHelper output)
: base(factory, output)
public BusinessServersTest(TestFixtureData fixtureData, Xunit.Abstractions.ITestOutputHelper output)
: base(fixtureData, output)
{
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ public class ChangeMasterInBSTest : BaseODataServiceIntegratedTest
/// <summary>
/// Конструктор по-умолчанию.
/// </summary>
/// <param name="factory">Фабрика для приложения.</param>
/// <param name="fixtureData">Данные для теста.</param>
/// <param name="output">Вывод отладочной информации.</param>
public ChangeMasterInBSTest(CustomWebApplicationFactory<ODataServiceSample.AspNetCore.Startup> factory, Xunit.Abstractions.ITestOutputHelper output)
: base(factory, output)
public ChangeMasterInBSTest(TestFixtureData fixtureData, Xunit.Abstractions.ITestOutputHelper output)
: base(fixtureData, output)
{
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public CreateWithPseudoDetailDefinedTest() : base(pseudoDetailDefinitions: GetPs
}
#endif
#if NETCOREAPP
public CreateWithPseudoDetailDefinedTest(CustomWebApplicationFactory<ODataServiceSample.AspNetCore.Startup> factory, Xunit.Abstractions.ITestOutputHelper output) : base(factory, output, pseudoDetailDefinitions: GetPseudoDetailDefinitions())
public CreateWithPseudoDetailDefinedTest(TestFixtureData fixtureData, Xunit.Abstractions.ITestOutputHelper output) : base(fixtureData, output, pseudoDetailDefinitions: GetPseudoDetailDefinitions())
{
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ public GisCRUDTest()
/// <summary>
/// Конструктор по-умолчанию.
/// </summary>
/// <param name="factory">Фабрика для приложения.</param>
/// <param name="fixtureData">Данные для теста.</param>
/// <param name="output">Вывод диагностической информации по тестам.</param>
public GisCRUDTest(CustomWebApplicationFactory<ODataServiceSample.AspNetCore.Startup> factory, ITestOutputHelper output)
: base(factory, output, false, true)
public GisCRUDTest(TestFixtureData fixtureData, ITestOutputHelper output)
: base(fixtureData, output, false, true)
{
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ public MultiThreadTests(Xunit.Abstractions.ITestOutputHelper output)
/// <summary>
/// Конструктор по-умолчанию.
/// </summary>
/// <param name="factory">Фабрика для приложения.</param>
/// <param name="fixtureData">Данные для теста.</param>
/// <param name="output">Вывод отладочной информации.</param>
public MultiThreadTests(CustomWebApplicationFactory<ODataServiceSample.AspNetCore.Startup> factory, Xunit.Abstractions.ITestOutputHelper output)
: base(factory, output)
public MultiThreadTests(TestFixtureData fixtureData, Xunit.Abstractions.ITestOutputHelper output)
: base(fixtureData, output)
{
}
#endif
Expand All @@ -71,15 +71,8 @@ public void CreateTest()
ActODataService(
args =>
{
// Arrange.
IUnityContainer container;
#if NETFRAMEWORK
container = UnityFactory.GetContainer();
#elif NETCOREAPP
container = args.UnityContainer;
#endif
RegisterCustomUser(container);
ICurrentUser currentUser = container.Resolve<ICurrentUser>();
RegisterCustomUser(_container);
ICurrentUser currentUser = _container.Resolve<ICurrentUser>();
// Регистрация событий.
args.Token.Events.CallbackBeforeCreate = dobj =>
Expand Down Expand Up @@ -126,11 +119,7 @@ public void ReadTest()
args =>
{
// Arrange.
#if NETFRAMEWORK
RegisterCustomUser(UnityFactory.GetContainer());
#elif NETCOREAPP
RegisterCustomUser(args.UnityContainer);
#endif
RegisterCustomUser(_container);
// Создаем объекты и кладем их в базу данных.
DataObject[] countries = new DataObject[ThreadCount];
Expand Down Expand Up @@ -170,14 +159,8 @@ public void UpdateTest()
args =>
{
// Arrange.
IUnityContainer container;
#if NETFRAMEWORK
container = UnityFactory.GetContainer();
#elif NETCOREAPP
container = args.UnityContainer;
#endif
RegisterCustomUser(container);
ICurrentUser currentUser = container.Resolve<ICurrentUser>();
RegisterCustomUser(_container);
ICurrentUser currentUser = _container.Resolve<ICurrentUser>();
// Создаем объекты и кладем их в базу данных.
DataObject[] countries = new DataObject[ThreadCount];
Expand Down Expand Up @@ -233,14 +216,8 @@ public void DeleteTest()
args =>
{
// Arrange.
IUnityContainer container;
#if NETFRAMEWORK
container = UnityFactory.GetContainer();
#elif NETCOREAPP
container = args.UnityContainer;
#endif
RegisterCustomUser(container);
ICurrentUser currentUser = container.Resolve<ICurrentUser>();
RegisterCustomUser(_container);
ICurrentUser currentUser = _container.Resolve<ICurrentUser>();
// Создаем объекты и кладем их в базу данных.
DataObject[] countries = new DataObject[ThreadCount];
Expand Down Expand Up @@ -302,14 +279,8 @@ public void BatchTest()
args =>
{
// Arrange.
IUnityContainer container;
#if NETFRAMEWORK
container = UnityFactory.GetContainer();
#elif NETCOREAPP
container = args.UnityContainer;
#endif
RegisterCustomUser(container);
ICurrentUser currentUser = container.Resolve<ICurrentUser>();
RegisterCustomUser(_container);
ICurrentUser currentUser = _container.Resolve<ICurrentUser>();
// Регистрация событий.
args.Token.Events.CallbackBeforeUpdate = dobj =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ public class BuiltinQueryFunctionsTest : BaseODataServiceIntegratedTest
/// <summary>
/// Конструктор по-умолчанию.
/// </summary>
/// <param name="factory">Фабрика для приложения.</param>
/// <param name="fixtureData">Данные для теста.</param>
/// <param name="output">Вывод отладочной информации.</param>
public BuiltinQueryFunctionsTest(CustomWebApplicationFactory<ODataServiceSample.AspNetCore.Startup> factory, Xunit.Abstractions.ITestOutputHelper output)
: base(factory, output)
public BuiltinQueryFunctionsTest(TestFixtureData fixtureData, Xunit.Abstractions.ITestOutputHelper output)
: base(fixtureData, output)
{
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ public class ExcelExportTest : BaseODataServiceIntegratedTest
/// <summary>
/// Конструктор по-умолчанию.
/// </summary>
/// <param name="factory">Фабрика для приложения.</param>
/// <param name="fixtureData">Данные для теста.</param>
/// <param name="output">Вывод отладочной информации.</param>
public ExcelExportTest(CustomWebApplicationFactory<ODataServiceSample.AspNetCore.Startup> factory, Xunit.Abstractions.ITestOutputHelper output)
: base(factory, output)
public ExcelExportTest(TestFixtureData fixtureData, Xunit.Abstractions.ITestOutputHelper output)
: base(fixtureData, output)
{
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ public class FilterByMasterDetailFieldTest : BaseODataServiceIntegratedTest
/// <summary>
/// Конструктор по-умолчанию.
/// </summary>
/// <param name="factory">Фабрика для приложения.</param>
/// <param name="fixtureData">Данные для теста.</param>
/// <param name="output">Вывод отладочной информации.</param>
public FilterByMasterDetailFieldTest(CustomWebApplicationFactory<ODataServiceSample.AspNetCore.Startup> factory, Xunit.Abstractions.ITestOutputHelper output)
: base(factory, output)
public FilterByMasterDetailFieldTest(TestFixtureData fixtureData, Xunit.Abstractions.ITestOutputHelper output)
: base(fixtureData, output)
{
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ public class FilterByMasterFieldTest : BaseODataServiceIntegratedTest
/// <summary>
/// Конструктор по-умолчанию.
/// </summary>
/// <param name="factory">Фабрика для приложения.</param>
/// <param name="fixtureData">Данные для теста.</param>
/// <param name="output">Вывод отладочной информации.</param>
public FilterByMasterFieldTest(CustomWebApplicationFactory<ODataServiceSample.AspNetCore.Startup> factory, Xunit.Abstractions.ITestOutputHelper output)
: base(factory, output)
public FilterByMasterFieldTest(TestFixtureData fixtureData, Xunit.Abstractions.ITestOutputHelper output)
: base(fixtureData, output)
{
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ public class FilterByMasterMasterDetailFieldTest : BaseODataServiceIntegratedTes
/// <summary>
/// Конструктор по-умолчанию.
/// </summary>
/// <param name="factory">Фабрика для приложения.</param>
/// <param name="fixtureData">Данные для теста.</param>
/// <param name="output">Вывод отладочной информации.</param>
public FilterByMasterMasterDetailFieldTest(CustomWebApplicationFactory<ODataServiceSample.AspNetCore.Startup> factory, Xunit.Abstractions.ITestOutputHelper output)
: base(factory, output)
public FilterByMasterMasterDetailFieldTest(TestFixtureData fixtureData, Xunit.Abstractions.ITestOutputHelper output)
: base(fixtureData, output)
{
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,10 @@ public FilterByPseudoDetailFieldTest() : base(pseudoDetailDefinitions: GetPseudo
}
#endif
#if NETCOREAPP
public FilterByPseudoDetailFieldTest(CustomWebApplicationFactory<ODataServiceSample.AspNetCore.Startup> factory, Xunit.Abstractions.ITestOutputHelper output)
: base(factory, output, pseudoDetailDefinitions: GetPseudoDetailDefinitions())
public FilterByPseudoDetailFieldTest(TestFixtureData fixtureData, Xunit.Abstractions.ITestOutputHelper output)
: base(fixtureData, output, pseudoDetailDefinitions: GetPseudoDetailDefinitions())
{
IUnityContainer container = UnityFactory.GetContainer();
container.RegisterInstance(GetPseudoDetailDefinitions());
_container.RegisterInstance(GetPseudoDetailDefinitions());
}
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ public class FilterTest : BaseODataServiceIntegratedTest
/// <summary>
/// Конструктор по-умолчанию.
/// </summary>
/// <param name="factory">Фабрика для приложения.</param>
/// <param name="fixtureData">Данные для теста.</param>
/// <param name="output">Вывод отладочной информации.</param>
public FilterTest(CustomWebApplicationFactory<ODataServiceSample.AspNetCore.Startup> factory, Xunit.Abstractions.ITestOutputHelper output)
: base(factory, output)
public FilterTest(TestFixtureData fixtureData, Xunit.Abstractions.ITestOutputHelper output)
: base(fixtureData, output)
{
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ public class MetaDataTest : BaseODataServiceIntegratedTest
/// <summary>
/// Конструктор по-умолчанию.
/// </summary>
/// <param name="factory">Фабрика для приложения.</param>
/// <param name="fixtureData">Данные для теста.</param>
/// <param name="output">Вывод отладочной информации.</param>
public MetaDataTest(CustomWebApplicationFactory<ODataServiceSample.AspNetCore.Startup> factory, Xunit.Abstractions.ITestOutputHelper output)
: base(factory, output)
public MetaDataTest(TestFixtureData fixtureData, Xunit.Abstractions.ITestOutputHelper output)
: base(fixtureData, output)
{
}
#endif
Expand Down
Loading

0 comments on commit c5b8a4c

Please sign in to comment.