mirror of https://github.com/Squidex/squidex.git
16 changed files with 293 additions and 92 deletions
@ -0,0 +1,55 @@ |
|||||
|
// ==========================================================================
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
||||
|
// All rights reserved. Licensed under the MIT license.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System.Threading.Tasks; |
||||
|
|
||||
|
namespace Squidex.Domain.Apps.Entities.Backup |
||||
|
{ |
||||
|
public static class CompatibilityExtensions |
||||
|
{ |
||||
|
private const string VersionFile = "Version.json"; |
||||
|
private static readonly FileVersion None = new FileVersion(); |
||||
|
private static readonly FileVersion Expected = new FileVersion { Major = 5 }; |
||||
|
|
||||
|
public sealed class FileVersion |
||||
|
{ |
||||
|
public int Major { get; set; } |
||||
|
|
||||
|
public bool Equals(FileVersion other) |
||||
|
{ |
||||
|
return Major == other.Major; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public static Task WriteVersionAsync(this IBackupWriter writer) |
||||
|
{ |
||||
|
return writer.WriteJsonAsync(VersionFile, Expected); |
||||
|
} |
||||
|
|
||||
|
public static async Task CheckCompatibilityAsync(this IBackupReader reader) |
||||
|
{ |
||||
|
var current = await reader.ReadVersionAsync(); |
||||
|
|
||||
|
if (!Expected.Equals(current)) |
||||
|
{ |
||||
|
throw new BackupRestoreException("Backup file is not compatible with this version."); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private static async Task<FileVersion> ReadVersionAsync(this IBackupReader reader) |
||||
|
{ |
||||
|
try |
||||
|
{ |
||||
|
return await reader.ReadJsonAsync<FileVersion>(VersionFile); |
||||
|
} |
||||
|
catch |
||||
|
{ |
||||
|
return None; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,62 @@ |
|||||
|
// ==========================================================================
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
||||
|
// All rights reserved. Licensed under the MIT license.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System.IO; |
||||
|
using System.Threading.Tasks; |
||||
|
using FakeItEasy; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Squidex.Domain.Apps.Entities.Backup |
||||
|
{ |
||||
|
public class BackupCompatibilityTests |
||||
|
{ |
||||
|
[Fact] |
||||
|
public async Task Should_writer_version() |
||||
|
{ |
||||
|
var writer = A.Fake<IBackupWriter>(); |
||||
|
|
||||
|
await writer.WriteVersionAsync(); |
||||
|
|
||||
|
A.CallTo(() => writer.WriteJsonAsync(A<string>._, |
||||
|
A<CompatibilityExtensions.FileVersion>.That.Matches(x => x.Major == 5))) |
||||
|
.MustHaveHappened(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_not_throw_exception_if_backup_has_correct_version() |
||||
|
{ |
||||
|
var reader = A.Fake<IBackupReader>(); |
||||
|
|
||||
|
A.CallTo(() => reader.ReadJsonAsync<CompatibilityExtensions.FileVersion>(A<string>._)) |
||||
|
.Returns(new CompatibilityExtensions.FileVersion { Major = 5 }); |
||||
|
|
||||
|
await reader.CheckCompatibilityAsync(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_throw_exception_if_backup_has_wrong_version() |
||||
|
{ |
||||
|
var reader = A.Fake<IBackupReader>(); |
||||
|
|
||||
|
A.CallTo(() => reader.ReadJsonAsync<CompatibilityExtensions.FileVersion>(A<string>._)) |
||||
|
.Returns(new CompatibilityExtensions.FileVersion { Major = 3 }); |
||||
|
|
||||
|
await Assert.ThrowsAsync<BackupRestoreException>(() => reader.CheckCompatibilityAsync()); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_throw_exception_if_backup_has_no_version() |
||||
|
{ |
||||
|
var reader = A.Fake<IBackupReader>(); |
||||
|
|
||||
|
A.CallTo(() => reader.ReadJsonAsync<CompatibilityExtensions.FileVersion>(A<string>._)) |
||||
|
.Throws(new FileNotFoundException()); |
||||
|
|
||||
|
await Assert.ThrowsAsync<BackupRestoreException>(() => reader.CheckCompatibilityAsync()); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue