Skip to content

Commit

Permalink
refactored all unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bezzad committed Nov 2, 2023
1 parent 067e1e7 commit 74558bc
Show file tree
Hide file tree
Showing 19 changed files with 1,849 additions and 1,822 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace Downloader.DummyHttpServer.Controllers
{
[ApiController]
[Route("[controller]")]
[DummyApiExceptionFilter]
public class DummyFileController : ControllerBase
{
private readonly ILogger<DummyFileController> _logger;
Expand Down Expand Up @@ -160,7 +161,7 @@ public FileStreamResult GetOverflowedFile(long size, int offset = 0)
/// <param name="offset">timeout offset</param>
/// <returns>File stream</returns>
[HttpGet]
[Route("file/size/{size}/timeout/{offset}")]
[Route("file/size/{size}/timeout/{offset}")]
public FileStreamResult GetSlowFile(long size, int offset = 0)
{
_logger.LogTrace($"file/size/{size}/timeout/{offset}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;net6.0;</TargetFrameworks>
<LangVersion>9.0</LangVersion>
<LangVersion>latestMajor</LangVersion>
<StartupObject>Downloader.DummyHttpServer.HttpServer</StartupObject>
<ApplicationIcon />
<OutputType>Exe</OutputType>
Expand Down
13 changes: 6 additions & 7 deletions src/Downloader.DummyHttpServer/DummyApiException.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using System.Net;

namespace Downloader.DummyHttpServer
namespace Downloader.DummyHttpServer;

public class DummyApiException : WebException
{
public class DummyApiException : WebException
public DummyApiException(string message)
: base(message, WebExceptionStatus.Timeout)
{
public DummyApiException(string message)
: base(message, WebExceptionStatus.Timeout)
{
}
}
}
}
21 changes: 21 additions & 0 deletions src/Downloader.DummyHttpServer/DummyApiExceptionFilterAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System.Net;
using static System.Console;

namespace Downloader.DummyHttpServer;

public class DummyApiExceptionFilterAttribute : ExceptionFilterAttribute
{
public override void OnException(ExceptionContext context)
{
if (context.Exception is DummyApiException)
{
context.Result = new StatusCodeResult((int)HttpStatusCode.RequestedRangeNotSatisfiable);
}
else
{
WriteLine($"Exception on {context.ActionDescriptor.DisplayName}: {context.Exception.Message}");
}
}
}
4 changes: 4 additions & 0 deletions src/Downloader.Test/ConfigAwait.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
using Fody;

[assembly: ConfigureAwait(false)]
namespace Downloader.Test;
2 changes: 2 additions & 0 deletions src/Downloader.Test/Downloader.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
<IncludeSymbols>True</IncludeSymbols>
<Title>Downloader Tests</Title>
<LangVersion>latestMajor</LangVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand All @@ -24,6 +25,7 @@
<PackageReference Include="AssertMessage.Fody" Version="2.1.0">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="ConfigureAwait.Fody" Version="3.3.2" />
<PackageReference Include="Fody" Version="6.8.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
1 change: 1 addition & 0 deletions src/Downloader.Test/FodyWeavers.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
<AssertMessage />
<ConfigureAwait ContinueOnCapturedContext="false" />
</Weavers>
5 changes: 5 additions & 0 deletions src/Downloader.Test/FodyWeavers.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
<xs:element name="Weavers">
<xs:complexType>
<xs:all>
<xs:element name="ConfigureAwait" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:attribute name="ContinueOnCapturedContext" type="xs:boolean" />
</xs:complexType>
</xs:element>
<xs:element name="AssertMessage" minOccurs="0" maxOccurs="1" type="xs:anyType" />
</xs:all>
<xs:attribute name="VerifyAssembly" type="xs:boolean">
Expand Down
50 changes: 24 additions & 26 deletions src/Downloader.Test/Helper/AssertHelper.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,35 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Linq;
using System.Reflection;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Downloader.Test.Helper
namespace Downloader.Test.Helper;

public static class AssertHelper
{
public static class AssertHelper
public static void DoesNotThrow<T>(Action action) where T : Exception
{
public static void DoesNotThrow<T>(Action action) where T : Exception
try
{
try
{
action();
}
catch (T)
{
Assert.Fail("Expected no {0} to be thrown", typeof(T).Name);
}
catch
{
return;
}
action();
}

public static void AreEquals(Chunk source, Chunk destination)
catch (T)
{
Assert.IsNotNull(source);
Assert.IsNotNull(destination);
Assert.Fail("Expected no {0} to be thrown", typeof(T).Name);
}
catch
{
return;
}
}

public static void AreEquals(Chunk source, Chunk destination)
{
Assert.IsNotNull(source);
Assert.IsNotNull(destination);

foreach (var prop in typeof(Chunk).GetProperties().Where(p => p.CanRead && p.CanWrite))
{
Assert.AreEqual(prop.GetValue(source), prop.GetValue(destination), prop.Name);
}
foreach (var prop in typeof(Chunk).GetProperties().Where(p => p.CanRead && p.CanWrite))
{
Assert.AreEqual(prop.GetValue(source), prop.GetValue(destination), prop.Name);
}
}
}
51 changes: 25 additions & 26 deletions src/Downloader.Test/Helper/DownloadServiceEventsState.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
using System;

namespace Downloader.Test.Helper
namespace Downloader.Test.Helper;

public class DownloadServiceEventsState
{
public class DownloadServiceEventsState
{
public bool DownloadStarted { get; set; }
public string ActualFileName { get; set; }
public bool DownloadSuccessfullCompleted { get; set; }
public bool IsDownloadCancelled { get; set; }
public bool DownloadProgressIsCorrect { get; set; } = true;
public int DownloadProgressCount { get; set; } = 0;
public Exception DownloadError { get; set; }
public bool DownloadStarted { get; set; }
public string ActualFileName { get; set; }
public bool DownloadSuccessfullCompleted { get; set; }
public bool IsDownloadCancelled { get; set; }
public bool DownloadProgressIsCorrect { get; set; } = true;
public int DownloadProgressCount { get; set; } = 0;
public Exception DownloadError { get; set; }

public DownloadServiceEventsState(IDownloadService downloadService)
{
downloadService.DownloadStarted += (s, e) => {
DownloadStarted = true;
ActualFileName = e.FileName;
};
public DownloadServiceEventsState(IDownloadService downloadService)
{
downloadService.DownloadStarted += (s, e) => {
DownloadStarted = true;
ActualFileName = e.FileName;
};

downloadService.DownloadProgressChanged += (s, e) => {
DownloadProgressCount++;
DownloadProgressIsCorrect &= e.ProgressPercentage == downloadService.Package.SaveProgress;
};
downloadService.DownloadProgressChanged += (s, e) => {
DownloadProgressCount++;
DownloadProgressIsCorrect &= e.ProgressPercentage == downloadService.Package.SaveProgress;
};

downloadService.DownloadFileCompleted += (s, e) => {
DownloadSuccessfullCompleted = e.Error == null && !e.Cancelled;
DownloadError = e.Error;
IsDownloadCancelled = DownloadSuccessfullCompleted == false && DownloadError == null;
};
}
downloadService.DownloadFileCompleted += (s, e) => {
DownloadSuccessfullCompleted = e.Error == null && !e.Cancelled;
DownloadError = e.Error;
IsDownloadCancelled = DownloadSuccessfullCompleted == false && DownloadError == null;
};
}
}
101 changes: 50 additions & 51 deletions src/Downloader.Test/Helper/ExceptionThrower.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,70 +3,69 @@
using System.Net;
using System.Net.Http;

namespace Downloader.Test.Helper
namespace Downloader.Test.Helper;

public static class ExceptionThrower
{
public static class ExceptionThrower
public static Exception GetException()
{
public static Exception GetException()
try
{
ThrowException();
return new Exception(); // This code will never run.
}
catch (Exception e)
{
try
{
ThrowException();
return new Exception(); // This code will never run.
}
catch (Exception e)
{
return e;
}
return e;
}
public static Exception GetWebException()
}
public static Exception GetWebException()
{
try
{
try
{
ThrowWebException();
return new WebException(); // This code will never run.
}
catch (Exception e)
{
return e;
}
ThrowWebException();
return new WebException(); // This code will never run.
}
private static void ThrowWebException()
catch (Exception e)
{
try
{
ThrowIoException();
}
catch (Exception e)
{
throw new WebException("High level exception", e);
}
return e;
}
private static void ThrowException()
}
private static void ThrowWebException()
{
try
{
try
{
ThrowIoException();
}
catch (Exception e)
{
throw new Exception("High level exception", e);
}
ThrowIoException();
}
private static void ThrowIoException()
catch (Exception e)
{
try
{
ThrowHttpRequestException();
}
catch (Exception e)
{
throw new IOException("Mid level exception", e);
}
throw new WebException("High level exception", e);
}
private static void ThrowHttpRequestException()
}
private static void ThrowException()
{
try
{
throw new HttpRequestException("Low level exception");
ThrowIoException();
}
catch (Exception e)
{
throw new Exception("High level exception", e);
}
}
private static void ThrowIoException()
{
try
{
ThrowHttpRequestException();
}
catch (Exception e)
{
throw new IOException("Mid level exception", e);
}
}
private static void ThrowHttpRequestException()
{
throw new HttpRequestException("Low level exception");
}
}
Loading

0 comments on commit 74558bc

Please sign in to comment.