Browse Source

Tests improved.

pull/76/head
Sebastian Stehle 9 years ago
parent
commit
47c7c50cf4
  1. 7
      src/Squidex.Infrastructure.GoogleCloud/Assets/GoogleCloudAssetStore.cs
  2. 110
      tests/Squidex.Infrastructure.Tests/Assets/AssetStoreTestsBase.cs
  3. 88
      tests/Squidex.Infrastructure.Tests/Assets/FolderAssetStoreTests.cs
  4. 22
      tests/Squidex.Infrastructure.Tests/Assets/GoogleCloudAssetStoreTests.cs
  5. 2
      tests/Squidex.Infrastructure.Tests/Squidex.Infrastructure.Tests.csproj

7
src/Squidex.Infrastructure.GoogleCloud/Assets/GoogleCloudAssetStore.cs

@ -96,9 +96,12 @@ namespace Squidex.Infrastructure.Assets
{ {
await storageClient.DeleteObjectAsync(bucketName, name); await storageClient.DeleteObjectAsync(bucketName, name);
} }
catch catch (GoogleApiException ex)
{ {
// ignored if (ex.HttpStatusCode != HttpStatusCode.NotFound)
{
throw;
}
} }
} }

110
tests/Squidex.Infrastructure.Tests/Assets/AssetStoreTestsBase.cs

@ -0,0 +1,110 @@
// ==========================================================================
// AssetStoreTestsBase.cs
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex Group
// All rights reserved.
// ==========================================================================
using System;
using System.IO;
using System.Threading.Tasks;
using Xunit;
// ReSharper disable VirtualMemberCallInConstructor
// ReSharper disable MemberCanBeProtected.Global
namespace Squidex.Infrastructure.Assets
{
public abstract class AssetStoreTests<T> : IDisposable where T : IAssetStore
{
private readonly T sut;
protected AssetStoreTests()
{
sut = CreateStore();
}
protected T Sut
{
get { return sut; }
}
public abstract T CreateStore();
public abstract void Dispose();
[Fact]
public Task Should_throw_exception_if_asset_to_download_is_not_found()
{
((IExternalSystem)Sut).Connect();
return Assert.ThrowsAsync<AssetNotFoundException>(() => Sut.DownloadAsync(Id(), 1, "suffix", new MemoryStream()));
}
[Fact]
public Task Should_throw_exception_if_asset_to_copy_is_not_found()
{
((IExternalSystem)Sut).Connect();
return Assert.ThrowsAsync<AssetNotFoundException>(() => Sut.CopyTemporaryAsync(Id(), Id(), 1, null));
}
[Fact]
public async Task Should_read_and_write_file()
{
((IExternalSystem)Sut).Connect();
var assetId = Id();
var assetData = new MemoryStream(new byte[] { 0x1, 0x2, 0x3, 0x4 });
await Sut.UploadAsync(assetId, 1, "suffix", assetData);
var readData = new MemoryStream();
await Sut.DownloadAsync(assetId, 1, "suffix", readData);
Assert.Equal(assetData.ToArray(), readData.ToArray());
}
[Fact]
public async Task Should_commit_temporary_file()
{
((IExternalSystem)Sut).Connect();
var tempId = Id();
var assetId = Id();
var assetData = new MemoryStream(new byte[] { 0x1, 0x2, 0x3, 0x4 });
await Sut.UploadTemporaryAsync(tempId, assetData);
await Sut.CopyTemporaryAsync(tempId, assetId, 1, "suffix");
var readData = new MemoryStream();
await Sut.DownloadAsync(assetId, 1, "suffix", readData);
Assert.Equal(assetData.ToArray(), readData.ToArray());
}
[Fact]
public async Task Should_ignore_when_deleting_twice()
{
((IExternalSystem)Sut).Connect();
var tempId = Id();
var assetData = new MemoryStream(new byte[] { 0x1, 0x2, 0x3, 0x4 });
await Sut.UploadTemporaryAsync(tempId, assetData);
await Sut.DeleteTemporaryAsync(tempId);
await Sut.DeleteTemporaryAsync(tempId);
}
private static string Id()
{
return Guid.NewGuid().ToString();
}
}
}

88
tests/Squidex.Infrastructure.Tests/Assets/FolderAssetStoreTests.cs

@ -8,24 +8,22 @@
using System; using System;
using System.IO; using System.IO;
using System.Threading.Tasks;
using Moq; using Moq;
using Squidex.Infrastructure.Log; using Squidex.Infrastructure.Log;
using Xunit; using Xunit;
namespace Squidex.Infrastructure.Assets namespace Squidex.Infrastructure.Assets
{ {
public class FolderAssetStoreTests : IDisposable public class FolderAssetStoreTests : AssetStoreTests<FolderAssetStore>
{ {
private readonly FolderAssetStore sut;
private readonly string testFolder = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); private readonly string testFolder = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
public FolderAssetStoreTests() public override FolderAssetStore CreateStore()
{ {
sut = new FolderAssetStore(testFolder, new Mock<ISemanticLog>().Object); return new FolderAssetStore(testFolder, new Mock<ISemanticLog>().Object);
} }
public void Dispose() public override void Dispose()
{ {
if (Directory.Exists(testFolder)) if (Directory.Exists(testFolder))
{ {
@ -33,14 +31,6 @@ namespace Squidex.Infrastructure.Assets
} }
} }
[Fact]
public void Should_create_directory_when_connecting()
{
sut.Connect();
Assert.True(Directory.Exists(testFolder));
}
[Fact] [Fact]
public void Should_throw_when_creating_directory_failed() public void Should_throw_when_creating_directory_failed()
{ {
@ -48,75 +38,11 @@ namespace Squidex.Infrastructure.Assets
} }
[Fact] [Fact]
public Task Should_throw_exception_if_asset_to_download_is_not_found() public void Should_create_directory_when_connecting()
{
sut.Connect();
return Assert.ThrowsAsync<AssetNotFoundException>(() => sut.DownloadAsync(Id(), 1, "suffix", new MemoryStream()));
}
[Fact]
public Task Should_throw_exception_if_asset_to_copy_is_not_found()
{
sut.Connect();
return Assert.ThrowsAsync<AssetNotFoundException>(() => sut.CopyTemporaryAsync(Id(), Id(), 1, null));
}
[Fact]
public async Task Should_read_and_write_file()
{
sut.Connect();
var assetId = Id();
var assetData = new MemoryStream(new byte[] { 0x1, 0x2, 0x3, 0x4 });
await sut.UploadAsync(assetId, 1, "suffix", assetData);
var readData = new MemoryStream();
await sut.DownloadAsync(assetId, 1, "suffix", readData);
Assert.Equal(assetData.ToArray(), readData.ToArray());
}
[Fact]
public async Task Should_commit_temporary_file()
{
sut.Connect();
var tempId = Id();
var assetId = Id();
var assetData = new MemoryStream(new byte[] { 0x1, 0x2, 0x3, 0x4 });
await sut.UploadTemporaryAsync(tempId, assetData);
await sut.CopyTemporaryAsync(tempId, assetId, 1, "suffix");
var readData = new MemoryStream();
await sut.DownloadAsync(assetId, 1, "suffix", readData);
Assert.Equal(assetData.ToArray(), readData.ToArray());
}
[Fact]
public async Task Should_ignore_when_deleting_twice()
{ {
sut.Connect(); Sut.Connect();
var tempId = Id();
var assetData = new MemoryStream(new byte[] { 0x1, 0x2, 0x3, 0x4 }); Assert.True(Directory.Exists(testFolder));
await sut.UploadTemporaryAsync(tempId, assetData);
await sut.DeleteTemporaryAsync(tempId);
await sut.DeleteTemporaryAsync(tempId);
}
private static string Id()
{
return Guid.NewGuid().ToString();
} }
private static string CreateInvalidPath() private static string CreateInvalidPath()

22
tests/Squidex.Infrastructure.Tests/Assets/GoogleCloudAssetStoreTests.cs

@ -0,0 +1,22 @@
// ==========================================================================
// GoogleCloudAssetStoreTests.cs
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex Group
// All rights reserved.
// ==========================================================================
namespace Squidex.Infrastructure.Assets
{
internal class GoogleCloudAssetStoreTests : AssetStoreTests<GoogleCloudAssetStore>
{
public override GoogleCloudAssetStore CreateStore()
{
return new GoogleCloudAssetStore("squidex-test");
}
public override void Dispose()
{
}
}
}

2
tests/Squidex.Infrastructure.Tests/Squidex.Infrastructure.Tests.csproj

@ -5,10 +5,12 @@
<RootNamespace>Squidex.Infrastructure</RootNamespace> <RootNamespace>Squidex.Infrastructure</RootNamespace>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\src\Squidex.Infrastructure.GoogleCloud\Squidex.Infrastructure.GoogleCloud.csproj" />
<ProjectReference Include="..\..\src\Squidex.Infrastructure\Squidex.Infrastructure.csproj" /> <ProjectReference Include="..\..\src\Squidex.Infrastructure\Squidex.Infrastructure.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="FluentAssertions" Version="4.19.3" /> <PackageReference Include="FluentAssertions" Version="4.19.3" />
<PackageReference Include="Google.Cloud.Storage.V1" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="1.1.2" /> <PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="1.1.2" /> <PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="1.1.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />

Loading…
Cancel
Save