Skip to content

Commit

Permalink
Merge related tests using [Theory]
Browse files Browse the repository at this point in the history
  • Loading branch information
WanjohiSammy committed Aug 15, 2024
1 parent 8f0698b commit 07655ee
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 153 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -520,49 +520,35 @@ public void ParseFilterEnumTypesWrongCast3()
parse.Throws<ODataException>(Strings.MetadataBinder_CastOrIsOfFunctionWithoutATypeArgument);
}

[Fact]
public void ParseFilterWithInOperatorWithEnumsFullyQualifiedMemberName()
{
// Arrange
string filterQuery = "NS.Color'White' in Colors";
string expectedLiteral = "NS.Color'White'";

IEdmEnumType colorType = this.GetIEdmType<IEdmEnumType>("NS.Color");
IEdmEnumMember enumMember = colorType.Members.Single(m => m.Name == "White");

// Act
FilterClause filterQueryNode = ParseFilter(filterQuery, this.userModel, this.entityType, this.entitySet);
SingleValueNode expression = filterQueryNode.Expression;

// Assert
InNode inNode = expression.ShouldBeInNode();
ConstantNode leftNode = Assert.IsType<ConstantNode>(inNode.Left);
leftNode.ShouldBeEnumNode(colorType, enumMember.Value.Value);

Assert.True(leftNode.TypeReference.IsEnum());
Assert.Equal(enumMember.Value.Value.ToString(), leftNode.Value.ToString());
Assert.Equal(expectedLiteral, leftNode.LiteralText);

CollectionPropertyAccessNode rightNode = Assert.IsType<CollectionPropertyAccessNode>(inNode.Right);
rightNode.ShouldBeCollectionPropertyAccessQueryNode(this.GetIEdmProperty("Colors"));
Assert.Equal(colorType, rightNode.ItemType.Definition);
}

[Fact]
public void ParseFilterWithInOperatorWithEnumsMemberNameWithoutFullyQualifiedName()
[Theory]
[InlineData("NS.Color'Green'")]
[InlineData("'Green'")]
[InlineData("'2'")]
[InlineData("2")]
public void ParseFilterWithInOperatorWithEnums(string filterOptionValue)
{
// Arrange
string filterQuery = $"{filterOptionValue} in Colors";
string enumValue = "Green";
string filterQuery = "'Green' in Colors"; // "'Green' in Colors"
string expectedLiteral = "'Green'";
string colorTypeName = "NS.Color";

IEdmEnumType colorType = this.GetIEdmType<IEdmEnumType>("NS.Color");
IEdmEnumType colorType = this.GetIEdmType<IEdmEnumType>(colorTypeName);
IEdmEnumMember enumMember = colorType.Members.First(m => m.Name == enumValue);

string expectedLiteral = "'Green'";
if (filterOptionValue.StartsWith(colorTypeName)) // if the filterOptionValue is already fully qualified, then the expectedLiteral should be the same as filterOptionValue
{
expectedLiteral = "NS.Color'Green'";
enumValue = enumMember.Value.Value.ToString(); // "2" <cref="ODataEnumValue.Value"/> The backing type, can be "3" or "White" or "Black,Yellow,Cyan"
}

// Act
FilterClause filterQueryNode = ParseFilter(filterQuery, this.userModel, this.entityType, this.entitySet);
SingleValueNode expression = filterQueryNode.Expression;

// Assert
Assert.Equal(2, enumMember.Value.Value);
Assert.Equal("Green", enumMember.Name);
InNode inNode = expression.ShouldBeInNode();
ConstantNode leftNode = Assert.IsType<ConstantNode>(inNode.Left);
leftNode.ShouldBeEnumNode(colorType, enumValue);
Expand All @@ -574,77 +560,14 @@ public void ParseFilterWithInOperatorWithEnumsMemberNameWithoutFullyQualifiedNam
CollectionPropertyAccessNode rightNode = Assert.IsType<CollectionPropertyAccessNode>(inNode.Right);
rightNode.ShouldBeCollectionPropertyAccessQueryNode(this.GetIEdmProperty("Colors"));
Assert.Equal(colorType, rightNode.ItemType.Definition);
}

[Fact]
public void ParseFilterWithInOperatorWithEnumsMemberIntegralValueWithSingleQuotes()
{
// Arrange
int enumValue = 1;
string filterQuery = "'1' in Colors";
string expectedLiteral = "'Red'";

IEdmEnumType colorType = this.GetIEdmType<IEdmEnumType>("NS.Color");
bool success = colorType.TryParse(enumValue, out IEdmEnumMember enumMember);

// Act
FilterClause filterQueryNode = ParseFilter(filterQuery, this.userModel, this.entityType, this.entitySet);
SingleValueNode expression = filterQueryNode.Expression;

// Assert
Assert.True(success);
InNode inNode = expression.ShouldBeInNode();
ConstantNode leftNode = Assert.IsType<ConstantNode>(inNode.Left);
leftNode.ShouldBeEnumNode(colorType, enumMember.Name);

Assert.True(leftNode.TypeReference.IsEnum());
Assert.Equal(enumMember.Name, leftNode.Value.ToString());
Assert.Equal(expectedLiteral, leftNode.LiteralText);

CollectionPropertyAccessNode rightNode = Assert.IsType<CollectionPropertyAccessNode>(inNode.Right);
rightNode.ShouldBeCollectionPropertyAccessQueryNode(this.GetIEdmProperty("Colors"));
Assert.Equal(colorType, rightNode.ItemType.Definition);
}

[Fact]
public void ParseFilterWithInOperatorWithEnumsMemberIntegralValueWithoutSingleQuotes()
{
// Arrange
int enumValue = 3;
string filterQuery = "3 in Colors";
string expectedLiteral = "'Blue'";

IEdmEnumType colorType = this.GetIEdmType<IEdmEnumType>("NS.Color");
bool success = colorType.TryParse(enumValue, out IEdmEnumMember enumMember);

// Act
FilterClause filterQueryNode = ParseFilter(filterQuery, this.userModel, this.entityType, this.entitySet);
SingleValueNode expression = filterQueryNode.Expression;

// Assert
Assert.True(success);
InNode inNode = expression.ShouldBeInNode();
ConstantNode leftNode = Assert.IsType<ConstantNode>(inNode.Left);
leftNode.ShouldBeEnumNode(colorType, enumMember.Name);

Assert.True(leftNode.TypeReference.IsEnum());
Assert.Equal(enumMember.Name, leftNode.Value.ToString());
Assert.Equal(expectedLiteral, leftNode.LiteralText);

CollectionPropertyAccessNode rightNode = Assert.IsType<CollectionPropertyAccessNode>(inNode.Right);
rightNode.ShouldBeCollectionPropertyAccessQueryNode(this.GetIEdmProperty("Colors"));
Assert.Equal(colorType, rightNode.ItemType.Definition);
}

[Theory]
[InlineData("3.0")]
[InlineData("3.1")]
[InlineData("4.0")]
[InlineData("5.0")]
public void ParseFilterWithInOperatorWithEnumsMemberFloatIntegralValue_ThrowCollectionItemTypeMustBeSameAsSingleItemTypeException(string floatString)
public void ParseFilterWithInOperatorWithEnumsMemberFloatIntegralValue_ThrowCollectionItemTypeMustBeSameAsSingleItemTypeException()
{
// Arrange
string filterQuery = $"{floatString} in Colors";
string filterQuery = $"3.0 in Colors";

// Act
Action test = () => ParseFilter(filterQuery, this.userModel, this.entityType, this.entitySet);
Expand All @@ -654,10 +577,10 @@ public void ParseFilterWithInOperatorWithEnumsMemberFloatIntegralValue_ThrowColl
}

[Theory]
[InlineData(-20)]
[InlineData("-20")]
[InlineData("'-20'")]
public void ParseFilterWithInOperatorWithEnumsInvalidIntegralValues_ThrowsIsNotValidEnumConstantException(object integralValue)
[InlineData("-20", "-20")]
[InlineData("'-20'", "-20")]
[InlineData("'3.0'", "3.0")]
public void ParseFilterWithInOperatorWithEnumsInvalidIntegralValues_ThrowsIsNotValidEnumConstantException(string integralValue, string errorMessageParam)
{
// Arrange
string filterQuery = $"{integralValue} in Colors";
Expand All @@ -666,7 +589,7 @@ public void ParseFilterWithInOperatorWithEnumsInvalidIntegralValues_ThrowsIsNotV
Action action = () => ParseFilter(filterQuery, this.userModel, this.entityType, this.entitySet);

// Assert
action.Throws<ODataException>(Strings.Binder_IsNotValidEnumConstant("-20"));
action.Throws<ODataException>(Strings.Binder_IsNotValidEnumConstant(errorMessageParam));
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2041,57 +2041,14 @@ public void FilterWithInOperationWithEnums()
Assert.Equal("FavoriteColors", Assert.IsType<CollectionPropertyAccessNode>(inNode.Right).Property.Name);
}

[Fact]
public void FilterWithInOperationWithEnumsMemberName_WithoutQualifiedNamespace()
{
// Arrange
string filterQuery = "'SolidYellow' in FavoriteColors";

string expectedLiteralText = "'SolidYellow'";
string expectedfullyQualifiedName = "Fully.Qualified.Namespace.ColorPattern'SolidYellow'";
string expectedCollectionPropertyName = "FavoriteColors";

// Act
FilterClause filter = ParseFilter(filterQuery, HardCodedTestModel.TestModel, HardCodedTestModel.GetPersonType());

// Assert
InNode inNode = Assert.IsType<InNode>(filter.Expression);
ConstantNode left = Assert.IsType<ConstantNode>(inNode.Left);
ODataEnumValue enumValue = Assert.IsType<ODataEnumValue>(left.Value);

Assert.Equal(expectedLiteralText, left.LiteralText);
Assert.Equal(expectedfullyQualifiedName, (enumValue.TypeName + left.LiteralText));
Assert.Equal(expectedCollectionPropertyName, Assert.IsType<CollectionPropertyAccessNode>(inNode.Right).Property.Name);
}

[Fact]
public void FilterWithInOperationWithEnumsMemberIntegralValue_WithSingleQuotes()
{
// Arrange
string filterQuery = "'12' in FavoriteColors";

string expectedLiteralText = "'SolidYellow'";
string expectedfullyQualifiedName = "Fully.Qualified.Namespace.ColorPattern'SolidYellow'";
string expectedCollectionPropertyName = "FavoriteColors";

// Act
FilterClause filter = ParseFilter(filterQuery, HardCodedTestModel.TestModel, HardCodedTestModel.GetPersonType());

// Assert
InNode inNode = Assert.IsType<InNode>(filter.Expression);
ConstantNode left = Assert.IsType<ConstantNode>(inNode.Left);
ODataEnumValue enumValue = Assert.IsType<ODataEnumValue>(left.Value);

Assert.Equal(expectedLiteralText, left.LiteralText);
Assert.Equal(expectedfullyQualifiedName, (enumValue.TypeName + left.LiteralText));
Assert.Equal(expectedCollectionPropertyName, Assert.IsType<CollectionPropertyAccessNode>(inNode.Right).Property.Name);
}

[Fact]
public void FilterWithInOperationWithEnumsMemberIntegralValue_WithoutSingleQuotes()
[Theory]
[InlineData("'SolidYellow'")]
[InlineData("'12'")]
[InlineData("12")]
public void FilterWithInOperationWithEnumsMemberIntegralValue(string filterOptionValue)
{
// Arrange
string filterQuery = "12 in FavoriteColors";
string filterQuery = $"{filterOptionValue} in FavoriteColors";

string expectedLiteralText = "'SolidYellow'";
string expectedfullyQualifiedName = "Fully.Qualified.Namespace.ColorPattern'SolidYellow'";
Expand All @@ -2111,7 +2068,6 @@ public void FilterWithInOperationWithEnumsMemberIntegralValue_WithoutSingleQuote
}

[Theory]
[InlineData(53)]
[InlineData("53")]
[InlineData("'53'")]
public void FilterWithInOperationWithEnumsInvalidMemberIntegralValue_ThrowsIsNotValidEnumConstantException(object integralValue)
Expand Down

0 comments on commit 07655ee

Please sign in to comment.