Skip to content

Commit

Permalink
Add test to filter by enum member name and a test to use ByKey to fil…
Browse files Browse the repository at this point in the history
…ter by composite keys
  • Loading branch information
WanjohiSammy committed Jul 16, 2024
1 parent cee9572 commit 04d79b1
Showing 1 changed file with 90 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ public DataServiceContextQueryTests()
public async Task SelectEntities_WithEnumAsKey_DoNotThrowException()
{
// Arrange
var expectedUri = $"{ServiceRoot}/Employees";

string response = @"{
""@odata.context"": ""http://localhost:8007/$metadata#Employees"",
""value"": [
Expand All @@ -92,19 +94,23 @@ public async Task SelectEntities_WithEnumAsKey_DoNotThrowException()
}
]
}";
SetupContextWithRequestPipeline(new DataServiceContext[] { _defaultContext }, response, "employees");
SetupContextWithRequestPipeline(new DataServiceContext[] { _defaultContext }, response, "Employees");

// Act
IEnumerable<Employee> employees = await _defaultContext.Employees.ExecuteAsync();
DataServiceQuery<Employee> query = _defaultContext.Employees;
IEnumerable<Employee> employees = await query.ExecuteAsync();

// Assert
Assert.Equal(expectedUri, query.ToString());
Assert.Equal(2, employees.Count());
}

[Fact]
public void SelectSpecificEntity_WithEnumAsKey_DoNotThrowException()
public void UseWhereToFilterByEmployeeNumberKey_WithEnumAsKey_DoNotThrowException()
{
// Arrange
string expectedUri = $"{ServiceRoot}/Employees?$filter=EmpNumber eq 8";

string response = @"{
""@odata.context"": ""http://localhost:8007/$metadata#Employees"",
""value"": [
Expand All @@ -116,16 +122,78 @@ public void SelectSpecificEntity_WithEnumAsKey_DoNotThrowException()
}
]
}";
SetupContextWithRequestPipeline(new DataServiceContext[] { _defaultContext }, response, "employees");
SetupContextWithRequestPipeline(new DataServiceContext[] { _defaultContext }, response, "Employees");

// Act
Employee employee = _defaultContext.Employees.Where(e => e.EmpNumber == 8).First();
IQueryable<Employee> query = _defaultContext.Employees.Where(e => e.EmpNumber == 8);
Employee employee = query.First();

// Assert
Assert.Equal(expectedUri, query.ToString());
Assert.Equal(EmployeeType.PartTime, employee.EmpType);
Assert.Equal("Employee Two", employee.Name);
}

[Fact]
public void UseWhereToFilterByEnumKey_WithEnumAsKey_DoNotThrowException()
{
// Arrange
string expectedUri = $"{ServiceRoot}/Employees?$filter=EmpType eq Microsoft.OData.Client.Tests.Tracking.EmployeeType'PartTime'";

string response = @"{
""@odata.context"": ""http://localhost:8007/$metadata#Employees"",
""value"": [
{
""EmpNumber"": 8,
""EmpType"": ""PartTime"",
""OrgId"": 1,
""Name"": ""Employee 45""
}
]
}";
SetupContextWithRequestPipeline(new DataServiceContext[] { _defaultContext }, response, "Employees");

// Act
var query = _defaultContext.Employees.Where(e => e.EmpType == EmployeeType.PartTime);
Employee employee = query.First();

// Assert
Assert.Equal(expectedUri, query.ToString());
Assert.Equal(8, employee.EmpNumber);
Assert.Equal(EmployeeType.PartTime, employee.EmpType);
}

[Fact]
public void UseByKeyToFilterByCompositeKeys_WithEnumAsKey_DoNotThrowException()
{
// Arrange
string expectedUri = $"{ServiceRoot}/Employees(EmpNumber=8,EmpType=Microsoft.OData.Client.Tests.Tracking.EmployeeType'PartTime',OrgId=1)";

string response = @"{
""@odata.context"": ""http://localhost:8007/$metadata#Employees"",
""value"": [
{
""EmpNumber"": 8,
""EmpType"": ""PartTime"",
""OrgId"": 1,
""Name"": ""Employee 24""
}
]
}";
SetupContextWithRequestPipeline(new DataServiceContext[] { _defaultContext }, response, "Employees");

// Act
EmployeeSingle query = _defaultContext.Employees.ByKey(
new Dictionary<string, object>() { { "EmpNumber", 8 }, { "EmpType", EmployeeType.PartTime }, { "OrgId", 1 } });

Employee employee = query.GetValue();

// Assert
Assert.Equal(expectedUri, query.Query.ToString());
Assert.Equal("Employee 24", employee.Name);
Assert.Equal(EmployeeType.PartTime, employee.EmpType);
}

private void SetupContextWithRequestPipeline(DataServiceContext[] contexts, string response, string path)
{
string location = $"{ServiceRoot}/{path}";
Expand Down Expand Up @@ -186,4 +254,21 @@ public enum EmployeeType
FullTime = 2,
PartTime = 3
}

public partial class EmployeeSingle : DataServiceQuerySingle<Employee>
{
/// <summary>
/// Initialize a new EmployeeSingle object.
/// </summary>
public EmployeeSingle(DataServiceContext context, string path)
: base(context, path) { }
}

public static class ExtensionMethods
{
public static EmployeeSingle ByKey(this DataServiceQuery<Employee> _source, IDictionary<string, object> _keys)
{
return new EmployeeSingle(_source.Context, _source.GetKeyPath(Serializer.GetKeyString(_source.Context, _keys)));
}
}
}

0 comments on commit 04d79b1

Please sign in to comment.