From 4bd84e8d8fa57e39bd3c21487d1c115e3cf2fa58 Mon Sep 17 00:00:00 2001 From: Josh Creek Date: Thu, 2 Sep 2021 20:18:09 +0100 Subject: [PATCH] feat(*): Use SystemTime.Now to enable testing created datetimes for RepoFile objects --- .../Creek.FileRepository.csproj | 1 + .../Repositories/SftpRepository.cs | 3 ++- UnitTests/SftpRepositoryShould.cs | 18 ++++++++++++------ UnitTests/UnitTests.csproj | 1 + 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/Creek.FileRepository/Creek.FileRepository.csproj b/Creek.FileRepository/Creek.FileRepository.csproj index efd839b..ce1967b 100644 --- a/Creek.FileRepository/Creek.FileRepository.csproj +++ b/Creek.FileRepository/Creek.FileRepository.csproj @@ -24,6 +24,7 @@ + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/Creek.FileRepository/Repositories/SftpRepository.cs b/Creek.FileRepository/Repositories/SftpRepository.cs index e457730..d7557d2 100644 --- a/Creek.FileRepository/Repositories/SftpRepository.cs +++ b/Creek.FileRepository/Repositories/SftpRepository.cs @@ -1,5 +1,6 @@ using Creek.FileRepository.Helpers; using Creek.FileRepository.Models; +using Creek.HelpfulExtensions; using Microsoft.Extensions.Configuration; using Renci.SshNet; using Renci.SshNet.Sftp; @@ -112,7 +113,7 @@ namespace Creek.FileRepository.Repositories { client.Connect(); - RepoFile repoFile = new RepoFile(filename, DateTime.Now); + RepoFile repoFile = new RepoFile(filename, SystemTime.Now()); client.DownloadFile($"{this.remoteDirectoryPath}/{filename}", output: repoFile.Content); diff --git a/UnitTests/SftpRepositoryShould.cs b/UnitTests/SftpRepositoryShould.cs index 27495af..81b058f 100644 --- a/UnitTests/SftpRepositoryShould.cs +++ b/UnitTests/SftpRepositoryShould.cs @@ -1,5 +1,6 @@ using Creek.FileRepository; using Creek.FileRepository.Models; +using Creek.HelpfulExtensions; using Microsoft.Extensions.Configuration; using Newtonsoft.Json.Linq; using NUnit.Framework; @@ -23,7 +24,7 @@ namespace UnitTests [Test] public async Task ShouldNotCreateAFileWithAnInvalidFilename() { - Assert.That(() => CreateTestFile("create>()); + Assert.That(() => CreateTestFile("create>()); } [Test] @@ -31,7 +32,7 @@ namespace UnitTests { try { - await CreateTestFile("createtest.txt"); + await CreateTestFile("createtest.txt", SystemTime.Now()); } catch (Exception ex) { @@ -57,11 +58,14 @@ namespace UnitTests { 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 try { // Create a file to read - await CreateTestFile(filename); + await CreateTestFile(filename, createdDateTime); } catch (Exception ex) { @@ -79,6 +83,8 @@ namespace UnitTests Assert.NotNull(repoFile.Filename); Assert.NotNull(repoFile.Content); Assert.NotNull(repoFile.Created); + Assert.AreEqual(filename, repoFile.Filename); + Assert.AreEqual(createdDateTime, repoFile.Created); } catch (Exception ex) { @@ -101,7 +107,7 @@ namespace UnitTests try { // Create a file to read - await CreateTestFile(filename); + await CreateTestFile(filename, SystemTime.Now()); } catch (Exception ex) { @@ -143,13 +149,13 @@ namespace UnitTests await DeleteTestFile(filename); } - private async Task CreateTestFile(String filename) + private async Task CreateTestFile(String filename, DateTime created) { IFileRepository fileRepository = Factory.GetFileRepository(repositoryType, config); 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); } diff --git a/UnitTests/UnitTests.csproj b/UnitTests/UnitTests.csproj index 560abc9..34e5c2b 100644 --- a/UnitTests/UnitTests.csproj +++ b/UnitTests/UnitTests.csproj @@ -17,6 +17,7 @@ +