/
ODataMetadataBuilderTestCase.cs
362 lines (302 loc) · 11.2 KB
/
ODataMetadataBuilderTestCase.cs
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
namespace Castle.MonoRail.Extension.OData.Tests
{
using System;
using System.Data.Services.Common;
using System.Data.Services.Providers;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using FluentAssertions;
using NUnit.Framework;
[TestFixture]
public class ODataMetadataBuilderTestCase
{
[Test]
public void SingleEntityWith0PropsWithKey_BuildsResource()
{
var model = new StubModel(t =>
{
t.EntitySet("name", new List<EntWithKey>().AsQueryable());
});
var result = ResourceMetadataBuilder.build(model.SchemaNamespace, model.Entities);
result.Should().NotBeNull();
var resType = result.ElementAt(0);
resType.Should().NotBeNull();
resType.Name.Should().Be("EntWithKey");
resType.Namespace.Should().Be("TestNamespace");
resType.ResourceTypeKind.Should().Be(ResourceTypeKind.EntityType);
resType.Properties.Count.Should().Be(1);
resType.Properties.ElementAt(0).Kind.Should().Be(ResourcePropertyKind.Primitive | ResourcePropertyKind.Key);
resType.Properties.ElementAt(0).Name.Should().Be("Id");
}
[Test]
public void SingleEntityWithPrimitiveProperties_BuildsResource()
{
var model = new StubModel(t =>
{
t.EntitySet("name", new List<EntWithPropsKey>().AsQueryable());
});
var result = ResourceMetadataBuilder.build(model.SchemaNamespace, model.Entities);
result.Should().NotBeNull();
var resType = result.ElementAt(0);
resType.Should().NotBeNull();
resType.Name.Should().Be("EntWithPropsKey");
resType.Namespace.Should().Be("TestNamespace");
resType.ResourceTypeKind.Should().Be(ResourceTypeKind.EntityType);
resType.Properties.Count.Should().Be(4);
var id = resType.Properties.FirstOrDefault(p => p.Name == "Id");
id.Kind.Should().Be(ResourcePropertyKind.Primitive | ResourcePropertyKind.Key);
id.ResourceType.InstanceType.Should().Be<int>();
var name = resType.Properties.FirstOrDefault(p => p.Name == "Name");
name.Kind.Should().Be(ResourcePropertyKind.Primitive);
name.ResourceType.InstanceType.Should().Be<string>();
var age = resType.Properties.FirstOrDefault(p => p.Name == "Age");
age.Kind.Should().Be(ResourcePropertyKind.Primitive);
age.ResourceType.InstanceType.Should().Be<int>();
var dob = resType.Properties.FirstOrDefault(p => p.Name == "DoB");
dob.Kind.Should().Be(ResourcePropertyKind.Primitive);
dob.ResourceType.InstanceType.Should().Be<DateTime>();
}
[Test]
public void SingleEntityWithPrimitivePropertiesAndAttributes_BuildsResource()
{
var model = new StubModel(t =>
{
t.EntitySet("name", new List<EntWithPropsKey>().AsQueryable()).AddAttribute(
new EntityPropertyMappingAttribute("Name", SyndicationItemProperty.Title, SyndicationTextContentKind.Plaintext, true));
});
var result = ResourceMetadataBuilder.build(model.SchemaNamespace, model.Entities);
result.Should().NotBeNull();
var resType = result.ElementAt(0);
resType.Should().NotBeNull();
resType.Name.Should().Be("EntWithPropsKey");
resType.Namespace.Should().Be("TestNamespace");
resType.ResourceTypeKind.Should().Be(ResourceTypeKind.EntityType);
resType.Properties.Count.Should().Be(4);
;
var d = resType.OwnEpmAttributes.Should().HaveCount(1);
var id = resType.Properties.FirstOrDefault(p => p.Name == "Id");
id.Kind.Should().Be(ResourcePropertyKind.Primitive | ResourcePropertyKind.Key);
id.ResourceType.InstanceType.Should().Be<int>();
var name = resType.Properties.FirstOrDefault(p => p.Name == "Name");
name.Kind.Should().Be(ResourcePropertyKind.Primitive);
name.ResourceType.InstanceType.Should().Be<string>();
var age = resType.Properties.FirstOrDefault(p => p.Name == "Age");
age.Kind.Should().Be(ResourcePropertyKind.Primitive);
age.ResourceType.InstanceType.Should().Be<int>();
var dob = resType.Properties.FirstOrDefault(p => p.Name == "DoB");
dob.Kind.Should().Be(ResourcePropertyKind.Primitive);
dob.ResourceType.InstanceType.Should().Be<DateTime>();
}
[Test]
public void SingleEntityWithComplexTypeProperty_BuildsResource()
{
var model = new StubModel(t =>
{
t.EntitySet("name", new List<EntWithComplexPropKey>().AsQueryable());
});
var result = ResourceMetadataBuilder.build(model.SchemaNamespace, model.Entities);
result.Should().NotBeNull();
var resType = result.ElementAt(0);
resType.Should().NotBeNull();
resType.Name.Should().Be("EntWithComplexPropKey");
resType.Namespace.Should().Be("TestNamespace");
resType.ResourceTypeKind.Should().Be(ResourceTypeKind.EntityType);
resType.Properties.Count.Should().Be(2);
var id = resType.Properties.FirstOrDefault(p => p.Name == "Id");
id.Kind.Should().Be(ResourcePropertyKind.Primitive | ResourcePropertyKind.Key);
id.ResourceType.InstanceType.Should().Be<int>();
var address = resType.Properties.FirstOrDefault(p => p.Name == "AddressProp");
address.Kind.Should().Be(ResourcePropertyKind.ComplexType);
address.ResourceType.InstanceType.Should().Be<Address>();
address.ResourceType.Properties.Count.Should().Be(2);
var address1 = address.ResourceType.Properties.FirstOrDefault(p => p.Name == "Address1");
address1.Should().NotBeNull();
address1.Kind.Should().Be(ResourcePropertyKind.Primitive);
address1.ResourceType.InstanceType.Should().Be<string>();
var city = address.ResourceType.Properties.FirstOrDefault(p => p.Name == "City");
city.Should().NotBeNull();
city.Kind.Should().Be(ResourcePropertyKind.Primitive);
city.ResourceType.InstanceType.Should().Be<string>();
}
[Test]
public void TwoEntitiesNotRelated_BuildsTwoResources()
{
var model = new StubModel(t =>
{
t.EntitySet("name1", new List<EntWithKey>().AsQueryable());
t.EntitySet("name2", new List<EntWithPropsKey>().AsQueryable());
});
var result = ResourceMetadataBuilder.build(model.SchemaNamespace, model.Entities);
result.Should().NotBeNull();
result.Count().Should().Be(2);
}
[Test]
public void TwoEntitiesRelatedByDirectResourceRef_CardinalityOne_BuildsTwoResources()
{
var model = new StubModel(t =>
{
t.EntitySet("product", new List<Product1>().AsQueryable());
t.EntitySet("supplier", new List<Supplier1>().AsQueryable());
});
var result = ResourceMetadataBuilder.build(model.SchemaNamespace, model.Entities);
result.Should().NotBeNull();
result.Count().Should().Be(2);
var productRT = result.FirstOrDefault(rt => rt.Name == "Product1");
productRT.Should().NotBeNull();
var supplierRT = result.FirstOrDefault(rt => rt.Name == "Supplier1");
supplierRT.Should().NotBeNull();
var supplierProp = productRT.Properties.FirstOrDefault(p => p.Name == "Supplier");
supplierProp.Should().NotBeNull();
supplierProp.Kind.Should().Be(ResourcePropertyKind.ResourceReference);
supplierProp.ResourceType.Should().BeSameAs(supplierRT);
}
[Test]
public void TwoEntitiesRelatedByDirectResourceRef_CardinalityMany_BuildsTwoResources()
{
var model = new StubModel(t =>
{
t.EntitySet("product", new List<Product2>().AsQueryable());
t.EntitySet("supplier", new List<Supplier2>().AsQueryable());
});
var result = ResourceMetadataBuilder.build(model.SchemaNamespace, model.Entities);
result.Should().NotBeNull();
result.Count().Should().Be(2);
var productRT = result.FirstOrDefault(rt => rt.Name == "Product2");
productRT.Should().NotBeNull();
var supplierRT = result.FirstOrDefault(rt => rt.Name == "Supplier2");
supplierRT.Should().NotBeNull();
var productsProp = supplierRT.Properties.FirstOrDefault(p => p.Name == "Products");
productsProp.Should().NotBeNull();
productsProp.Kind.Should().Be(ResourcePropertyKind.ResourceSetReference);
productsProp.ResourceType.Should().BeSameAs(productRT);
}
[Test]
public void TwoEntitiesReferencingEachOther_CardinalityOneAndMany_BuildsTwoResources()
{
var model = new StubModel(t =>
{
t.EntitySet("product", new List<Product3>().AsQueryable());
t.EntitySet("supplier", new List<Supplier3>().AsQueryable());
});
var result = ResourceMetadataBuilder.build(model.SchemaNamespace, model.Entities);
result.Should().NotBeNull();
result.Count().Should().Be(2);
var productRT = result.FirstOrDefault(rt => rt.Name == "Product3");
productRT.Should().NotBeNull();
var supplierRT = result.FirstOrDefault(rt => rt.Name == "Supplier3");
supplierRT.Should().NotBeNull();
var productsProp = supplierRT.Properties.FirstOrDefault(p => p.Name == "Products");
productsProp.Should().NotBeNull();
productsProp.Kind.Should().Be(ResourcePropertyKind.ResourceSetReference);
productsProp.ResourceType.Should().BeSameAs(productRT);
}
[Test]
public void ComplexType_()
{
var model = new StubModel(t =>
{
t.EntitySet("supplier", new List<Supplier4>().AsQueryable());
});
var result = ResourceMetadataBuilder.build(model.SchemaNamespace, model.Entities);
result.Should().NotBeNull();
result.Count().Should().Be(2);
var addressRT = result.FirstOrDefault(rt => rt.Name == "Address4");
addressRT.Should().NotBeNull();
addressRT.ResourceTypeKind.Should().Be(ResourceTypeKind.ComplexType);
var supplierRT = result.FirstOrDefault(rt => rt.Name == "Supplier4");
supplierRT.Should().NotBeNull();
var productsProp = supplierRT.Properties.FirstOrDefault(p => p.Name == "Address");
productsProp.Should().NotBeNull();
productsProp.Kind.Should().Be(ResourcePropertyKind.ComplexType);
productsProp.ResourceType.Should().BeSameAs(addressRT);
}
public class EntWithKey
{
[Key]
public int Id { get; set; }
}
public class EntWithPropsKey
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public DateTime DoB { get; set; }
}
public class Address
{
public string Address1 { get; set; }
public string City { get; set; }
}
public class EntWithComplexPropKey
{
[Key]
public int Id { get; set; }
public Address AddressProp { get; set; }
}
// ---------------------------------
public class Supplier1
{
[Key]
public int Id { get; set; }
}
public class Product1
{
[Key]
public int Id { get; set; }
public Supplier1 Supplier { get; set; }
}
// ---------------------------------
public class Supplier2
{
[Key]
public int Id { get; set; }
public IList<Product2> Products { get; set; }
}
public class Product2
{
[Key]
public int Id { get; set; }
}
// ---------------------------------
public class Supplier3
{
[Key]
public int Id { get; set; }
public IEnumerable<Product3> Products { get; set; }
}
public class Product3
{
[Key]
public int Id { get; set; }
public Supplier3 Supplier { get; set; }
}
// ---------------------------------
public class Address4
{
public string Street { get; set; }
public string Zip { get; set; }
public string Country { get; set; }
}
public class Supplier4
{
[Key]
public int Id { get; set; }
public Address4 Address { get; set; }
}
public class Catalog4
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public IList<Product4> Products { get; set; }
}
public class Product4
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
}
}
}