update unit tests

This commit is contained in:
RaidMax 2020-11-12 19:46:17 -06:00
parent 5d9c8f5369
commit e997b94b3b
10 changed files with 52 additions and 58 deletions

View File

@ -17,7 +17,7 @@ namespace IW4MAdmin.Application.IO
private readonly bool _ignoreBots; private readonly bool _ignoreBots;
private readonly ILogger _logger; private readonly ILogger _logger;
public GameLogEventDetection(ILogger<GameLogEventDetection> logger, Server server, Uri[] gameLogUris, IGameLogReaderFactory gameLogReaderFactory) public GameLogEventDetection(ILogger<GameLogEventDetection> logger, IW4MServer server, Uri[] gameLogUris, IGameLogReaderFactory gameLogReaderFactory)
{ {
_reader = gameLogReaderFactory.CreateGameLogReader(gameLogUris, server.EventParser); _reader = gameLogReaderFactory.CreateGameLogReader(gameLogUris, server.EventParser);
_server = server; _server = server;

View File

@ -9,8 +9,8 @@ using SharedLibraryCore;
using SharedLibraryCore.Configuration; using SharedLibraryCore.Configuration;
using SharedLibraryCore.Interfaces; using SharedLibraryCore.Interfaces;
using System; using System;
using Microsoft.Extensions.Logging;
using static SharedLibraryCore.GameEvent; using static SharedLibraryCore.GameEvent;
using ILogger = Microsoft.Extensions.Logging.ILogger;
namespace ApplicationTests namespace ApplicationTests
{ {

View File

@ -8,6 +8,7 @@ using SharedLibraryCore.Database.Models;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
using SharedLibraryCore.Configuration;
namespace ApplicationTests namespace ApplicationTests
{ {

View File

@ -16,6 +16,7 @@ using SharedLibraryCore.Services;
using static SharedLibraryCore.Database.Models.EFClient; using static SharedLibraryCore.Database.Models.EFClient;
using FluentAssertions; using FluentAssertions;
using FluentAssertions.Extensions; using FluentAssertions.Extensions;
using ILogger = Microsoft.Extensions.Logging.ILogger;
namespace ApplicationTests namespace ApplicationTests
{ {
@ -50,6 +51,10 @@ namespace ApplicationTests
appConfig = serviceProvider.GetRequiredService<ApplicationConfiguration>(); appConfig = serviceProvider.GetRequiredService<ApplicationConfiguration>();
appConfig.MapChangeDelaySeconds = 1; appConfig.MapChangeDelaySeconds = 1;
cmdConfig = serviceProvider.GetRequiredService<CommandConfiguration>(); cmdConfig = serviceProvider.GetRequiredService<CommandConfiguration>();
serviceProvider.GetService<IW4MServer>().RconParser =
serviceProvider.GetService<IRConParser>();
Utilities.DefaultLogger = serviceProvider.GetRequiredService<ILogger>();
A.CallTo(() => manager.GetClientService()) A.CallTo(() => manager.GetClientService())
.Returns(clientService); .Returns(clientService);

View File

@ -6,8 +6,7 @@ using Microsoft.Extensions.DependencyInjection;
using SharedLibraryCore.Configuration; using SharedLibraryCore.Configuration;
using SharedLibraryCore.Interfaces; using SharedLibraryCore.Interfaces;
using System; using System;
using Microsoft.Extensions.Logging; using ILogger = Microsoft.Extensions.Logging.ILogger;
using SharedLibraryCore;
namespace ApplicationTests namespace ApplicationTests
{ {
@ -28,7 +27,6 @@ namespace ApplicationTests
} }
var manager = A.Fake<IManager>(); var manager = A.Fake<IManager>();
var logger = A.Fake<ILogger>();
var transLookup = A.Fake<ITranslationLookup>(); var transLookup = A.Fake<ITranslationLookup>();
A.CallTo(() => transLookup[A<string>.Ignored]) A.CallTo(() => transLookup[A<string>.Ignored])
@ -37,8 +35,11 @@ namespace ApplicationTests
serviceCollection serviceCollection
.AddLogging() .AddLogging()
.AddSingleton(A.Fake<ILogger>()) .AddSingleton(A.Fake<ILogger>())
.AddSingleton(A.Fake<SharedLibraryCore.Interfaces.ILogger>())
.AddSingleton(new ServerConfiguration { IPAddress = "127.0.0.1", Port = 28960 })
.AddSingleton(manager) .AddSingleton(manager)
.AddSingleton<IDatabaseContextFactory, DatabaseContextFactoryMock>() .AddSingleton<IDatabaseContextFactory, DatabaseContextFactoryMock>()
.AddSingleton<IW4MServer>()
.AddSingleton(A.Fake<IRConConnectionFactory>()) .AddSingleton(A.Fake<IRConConnectionFactory>())
.AddSingleton(A.Fake<IRConConnection>()) .AddSingleton(A.Fake<IRConConnection>())
.AddSingleton(transLookup) .AddSingleton(transLookup)
@ -52,17 +53,6 @@ namespace ApplicationTests
.AddSingleton(ConfigurationGenerators.CreateCommandConfiguration()) .AddSingleton(ConfigurationGenerators.CreateCommandConfiguration())
.AddSingleton<IConfigurationHandler<ApplicationConfiguration>, ApplicationConfigurationHandlerMock>(); .AddSingleton<IConfigurationHandler<ApplicationConfiguration>, ApplicationConfigurationHandlerMock>();
serviceCollection.AddSingleton(_sp => new IW4MServer(_sp.GetRequiredService<IManager>(),
ConfigurationGenerators.CreateServerConfiguration(),
_sp.GetRequiredService<ITranslationLookup>(),
_sp.GetRequiredService<IRConConnectionFactory>(),
_sp.GetRequiredService<IGameLogReaderFactory>(),
_sp.GetRequiredService<IMetaService>(),
_sp.GetRequiredService<ILogger<Server>>())
{
RconParser = _sp.GetRequiredService<IRConParser>()
});
return serviceCollection; return serviceCollection;
} }

View File

@ -1,12 +1,11 @@
using FakeItEasy; using FakeItEasy;
using IW4MAdmin;
using IW4MAdmin.Application.IO; using IW4MAdmin.Application.IO;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework; using NUnit.Framework;
using SharedLibraryCore;
using SharedLibraryCore.Interfaces; using SharedLibraryCore.Interfaces;
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using SharedLibraryCore;
namespace ApplicationTests namespace ApplicationTests
{ {
@ -19,19 +18,24 @@ namespace ApplicationTests
[SetUp] [SetUp]
public void Setup() public void Setup()
{ {
serviceProvider = new ServiceCollection().BuildBase().BuildServiceProvider(); serviceProvider = new ServiceCollection()
.BuildBase()
.AddSingleton(new Uri[] { new Uri("C:\\test.log")})
.AddSingleton(A.Fake<IGameLogReaderFactory>())
.AddSingleton<GameLogEventDetection>()
.BuildServiceProvider();
} }
[Test] [Test]
public async Task GameLogEventDetection_WorksAfterFileSizeReset() public async Task GameLogEventDetection_WorksAfterFileSizeReset()
{ {
var reader = A.Fake<IGameLogReader>(); var reader = A.Fake<IGameLogReader>();
var factory = A.Fake<IGameLogReaderFactory>(); var factory = serviceProvider.GetRequiredService<IGameLogReaderFactory>();
A.CallTo(() => factory.CreateGameLogReader(A<Uri[]>.Ignored, A<IEventParser>.Ignored)) A.CallTo(() => factory.CreateGameLogReader(A<Uri[]>.Ignored, A<IEventParser>.Ignored))
.Returns(reader); .Returns(reader);
var detect = new GameLogEventDetection(serviceProvider.GetService<IW4MServer>(), new Uri[] { new Uri("C:\\test.log") }, factory); var detect = serviceProvider.GetRequiredService<GameLogEventDetection>();
A.CallTo(() => reader.Length) A.CallTo(() => reader.Length)
.Returns(100) .Returns(100)

View File

@ -41,6 +41,8 @@ namespace ApplicationTests
fakeRConParser = serviceProvider.GetRequiredService<IRConParser>(); fakeRConParser = serviceProvider.GetRequiredService<IRConParser>();
mockEventHandler = serviceProvider.GetRequiredService<EventHandlerMock>(); mockEventHandler = serviceProvider.GetRequiredService<EventHandlerMock>();
appConfig = serviceProvider.GetRequiredService<ApplicationConfiguration>(); appConfig = serviceProvider.GetRequiredService<ApplicationConfiguration>();
serviceProvider.GetService<IW4MServer>().RconParser =
serviceProvider.GetService<IRConParser>();
var rconConnectionFactory = serviceProvider.GetRequiredService<IRConConnectionFactory>(); var rconConnectionFactory = serviceProvider.GetRequiredService<IRConConnectionFactory>();
@ -215,7 +217,7 @@ namespace ApplicationTests
await server.Ban("test reason", target, origin); await server.Ban("test reason", target, origin);
A.CallTo(() => fakeRConParser.ExecuteCommandAsync(fakeRConConnection, "kick")) A.CallTo(() => server.RconParser.ExecuteCommandAsync(A<IRConConnection>.Ignored, "kick"))
.MustHaveHappenedOnceExactly(); .MustHaveHappenedOnceExactly();
} }
@ -290,7 +292,7 @@ namespace ApplicationTests
await server.TempBan("test reason", TimeSpan.Zero, target, origin); await server.TempBan("test reason", TimeSpan.Zero, target, origin);
A.CallTo(() => fakeRConParser.ExecuteCommandAsync(fakeRConConnection, "kick")) A.CallTo(() => server.RconParser.ExecuteCommandAsync(A<IRConConnection>.Ignored, "kick"))
.MustHaveHappenedOnceExactly(); .MustHaveHappenedOnceExactly();
} }
@ -312,7 +314,7 @@ namespace ApplicationTests
await server.TempBan("test reason", TimeSpan.Zero, target, origin); await server.TempBan("test reason", TimeSpan.Zero, target, origin);
A.CallTo(() => fakeRConParser.ExecuteCommandAsync(fakeRConConnection, "kick")) A.CallTo(() => server.RconParser.ExecuteCommandAsync(A<IRConConnection>.Ignored, "kick"))
.MustHaveHappenedOnceExactly(); .MustHaveHappenedOnceExactly();
} }
#endregion #endregion
@ -348,7 +350,7 @@ namespace ApplicationTests
await server.Kick("test reason", target, origin); await server.Kick("test reason", target, origin);
A.CallTo(() => fakeRConParser.ExecuteCommandAsync(fakeRConConnection, "kick")) A.CallTo(() => server.RconParser.ExecuteCommandAsync(A<IRConConnection>.Ignored, "kick"))
.MustHaveHappenedOnceExactly(); .MustHaveHappenedOnceExactly();
} }

View File

@ -13,7 +13,7 @@ using SharedLibraryCore.Services;
using System; using System;
using System.IO; using System.IO;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Logging; using ILogger = Microsoft.Extensions.Logging.ILogger;
namespace ApplicationTests namespace ApplicationTests
{ {

View File

@ -1,36 +1,32 @@
using FakeItEasy; using FakeItEasy;
using IW4MAdmin; using IW4MAdmin;
using IW4MAdmin.Application;
using IW4MAdmin.Application.EventParsers; using IW4MAdmin.Application.EventParsers;
using NUnit.Framework; using NUnit.Framework;
using SharedLibraryCore.Configuration; using SharedLibraryCore.Configuration;
using SharedLibraryCore.Interfaces; using SharedLibraryCore.Interfaces;
using System; using System;
using System.Diagnostics; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using ILogger = Microsoft.Extensions.Logging.ILogger;
using SharedLibraryCore;
namespace ApplicationTests namespace ApplicationTests
{ {
[TestFixture] [TestFixture]
public class ServerTests public class ServerTests
{ {
private IServiceProvider _serviceProvider;
[SetUp] [SetUp]
public void Setup() public void Setup()
{ {
_serviceProvider = new ServiceCollection()
.BuildBase()
.BuildServiceProvider();
} }
[Test] [Test]
public void GameTimeFalseQuitTest() public void GameTimeFalseQuitTest()
{ {
var mgr = A.Fake<IManager>(); var server = _serviceProvider.GetRequiredService<IW4MServer>();
var server = new IW4MServer(mgr,
new SharedLibraryCore.Configuration.ServerConfiguration() { IPAddress = "127.0.0.1", Port = 28960 },
A.Fake<ITranslationLookup>(), A.Fake<IRConConnectionFactory>(),
A.Fake<IGameLogReaderFactory>(), A.Fake<IMetaService>(), A.Fake<ILogger<Server>>());
var parser = new BaseEventParser(A.Fake<IParserRegexFactory>(), A.Fake<ILogger>(), A.Fake<ApplicationConfiguration>()); var parser = new BaseEventParser(A.Fake<IParserRegexFactory>(), A.Fake<ILogger>(), A.Fake<ApplicationConfiguration>());
parser.Configuration.GuidNumberStyle = System.Globalization.NumberStyles.Integer; parser.Configuration.GuidNumberStyle = System.Globalization.NumberStyles.Integer;
@ -50,13 +46,7 @@ namespace ApplicationTests
[Test] [Test]
public void LogFileReplay() public void LogFileReplay()
{ {
var mgr = A.Fake<IManager>(); var server = _serviceProvider.GetRequiredService<IW4MServer>();
var server = new IW4MServer(mgr,
new SharedLibraryCore.Configuration.ServerConfiguration() { IPAddress = "127.0.0.1", Port = 28960 },
A.Fake<ITranslationLookup>(), A.Fake<IRConConnectionFactory>(), A.Fake<IGameLogReaderFactory>(), A.Fake<IMetaService>(),
A.Fake<ILogger<Server>>());
var parser = new BaseEventParser(A.Fake<IParserRegexFactory>(), A.Fake<ILogger>(), A.Fake<ApplicationConfiguration>()); var parser = new BaseEventParser(A.Fake<IParserRegexFactory>(), A.Fake<ILogger>(), A.Fake<ApplicationConfiguration>());
parser.Configuration.GuidNumberStyle = System.Globalization.NumberStyles.Integer; parser.Configuration.GuidNumberStyle = System.Globalization.NumberStyles.Integer;

View File

@ -13,8 +13,6 @@ using Microsoft.Extensions.DependencyInjection;
using IW4MAdmin.Plugins.Stats.Helpers; using IW4MAdmin.Plugins.Stats.Helpers;
using ApplicationTests.Fixtures; using ApplicationTests.Fixtures;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using SharedLibraryCore;
using Stats.Helpers; using Stats.Helpers;
using Stats.Dtos; using Stats.Dtos;
using SharedLibraryCore.Configuration; using SharedLibraryCore.Configuration;
@ -37,8 +35,11 @@ namespace ApplicationTests
handlerFactory = A.Fake<IConfigurationHandlerFactory>(); handlerFactory = A.Fake<IConfigurationHandlerFactory>();
serviceProvider = new ServiceCollection() serviceProvider = new ServiceCollection()
.AddSingleton<StatsResourceQueryHelper>()
.BuildBase() .BuildBase()
.AddSingleton(A.Fake<IConfigurationHandler<StatsConfiguration>>())
.AddSingleton<StatsResourceQueryHelper>()
.AddSingleton(new ServerConfiguration() { IPAddress = "127.0.0.1", Port = 28960 })
.AddSingleton<StatManager>()
.AddSingleton<IW4MAdmin.Plugins.Stats.Plugin>() .AddSingleton<IW4MAdmin.Plugins.Stats.Plugin>()
.BuildServiceProvider(); .BuildServiceProvider();
@ -61,11 +62,7 @@ namespace ApplicationTests
A.CallTo(() => handlerFactory.GetConfigurationHandler<StatsConfiguration>(A<string>.Ignored)) A.CallTo(() => handlerFactory.GetConfigurationHandler<StatsConfiguration>(A<string>.Ignored))
.Returns(config); .Returns(config);
var server = new IW4MServer(mgr, var server = serviceProvider.GetRequiredService<IW4MServer>();
new SharedLibraryCore.Configuration.ServerConfiguration() { IPAddress = "127.0.0.1", Port = 28960 },
A.Fake<ITranslationLookup>(),
A.Fake<IRConConnectionFactory>(),
A.Fake<IGameLogReaderFactory>(), A.Fake<IMetaService>(), A.Fake<ILogger<Server>>());
var parser = new BaseEventParser(A.Fake<IParserRegexFactory>(), A.Fake<ILogger>(), A.Fake<ApplicationConfiguration>()); var parser = new BaseEventParser(A.Fake<IParserRegexFactory>(), A.Fake<ILogger>(), A.Fake<ApplicationConfiguration>());
parser.Configuration.GuidNumberStyle = System.Globalization.NumberStyles.Integer; parser.Configuration.GuidNumberStyle = System.Globalization.NumberStyles.Integer;
@ -128,8 +125,8 @@ namespace ApplicationTests
public async Task Test_ConcurrentCallsToUpdateStatHistoryDoesNotCauseException() public async Task Test_ConcurrentCallsToUpdateStatHistoryDoesNotCauseException()
{ {
var server = serviceProvider.GetRequiredService<IW4MServer>(); var server = serviceProvider.GetRequiredService<IW4MServer>();
var configHandler = A.Fake<IConfigurationHandler<StatsConfiguration>>(); var configHandler = serviceProvider.GetRequiredService<IConfigurationHandler<StatsConfiguration>>();
var mgr = new StatManager(serviceProvider.GetRequiredService<IManager>(), serviceProvider.GetRequiredService<IDatabaseContextFactory>(), configHandler); var mgr = serviceProvider.GetRequiredService<StatManager>();
var target = ClientGenerators.CreateDatabaseClient(); var target = ClientGenerators.CreateDatabaseClient();
target.CurrentServer = server; target.CurrentServer = server;
@ -141,10 +138,11 @@ namespace ApplicationTests
var dbFactory = serviceProvider.GetRequiredService<IDatabaseContextFactory>(); var dbFactory = serviceProvider.GetRequiredService<IDatabaseContextFactory>();
var db = dbFactory.CreateContext(true); var db = dbFactory.CreateContext(true);
db.Set<EFServer>().Add(new EFServer() var efServer = new EFServer()
{ {
EndPoint = server.EndPoint.ToString() EndPoint = server.EndPoint.ToString()
}); };
db.Set<EFServer>().Add(efServer);
db.Clients.Add(target); db.Clients.Add(target);
db.SaveChanges(); db.SaveChanges();
@ -154,6 +152,10 @@ namespace ApplicationTests
var stats = target.GetAdditionalProperty<EFClientStatistics>("ClientStats"); var stats = target.GetAdditionalProperty<EFClientStatistics>("ClientStats");
await mgr.UpdateStatHistory(target, stats); await mgr.UpdateStatHistory(target, stats);
db.Clients.Remove(target);
db.Set<EFServer>().Remove(efServer);
await db.SaveChangesAsync();
} }
#region QUERY_HELPER #region QUERY_HELPER
@ -161,7 +163,7 @@ namespace ApplicationTests
public async Task Test_StatsQueryHelper_Get() public async Task Test_StatsQueryHelper_Get()
{ {
var queryHelper = serviceProvider.GetRequiredService<StatsResourceQueryHelper>(); var queryHelper = serviceProvider.GetRequiredService<StatsResourceQueryHelper>();
using var context = contextFactory.CreateContext(); await using var context = contextFactory.CreateContext();
var server = new EFServer() { ServerId = 1 }; var server = new EFServer() { ServerId = 1 };
var stats = new EFClientStatistics() var stats = new EFClientStatistics()