Skip to content

Commit

Permalink
Add a test to filter by enum key only using ByKey()
Browse files Browse the repository at this point in the history
  • Loading branch information
WanjohiSammy committed Jul 19, 2024
1 parent 04d79b1 commit 6a5667f
Showing 1 changed file with 67 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ public async Task SelectEntities_WithEnumAsKey_DoNotThrowException()
}
]
}";
SetupContextWithRequestPipeline(new DataServiceContext[] { _defaultContext }, response, "Employees");
SetupContextWithRequestPipeline(_defaultContext, response, "Employees");
_defaultContext.SendingRequest2 += (sender, args) =>
{
Assert.Equal(expectedUri, args.RequestMessage.Url.ToString());
};

// Act
DataServiceQuery<Employee> query = _defaultContext.Employees;
Expand All @@ -106,7 +110,7 @@ public async Task SelectEntities_WithEnumAsKey_DoNotThrowException()
}

[Fact]
public void UseWhereToFilterByEmployeeNumberKey_WithEnumAsKey_DoNotThrowException()
public void UseWhereToFilterByOtherKeyOtherThanEnumKey_WithEnumAsKey_DoNotThrowException()
{
// Arrange
string expectedUri = $"{ServiceRoot}/Employees?$filter=EmpNumber eq 8";
Expand All @@ -122,7 +126,11 @@ public void UseWhereToFilterByEmployeeNumberKey_WithEnumAsKey_DoNotThrowExceptio
}
]
}";
SetupContextWithRequestPipeline(new DataServiceContext[] { _defaultContext }, response, "Employees");
SetupContextWithRequestPipeline(_defaultContext, response, "Employees");
_defaultContext.SendingRequest2 += (sender, args) =>
{
Assert.Equal($"{expectedUri}&$top=1", args.RequestMessage.Url.ToString());
};

// Act
IQueryable<Employee> query = _defaultContext.Employees.Where(e => e.EmpNumber == 8);
Expand Down Expand Up @@ -151,7 +159,11 @@ public void UseWhereToFilterByEnumKey_WithEnumAsKey_DoNotThrowException()
}
]
}";
SetupContextWithRequestPipeline(new DataServiceContext[] { _defaultContext }, response, "Employees");
SetupContextWithRequestPipeline(_defaultContext, response, "Employees");
_defaultContext.SendingRequest2 += (sender, args) =>
{
Assert.Equal($"{expectedUri}&$top=1", args.RequestMessage.Url.ToString());
};

// Act
var query = _defaultContext.Employees.Where(e => e.EmpType == EmployeeType.PartTime);
Expand All @@ -164,7 +176,7 @@ public void UseWhereToFilterByEnumKey_WithEnumAsKey_DoNotThrowException()
}

[Fact]
public void UseByKeyToFilterByCompositeKeys_WithEnumAsKey_DoNotThrowException()
public async Task FilterByCompositeKeys_WithEnumAsKey_DoNotThrowException()
{
// Arrange
string expectedUri = $"{ServiceRoot}/Employees(EmpNumber=8,EmpType=Microsoft.OData.Client.Tests.Tracking.EmployeeType'PartTime',OrgId=1)";
Expand All @@ -180,36 +192,71 @@ public void UseByKeyToFilterByCompositeKeys_WithEnumAsKey_DoNotThrowException()
}
]
}";
SetupContextWithRequestPipeline(new DataServiceContext[] { _defaultContext }, response, "Employees");
SetupContextWithRequestPipeline(_defaultContext, response, "Employees");
_defaultContext.SendingRequest2 += (sender, args) =>
{
Assert.Equal(expectedUri, args.RequestMessage.Url.ToString());
};

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

Employee employee = query.GetValue();
Employee employee = await query.GetValueAsync().ConfigureAwait(false);

// 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)
[Fact]
public void FilterByEnumKey_WithEnumAsKey_DoNotThrowException()
{
string location = $"{ServiceRoot}/{path}";
// Arrange
string expectedUri = $"{ServiceRoot}/Employees(Microsoft.OData.Client.Tests.Tracking.EmployeeType'FullTime')";

foreach (var context in contexts)
string response = @"{
""@odata.context"": ""http://localhost:8007/$metadata#Employees"",
""value"": [
{
""EmpNumber"": 9,
""EmpType"": ""FullTime"",
""OrgId"": 1,
""Name"": ""John Doe""
}
]
}";
SetupContextWithRequestPipeline(_defaultContext, response, "Employees");
_defaultContext.SendingRequest2 += (sender, args) =>
{
context.Configurations.RequestPipeline.OnMessageCreating =
(args) => new CustomizedRequestMessage(
args,
response,
new Dictionary<string, string>()
{
{ "Content-Type", "application/json;charset=utf-8" },
{ "Location", location },
});
}
Assert.Equal(expectedUri, args.RequestMessage.Url.ToString());
};

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

Employee employee = query.GetValue();

// Assert
Assert.Equal(expectedUri, query.Query.ToString());
Assert.Equal(9, employee.EmpNumber);
Assert.Equal(EmployeeType.FullTime, employee.EmpType);
}

private void SetupContextWithRequestPipeline(DataServiceContext context, string response, string path)
{
string location = $"{ServiceRoot}/{path}";

context.Configurations.RequestPipeline.OnMessageCreating = (args) => new CustomizedRequestMessage(
args,
response,
new Dictionary<string, string>()
{
{ "Content-Type", "application/json;charset=utf-8" },
{ "Location", location },
});
}

class Container : DataServiceContext
Expand Down

0 comments on commit 6a5667f

Please sign in to comment.