mirror of
https://github.com/jcreek/Creek.FileRepository.git
synced 2026-07-13 02:43:43 +00:00
feat(*): Add SftpRepositoryShould tests
This commit is contained in:
@@ -0,0 +1,166 @@
|
|||||||
|
using FileRepository;
|
||||||
|
using FileRepository.Models;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
|
using NUnit.Framework;
|
||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using UnitTests.Helpers;
|
||||||
|
|
||||||
|
namespace UnitTests
|
||||||
|
{
|
||||||
|
public class SftpRepositoryShould
|
||||||
|
{
|
||||||
|
private readonly IConfiguration config = InitConfiguration();
|
||||||
|
private readonly Factory.RepositoryType repositoryType = Factory.RepositoryType.Sftp;
|
||||||
|
|
||||||
|
[SetUp]
|
||||||
|
public void Setup()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task ShouldCreateAFileWithoutThrowing()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await CreateTestFile("createtest.txt");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Assert.Fail(ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task ShouldDeleteAFile()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await DeleteTestFile("createtest.txt");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Assert.Fail(ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task ShouldReadAFile()
|
||||||
|
{
|
||||||
|
string filename = "readtest.txt";
|
||||||
|
|
||||||
|
// Setup
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Create a file to read
|
||||||
|
await CreateTestFile(filename);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Assert.Fail($"Failed to create a file to read - {ex.Message}");
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Read the file
|
||||||
|
IFileRepository fileRepository = Factory.GetFileRepository(repositoryType, config);
|
||||||
|
|
||||||
|
RepoFile repoFile = await fileRepository.ReadFileAsync(filename);
|
||||||
|
|
||||||
|
Assert.NotNull(repoFile);
|
||||||
|
Assert.NotNull(repoFile.FileName);
|
||||||
|
Assert.NotNull(repoFile.Content);
|
||||||
|
Assert.NotNull(repoFile.Created);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
// Clean up
|
||||||
|
await DeleteTestFile(filename);
|
||||||
|
|
||||||
|
Assert.Fail(ex.Message);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clean up
|
||||||
|
await DeleteTestFile(filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task ShouldUpdateAFile()
|
||||||
|
{
|
||||||
|
string filename = "updatetest.txt";
|
||||||
|
|
||||||
|
// Setup file to read
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Create a file to read
|
||||||
|
await CreateTestFile(filename);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Assert.Fail($"Failed to create a file to read - {ex.Message}");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read the file and update it
|
||||||
|
try
|
||||||
|
{
|
||||||
|
IFileRepository fileRepository = Factory.GetFileRepository(repositoryType, config);
|
||||||
|
RepoFile repoFile = await fileRepository.ReadFileAsync(filename);
|
||||||
|
|
||||||
|
// Modify the content and update the file
|
||||||
|
string updateTestText = "update test text";
|
||||||
|
repoFile.Content = StreamHelper.GenerateStreamFromString(updateTestText);
|
||||||
|
await fileRepository.UpdateFileAsync(repoFile);
|
||||||
|
|
||||||
|
// Load the file anew to check for changes
|
||||||
|
RepoFile repoFileUpdated = await fileRepository.ReadFileAsync(filename);
|
||||||
|
|
||||||
|
// Move the pointer back to the beginning of the stream so we can read it here
|
||||||
|
repoFileUpdated.Content.Seek(0, SeekOrigin.Begin);
|
||||||
|
|
||||||
|
using (StreamReader reader = new StreamReader(repoFileUpdated.Content))
|
||||||
|
{
|
||||||
|
string contents = await reader.ReadToEndAsync();
|
||||||
|
Assert.AreEqual(updateTestText, contents);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
// Clean up
|
||||||
|
await DeleteTestFile(filename);
|
||||||
|
|
||||||
|
Assert.Fail(ex.Message);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clean up
|
||||||
|
await DeleteTestFile(filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task CreateTestFile(String filename)
|
||||||
|
{
|
||||||
|
IFileRepository fileRepository = Factory.GetFileRepository(repositoryType, config);
|
||||||
|
RepoFile repoFile = new RepoFile();
|
||||||
|
Stream contentStream = StreamHelper.GenerateStreamFromString("a,b \n c,d");
|
||||||
|
|
||||||
|
repoFile = repoFile.GenerateFile(filename, contentStream);
|
||||||
|
|
||||||
|
await fileRepository.CreateFileAsync(repoFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task DeleteTestFile(String filename)
|
||||||
|
{
|
||||||
|
IFileRepository fileRepository = Factory.GetFileRepository(repositoryType, config);
|
||||||
|
|
||||||
|
await fileRepository.DeleteFileAsync(filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static IConfiguration InitConfiguration()
|
||||||
|
{
|
||||||
|
IConfigurationRoot configBuilder = new ConfigurationBuilder()
|
||||||
|
.AddJsonFile("appsettings.json")
|
||||||
|
.Build();
|
||||||
|
return configBuilder;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
using NUnit.Framework;
|
|
||||||
|
|
||||||
namespace UnitTests
|
|
||||||
{
|
|
||||||
public class Tests
|
|
||||||
{
|
|
||||||
[SetUp]
|
|
||||||
public void Setup()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
[Test]
|
|
||||||
public void Test1()
|
|
||||||
{
|
|
||||||
Assert.Pass();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -7,10 +7,25 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<None Remove="appsettings.json" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Content Include="appsettings.json">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
</Content>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<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" />
|
||||||
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
|
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
|
||||||
<PackageReference Include="coverlet.collector" Version="3.0.2" />
|
<PackageReference Include="coverlet.collector" Version="3.0.2" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\FileRepository\FileRepository.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
Reference in New Issue
Block a user