|
| 1 | +using Altinn.App.Core.Models; |
| 2 | +using Altinn.Platform.Storage.Interface.Models; |
| 3 | + |
| 4 | +namespace Altinn.App.Core.Tests.Models; |
| 5 | + |
| 6 | +public class DataElementIdentifierTests |
| 7 | +{ |
| 8 | + [Fact] |
| 9 | + public void NullableDataElementIdentifier_Default_ShouldBeNull() |
| 10 | + { |
| 11 | + DataElementIdentifier? identifier = default; |
| 12 | + |
| 13 | + Assert.True(identifier is null); |
| 14 | + Assert.False(identifier.HasValue); |
| 15 | + } |
| 16 | + |
| 17 | + [Fact] |
| 18 | + public void NullableDataElementIdentifier_Null_ShouldBeNull() |
| 19 | + { |
| 20 | + DataElementIdentifier? identifier = null; |
| 21 | + |
| 22 | + Assert.True(identifier is null); |
| 23 | + Assert.False(identifier.HasValue); |
| 24 | + } |
| 25 | + |
| 26 | + [Fact] |
| 27 | + public void NullableDataElementIdentifier_FromNullDataElement_ShouldBeNull() |
| 28 | + { |
| 29 | + DataElement? dataElement = null; |
| 30 | + DataElementIdentifier? identifier = dataElement; |
| 31 | + |
| 32 | + Assert.True(identifier is null); |
| 33 | + Assert.False(identifier.HasValue); |
| 34 | + } |
| 35 | + |
| 36 | + [Fact] |
| 37 | + public void NullableDataElementIdentifier_FromDataElement_ShouldHaveValue() |
| 38 | + { |
| 39 | + var guid = Guid.NewGuid(); |
| 40 | + DataElement dataElement = new() { Id = guid.ToString(), DataType = "Model" }; |
| 41 | + DataElementIdentifier? identifier = dataElement; |
| 42 | + |
| 43 | + Assert.True(identifier.HasValue); |
| 44 | + Assert.Equal(guid, identifier.Value.Guid); |
| 45 | + Assert.Equal(guid.ToString(), identifier.Value.Id); |
| 46 | + Assert.Equal("Model", identifier.Value.DataTypeId); |
| 47 | + } |
| 48 | + |
| 49 | + [Fact] |
| 50 | + public void DataElementIdentifier_FromNullDataElement_ShouldThrowNullReferenceException() |
| 51 | + { |
| 52 | + DataElement? dataElement = null; |
| 53 | + |
| 54 | + // The non-nullable implicit operator doesn't guard against null input, |
| 55 | + // so it throws NullReferenceException when accessing dataElement.Id |
| 56 | + Assert.Throws<NullReferenceException>(() => |
| 57 | + { |
| 58 | + DataElementIdentifier identifier = dataElement!; |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + [Fact] |
| 63 | + public void DataElementIdentifier_FromDataElement_ShouldWork() |
| 64 | + { |
| 65 | + var guid = Guid.NewGuid(); |
| 66 | + DataElement dataElement = new() { Id = guid.ToString(), DataType = "Model" }; |
| 67 | + DataElementIdentifier identifier = dataElement; |
| 68 | + |
| 69 | + Assert.Equal(guid, identifier.Guid); |
| 70 | + Assert.Equal(guid.ToString(), identifier.Id); |
| 71 | + Assert.Equal("Model", identifier.DataTypeId); |
| 72 | + } |
| 73 | + |
| 74 | + [Fact] |
| 75 | + public void DataElementIdentifier_FromString_ShouldWork() |
| 76 | + { |
| 77 | + var guid = Guid.NewGuid(); |
| 78 | + var identifier = new DataElementIdentifier(guid.ToString()); |
| 79 | + |
| 80 | + Assert.Equal(guid, identifier.Guid); |
| 81 | + Assert.Equal(guid.ToString(), identifier.Id); |
| 82 | + Assert.Null(identifier.DataTypeId); |
| 83 | + } |
| 84 | + |
| 85 | + [Fact] |
| 86 | + public void DataElementIdentifier_FromGuid_ShouldWork() |
| 87 | + { |
| 88 | + var guid = Guid.NewGuid(); |
| 89 | + var identifier = new DataElementIdentifier(guid); |
| 90 | + |
| 91 | + Assert.Equal(guid, identifier.Guid); |
| 92 | + Assert.Equal(guid.ToString(), identifier.Id); |
| 93 | + Assert.Null(identifier.DataTypeId); |
| 94 | + } |
| 95 | + |
| 96 | + [Fact] |
| 97 | + public void DataElementIdentifier_Equality_ShouldCompareByGuid() |
| 98 | + { |
| 99 | + var guid = Guid.NewGuid(); |
| 100 | + var identifier1 = new DataElementIdentifier(guid); |
| 101 | + var identifier2 = new DataElementIdentifier(guid.ToString()); |
| 102 | + |
| 103 | + Assert.True(identifier1 == identifier2); |
| 104 | + Assert.True(identifier1.Equals(identifier2)); |
| 105 | + Assert.Equal(identifier1.GetHashCode(), identifier2.GetHashCode()); |
| 106 | + } |
| 107 | + |
| 108 | + [Fact] |
| 109 | + public void DataElementIdentifier_Inequality_ShouldCompareByGuid() |
| 110 | + { |
| 111 | + var identifier1 = new DataElementIdentifier(Guid.NewGuid()); |
| 112 | + var identifier2 = new DataElementIdentifier(Guid.NewGuid()); |
| 113 | + |
| 114 | + Assert.True(identifier1 != identifier2); |
| 115 | + Assert.False(identifier1.Equals(identifier2)); |
| 116 | + } |
| 117 | + |
| 118 | + [Fact] |
| 119 | + public void DataElementIdentifier_ToString_ShouldReturnId() |
| 120 | + { |
| 121 | + var guid = Guid.NewGuid(); |
| 122 | + var identifier = new DataElementIdentifier(guid); |
| 123 | + |
| 124 | + Assert.Equal(guid.ToString(), identifier.ToString()); |
| 125 | + } |
| 126 | +} |
0 commit comments