Skip to content

Commit

Permalink
Merge pull request #46 from pyrocumulus/factory-coverage
Browse files Browse the repository at this point in the history
Improve factory code coverage, improved interface structure for the object and reader factories and removed dead code. Further enhanced code coverage with more unit tests.
  • Loading branch information
pyrocumulus authored Oct 8, 2020
2 parents de44ba9 + b492ccf commit f411ad3
Show file tree
Hide file tree
Showing 22 changed files with 135 additions and 51 deletions.
7 changes: 7 additions & 0 deletions src/PVOutput.Net/Objects/Core/IArrayStringFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace PVOutput.Net.Objects.Core
{
internal interface IArrayStringFactory<TObjectType> : IObjectStringFactory<TObjectType>
{
IArrayStringReader<TObjectType> CreateArrayReader();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

namespace PVOutput.Net.Objects.Core
{
internal interface IStringFactory<TObjectType>
internal interface IObjectStringFactory<TObjectType>
{
IObjectStringReader<TObjectType> CreateObjectReader();

IArrayStringReader<TObjectType> CreateArrayReader();
}
}
4 changes: 2 additions & 2 deletions src/PVOutput.Net/Objects/Factories/AggregatedOutputFactory.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using PVOutput.Net.Objects.Core;
using PVOutput.Net.Objects.Core;
using PVOutput.Net.Objects.Modules;
using PVOutput.Net.Objects.Modules.Readers;

namespace PVOutput.Net.Objects.Factories
{
internal class AggregatedOutputFactory : IStringFactory<IAggregatedOutput>
internal class AggregatedOutputFactory : IArrayStringFactory<IAggregatedOutput>
{
public IArrayStringReader<IAggregatedOutput> CreateArrayReader() => new CharacterDelimitedArrayStringReader<IAggregatedOutput>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace PVOutput.Net.Objects.Factories
{
internal class BatchStatusPostResultFactory : IStringFactory<IBatchStatusPostResult>
internal class BatchStatusPostResultFactory : IArrayStringFactory<IBatchStatusPostResult>
{
public IArrayStringReader<IBatchStatusPostResult> CreateArrayReader() => new CharacterDelimitedArrayStringReader<IBatchStatusPostResult>();

Expand Down
7 changes: 3 additions & 4 deletions src/PVOutput.Net/Objects/Factories/DayStatisticsFactory.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using PVOutput.Net.Objects.Core;
using System;
using PVOutput.Net.Objects.Core;
using PVOutput.Net.Objects.Modules.Readers;

namespace PVOutput.Net.Objects.Factories
{
internal class DayStatisticsFactory : IStringFactory<IDayStatistics>
internal class DayStatisticsFactory : IObjectStringFactory<IDayStatistics>
{
public IArrayStringReader<IDayStatistics> CreateArrayReader() => new CharacterDelimitedArrayStringReader<IDayStatistics>();

public IObjectStringReader<IDayStatistics> CreateObjectReader() => new DayStatisticsObjectStringReader();
}
}
2 changes: 1 addition & 1 deletion src/PVOutput.Net/Objects/Factories/ExtendedFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace PVOutput.Net.Objects.Factories
{
internal class ExtendedFactory : IStringFactory<IExtended>
internal class ExtendedFactory : IArrayStringFactory<IExtended>
{
public IArrayStringReader<IExtended> CreateArrayReader() => new CharacterDelimitedArrayStringReader<IExtended>();

Expand Down
2 changes: 1 addition & 1 deletion src/PVOutput.Net/Objects/Factories/FavouriteFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace PVOutput.Net.Objects.Factories
{
internal class FavouriteFactory : IStringFactory<IFavourite>
internal class FavouriteFactory : IArrayStringFactory<IFavourite>
{
public IArrayStringReader<IFavourite> CreateArrayReader() => new CharacterDelimitedArrayStringReader<IFavourite>('\n');

Expand Down
2 changes: 1 addition & 1 deletion src/PVOutput.Net/Objects/Factories/InsolationFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace PVOutput.Net.Objects.Factories
{
internal class InsolationFactory : IStringFactory<IInsolation>
internal class InsolationFactory : IArrayStringFactory<IInsolation>
{
public IArrayStringReader<IInsolation> CreateArrayReader() => new CharacterDelimitedArrayStringReader<IInsolation>();

Expand Down
6 changes: 2 additions & 4 deletions src/PVOutput.Net/Objects/Factories/MissingFactory.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
using System;
using System;
using PVOutput.Net.Objects.Core;
using PVOutput.Net.Objects.Modules;
using PVOutput.Net.Objects.Modules.Readers;

namespace PVOutput.Net.Objects.Factories
{
internal class MissingFactory : IStringFactory<IMissing>
internal class MissingFactory : IObjectStringFactory<IMissing>
{
public IArrayStringReader<IMissing> CreateArrayReader() => throw new NotImplementedException();

public IObjectStringReader<IMissing> CreateObjectReader() => new MissingObjectStringReader();
}
}
4 changes: 2 additions & 2 deletions src/PVOutput.Net/Objects/Factories/OutputFactory.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using PVOutput.Net.Objects.Core;
using PVOutput.Net.Objects.Core;
using PVOutput.Net.Objects.Modules;
using PVOutput.Net.Objects.Modules.Readers;

namespace PVOutput.Net.Objects.Factories
{
internal class OutputFactory : IStringFactory<IOutput>
internal class OutputFactory : IArrayStringFactory<IOutput>
{
public IArrayStringReader<IOutput> CreateArrayReader() => new CharacterDelimitedArrayStringReader<IOutput>();

Expand Down
6 changes: 2 additions & 4 deletions src/PVOutput.Net/Objects/Factories/StatisticFactory.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using PVOutput.Net.Objects.Core;
using PVOutput.Net.Objects.Core;
using PVOutput.Net.Objects.Modules;
using PVOutput.Net.Objects.Modules.Readers;

namespace PVOutput.Net.Objects.Factories
{
internal class StatisticFactory : IStringFactory<IStatistic>
internal class StatisticFactory : IObjectStringFactory<IStatistic>
{
public IArrayStringReader<IStatistic> CreateArrayReader() => new CharacterDelimitedArrayStringReader<IStatistic>();

public IObjectStringReader<IStatistic> CreateObjectReader() => new StatisticObjectStringReader();
}
}
4 changes: 1 addition & 3 deletions src/PVOutput.Net/Objects/Factories/StatusFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@

namespace PVOutput.Net.Objects.Factories
{
internal class StatusFactory : IStringFactory<IStatus>
internal class StatusFactory : IObjectStringFactory<IStatus>
{
public IArrayStringReader<IStatus> CreateArrayReader() => new CharacterDelimitedArrayStringReader<IStatus>();

public IObjectStringReader<IStatus> CreateObjectReader() => new StatusObjectStringReader();
}
}
2 changes: 1 addition & 1 deletion src/PVOutput.Net/Objects/Factories/StatusHistoryFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace PVOutput.Net.Objects.Factories
{
internal class StatusHistoryFactory : IStringFactory<IStatusHistory>
internal class StatusHistoryFactory : IArrayStringFactory<IStatusHistory>
{
public IArrayStringReader<IStatusHistory> CreateArrayReader() => new CharacterDelimitedArrayStringReader<IStatusHistory>();

Expand Down
20 changes: 12 additions & 8 deletions src/PVOutput.Net/Objects/Factories/StringFactoryContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,32 @@ static StringFactoryContainer()
_readerFactories.Add(typeof(ISystemSearchResult), new SystemSearchResultFactory());
}

public static IStringFactory<TReturnType> GetStringFactory<TReturnType>()
private static object GetObjectStringFactory<TReturnType>()
{
var type = typeof(TReturnType);

Type type = typeof(TReturnType);
if (!_readerFactories.ContainsKey(type))
{
throw new NotSupportedException($"Factory for {type} is not known");
throw new InvalidOperationException($"Factory for {type} is not known");
}

return (IStringFactory<TReturnType>)_readerFactories[type];
return _readerFactories[type];
}

public static IObjectStringReader<TReturnType> CreateObjectReader<TReturnType>()
{
var factory = GetStringFactory<TReturnType>();
// Currently every factory is an ObjectStringFactory at minimum
var factory = GetObjectStringFactory<TReturnType>() as IObjectStringFactory<TReturnType>;
return factory.CreateObjectReader();
}

public static IArrayStringReader<TReturnType> CreateArrayReader<TReturnType>()
{
var factory = GetStringFactory<TReturnType>();
return factory.CreateArrayReader();
var factory = GetObjectStringFactory<TReturnType>();
if (factory is IArrayStringFactory<TReturnType> arrayFactory)
{
return arrayFactory.CreateArrayReader();
}
throw new InvalidOperationException($"Factory for {typeof(TReturnType)} is not an array factory");
}
}
}
2 changes: 1 addition & 1 deletion src/PVOutput.Net/Objects/Factories/SupplyFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace PVOutput.Net.Objects.Factories
{
internal class SupplyFactory : IStringFactory<ISupply>
internal class SupplyFactory : IArrayStringFactory<ISupply>
{
public IArrayStringReader<ISupply> CreateArrayReader() => new CharacterDelimitedArrayStringReader<ISupply>();

Expand Down
6 changes: 2 additions & 4 deletions src/PVOutput.Net/Objects/Factories/SystemFactory.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using PVOutput.Net.Objects.Core;
using PVOutput.Net.Objects.Core;
using PVOutput.Net.Objects.Modules;
using PVOutput.Net.Objects.Modules.Readers;

namespace PVOutput.Net.Objects.Factories
{
internal class SystemFactory : IStringFactory<ISystem>
internal class SystemFactory : IObjectStringFactory<ISystem>
{
public IArrayStringReader<ISystem> CreateArrayReader() => new CharacterDelimitedArrayStringReader<ISystem>();

public IObjectStringReader<ISystem> CreateObjectReader() => new SystemObjectStringReader();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace PVOutput.Net.Objects.Factories
{
internal class SystemSearchResultFactory : IStringFactory<ISystemSearchResult>
internal class SystemSearchResultFactory : IArrayStringFactory<ISystemSearchResult>
{
public IArrayStringReader<ISystemSearchResult> CreateArrayReader() => new LineDelimitedArrayStringReader<ISystemSearchResult>();

Expand Down
4 changes: 1 addition & 3 deletions src/PVOutput.Net/Objects/Factories/TeamFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@

namespace PVOutput.Net.Objects.Factories
{
internal class TeamFactory : IStringFactory<ITeam>
internal class TeamFactory : IObjectStringFactory<ITeam>
{
public IArrayStringReader<ITeam> CreateArrayReader() => throw new NotImplementedException();

public IObjectStringReader<ITeam> CreateObjectReader() => new TeamObjectStringReader();
}
}
4 changes: 2 additions & 2 deletions src/PVOutput.Net/Objects/Factories/TeamOutputFactory.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using PVOutput.Net.Objects.Core;
using PVOutput.Net.Objects.Core;
using PVOutput.Net.Objects.Modules;
using PVOutput.Net.Objects.Modules.Readers;

namespace PVOutput.Net.Objects.Factories
{
internal class TeamOutputFactory : IStringFactory<ITeamOutput>
internal class TeamOutputFactory : IArrayStringFactory<ITeamOutput>
{
public IArrayStringReader<ITeamOutput> CreateArrayReader() => new CharacterDelimitedArrayStringReader<ITeamOutput>();

Expand Down
6 changes: 3 additions & 3 deletions src/PVOutput.Net/Requests/Handler/RequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ internal async Task<PVOutputResponse<TResponseContentType>> ExecuteSingleItemReq
}
finally
{
responseMessage?.Dispose();
responseMessage.Dispose();
}
}

Expand Down Expand Up @@ -96,7 +96,7 @@ internal async Task<PVOutputArrayResponse<TResponseContentType>> ExecuteArrayReq
}
finally
{
responseMessage?.Dispose();
responseMessage.Dispose();
}
}

Expand Down Expand Up @@ -129,7 +129,7 @@ internal async Task<PVOutputBasicResponse> ExecutePostRequestAsync(IRequest requ
}
finally
{
responseMessage?.Dispose();
responseMessage.Dispose();
}
}

Expand Down
38 changes: 38 additions & 0 deletions tests/PVOutput.Net.Tests/Handler/BaseRequestHandlingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using NUnit.Framework;
using PVOutput.Net.Enums;
using PVOutput.Net.Objects;
using PVOutput.Net.Objects.Core;
using PVOutput.Net.Objects.Factories;
using PVOutput.Net.Objects.Modules.Implementations;
using PVOutput.Net.Responses;
using PVOutput.Net.Tests.Utils;
Expand Down Expand Up @@ -225,5 +227,41 @@ public void BaseResponse_ImplicitBoolConversion_ReturnsSuccessState()
Assert.That((bool)response3, Is.False);
});
}

[Test]
public async Task BaseObjectReader_WithNullStream_ReturnsDefaultForType()
{
IObjectStringReader<IStatus> reader = StringFactoryContainer.CreateObjectReader<IStatus>();
IStatus content = await reader.ReadObjectAsync(stream: null, cancellationToken: default).ConfigureAwait(false);

Assert.That(content, Is.Null);
}

[Test]
public async Task BaseArrayReader_WithNullStream_ReturnsDefaultForType()
{
IArrayStringReader<ISystemSearchResult> reader = StringFactoryContainer.CreateArrayReader<ISystemSearchResult>();
IEnumerable<ISystemSearchResult> content = await reader.ReadArrayAsync(stream: null, cancellationToken: default).ConfigureAwait(false);

Assert.That(content, Is.Null);
}

[Test]
public async Task CharacterDelimitedArrayReader_WithNullStream_ReturnsDefaultForType()
{
IArrayStringReader<ISystemSearchResult> reader = new CharacterDelimitedArrayStringReader<ISystemSearchResult>();
IEnumerable<ISystemSearchResult> content = await reader.ReadArrayAsync(stream: null, cancellationToken: default).ConfigureAwait(false);

Assert.That(content, Is.Null);
}

[Test]
public async Task LineDelimitedArrayReader_WithNullStream_ReturnsDefaultForType()
{
IArrayStringReader<ISystemSearchResult> reader = new LineDelimitedArrayStringReader<ISystemSearchResult>();
IEnumerable<ISystemSearchResult> content = await reader.ReadArrayAsync(stream: null, cancellationToken: default).ConfigureAwait(false);

Assert.That(content, Is.Null);
}
}
}
Loading

0 comments on commit f411ad3

Please sign in to comment.