diff --git a/test/FunctionalTests/Microsoft.OData.Client.Tests/Tracking/DataServiceContextQueryTests.cs b/test/FunctionalTests/Microsoft.OData.Client.Tests/Tracking/DataServiceContextQueryTests.cs new file mode 100644 index 0000000000..4c95e2be16 --- /dev/null +++ b/test/FunctionalTests/Microsoft.OData.Client.Tests/Tracking/DataServiceContextQueryTests.cs @@ -0,0 +1,161 @@ +//--------------------------------------------------------------------- +// +// Copyright (C) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information. +// +//--------------------------------------------------------------------- + +using Microsoft.OData.Edm.Csdl; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using System.Xml; +using Xunit; + +namespace Microsoft.OData.Client.Tests.Tracking +{ + public class DataServiceContextQueryTests + { + private readonly Container _defaultContext; + + #region Test Edmx + private const string Edmx = @" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +"; + #endregion + + public DataServiceContextQueryTests() + { + var uri = new Uri("http://localhost:8000"); + _defaultContext = new Container(uri); + } + + [Fact] + public async Task SelectEntities_WithEnumAsKey_DoNotThrowException() + { + // Arrange + string response = @"{ + ""@odata.context"": ""http://localhost:5128/$metadata#Employees"", + ""value"": [ + { + ""EmpNumber"": 1, + ""EmpType"": ""FullTime"", + ""OrgId"": 1, + ""Name"": ""John Doe"" + }, + { + ""EmpNumber"": 2, + ""EmpType"": ""PartTime"", + ""OrgId"": 1, + ""Name"": ""Jane Doe"" + } + ] +}"; + SetupContextWithRequestPipeline(new DataServiceContext[] { _defaultContext }, response, "http://localhost:8000/employees"); + + // Act + IEnumerable employees = await _defaultContext.Employees.ExecuteAsync(); + + // Assert + Assert.Equal(2, employees.Count()); + } + + private void SetupContextWithRequestPipeline(DataServiceContext[] contexts, string response, string location) + { + foreach (var context in contexts) + { + context.Configurations.RequestPipeline.OnMessageCreating = + (args) => new CustomizedRequestMessage( + args, + response, + new Dictionary() + { + { "Content-Type", "application/json;charset=utf-8" }, + { "Location", location }, + }); + } + } + + class Container : DataServiceContext + { + public Container(Uri serviceRoot) : + base(serviceRoot, ODataProtocolVersion.V4) + { + Format.LoadServiceModel = () => CsdlReader.Parse(XmlReader.Create(new StringReader(Edmx))); + Format.UseJson(); + Employees = base.CreateQuery("Employees"); + } + + public DataServiceQuery Employees { get; private set; } + } + } + + [Key("EmpNumber", "EmpType", "OrgId")] + public class Employee : BaseEntityType + { + public int EmpNumber { get; set; } + + // Enum - Employee Type Key + public EmployeeType EmpType { get; set; } + + public int OrgId { get; set; } + + public string Name { get; set; } + + [ForeignKey("OrgId")] + public virtual Organization Organization { get; set; } + } + + public class Organization + { + public int Id { get; set; } + public string Name { get; set; } + } + + public enum EmployeeType + { + None = 1, + FullTime = 2, + PartTime = 3 + } +}