Skip to content

Commit

Permalink
Replace synchronous .Result to be async await
Browse files Browse the repository at this point in the history
  • Loading branch information
ManhOptimizely committed Dec 16, 2024
1 parent c7d89c8 commit 3e44362
Show file tree
Hide file tree
Showing 16 changed files with 174 additions and 171 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ namespace EPiServer.ContentGraph.IntegrationTests.QueryTests
public class BoolFilterTest : IntegrationFixture
{
[ClassInitialize]
public static void ClassInitialize(TestContext testContext)
public static async Task ClassInitialize(TestContext testContext)
{
var item1 = TestDataCreator.generateIndexActionJson("1", "en", new IndexActionData { ContentType = new[] { "HomePage" }, Id = "content1", NameSearchable = "Home 1", Status = TestDataCreator.STATUS_PUBLISHED, Priority = 100, RolesWithReadAccess = TestDataCreator.ROLES_EVERYONE, StartPublish = DateTime.Parse("2022-10-11T17:17:56Z",null,System.Globalization.DateTimeStyles.AdjustToUniversal) });
var item2 = TestDataCreator.generateIndexActionJson("2", "en", new IndexActionData { ContentType = new[] { "HomePage" }, Id = "content2", NameSearchable = "Home 2", Status = TestDataCreator.STATUS_PUBLISHED, Priority = 100, RolesWithReadAccess = TestDataCreator.ROLES_EVERYONE, StartPublish = DateTime.Parse("2022-10-12T17:17:56Z", null, System.Globalization.DateTimeStyles.AdjustToUniversal) });
var item3 = TestDataCreator.generateIndexActionJson("3", "en", new IndexActionData { ContentType = new[] { "HomePage" }, Id = "content3", NameSearchable = "Not exists priority", Status = TestDataCreator.STATUS_PUBLISHED, RolesWithReadAccess = TestDataCreator.ROLES_EVERYONE, StartPublish = DateTime.Parse("2022-10-13T17:17:56Z", null, System.Globalization.DateTimeStyles.AdjustToUniversal) });
var item4 = TestDataCreator.generateIndexActionJson("4", "en", new IndexActionData { ContentType = new[] { "HomePage" }, Id = "content4", NameSearchable = "Home 4", Status = TestDataCreator.STATUS_PUBLISHED, Priority = 300, RolesWithReadAccess = TestDataCreator.ROLES_EVERYONE, StartPublish = DateTime.Parse("2022-10-14T17:17:56Z", null, System.Globalization.DateTimeStyles.AdjustToUniversal) });

SetupData<HomePage>(item1 + item2 + item3 + item4, "t1");
await SetupData<HomePage>(item1 + item2 + item3 + item4, "t1");
}
#region And filter
[TestMethod]
public void search_single_and_filter_with_priority_100_should_return_2_items()
public async Task search_single_and_filter_with_priority_100_should_return_2_items()
{
IFilter andFilter = new AndFilter<HomePage>()
.And(x => x.Priority, new NumericFilterOperators().Eq(100));
Expand All @@ -33,11 +33,11 @@ public void search_single_and_filter_with_priority_100_should_return_2_items()
.Where(andFilter)
.ToQuery()
.BuildQueries();
var rs = query.GetResultAsync<HomePage>().Result;
var rs = await query.GetResultAsync<HomePage>();
Assert.IsTrue(rs.Content.Hits.Count().Equals(2), "Expected 2 items for single AND filter with priority 100, but found " + rs.Content.Hits.Count() + ".");
}
[TestMethod]
public void search_2_conditions_in_and_filter_should_return_1_item()
public async Task search_2_conditions_in_and_filter_should_return_1_item()
{
IFilter andFilter = new AndFilter<HomePage>()
.And(x => x.Priority, new NumericFilterOperators().Eq(100))
Expand All @@ -49,15 +49,15 @@ public void search_2_conditions_in_and_filter_should_return_1_item()
.Where(andFilter)
.ToQuery()
.BuildQueries();
var rs = query.GetResultAsync<HomePage>().Result;
var rs = await query.GetResultAsync<HomePage>();
Assert.IsTrue(rs.Content.Hits.Count().Equals(1));
Assert.IsTrue(rs.Content.Hits.First().Name.Equals("Home 2"));
}

#endregion
#region Or filter
[TestMethod]
public void search_single_or_filter_with_priority_100_should_return_2_items()
public async Task search_single_or_filter_with_priority_100_should_return_2_items()
{
IFilter orFilter = new OrFilter<HomePage>()
.Or(x => x.Priority, new NumericFilterOperators().Eq(100));
Expand All @@ -68,11 +68,11 @@ public void search_single_or_filter_with_priority_100_should_return_2_items()
.Where(orFilter)
.ToQuery()
.BuildQueries();
var rs = query.GetResultAsync<HomePage>().Result;
var rs = await query.GetResultAsync<HomePage>();
Assert.IsTrue(rs.Content.Hits.Count().Equals(2), "Expected 2 items for single OR filter with priority 100, but found " + rs.Content.Hits.Count() + ".");
}
[TestMethod]
public void search_2_conditions_in_or_filter_should_return_3_items()
public async Task search_2_conditions_in_or_filter_should_return_3_items()
{
IFilter orFilter = new OrFilter<HomePage>()
.Or(x => x.Priority, new NumericFilterOperators().Eq(100))
Expand All @@ -84,13 +84,13 @@ public void search_2_conditions_in_or_filter_should_return_3_items()
.Where(orFilter)
.ToQuery()
.BuildQueries();
var rs = query.GetResultAsync<HomePage>().Result;
var rs = await query.GetResultAsync<HomePage>();
Assert.IsTrue(rs.Content.Hits.Count().Equals(3), "Expected 3 items for OR filter with 2 conditions, but found " + rs.Content.Hits.Count() + ".");
}
#endregion
#region Not filter
[TestMethod]
public void search_single_not_filter_with_priority_100_should_return_2_items()
public async Task search_single_not_filter_with_priority_100_should_return_2_items()
{
IFilter notFilter = new NotFilter<HomePage>()
.Not(x => x.Priority, new NumericFilterOperators().Eq(100));
Expand All @@ -101,11 +101,11 @@ public void search_single_not_filter_with_priority_100_should_return_2_items()
.Where(notFilter)
.ToQuery()
.BuildQueries();
var rs = query.GetResultAsync<HomePage>().Result;
var rs = await query.GetResultAsync<HomePage>();
Assert.IsTrue(rs.Content.Hits.Count().Equals(2), "Expected 2 items for single NOT filter with priority 100, but found " + rs.Content.Hits.Count() + ".");
}
[TestMethod]
public void search_2_conditions_in_not_filter_should_return_2_items()
public async Task search_2_conditions_in_not_filter_should_return_2_items()
{
IFilter notFilter = new NotFilter<HomePage>()
.Not(x => x.Priority, new NumericFilterOperators().Eq(100))
Expand All @@ -117,14 +117,14 @@ public void search_2_conditions_in_not_filter_should_return_2_items()
.Where(notFilter)
.ToQuery()
.BuildQueries();
var rs = query.GetResultAsync<HomePage>().Result;
var rs = await query.GetResultAsync<HomePage>();
Assert.IsTrue(rs.Content.Hits.First().Name.Equals("Home 4"), "Expected 'Home 4' to match NOT filter with 2 conditions, but found '" + rs.Content.Hits.First().Name + "'.");
}
#endregion

#region more complex boolean queries
[TestMethod]
public void search_combine_3_boolean_filters_should_return_3_items()
public async Task search_combine_3_boolean_filters_should_return_3_items()
{
// expect 1 item missing Priority field
OrFilter<HomePage> orFilter = new OrFilter<HomePage>()
Expand All @@ -146,7 +146,7 @@ public void search_combine_3_boolean_filters_should_return_3_items()
.Where(orFilter | andFilter1 | andFilter2)
.ToQuery()
.BuildQueries();
var rs = query.GetResultAsync<HomePage>().Result;
var rs = await query.GetResultAsync<HomePage>();
Assert.IsTrue(rs.Content.Hits.Count().Equals(3), "Expected 3 items when combining 3 boolean filters, but found " + rs.Content.Hits.Count() + ".");
}
#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace EPiServer.ContentGraph.IntegrationTests.QueryTests
public class ComplexQueriesTest : IntegrationFixture
{
[ClassInitialize]
public static void ClassInitialize(TestContext testContext)
public static async Task ClassInitialize(TestContext testContext)
{
var item1 = TestDataCreator.generateIndexActionJson("1", "en", new IndexActionData {
ContentType = new[] { "Content" }, TypeName = "Content", Id = "content1",
Expand All @@ -28,7 +28,7 @@ public static void ClassInitialize(TestContext testContext)
MainBodySearchable = "Semantic search is supported on searchable string fields, and for the full-text search operators contains and match. It is recommended to set fields that have a lot of content (such as the MainBody in the Optimizely CMS) as searchable to unlock the full-text search capabilities. Optimizely Graph uses a pre-trained model for semantic search.",
Priority = 0, IsSecret = false, Status = TestDataCreator.STATUS_PUBLISHED, RolesWithReadAccess = TestDataCreator.ROLES_EVERYONE });

SetupData<HomePage>(item1 + item2 + item3, "t2");
await SetupData<HomePage>(item1 + item2 + item3, "t2");
}

[TestCategory("Subtype test")]
Expand All @@ -38,7 +38,7 @@ public void given_1_parent_and_2_children_objects_search_response_should_returns
IQuery query = new GraphQueryBuilder(_configOptions, _httpClientFactory)
.ForType<Content>()
.Fields(x => x.Name)
.AsType<HomePage>(x=>x.MainBody)
.InlineFragment<HomePage>(x=>x.MainBody)
.ToQuery()
.BuildQueries();
var rs = query.GetResultAsync().Result;
Expand All @@ -52,7 +52,7 @@ public void given_1_parent_and_2_children_objects_search_response_should_returns
IQuery query = new GraphQueryBuilder(_configOptions, _httpClientFactory)
.ForType<Content>()
.Fields(x => x.Name)
.AsType<HomePage>(x => x.MainBody)
.InlineFragment<HomePage>(x => x.MainBody)
.ToQuery()
.BuildQueries();
var rs = query.GetResultAsync().Result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ namespace EPiServer.ContentGraph.IntegrationTests.QueryTests
public class FacetsTest : IntegrationFixture
{
[ClassInitialize]
public static void ClassInitialize(TestContext testContext)
public static async Task ClassInitialize(TestContext testContext)
{
var item1 = TestDataCreator.generateIndexActionJson("1", "en", new IndexActionData { ContentType = new[] { "HomePage" }, Id = "content1", NameSearchable = "Home 1", Priority = 100, IsSecret = true, Status = TestDataCreator.STATUS_PUBLISHED, RolesWithReadAccess = TestDataCreator.ROLES_EVERYONE });
var item2 = TestDataCreator.generateIndexActionJson("2", "en", new IndexActionData { ContentType = new[] { "HomePage" }, Id = "content2", NameSearchable = "Home 2", Priority = 100, IsSecret = true, Status = TestDataCreator.STATUS_PUBLISHED, RolesWithReadAccess = TestDataCreator.ROLES_EVERYONE });
var item3 = TestDataCreator.generateIndexActionJson("3", "en", new IndexActionData { ContentType = new[] { "HomePage" }, Id = "content3", NameSearchable = "Not exists priority", IsSecret = false, Status = TestDataCreator.STATUS_PUBLISHED, RolesWithReadAccess = TestDataCreator.ROLES_EVERYONE });
var item4 = TestDataCreator.generateIndexActionJson("4", "en", new IndexActionData { ContentType = new[] { "HomePage" }, Id = "content4", NameSearchable = "Home 4", Priority = 300, IsSecret = false, Status = TestDataCreator.STATUS_PUBLISHED, RolesWithReadAccess = TestDataCreator.ROLES_EVERYONE });

SetupData<HomePage>(item1 + item2 + item3 + item4, "t3");
await SetupData<HomePage>(item1 + item2 + item3 + item4, "t3");
}
[TestMethod]
public void search_with_string_facet_should_return_2_facets()
public async Task search_with_string_facet_should_return_2_facets()
{
IQuery query = new GraphQueryBuilder(_configOptions, _httpClientFactory)
.ForType<HomePage>()
Expand All @@ -33,15 +33,15 @@ public void search_with_string_facet_should_return_2_facets()
.OrderType(OrderType.VALUE))
.ToQuery()
.BuildQueries();
var rs = query.GetResultAsync<HomePage>().Result;
var rs = await query.GetResultAsync<HomePage>();
Assert.IsTrue(rs.Content.Facets["IsSecret"].First().Count.Equals(2), "Expected 2 facets for 'IsSecret' with value 'true', but found " + rs.Content.Facets["IsSecret"].First().Count + ".");
Assert.IsTrue(rs.Content.Facets["IsSecret"].First().Name.Equals("true"), "Expected first facet name to be 'true', but found '" + rs.Content.Facets["IsSecret"].First().Name + "'.");
Assert.IsTrue(rs.Content.Facets["IsSecret"].Last().Count.Equals(2), "Expected 2 facets for 'IsSecret' with value 'false', but found " + rs.Content.Facets["IsSecret"].Last().Count + ".");
Assert.IsTrue(rs.Content.Facets["IsSecret"].Last().Name.Equals("false"), "Expected last facet name to be 'false', but found '" + rs.Content.Facets["IsSecret"].Last().Name + "'.");
}

[TestMethod]
public void search_with_facet_in_2_ranges_should_return_2_facets()
public async Task search_with_facet_in_2_ranges_should_return_2_facets()
{
IQuery query = new GraphQueryBuilder(_configOptions, _httpClientFactory)
.ForType<HomePage>()
Expand All @@ -50,29 +50,29 @@ public void search_with_facet_in_2_ranges_should_return_2_facets()
.Projection(FacetProperty.name, FacetProperty.count))
.ToQuery()
.BuildQueries();
var rs = query.GetResultAsync<HomePage>().Result;
var rs = await query.GetResultAsync<HomePage>();
Assert.IsTrue(rs.Content.Facets["Priority"].Count().Equals(2), "Expected 2 facets for 'Priority', but found " + rs.Content.Facets["Priority"].Count() + ".");
Assert.IsTrue(rs.Content.Facets["Priority"].First().Name.Equals("[100,200)"), "Expected first priority facet to be in range [100,200), but found '" + rs.Content.Facets["Priority"].First().Name + "'.");
Assert.IsTrue(rs.Content.Facets["Priority"].First().Count.Equals(2), "Expected 2 items in the first priority range, but found " + rs.Content.Facets["Priority"].First().Count + ".");
Assert.IsTrue(rs.Content.Facets["Priority"].Last().Name.Equals("[200,300)"), "Expected last priority facet to be in range [200,300), but found '" + rs.Content.Facets["Priority"].Last().Name + "'.");
Assert.IsTrue(rs.Content.Facets["Priority"].Last().Count.Equals(0), "Expected 0 items in the last priority range, but found " + rs.Content.Facets["Priority"].Last().Count + ".");
}
[TestMethod]
public void search_with_facet_filters_should_return_correct_items()
public async Task search_with_facet_filters_should_return_correct_items()
{
IQuery query = new GraphQueryBuilder(_configOptions, _httpClientFactory)
.ForType<HomePage>()
.Field(x=>x.IsSecret)
.Facet(x => x.IsSecret, new StringFacetFilterOperators().Filters("true"))
.ToQuery()
.BuildQueries();
var rs = query.GetResultAsync<HomePage>().Result;
var rs = await query.GetResultAsync<HomePage>();
Assert.IsTrue(rs.Content.Facets["IsSecret"].Count().Equals(2), "Expected 2 facets for 'IsSecret', but found " + rs.Content.Facets["IsSecret"].Count() + ".");
Assert.IsTrue(rs.Content.Hits.Count().Equals(2), "Expected 2 items when filtering by 'IsSecret'='true', but found " + rs.Content.Hits.Count() + ".");
Assert.IsTrue(rs.Content.Hits.Select(x=>x.IsSecret).ToList().TrueForAll(x => x));
}
[TestMethod]
public void search_with_2_facet_should_return_2_facets()
public async Task search_with_2_facet_should_return_2_facets()
{
IQuery query = new GraphQueryBuilder(_configOptions, _httpClientFactory)
.ForType<HomePage>()
Expand All @@ -81,21 +81,21 @@ public void search_with_2_facet_should_return_2_facets()
.Facet(x => x.Status, new StringFacetFilterOperators().Filters(TestDataCreator.STATUS_PUBLISHED))
.ToQuery()
.BuildQueries();
var rs = query.GetResultAsync<HomePage>().Result;
var rs = await query.GetResultAsync<HomePage>();
Assert.IsTrue(rs.Content.Facets.Count.Equals(2), "Expected 2 facets, but found " + rs.Content.Facets.Count + ".");
Assert.IsNotNull(rs.Content.Facets["IsSecret"], "Expected 'IsSecret' facet to be present, but it was not found.");
Assert.IsNotNull(rs.Content.Facets["Status"], "Expected 'Status' facet to be present, but it was not found.");
}
[TestMethod]
public void search_with_facet_limit_1_should_return_facet_count_equals_1()
public async Task search_with_facet_limit_1_should_return_facet_count_equals_1()
{
IQuery query = new GraphQueryBuilder(_configOptions, _httpClientFactory)
.ForType<HomePage>()
.Facet(x => x.IsSecret, new StringFacetFilterOperators().Filters("true").Limit(1))
.Facet(x => x.Status, new StringFacetFilterOperators().Filters(TestDataCreator.STATUS_PUBLISHED))
.ToQuery()
.BuildQueries();
var rs = query.GetResultAsync<HomePage>().Result;
var rs = await query.GetResultAsync<HomePage>();
Assert.IsTrue(rs.Content.Facets.Count.Equals(2));
Assert.IsNotNull(rs.Content.Facets["IsSecret"]);
Assert.IsNotNull(rs.Content.Facets["Status"]);
Expand Down
Loading

0 comments on commit 3e44362

Please sign in to comment.