aboutsummaryrefslogtreecommitdiff
path: root/tests/Jellyfin.Extensions.Tests/Json/Converters/JsonGuidConverterTests.cs
blob: 8465d465ab528314596befcdbe301996bd28f188 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
using System.Text.Json;
using Jellyfin.Extensions.Json.Converters;
using Xunit;

namespace Jellyfin.Extensions.Tests.Json.Converters
{
    public class JsonGuidConverterTests
    {
        private readonly JsonSerializerOptions _options;

        public JsonGuidConverterTests()
        {
            _options = new JsonSerializerOptions();
            _options.Converters.Add(new JsonGuidConverter());
        }

        [Fact]
        public void Deserialize_Valid_Success()
        {
            Guid value = JsonSerializer.Deserialize<Guid>(@"""a852a27afe324084ae66db579ee3ee18""", _options);
            Assert.Equal(new Guid("a852a27afe324084ae66db579ee3ee18"), value);
        }

        [Fact]
        public void Deserialize_ValidDashed_Success()
        {
            Guid value = JsonSerializer.Deserialize<Guid>(@"""e9b2dcaa-529c-426e-9433-5e9981f27f2e""", _options);
            Assert.Equal(new Guid("e9b2dcaa-529c-426e-9433-5e9981f27f2e"), value);
        }

        [Fact]
        public void Roundtrip_Valid_Success()
        {
            Guid guid = new Guid("a852a27afe324084ae66db579ee3ee18");
            string value = JsonSerializer.Serialize(guid, _options);
            Assert.Equal(guid, JsonSerializer.Deserialize<Guid>(value, _options));
        }

        [Fact]
        public void Deserialize_Null_EmptyGuid()
        {
            Assert.Equal(Guid.Empty, JsonSerializer.Deserialize<Guid>("null", _options));
        }

        [Fact]
        public void Serialize_EmptyGuid_EmptyGuid()
        {
            Assert.Equal($"\"{Guid.Empty:N}\"", JsonSerializer.Serialize(Guid.Empty, _options));
        }

        [Fact]
        public void Serialize_Valid_NoDash_Success()
        {
            var guid = new Guid("531797E9-9457-40E0-88BC-B1D6D38752FA");
            var str = JsonSerializer.Serialize(guid, _options);
            Assert.Equal($"\"{guid:N}\"", str);
        }

        [Fact]
        public void Serialize_Nullable_Success()
        {
            Guid? guid = new Guid("531797E9-9457-40E0-88BC-B1D6D38752FA");
            var str = JsonSerializer.Serialize(guid, _options);
            Assert.Equal($"\"{guid:N}\"", str);
        }
    }
}