feat(*): Use SystemTime.Now to enable testing created datetimes for RepoFile objects

This commit is contained in:
Josh Creek
2021-09-02 20:18:09 +01:00
parent 45d0f05d2d
commit 4bd84e8d8f
4 changed files with 16 additions and 7 deletions
@@ -24,6 +24,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Creek.HelpfulExtensions" Version="1.2.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0"> <PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
@@ -1,5 +1,6 @@
using Creek.FileRepository.Helpers; using Creek.FileRepository.Helpers;
using Creek.FileRepository.Models; using Creek.FileRepository.Models;
using Creek.HelpfulExtensions;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Renci.SshNet; using Renci.SshNet;
using Renci.SshNet.Sftp; using Renci.SshNet.Sftp;
@@ -112,7 +113,7 @@ namespace Creek.FileRepository.Repositories
{ {
client.Connect(); client.Connect();
RepoFile repoFile = new RepoFile(filename, DateTime.Now); RepoFile repoFile = new RepoFile(filename, SystemTime.Now());
client.DownloadFile($"{this.remoteDirectoryPath}/{filename}", output: repoFile.Content); client.DownloadFile($"{this.remoteDirectoryPath}/{filename}", output: repoFile.Content);
+12 -6
View File
@@ -1,5 +1,6 @@
using Creek.FileRepository; using Creek.FileRepository;
using Creek.FileRepository.Models; using Creek.FileRepository.Models;
using Creek.HelpfulExtensions;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using NUnit.Framework; using NUnit.Framework;
@@ -23,7 +24,7 @@ namespace UnitTests
[Test] [Test]
public async Task ShouldNotCreateAFileWithAnInvalidFilename() public async Task ShouldNotCreateAFileWithAnInvalidFilename()
{ {
Assert.That(() => CreateTestFile("create><invalid?/test.txt"), Throws.TypeOf<ArgumentException>()); Assert.That(() => CreateTestFile("create><invalid?/test.txt", SystemTime.Now()), Throws.TypeOf<ArgumentException>());
} }
[Test] [Test]
@@ -31,7 +32,7 @@ namespace UnitTests
{ {
try try
{ {
await CreateTestFile("createtest.txt"); await CreateTestFile("createtest.txt", SystemTime.Now());
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -57,11 +58,14 @@ namespace UnitTests
{ {
string filename = "readtest.txt"; string filename = "readtest.txt";
DateTime createdDateTime = new DateTime(2021, 9, 2, 12, 30, 21);
SystemTime.Now = () => new DateTime(2021, 9, 2, 12, 30, 21);
// Setup // Setup
try try
{ {
// Create a file to read // Create a file to read
await CreateTestFile(filename); await CreateTestFile(filename, createdDateTime);
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -79,6 +83,8 @@ namespace UnitTests
Assert.NotNull(repoFile.Filename); Assert.NotNull(repoFile.Filename);
Assert.NotNull(repoFile.Content); Assert.NotNull(repoFile.Content);
Assert.NotNull(repoFile.Created); Assert.NotNull(repoFile.Created);
Assert.AreEqual(filename, repoFile.Filename);
Assert.AreEqual(createdDateTime, repoFile.Created);
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -101,7 +107,7 @@ namespace UnitTests
try try
{ {
// Create a file to read // Create a file to read
await CreateTestFile(filename); await CreateTestFile(filename, SystemTime.Now());
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -143,13 +149,13 @@ namespace UnitTests
await DeleteTestFile(filename); await DeleteTestFile(filename);
} }
private async Task CreateTestFile(String filename) private async Task CreateTestFile(String filename, DateTime created)
{ {
IFileRepository fileRepository = Factory.GetFileRepository(repositoryType, config); IFileRepository fileRepository = Factory.GetFileRepository(repositoryType, config);
Stream contentStream = StreamHelper.GenerateStreamFromString("a,b \n c,d"); Stream contentStream = StreamHelper.GenerateStreamFromString("a,b \n c,d");
RepoFile repoFile = new RepoFile(filename, DateTime.Now, contentStream); RepoFile repoFile = new RepoFile(filename, created, contentStream);
await fileRepository.CreateFileAsync(repoFile); await fileRepository.CreateFileAsync(repoFile);
} }
+1
View File
@@ -17,6 +17,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Creek.HelpfulExtensions" Version="1.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" /> <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="NUnit" Version="3.13.1" /> <PackageReference Include="NUnit" Version="3.13.1" />