Skip to content

Commit

Permalink
Complete support for Enum with complete Test sets. document enum supp…
Browse files Browse the repository at this point in the history
…ort #261
  • Loading branch information
mikependon committed Jun 10, 2019
1 parent 5217208 commit dac6ee8
Show file tree
Hide file tree
Showing 15 changed files with 288 additions and 170 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,25 +96,25 @@ public void TestQueryGroupForEnumViaDynamic()
}
}

//[TestMethod]
//public void TestQueryGroupForEnumViaExpression()
//{
// // Setup
// var entities = Helper.CreateEnumCompleteTables(10);

// using (var connection = new SqlConnection(Database.ConnectionStringForRepoDb))
// {
// // Act
// var insertAllResult = connection.InsertAll<EnumCompleteTable>(entities);
// var queryResult = connection.Query<EnumCompleteTable>(e => e.ColumnNVarChar == Direction.West);

// // Assert
// Assert.AreEqual(entities.Count, queryResult.Count());

// // Assert
// entities.ForEach(entity => Helper.AssertPropertiesEquality(entity, queryResult.Where(item => item.SessionId == entity.SessionId)));
// }
//}
[TestMethod]
public void TestQueryGroupForEnumViaExpression()
{
// Setup
var entities = Helper.CreateEnumCompleteTables(10);

using (var connection = new SqlConnection(Database.ConnectionStringForRepoDb))
{
// Act
var insertAllResult = connection.InsertAll<EnumCompleteTable>(entities);
var queryResult = connection.Query<EnumCompleteTable>(e => e.ColumnNVarChar == Direction.West);

// Assert
Assert.AreEqual(entities.Count, queryResult.Count());

// Assert
entities.ForEach(entity => Helper.AssertPropertiesEquality(entity, queryResult.Where(item => item.SessionId == entity.SessionId)));
}
}

[TestMethod]
public void TestQueryGroupForEnumViaQueryField()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,20 @@ public void TestQueryGroupParseDynamicValueForMultipleFields()
Assert.AreEqual(1, actual1);
Assert.AreEqual(2, actual2);
}

[TestMethod]
public void TestQueryGroupParseDynamicValueForEnums()
{
// Setup
var parsed = QueryGroup.Parse(new { Field1 = Direction.West, Field2 = Direction.East });

// Act
var actual1 = parsed.QueryFields.First().Parameter.Value;
var actual2 = parsed.QueryFields.Last().Parameter.Value;

// Assert
Assert.AreEqual(Direction.West, actual1);
Assert.AreEqual(Direction.East, actual2);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ public class QueryGroupTestExpressionClassMember
public string PropertyString { get; set; }
}

public enum Direction
{
North,
South,
East,
West
}

public class QueryGroupTestExpressionClass
{
public int PropertyInt { get; set; }
Expand All @@ -21,6 +29,7 @@ public class QueryGroupTestExpressionClass
public Guid PropertyGuid { get; set; }
public Boolean PropertyBoolean { get; set; }
public Byte[] PropertyBytes { get; set; }
public Direction Direction { get; set; }
[Map("PropertyString")]
public string OtherPropertyString { get; set; }
public QueryGroupTestExpressionClassMember Member { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ public void TestQueryGroupParseExpressionArrayContainsEqualsFalse()

// Act
var actual = parsed.GetString();
var expected = "NOT ([PropertyInt] IN (@PropertyInt_In_0, @PropertyInt_In_1))";
var expected = "([PropertyInt] NOT IN (@PropertyInt_In_0, @PropertyInt_In_1))";

// Assert
Assert.AreEqual(expected, actual);
Expand Down Expand Up @@ -544,7 +544,7 @@ public void TestQueryGroupParseExpressionArrayContainsFromVariableEqualsFalse()

// Act
var actual = parsed.GetString();
var expected = "NOT ([PropertyInt] IN (@PropertyInt_In_0, @PropertyInt_In_1))";
var expected = "([PropertyInt] NOT IN (@PropertyInt_In_0, @PropertyInt_In_1))";

// Assert
Assert.AreEqual(expected, actual);
Expand Down Expand Up @@ -662,7 +662,7 @@ public void TestQueryGroupParseExpressionListContainsEqualsFalse()

// Act
var actual = parsed.GetString();
var expected = "NOT ([PropertyInt] IN (@PropertyInt_In_0, @PropertyInt_In_1))";
var expected = "([PropertyInt] NOT IN (@PropertyInt_In_0, @PropertyInt_In_1))";

// Assert
Assert.AreEqual(expected, actual);
Expand Down Expand Up @@ -692,7 +692,7 @@ public void TestQueryGroupParseExpressionListContainsFromVariableEqualsFalse()

// Act
var actual = parsed.GetString();
var expected = "NOT ([PropertyInt] IN (@PropertyInt_In_0, @PropertyInt_In_1))";
var expected = "([PropertyInt] NOT IN (@PropertyInt_In_0, @PropertyInt_In_1))";

// Assert
Assert.AreEqual(expected, actual);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace RepoDb.UnitTests
{
public partial class QueryGroupTest
{
// Values
#region Values

[TestMethod]
public void TestQueryGroupParseExpressionValueIntConstant()
Expand Down Expand Up @@ -362,7 +362,9 @@ public void TestQueryGroupParseExpressionWithDefaultValue()
Assert.AreEqual(expected, actual);
}

// Contains
#endregion

#region Contains

[TestMethod]
public void TestQueryGroupParseExpressionValueForStringPropertyContains()
Expand Down Expand Up @@ -522,7 +524,9 @@ public void TestQueryGroupParseExpressionValueForArrayNotContainsEqualsFalse()
Assert.AreEqual("B", ((Array)actual).GetValue(1));
}

// All
#endregion

#region All

[TestMethod]
public void TestQueryGroupParseExpressionValueForArrayAll()
Expand Down Expand Up @@ -613,7 +617,9 @@ public void TestQueryGroupParseExpressionValueForArrayNotAllEqualsTrue()
Assert.AreEqual("B", actual2);
}

// Any
#endregion

#region Any

[TestMethod]
public void TestQueryGroupParseExpressionValueForArrayAny()
Expand Down Expand Up @@ -703,5 +709,39 @@ public void TestQueryGroupParseExpressionValueForArrayNotAnyEqualsTrue()
Assert.AreEqual("A", actual1);
Assert.AreEqual("B", actual2);
}

#endregion

#region Enums

[TestMethod]
public void TestQueryGroupParseExpressionValueForEnum()
{
// Setup
var parsed = QueryGroup.Parse<QueryGroupTestExpressionClass>(p => p.Direction == Direction.East);

// Act
var actual = parsed.QueryFields.First().Parameter.Value;

// Assert
Assert.AreEqual(Direction.East, actual);
}

[TestMethod]
public void TestQueryGroupParseExpressionValueForEnums()
{
// Setup
var parsed = QueryGroup.Parse<QueryGroupTestExpressionClass>(p => p.Direction == Direction.East || p.Direction == Direction.West);

// Act
var actual1 = parsed.QueryGroups.First().QueryFields.First().Parameter.Value;
var actual2 = parsed.QueryGroups.Last().QueryFields.First().Parameter.Value;

// Assert
Assert.AreEqual(Direction.East, actual1);
Assert.AreEqual(Direction.West, actual2);
}

#endregion
}
}
6 changes: 3 additions & 3 deletions RepoDb.Core/RepoDb/Extensions/ExpressionExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static class ExpressionExtension
/// </summary>
/// <param name="expression">The instance of <see cref="Expression"/> object to be identified.</param>
/// <returns>Returns true if the expression can be extracted as <see cref="QueryField"/> object.</returns>
public static bool IsExtractable(this Expression expression)
internal static bool IsExtractable(this Expression expression)
{
return m_extractableExpressionTypes.Contains(expression.NodeType);
}
Expand All @@ -49,7 +49,7 @@ public static bool IsExtractable(this Expression expression)
/// </summary>
/// <param name="expression">The instance of <see cref="Expression"/> object to be identified.</param>
/// <returns>Returns true if the expression can be grouped as <see cref="QueryGroup"/> object.</returns>
public static bool IsGroupable(this Expression expression)
internal static bool IsGroupable(this Expression expression)
{
return expression.NodeType == ExpressionType.AndAlso || expression.NodeType == ExpressionType.OrElse;
}
Expand All @@ -59,7 +59,7 @@ public static bool IsGroupable(this Expression expression)
/// </summary>
/// <param name="expression">The instance of <see cref="Expression"/> object to be identified.</param>
/// <returns>Returns true if the expression is using the <see cref="Math"/> object operations.</returns>
public static bool IsMathematical(this Expression expression)
internal static bool IsMathematical(this Expression expression)
{
return m_mathematicalExpressionTypes.Contains(expression.NodeType);
}
Expand Down
56 changes: 28 additions & 28 deletions RepoDb.Core/RepoDb/Extensions/StringExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,22 @@ internal static string AsAlphaNumeric(this string value, bool trim = false)
}

/// <summary>
/// Removes the database quotes from the string.
/// Unquotes a string.
/// </summary>
/// <param name="value">The string value where the database quotes will be removed.</param>
/// <param name="value">The string value to be unqouted.</param>
/// <returns>The unquoted string.</returns>
public static string AsUnquoted(this string value)
{
return Regex.Replace(value, @"[\[\]']+", "");
}

/// <summary>
/// Unquotes a string.
/// </summary>
/// <param name="value">The string value to be unqouted.</param>
/// <param name="trim">The boolean value that indicates whether to trim the string before unquoting.</param>
/// <param name="separator">The separator in which the quotes will be removed.</param>
/// <returns>The quoted string.</returns>
/// <returns>The unquoted string.</returns>
public static string AsUnquoted(this string value, bool trim = false, string separator = ".")
{
if (trim)
Expand All @@ -62,19 +72,27 @@ public static string AsUnquoted(this string value, bool trim = false, string sep
}

/// <summary>
/// Remove the quotes from the string.
/// Quotes a string.
/// </summary>
/// <param name="value">The string value where the database quotes will be removed.</param>
/// <returns></returns>
private static string AsUnquoted(this string value)
/// <param name="value">The string value to be quoted.</param>
/// <returns>The quoted string.</returns>
public static string AsQuoted(this string value)
{
return Regex.Replace(value, @"[\[\]']+", "");
if (!value.StartsWith("["))
{
value = string.Concat("[", value);
}
if (!value.EndsWith("]"))
{
value = string.Concat(value, "]");
}
return value;
}

/// <summary>
/// Adds a quotes to the string.
/// Quotes a string.
/// </summary>
/// <param name="value">The string value where the database quotes will be added.</param>
/// <param name="value">The string value to be quoted.</param>
/// <param name="trim">The boolean value that indicates whether to trim the string before quoting.</param>
/// <param name="separator">The separator in which the quotes will be placed.</param>
/// <returns>The quoted string.</returns>
Expand All @@ -95,24 +113,6 @@ public static string AsQuoted(this string value, bool trim = false, string separ
}
}

/// <summary>
/// Add the quotes into the string.
/// </summary>
/// <param name="value">The string value where the database quotes will be added.</param>
/// <returns></returns>
private static string AsQuoted(this string value)
{
if (!value.StartsWith("["))
{
value = string.Concat("[", value);
}
if (!value.EndsWith("]"))
{
value = string.Concat(value, "]");
}
return value;
}

// AsEnumerable
internal static IEnumerable<string> AsEnumerable(this string value)
{
Expand Down
Loading

0 comments on commit dac6ee8

Please sign in to comment.