mirror of https://github.com/Squidex/squidex.git
15 changed files with 139 additions and 225 deletions
@ -1,134 +0,0 @@ |
|||||
// ==========================================================================
|
|
||||
// CachingSchemaProviderTests.cs
|
|
||||
// Squidex Headless CMS
|
|
||||
// ==========================================================================
|
|
||||
// Copyright (c) Squidex Group
|
|
||||
// All rights reserved.
|
|
||||
// ==========================================================================
|
|
||||
|
|
||||
using System; |
|
||||
using System.Threading.Tasks; |
|
||||
using FakeItEasy; |
|
||||
using Microsoft.Extensions.Caching.Memory; |
|
||||
using Microsoft.Extensions.Options; |
|
||||
using Squidex.Domain.Apps.Events.Schemas; |
|
||||
using Squidex.Domain.Apps.Read.Schemas.Repositories; |
|
||||
using Squidex.Domain.Apps.Read.Schemas.Services.Implementations; |
|
||||
using Squidex.Infrastructure; |
|
||||
using Squidex.Infrastructure.CQRS.Events; |
|
||||
using Xunit; |
|
||||
|
|
||||
namespace Squidex.Domain.Apps.Read.Schemas |
|
||||
{ |
|
||||
public class CachingSchemaProviderTests |
|
||||
{ |
|
||||
private readonly IMemoryCache cache = new MemoryCache(Options.Create(new MemoryCacheOptions())); |
|
||||
private readonly ISchemaRepository repository = A.Fake<ISchemaRepository>(); |
|
||||
private readonly CachingSchemaProvider sut; |
|
||||
private readonly ISchemaEntity schemaV1 = A.Dummy<ISchemaEntity>(); |
|
||||
private readonly ISchemaEntity schemaV2 = A.Dummy<ISchemaEntity>(); |
|
||||
private readonly NamedId<Guid> schemaId = new NamedId<Guid>(Guid.NewGuid(), "my-schema"); |
|
||||
private readonly NamedId<Guid> appId = new NamedId<Guid>(Guid.NewGuid(), "my-app"); |
|
||||
|
|
||||
public CachingSchemaProviderTests() |
|
||||
{ |
|
||||
A.CallTo(() => schemaV1.Id).Returns(schemaId.Id); |
|
||||
A.CallTo(() => schemaV1.Name).Returns(schemaId.Name); |
|
||||
A.CallTo(() => schemaV1.AppId).Returns(appId.Id); |
|
||||
|
|
||||
A.CallTo(() => schemaV2.Id).Returns(schemaId.Id); |
|
||||
A.CallTo(() => schemaV2.Name).Returns(schemaId.Name); |
|
||||
A.CallTo(() => schemaV2.AppId).Returns(appId.Id); |
|
||||
|
|
||||
sut = new CachingSchemaProvider(cache, repository); |
|
||||
} |
|
||||
|
|
||||
[Fact] |
|
||||
public void Should_return_empty_for_events_filter() |
|
||||
{ |
|
||||
Assert.Equal(string.Empty, sut.EventsFilter); |
|
||||
} |
|
||||
|
|
||||
[Fact] |
|
||||
public void Should_return_empty_for_name() |
|
||||
{ |
|
||||
Assert.Equal(typeof(CachingSchemaProvider).Name, sut.Name); |
|
||||
} |
|
||||
|
|
||||
[Fact] |
|
||||
public void Should_do_nothing_when_clearing() |
|
||||
{ |
|
||||
Assert.NotNull(sut.ClearAsync()); |
|
||||
} |
|
||||
|
|
||||
[Fact] |
|
||||
public async Task Should_also_retrieve_schema_by_name_if_retrieved_by_id_before() |
|
||||
{ |
|
||||
A.CallTo(() => repository.FindSchemaAsync(schemaId.Id)) |
|
||||
.Returns(schemaV1); |
|
||||
|
|
||||
await ProvideSchemaById(schemaV1); |
|
||||
await ProvideSchemaByName(schemaV1); |
|
||||
|
|
||||
A.CallTo(() => repository.FindSchemaAsync(schemaId.Id)).MustHaveHappened(); |
|
||||
A.CallTo(() => repository.FindSchemaAsync(appId.Id, schemaId.Name)).MustNotHaveHappened(); |
|
||||
} |
|
||||
|
|
||||
[Fact] |
|
||||
public async Task Should_also_retrieve_schema_by_id_if_retrieved_by_name_before() |
|
||||
{ |
|
||||
A.CallTo(() => repository.FindSchemaAsync(appId.Id, schemaId.Name)) |
|
||||
.Returns(schemaV1); |
|
||||
|
|
||||
await ProvideSchemaByName(schemaV1); |
|
||||
await ProvideSchemaById(schemaV1); |
|
||||
|
|
||||
A.CallTo(() => repository.FindSchemaAsync(schemaId.Id)).MustNotHaveHappened(); |
|
||||
A.CallTo(() => repository.FindSchemaAsync(appId.Id, schemaId.Name)).MustHaveHappened(); |
|
||||
} |
|
||||
|
|
||||
[Fact] |
|
||||
public async Task Should_clear_cache_for_id_after_update_event() |
|
||||
{ |
|
||||
A.CallTo(() => repository.FindSchemaAsync(schemaId.Id)) |
|
||||
.Returns(schemaV2); |
|
||||
A.CallTo(() => repository.FindSchemaAsync(schemaId.Id)) |
|
||||
.Returns(schemaV1).Once(); |
|
||||
|
|
||||
await ProvideSchemaById(schemaV1); |
|
||||
|
|
||||
sut.On(Envelope.Create(new FieldAdded { AppId = appId, SchemaId = schemaId })).Wait(); |
|
||||
|
|
||||
await ProvideSchemaById(schemaV2); |
|
||||
|
|
||||
A.CallTo(() => repository.FindSchemaAsync(schemaId.Id)).MustHaveHappened(Repeated.Exactly.Times(2)); |
|
||||
} |
|
||||
|
|
||||
[Fact] |
|
||||
public async Task Should_clear_cache_for_name_after_update_event() |
|
||||
{ |
|
||||
A.CallTo(() => repository.FindSchemaAsync(appId.Id, schemaId.Name)) |
|
||||
.Returns(schemaV2); |
|
||||
A.CallTo(() => repository.FindSchemaAsync(appId.Id, schemaId.Name)) |
|
||||
.Returns(schemaV1).Once(); |
|
||||
|
|
||||
await ProvideSchemaByName(schemaV1); |
|
||||
|
|
||||
sut.On(Envelope.Create(new SchemaUpdated { AppId = appId, SchemaId = schemaId })).Wait(); |
|
||||
|
|
||||
await ProvideSchemaByName(schemaV2); |
|
||||
|
|
||||
A.CallTo(() => repository.FindSchemaAsync(appId.Id, schemaId.Name)).MustHaveHappened(Repeated.Exactly.Times(2)); |
|
||||
} |
|
||||
|
|
||||
private async Task ProvideSchemaById(ISchemaEntity schema) |
|
||||
{ |
|
||||
Assert.Equal(schema, await sut.FindSchemaByIdAsync(schemaId.Id)); |
|
||||
} |
|
||||
|
|
||||
private async Task ProvideSchemaByName(ISchemaEntity schema) |
|
||||
{ |
|
||||
Assert.Equal(schema, await sut.FindSchemaByNameAsync(appId.Id, schemaId.Name)); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,62 @@ |
|||||
|
// ==========================================================================
|
||||
|
// SimpleCopierTests.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Squidex.Infrastructure.Reflection |
||||
|
{ |
||||
|
public class SimpleCopierTests |
||||
|
{ |
||||
|
public class Cloneable : ICloneable |
||||
|
{ |
||||
|
public int Value { get; } |
||||
|
|
||||
|
public Cloneable(int value) |
||||
|
{ |
||||
|
Value = value; |
||||
|
} |
||||
|
|
||||
|
public object Clone() |
||||
|
{ |
||||
|
return new Cloneable(Value); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public class MyClass1Base |
||||
|
{ |
||||
|
public int Value1 { get; set; } |
||||
|
} |
||||
|
|
||||
|
public class MyClass1 : MyClass1Base |
||||
|
{ |
||||
|
public int Value2 { get; set; } |
||||
|
|
||||
|
public Cloneable Cloneable { get; set; } |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_copy_class() |
||||
|
{ |
||||
|
var value = new MyClass1 |
||||
|
{ |
||||
|
Value1 = 1, |
||||
|
Value2 = 2, |
||||
|
Cloneable = new Cloneable(4) |
||||
|
}; |
||||
|
|
||||
|
var copy = value.Copy(); |
||||
|
|
||||
|
Assert.Equal(value.Value1, copy.Value1); |
||||
|
Assert.Equal(value.Value2, copy.Value2); |
||||
|
|
||||
|
Assert.Equal(value.Cloneable.Value, copy.Cloneable.Value); |
||||
|
Assert.NotSame(value.Cloneable, copy.Cloneable); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue