Skip to content

Commit

Permalink
(GH-448) Add a search criteria for "Contains"
Browse files Browse the repository at this point in the history
  • Loading branch information
Jericho committed Apr 16, 2022
1 parent cea9b3e commit e286570
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using Shouldly;
using StrongGrid.Models.Search.Legacy;
using Xunit;

namespace StrongGrid.UnitTests.Models.Search.Legacy
{
public class SearchCriteriaTests
{
[Fact]
public void SearchCriteriaBetween()
{
// Arrange
var searchCriteria = new SearchCriteriaBetween(EmailActivitiesFilterField.Clicks, 1, 5);

// Act
var result = searchCriteria.ToString();

// Assert
result.ShouldNotBeNull();
result.ShouldBe("clicks BETWEEN 1 AND 5");
}

[Fact]
public void SearchCriteriaIn()
{
// Arrange
var searchCriteria = new SearchCriteriaIn(EmailActivitiesFilterField.Subject, new[] { "Subject1", "Subject2" });

// Act
var result = searchCriteria.ToString();

// Assert
result.ShouldNotBeNull();
result.ShouldBe("subject IN (\"Subject1\",\"Subject2\")");
}

[Fact]
public void SearchCriteriaEqual()
{
// Arrange
var searchCriteria = new SearchCriteriaEqual(EmailActivitiesFilterField.CampaignName, "abc123");

// Act
var result = searchCriteria.ToString();

// Assert
result.ShouldNotBeNull();
result.ShouldBe("campaign_name=\"abc123\"");
}
}
}
18 changes: 9 additions & 9 deletions Source/StrongGrid.UnitTests/Models/Search/SearchCriteriaTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Shouldly;
using StrongGrid.Models.Search.Legacy;
using StrongGrid.Models.Search;
using Xunit;

namespace StrongGrid.UnitTests.Models.Search
Expand All @@ -10,42 +10,42 @@ public class SearchCriteriaTests
public void SearchCriteriaBetween()
{
// Arrange
var searchCriteria = new SearchCriteriaBetween(EmailActivitiesFilterField.Clicks, 1, 5);
var searchCriteria = new SearchCriteriaBetween<ContactsFilterField>(ContactsFilterField.FirstName, "Allen", "Bob");

// Act
var result = searchCriteria.ToString();

// Assert
result.ShouldNotBeNull();
result.ShouldBe("clicks BETWEEN 1 AND 5");
result.ShouldBe("first_name BETWEEN \"Allen\" AND \"Bob\"");
}

[Fact]
public void SearchCriteriaIn()
public void SearchCriteriaEqual()
{
// Arrange
var searchCriteria = new SearchCriteriaIn(EmailActivitiesFilterField.Subject, new[] { "Subject1", "Subject2" });
var searchCriteria = new SearchCriteriaEqual<ContactsFilterField>(ContactsFilterField.LastName, "Smith");

// Act
var result = searchCriteria.ToString();

// Assert
result.ShouldNotBeNull();
result.ShouldBe("subject IN (\"Subject1\",\"Subject2\")");
result.ShouldBe("last_name=\"Smith\"");
}

[Fact]
public void SearchCriteriaEqual()
public void SearchCriteriaContains()
{
// Arrange
var searchCriteria = new SearchCriteriaEqual(EmailActivitiesFilterField.CampaignName, "abc123");
var searchCriteria = new SearchCriteriaContains<ContactsFilterField>(ContactsFilterField.ListIds, "my-list-id");

// Act
var result = searchCriteria.ToString();

// Assert
result.ShouldNotBeNull();
result.ShouldBe("campaign_name=\"abc123\"");
result.ShouldBe("CONTAINS(list_ids,\"my-list-id\")");
}
}
}
8 changes: 7 additions & 1 deletion Source/StrongGrid/Models/Search/SearchComparisonOperator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ public enum SearchComparisonOperator
/// Not Null.
/// </summary>
[EnumMember(Value = "IS NOT NULL")]
IsNotNull
IsNotNull,

/// <summary>
/// Contains.
/// </summary>
[EnumMember(Value = "CONTAINS")]
Contains
}
}
33 changes: 33 additions & 0 deletions Source/StrongGrid/Models/Search/SearchCriteriaContains.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;

namespace StrongGrid.Models.Search
{
/// <summary>
/// Filter the result of a search for a field to contain a value.
/// </summary>
/// <typeparam name="TEnum">The type containing an enum of fields that can used for searching/segmenting.</typeparam>
public class SearchCriteriaContains<TEnum> : SearchCriteria<TEnum>
where TEnum : Enum
{
/// <summary>
/// Initializes a new instance of the <see cref="SearchCriteriaContains{TEnum}"/> class.
/// </summary>
/// <param name="filterField">The filter field.</param>
/// <param name="filterValue">The filter value.</param>
public SearchCriteriaContains(TEnum filterField, object filterValue)
: base(filterField, SearchComparisonOperator.Contains, filterValue)
{
}

/// <summary>
/// Returns a string representation of the search criteria.
/// </summary>
/// <returns>A <see cref="string"/> representation of the search criteria.</returns>
public override string ToString()
{
var filterOperator = ConvertOperatorToString();
var filterValue = ConvertValueToString();
return $"{filterOperator}({FilterField},{filterValue})";
}
}
}

0 comments on commit e286570

Please sign in to comment.