mirror of https://github.com/Squidex/squidex.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
2.6 KiB
92 lines
2.6 KiB
// ==========================================================================
|
|
// Squidex Headless CMS
|
|
// ==========================================================================
|
|
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|
// All rights reserved. Licensed under the MIT license.
|
|
// ==========================================================================
|
|
|
|
using MongoDB.Bson;
|
|
using MongoDB.Bson.IO;
|
|
using MongoDB.Bson.Serialization;
|
|
using MongoDB.Bson.Serialization.Attributes;
|
|
using Squidex.Infrastructure.TestHelpers;
|
|
using Xunit;
|
|
|
|
namespace Squidex.Infrastructure.MongoDb
|
|
{
|
|
public class DomainIdSerializerTests
|
|
{
|
|
private sealed class StringEntity<T>
|
|
{
|
|
[BsonRepresentation(BsonType.String)]
|
|
public T Id { get; set; }
|
|
}
|
|
|
|
private sealed class IdEntity<T>
|
|
{
|
|
public T Id { get; set; }
|
|
}
|
|
|
|
public DomainIdSerializerTests()
|
|
{
|
|
TestUtils.SetupBson();
|
|
}
|
|
|
|
[Fact]
|
|
public void Should_deserialize_from_string()
|
|
{
|
|
var id = Guid.NewGuid();
|
|
|
|
var source = new IdEntity<string> { Id = id.ToString() };
|
|
|
|
var result = SerializeAndDeserializeBson<IdEntity<string>, IdEntity<DomainId>>(source);
|
|
|
|
Assert.Equal(result.Id.ToString(), id.ToString());
|
|
}
|
|
|
|
[Fact]
|
|
public void Should_deserialize_from_guid_string()
|
|
{
|
|
var id = Guid.NewGuid();
|
|
|
|
var source = new StringEntity<Guid> { Id = id };
|
|
|
|
var result = SerializeAndDeserializeBson<StringEntity<Guid>, IdEntity<DomainId>>(source);
|
|
|
|
Assert.Equal(result.Id.ToString(), id.ToString());
|
|
}
|
|
|
|
[Fact]
|
|
public void Should_deserialize_from_guid_bytes()
|
|
{
|
|
var id = Guid.NewGuid();
|
|
|
|
var source = new IdEntity<Guid> { Id = id };
|
|
|
|
var result = SerializeAndDeserializeBson<IdEntity<Guid>, IdEntity<DomainId>>(source);
|
|
|
|
Assert.Equal(result.Id.ToString(), id.ToString());
|
|
}
|
|
|
|
private static TOut SerializeAndDeserializeBson<TIn, TOut>(TIn source)
|
|
{
|
|
var stream = new MemoryStream();
|
|
|
|
using (var writer = new BsonBinaryWriter(stream))
|
|
{
|
|
BsonSerializer.Serialize(writer, source);
|
|
|
|
writer.Flush();
|
|
}
|
|
|
|
stream.Position = 0;
|
|
|
|
using (var reader = new BsonBinaryReader(stream))
|
|
{
|
|
var target = BsonSerializer.Deserialize<TOut>(reader);
|
|
|
|
return target;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|