Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port Singleton Tests #3163

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//---------------------------------------------------------------------
// <copyright file="ODataAnnotatableExtensions.cs" company="Microsoft">
// Copyright (C) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
// </copyright>
//---------------------------------------------------------------------

using System.Collections.Concurrent;
using System.Diagnostics;

namespace Microsoft.OData.Client.E2E.TestCommon.Common;

public static class ODataAnnotatableExtensions
{
public static void SetAnnotation<T>(this ODataAnnotatable annotatable, T annotation)
where T : class
{
Debug.Assert(annotatable != null, "annotatable != null");
Debug.Assert(annotation != null, "annotation != null");

InternalDictionary<T>.SetAnnotation(annotatable, annotation);
}

public static T GetAnnotation<T>(this ODataAnnotatable annotatable)
where T : class
{
Debug.Assert(annotatable != null, "annotatable != null");

return InternalDictionary<T>.GetAnnotation(annotatable);
}

private static class InternalDictionary<T> where T : class
{
private static readonly ConcurrentDictionary<ODataAnnotatable, T> Dictionary =
new ConcurrentDictionary<ODataAnnotatable, T>();

public static void SetAnnotation(ODataAnnotatable annotatable, T annotation)
{
Dictionary[annotatable] = annotation;
}

public static T GetAnnotation(ODataAnnotatable annotatable)
{
if (Dictionary.TryGetValue(annotatable, out T? annotation))
{
return annotation;
}

return default(T);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ public static class ODataValueAssertEqualHelper

public static void AssertODataValueEqual(ODataValue expected, ODataValue actual)
{
ODataPrimitiveValue expectedPrimitiveValue = expected as ODataPrimitiveValue;
ODataPrimitiveValue actualPrimitiveValue = actual as ODataPrimitiveValue;
var expectedPrimitiveValue = expected as ODataPrimitiveValue;
var actualPrimitiveValue = actual as ODataPrimitiveValue;
if (expectedPrimitiveValue != null && actualPrimitiveValue != null)
{
AssertODataPrimitiveValueEqual(expectedPrimitiveValue, actualPrimitiveValue);
}
else
{
ODataEnumValue expectedEnumValue = expected as ODataEnumValue;
ODataEnumValue actualEnumValue = actual as ODataEnumValue;
var expectedEnumValue = expected as ODataEnumValue;
var actualEnumValue = actual as ODataEnumValue;
if (expectedEnumValue != null && actualEnumValue != null)
{
AssertODataEnumValueEqual(expectedEnumValue, actualEnumValue);
Expand All @@ -42,10 +42,12 @@ private static void AssertODataCollectionValueEqual(ODataCollectionValue expecte
Assert.NotNull(expectedCollectionValue);
Assert.NotNull(actualCollectionValue);
Assert.Equal(expectedCollectionValue.TypeName, actualCollectionValue.TypeName);

var expectedItemsArray = expectedCollectionValue.Items.OfType<object>().ToArray();
var actualItemsArray = actualCollectionValue.Items.OfType<object>().ToArray();

Assert.Equal(expectedItemsArray.Length, actualItemsArray.Length);

for (int i = 0; i < expectedItemsArray.Length; i++)
{
var expectedOdataValue = expectedItemsArray[i] as ODataValue;
Expand All @@ -61,7 +63,7 @@ private static void AssertODataCollectionValueEqual(ODataCollectionValue expecte
}
}

public static void AssertODataPropertiesEqual(IEnumerable<ODataProperty> expectedProperties, IEnumerable<ODataProperty> actualProperties)
public static void AssertODataPropertiesEqual(IEnumerable<ODataPropertyInfo> expectedProperties, IEnumerable<ODataPropertyInfo> actualProperties)
{
if (expectedProperties == null && actualProperties == null)
{
Expand All @@ -70,21 +72,43 @@ public static void AssertODataPropertiesEqual(IEnumerable<ODataProperty> expecte

Assert.NotNull(expectedProperties);
Assert.NotNull(actualProperties);

var expectedPropertyArray = expectedProperties.ToArray();
var actualPropertyArray = actualProperties.ToArray();
Assert.Equal(expectedPropertyArray.Length, actualPropertyArray.Length);

for (int i = 0; i < expectedPropertyArray.Length; i++)
{
AssertODataPropertyEqual(expectedPropertyArray[i], actualPropertyArray[i]);
}
}

public static void AssertODataPropertyEqual(ODataProperty expectedOdataProperty, ODataProperty actualOdataProperty)
public static void AssertODataPropertyAndResourceEqual(ODataResource expectedOdataProperty, ODataResource actualOdataProperty)
{
Assert.NotNull(expectedOdataProperty);
Assert.NotNull(actualOdataProperty);
Assert.Equal(expectedOdataProperty.Name, actualOdataProperty.Name);
AssertODataValueEqual(ToODataValue(expectedOdataProperty.Value), ToODataValue(actualOdataProperty.Value));
AssertODataValueAndResourceEqual(expectedOdataProperty, actualOdataProperty);
}

public static void AssertODataValueAndResourceEqual(ODataResource expected, ODataResource actual)
{
Assert.Equal(expected.TypeName, actual.TypeName);
AssertODataPropertiesEqual(expected.Properties, actual.Properties);
}

public static void AssertODataPropertyEqual(ODataPropertyInfo expectedPropertyInfo, ODataPropertyInfo actualPropertyInfo)
{
Assert.NotNull(expectedPropertyInfo);
Assert.NotNull(actualPropertyInfo);
Assert.Equal(expectedPropertyInfo.Name, actualPropertyInfo.Name);

var expectedProperty = expectedPropertyInfo as ODataProperty;
var actualProperty = actualPropertyInfo as ODataProperty;

Assert.NotNull(expectedProperty);
Assert.NotNull(actualProperty);

AssertODataValueEqual(ToODataValue(expectedProperty.Value), ToODataValue(actualProperty.Value));
}

private static ODataValue ToODataValue(object value)
Expand Down Expand Up @@ -119,5 +143,4 @@ private static void AssertODataEnumValueEqual(ODataEnumValue expectedEnumValue,
}

#endregion Util methods to AssertEqual ODataValues

}
Original file line number Diff line number Diff line change
Expand Up @@ -7305,7 +7305,11 @@ private abstract class GeneratedEdmModel
try
{
var assembly = global::System.Reflection.Assembly.GetExecutingAssembly();
var resourcePath = global::System.Linq.Enumerable.Single(assembly.GetManifestResourceNames(), str => str.EndsWith(filePath));
// If multiple resource names end with the file name, select the shortest one.
var resourcePath = global::System.Linq.Enumerable.First(
global::System.Linq.Enumerable.OrderBy(
global::System.Linq.Enumerable.Where(assembly.GetManifestResourceNames(), name => name.EndsWith(filePath)),
filteredName => filteredName.Length));
global::System.IO.Stream stream = assembly.GetManifestResourceStream(resourcePath);
return global::System.Xml.XmlReader.Create(new global::System.IO.StreamReader(stream));
}
Expand Down Expand Up @@ -7402,6 +7406,20 @@ private abstract class GeneratedEdmModel
/// </summary>
public static class ExtensionMethods
{
/// <summary>
/// There are no comments for GetEmployeesCount in the schema.
/// </summary>
[global::Microsoft.OData.Client.OriginalNameAttribute("GetEmployeesCount")]
public static global::Microsoft.OData.Client.DataServiceQuerySingle<int> GetEmployeesCount(this global::Microsoft.OData.Client.DataServiceQuerySingle<global::Microsoft.OData.Client.E2E.Tests.Common.Client.Default.Company> _source)
{
if (!_source.IsComposable)
{
throw new global::System.NotSupportedException("The previous function is not composable.");
}

return _source.CreateFunctionQuerySingle<int>("Default.GetEmployeesCount", false);
}

/// <summary>
/// There are no comments for GetProductDetails in the schema.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,10 @@
<ReturnType Type="Microsoft.OData.Client.E2E.Tests.Common.Server.Default.Address" />
</Action>
<Action Name="ResetDefaultDataSource" />
<Function Name="GetEmployeesCount" IsBound="true">
<Parameter Name="bindingParameter" Type="Microsoft.OData.Client.E2E.Tests.Common.Server.Default.Company" />
<ReturnType Type="Edm.Int32" Nullable="false" />
</Function>
<Function Name="GetProductDetails" IsBound="true" EntitySetPath="bindingParameter/Details">
<Parameter Name="bindingParameter" Type="Microsoft.OData.Client.E2E.Tests.Common.Server.Default.Product" />
<Parameter Name="count" Type="Edm.Int32" Nullable="false" />
Expand Down Expand Up @@ -492,7 +496,8 @@
<NavigationPropertyBinding Path="Departments" Target="Departments" />
<NavigationPropertyBinding Path="Employees" Target="Employees" />
<NavigationPropertyBinding Path="VipCustomer" Target="Customers" />
</Singleton>
<NavigationPropertyBinding Path="Microsoft.OData.Client.E2E.Tests.Common.Server.Default.PublicCompany/LabourUnion" Target="LabourUnion" />
</Singleton>
<Singleton Name="LabourUnion" Type="Microsoft.OData.Client.E2E.Tests.Common.Server.Default.LabourUnion" />
<Singleton Name="DefaultStoredPI" Type="Microsoft.OData.Client.E2E.Tests.Common.Server.Default.StoredPI" />
<ActionImport Name="Discount" Action="Default.Discount" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public static IEdmModel GetEdmModel()
builder.EntitySet<OrderDetail>("OrderDetails");
builder.EntitySet<Department>("Departments");
builder.Singleton<Company>("Company");
builder.Singleton<Company>("PublicCompany");
builder.Singleton<LabourUnion>("LabourUnion");
builder.Singleton<Company>("PublicCompany").HasSingletonBinding((PublicCompany p) => p.LabourUnion, "LabourUnion");
// builder.Singleton<LabourUnion>("LabourUnion");
builder.EntitySet<Account>("Accounts");
builder.EntitySet<Order>("Orders");
builder.EntitySet<PaymentInstrument>("PaymentInstruments");
Expand Down Expand Up @@ -95,6 +95,10 @@ public static IEdmModel GetEdmModel()

builder.Action("ResetDefaultDataSource");

builder.EntityType<Company>()
.Function("GetEmployeesCount")
.Returns<int>();

builder.EntityType<Product>()
.Function("GetProductDetails")
.ReturnsCollectionViaEntitySetPath<ProductDetail>("bindingParameter/Details")
Expand Down
Loading