-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathPrimitiveExampleValueTests.cs
More file actions
149 lines (131 loc) · 5.75 KB
/
PrimitiveExampleValueTests.cs
File metadata and controls
149 lines (131 loc) · 5.75 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// ------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Microsoft.OData.Edm;
using Microsoft.OData.Edm.Csdl;
using Microsoft.OData.Edm.Vocabularies;
using Microsoft.OpenApi.OData.Edm;
using Microsoft.OpenApi.OData.Vocabulary.Core;
using Xunit;
namespace Microsoft.OpenApi.OData.Reader.Vocabulary.Core.Tests
{
public class PrimitiveExampleValueTests
{
[Fact]
public void DefaultPropertyAsNull()
{
// Arrange
PrimitiveExampleValue value = new PrimitiveExampleValue();
// Act & Assert
Assert.Null(value.Description);
Assert.Null(value.Value);
}
[Fact]
public void InitializeWithNullRecordThrows()
{
// Arrange & Act
PrimitiveExampleValue value = new PrimitiveExampleValue();
// Assert
Assert.Throws<ArgumentNullException>("record", () => value.Initialize(record: null));
}
[Fact]
public void InitializeWithPrimitiveValueRecordSuccess()
{
// Arrange
IEdmRecordExpression record = new EdmRecordExpression(
new EdmPropertyConstructor("Description", new EdmStringConstant("HelloWorld!")),
new EdmPropertyConstructor("Value", new EdmBooleanConstant(true)));
PrimitiveExampleValue value = new PrimitiveExampleValue();
Assert.Null(value.Description);
Assert.Null(value.Value);
// Act
value.Initialize(record);
// Assert
Assert.NotNull(value.Description);
Assert.Equal("HelloWorld!", value.Description);
Assert.NotNull(value.Value);
Assert.NotNull(value.Value.Value);
Assert.Equal(true, value.Value.Value);
}
public static IEnumerable<object[]> PrimitiveData =>
new List<object[]>
{
new object[] { @"String=""Hello World""", "Hello World" },
new object[] { @"Int=""42""", (long)42 },
new object[] { @"Bool=""true""", true },
new object[] { @"Bool=""false""", false },
new object[] { @"TimeOfDay=""15:38:25.1090000""", new TimeOnly(15, 38, 25, 109) },
new object[] { @"Date=""2014-10-13""", new DateOnly(2014, 10, 13) },
new object[] { @"Duration=""PT0S""", new TimeSpan() },
// new object[] { @"Binary=""AQ==""", new byte[] { 1 }, }, has problem in ODL?
new object[] { @"Float=""3.14""", 3.14 },
new object[] { @"Decimal=""3.14""", 3.14m },
new object[] { @"DateTimeOffset=""0001-01-01T00:00:00Z""", new DateTimeOffset() },
new object[] { @"Guid=""21EC2020-3AEA-1069-A2DD-08002B30309D""", new Guid("21EC2020-3AEA-1069-A2DD-08002B30309D") },
};
[Theory]
[MemberData(nameof(PrimitiveData))]
public void PrimitiveExamplevalueInitializeWorksForPrimitiveData(string data, object except)
{
// Arrange
string annotation = $@"<Annotation Term=""Org.OData.Core.V1.Example"">
<Record Type=""Org.OData.Core.V1.PrimitiveExampleValue"">
<PropertyValue Property=""Description"" String=""Primitive example value"" />
<PropertyValue Property=""Value"" {data} />
</Record>
</Annotation>";
IEdmModel model = GetEdmModel(annotation);
Assert.NotNull(model); // guard
IEdmEntityType customer = model.SchemaElements.OfType<IEdmEntityType>().First(c => c.Name == "Customer");
Assert.NotNull(customer); // guard
IEdmProperty dataProperty = customer.FindProperty("Data");
Assert.NotNull(dataProperty);
// Act
PrimitiveExampleValue value = model.GetRecord<PrimitiveExampleValue>(dataProperty, "Org.OData.Core.V1.Example");
// Assert
Assert.NotNull(value);
Assert.Equal("Primitive example value", value.Description);
Assert.NotNull(value.Value);
switch (except)
{
case DateOnly dateOnly:
Assert.Equal(dateOnly.ToString("yyyy-MM-dd"), value.Value.Value.ToString());
break;
case TimeOnly timeOnly:
Assert.Equal(timeOnly.ToString("HH:mm:ss.fffffff"), value.Value.Value.ToString());
break;
default:
Assert.Equal(except, value.Value.Value);
break;
}
}
private IEdmModel GetEdmModel(string annotation)
{
const string template = @"<edmx:Edmx Version=""4.0"" xmlns:edmx=""http://docs.oasis-open.org/odata/ns/edmx"">
<edmx:DataServices>
<Schema Namespace=""NS"" xmlns=""http://docs.oasis-open.org/odata/ns/edm"">
<EntityType Name=""Customer"">
<Key>
<PropertyRef Name=""ID"" />
</Key>
<Property Name=""ID"" Type=""Edm.Int32"" Nullable=""false"" />
<Property Name=""Data"" Type=""Edm.PrimitiveType"" Nullable=""false"" >
{0}
</Property>
</EntityType>
</Schema>
</edmx:DataServices>
</edmx:Edmx>";
string modelText = string.Format(template, annotation);
IEdmModel model;
bool result = CsdlReader.TryParse(XElement.Parse(modelText).CreateReader(), out model, out _);
Assert.True(result);
return model;
}
}
}