Skip to content

Commit

Permalink
Fix sync of asset folders.
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianStehle committed Oct 4, 2021
1 parent d9e293e commit 3d99fa6
Show file tree
Hide file tree
Showing 6 changed files with 354 additions and 30 deletions.
280 changes: 280 additions & 0 deletions cli/Squidex.CLI/Squidex.CLI.Tests/FolderTreeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,280 @@
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using FakeItEasy;
using Squidex.CLI.Commands.Implementation;
using Squidex.CLI.Commands.Implementation.Sync.Assets;
using Squidex.ClientLibrary.Management;
using Xunit;

namespace Squidex.CLI.Tests
{
public class FolderTreeTests
{
private static readonly string RootId = Guid.Empty.ToString();
private readonly ISession session = A.Fake<ISession>();
private readonly IAssetsClient assets = A.Fake<IAssetsClient>();
private readonly FolderTree sut;

public FolderTreeTests()
{
A.CallTo(() => session.Assets)
.Returns(assets);

A.CallTo(() => session.App)
.Returns("my-app");

sut = new FolderTree(session);
}

[Theory]
[InlineData(null)]
[InlineData("")]
[InlineData(" ")]
[InlineData("/")]
[InlineData(".")]
[InlineData("./")]
[InlineData("\\")]
public async Task Should_provide_null_if_for_root_path(string path)
{
var id = await sut.GetIdAsync(path);

Assert.Null(id);
}

[Fact]
public async Task Should_query_for_path_once_for_each_subtree()
{
// * folder1
// * folder2
var folder1 = new AssetFolderDto
{
Id = Guid.NewGuid().ToString(),
FolderName = "folder1"
};

var folder2 = new AssetFolderDto
{
Id = Guid.NewGuid().ToString(),
FolderName = "folder2"
};

A.CallTo(() => assets.GetAssetFoldersAsync("my-app", folder1.Id, A<CancellationToken>._))
.Returns(new AssetFoldersDto
{
Items = new List<AssetFolderDto>(),
Path = new List<AssetFolderDto>
{
folder1
}
});

A.CallTo(() => assets.GetAssetFoldersAsync("my-app", folder2.Id, A<CancellationToken>._))
.Returns(new AssetFoldersDto
{
Items = new List<AssetFolderDto>(),
Path = new List<AssetFolderDto>
{
folder2
}
});

Assert.Equal("folder1", await sut.GetPathAsync(folder1.Id));
Assert.Equal("folder2", await sut.GetPathAsync(folder2.Id));

A.CallTo(() => assets.GetAssetFoldersAsync("my-app", A<string>._, A<CancellationToken>._))
.MustHaveHappenedTwiceExactly();
}

[Fact]
public async Task Should_not_query_for_path_again_if_child_already_queried()
{
// * folder1
// * folder2
var folder1 = new AssetFolderDto
{
Id = Guid.NewGuid().ToString(),
FolderName = "folder1"
};

var folder2 = new AssetFolderDto
{
Id = Guid.NewGuid().ToString(),
FolderName = "folder2"
};

A.CallTo(() => assets.GetAssetFoldersAsync("my-app", folder2.Id, A<CancellationToken>._))
.Returns(new AssetFoldersDto
{
Items = new List<AssetFolderDto>(),
Path = new List<AssetFolderDto>
{
folder1,
folder2
}
});

Assert.Equal("folder1/folder2", await sut.GetPathAsync(folder2.Id));
Assert.Equal("folder1", await sut.GetPathAsync(folder1.Id));

A.CallTo(() => assets.GetAssetFoldersAsync("my-app", A<string>._, A<CancellationToken>._))
.MustHaveHappenedOnceExactly();
}

[Fact]
public async Task Should_not_query_for_path_again_if_parent_already_queried()
{
// * folder1
// * folder2
var folder1 = new AssetFolderDto
{
Id = Guid.NewGuid().ToString(),
FolderName = "folder1"
};

var folder2 = new AssetFolderDto
{
Id = Guid.NewGuid().ToString(),
FolderName = "folder2"
};

A.CallTo(() => assets.GetAssetFoldersAsync("my-app", folder1.Id, A<CancellationToken>._))
.Returns(new AssetFoldersDto
{
Items = new List<AssetFolderDto>
{
folder2
},
Path = new List<AssetFolderDto>
{
folder1
}
});

Assert.Equal("folder1", await sut.GetPathAsync(folder1.Id));
Assert.Equal("folder1/folder2", await sut.GetPathAsync(folder2.Id));

A.CallTo(() => assets.GetAssetFoldersAsync("my-app", A<string>._, A<CancellationToken>._))
.MustHaveHappenedOnceExactly();
}

[Fact]
public async Task Should_query_for_id_once_for_each_tree_item()
{
// * folder1
// * folder2
var folder1 = new AssetFolderDto
{
Id = Guid.NewGuid().ToString(),
FolderName = "folder1"
};

var folder2 = new AssetFolderDto
{
Id = Guid.NewGuid().ToString(),
FolderName = "folder2"
};

A.CallTo(() => assets.GetAssetFoldersAsync("my-app", RootId, A<CancellationToken>._))
.Returns(new AssetFoldersDto
{
Items = new List<AssetFolderDto>
{
folder1,
folder2
},
Path = new List<AssetFolderDto>()
});

Assert.Equal(folder1.Id, await sut.GetIdAsync("folder1"));
Assert.Equal(folder2.Id, await sut.GetIdAsync("folder2"));

A.CallTo(() => assets.GetAssetFoldersAsync("my-app", A<string>._, A<CancellationToken>._))
.MustHaveHappenedOnceExactly();
}

[Fact]
public async Task Should_not_query_for_id_again_if_parent_already_queried()
{
// * folder1
// * folder2
var folder1 = new AssetFolderDto
{
Id = Guid.NewGuid().ToString(),
FolderName = "folder1"
};

var folder2 = new AssetFolderDto
{
Id = Guid.NewGuid().ToString(),
FolderName = "folder2"
};

A.CallTo(() => assets.GetAssetFoldersAsync("my-app", folder1.Id, A<CancellationToken>._))
.Returns(new AssetFoldersDto
{
Items = new List<AssetFolderDto>
{
folder2
},
Path = new List<AssetFolderDto>
{
folder1
}
});

Assert.Equal("folder1", await sut.GetPathAsync(folder1.Id));
Assert.Equal("folder1/folder2", await sut.GetPathAsync(folder2.Id));

A.CallTo(() => assets.GetAssetFoldersAsync("my-app", A<string>._, A<CancellationToken>._))
.MustHaveHappenedOnceExactly();
}

[Fact]
public async Task Should_query_for_id_once_and_create_new_folder()
{
// * folder1
// * folder2
var folder1 = new AssetFolderDto
{
Id = Guid.NewGuid().ToString(),
FolderName = "folder1"
};

var folder2 = new AssetFolderDto
{
Id = Guid.NewGuid().ToString(),
FolderName = "folder2"
};

A.CallTo(() => assets.GetAssetFoldersAsync("my-app", RootId, A<CancellationToken>._))
.Returns(new AssetFoldersDto
{
Items = new List<AssetFolderDto>
{
folder1
},
Path = new List<AssetFolderDto>()
});

A.CallTo(() => assets.PostAssetFolderAsync("my-app",
A<CreateAssetFolderDto>.That.Matches(x => x.FolderName == "folder2" && x.ParentId == RootId),
A<CancellationToken>._))
.Returns(folder2);

Assert.Equal(folder1.Id, await sut.GetIdAsync("folder1"));
Assert.Equal(folder2.Id, await sut.GetIdAsync("folder2"));

A.CallTo(() => assets.GetAssetFoldersAsync("my-app", A<string>._, A<CancellationToken>._))
.MustHaveHappenedOnceExactly();
}
}
}
2 changes: 2 additions & 0 deletions cli/Squidex.CLI/Squidex.CLI.Tests/Squidex.CLI.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FakeItEasy" Version="7.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.10.0" />
<PackageReference Include="CommandDotNet.FluentValidation" Version="3.0.1" />
<PackageReference Include="RefactoringEssentials" Version="5.6.0" PrivateAssets="all" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118" PrivateAssets="all" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ public sealed record FolderNode(string Id, string Path)

public FolderNode Parent { get; set; }

public FolderNode Add(FolderNode child, string name)
public bool HasBeenQueried { get; set; }

public void Add(FolderNode child, string name)
{
Children[name] = child;
child.Parent = this;

return child;
}
}
}
Loading

0 comments on commit 3d99fa6

Please sign in to comment.