Skip to content

Commit c5c54ec

Browse files
committed
Reduce Duplicate Code
1 parent 8f775d4 commit c5c54ec

File tree

3 files changed

+285
-452
lines changed

3 files changed

+285
-452
lines changed

src/Service.Tests/SqlTests/GraphQLFilterTests/GraphQLFilterTestBase.cs

Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,10 +705,265 @@ public async Task TestPassingVariablesToAndField()
705705
SqlTestHelper.PerformTestEqualJsonStrings(expected, actual.ToString());
706706
}
707707

708+
/// <summary>
709+
/// Test Nested Filter for Many-One relationship.
710+
/// </summary>
711+
[TestMethod]
712+
public async Task TestNestedFilterManyOne(string existsPredicate)
713+
{
714+
string graphQLQueryName = "comics";
715+
// Gets all the comics that have their series name = 'Foundation'
716+
string gqlQuery = @"{
717+
comics (" + QueryBuilder.FILTER_FIELD_NAME + ": {" +
718+
@"myseries: { name: { eq: ""Foundation"" }}})
719+
{
720+
items {
721+
id
722+
title
723+
}
724+
}
725+
}";
726+
727+
string dbQuery = MakeQueryOn(
728+
table: "comics",
729+
queriedColumns: new List<string> { "id", "title" },
730+
existsPredicate,
731+
GetDefaultSchema());
732+
733+
JsonElement actual = await ExecuteGraphQLRequestAsync(
734+
gqlQuery,
735+
graphQLQueryName,
736+
isAuthenticated: false);
737+
string expected = await GetDatabaseResultAsync(dbQuery);
738+
SqlTestHelper.PerformTestEqualJsonStrings(expected, actual.ToString());
739+
}
740+
741+
/// <summary>
742+
/// Test Nested Filter for One-Many relationship
743+
/// </summary>
744+
[TestMethod]
745+
public async Task TestNestedFilterOneMany(string existsPredicate)
746+
{
747+
string graphQLQueryName = "series";
748+
// Gets the series that have comics with categoryName containing Fairy
749+
string gqlQuery = @"{
750+
series (" + QueryBuilder.FILTER_FIELD_NAME +
751+
@": { comics: { categoryName: { contains: ""Fairy"" }}} )
752+
{
753+
items {
754+
id
755+
name
756+
}
757+
}
758+
}";
759+
760+
string dbQuery = MakeQueryOn(
761+
table: "series",
762+
queriedColumns: new List<string> { "id", "name" },
763+
existsPredicate,
764+
GetDefaultSchema());
765+
766+
JsonElement actual = await ExecuteGraphQLRequestAsync(
767+
gqlQuery,
768+
graphQLQueryName,
769+
isAuthenticated: false);
770+
string expected = await GetDatabaseResultAsync(dbQuery);
771+
SqlTestHelper.PerformTestEqualJsonStrings(expected, actual.ToString());
772+
}
773+
774+
/// <summary>
775+
/// Test Nested Filter for Many-Many relationship
776+
/// </summary>
777+
[TestMethod]
778+
public async Task TestNestedFilterManyMany(string existsPredicate)
779+
{
780+
string graphQLQueryName = "books";
781+
// Gets the books that have been written by Aaron as author
782+
string gqlQuery = @"{
783+
books (" + QueryBuilder.FILTER_FIELD_NAME +
784+
@": { authors : { name: { eq: ""Aaron""}}} )
785+
{
786+
items {
787+
title
788+
}
789+
}
790+
}";
791+
792+
string dbQuery = MakeQueryOn(
793+
table: "books",
794+
queriedColumns: new List<string> { "title" },
795+
existsPredicate,
796+
GetDefaultSchema());
797+
798+
JsonElement actual = await ExecuteGraphQLRequestAsync(
799+
gqlQuery,
800+
graphQLQueryName,
801+
isAuthenticated: false);
802+
string expected = await GetDatabaseResultAsync(dbQuery);
803+
SqlTestHelper.PerformTestEqualJsonStrings(expected, actual.ToString());
804+
}
805+
806+
/// <summary>
807+
/// Test a field of the nested filter is null.
808+
/// </summary>
809+
[TestMethod]
810+
public async Task TestNestedFilterFieldIsNull(string existsPredicate)
811+
{
812+
string graphQLQueryName = "stocks";
813+
// Gets stocks which have a null price.
814+
string gqlQuery = @"{
815+
stocks (" + QueryBuilder.FILTER_FIELD_NAME +
816+
@": { stocks_price: { price: { isNull: true }}} )
817+
{
818+
items {
819+
categoryName
820+
}
821+
}
822+
}";
823+
824+
string dbQuery = MakeQueryOn(
825+
table: "stocks",
826+
queriedColumns: new List<string> { "categoryName" },
827+
existsPredicate,
828+
GetDefaultSchema(),
829+
pkColumns: new List<string> { "categoryid", "pieceid" });
830+
831+
JsonElement actual = await ExecuteGraphQLRequestAsync(
832+
gqlQuery,
833+
graphQLQueryName,
834+
isAuthenticated: false);
835+
string expected = await GetDatabaseResultAsync(dbQuery);
836+
SqlTestHelper.PerformTestEqualJsonStrings(expected, actual.ToString());
837+
}
838+
839+
/// <summary>
840+
/// Tests nested filter having another nested filter.
841+
/// </summary>
842+
[TestMethod]
843+
public async Task TestNestedFilterWithinNestedFilter(string existsPredicate)
844+
{
845+
string graphQLQueryName = "books";
846+
847+
// Gets all the books written by Aaron
848+
// only if the title of one of his books contains 'Awesome'.
849+
string gqlQuery = @"{
850+
books (" + QueryBuilder.FILTER_FIELD_NAME +
851+
@": { authors: {
852+
books: { title: { contains: ""Awesome"" }}
853+
name: { eq: ""Aaron"" }
854+
}} )
855+
{
856+
items {
857+
title
858+
}
859+
}
860+
}";
861+
862+
string dbQuery = MakeQueryOn(
863+
table: "books",
864+
queriedColumns: new List<string> { "title" },
865+
existsPredicate,
866+
GetDefaultSchema());
867+
868+
JsonElement actual = await ExecuteGraphQLRequestAsync(
869+
gqlQuery,
870+
graphQLQueryName,
871+
isAuthenticated: false);
872+
string expected = await GetDatabaseResultAsync(dbQuery);
873+
SqlTestHelper.PerformTestEqualJsonStrings(expected, actual.ToString());
874+
}
875+
876+
/// <summary>
877+
/// Tests nested filter and an AND clause.
878+
/// </summary>
879+
[TestMethod]
880+
public async Task TestNestedFilterWithAnd(string existsPredicate)
881+
{
882+
string graphQLQueryName = "books";
883+
884+
// Gets all the books written by Aniruddh and the publisher is 'Small Town Publisher'.
885+
string gqlQuery = @"{
886+
books (" + QueryBuilder.FILTER_FIELD_NAME +
887+
@": { authors: {
888+
name: { eq: ""Aniruddh""}
889+
}
890+
and: {
891+
publishers: { name: { eq: ""Small Town Publisher"" } }
892+
}
893+
})
894+
{
895+
items {
896+
title
897+
}
898+
}
899+
}";
900+
901+
string dbQuery = MakeQueryOn(
902+
table: "books",
903+
queriedColumns: new List<string> { "title" },
904+
existsPredicate,
905+
GetDefaultSchema());
906+
907+
JsonElement actual = await ExecuteGraphQLRequestAsync(
908+
gqlQuery,
909+
graphQLQueryName,
910+
isAuthenticated: false);
911+
string expected = await GetDatabaseResultAsync(dbQuery);
912+
SqlTestHelper.PerformTestEqualJsonStrings(expected, actual.ToString());
913+
}
914+
915+
/// <summary>
916+
/// Tests nested filter alongwith an OR clause.
917+
/// </summary>
918+
[TestMethod]
919+
public async Task TestNestedFilterWithOr(string existsPredicate)
920+
{
921+
string graphQLQueryName = "books";
922+
923+
// Gets all the books written by Aniruddh OR if their publisher is 'TBD Publishing One'.
924+
string gqlQuery = @"{
925+
books (" + QueryBuilder.FILTER_FIELD_NAME +
926+
@": { or: [{
927+
publishers: { name: { eq: ""TBD Publishing One"" } } }
928+
{ authors : {
929+
name: { eq: ""Aniruddh""}}}
930+
]
931+
})
932+
{
933+
items {
934+
title
935+
}
936+
}
937+
}";
938+
939+
string dbQuery = MakeQueryOn(
940+
table: "books",
941+
queriedColumns: new List<string> { "title" },
942+
existsPredicate,
943+
GetDefaultSchema());
944+
945+
JsonElement actual = await ExecuteGraphQLRequestAsync(
946+
gqlQuery,
947+
graphQLQueryName,
948+
isAuthenticated: false);
949+
string expected = await GetDatabaseResultAsync(dbQuery);
950+
SqlTestHelper.PerformTestEqualJsonStrings(expected, actual.ToString());
951+
}
952+
708953
#endregion
709954

710955
protected abstract string GetDefaultSchema();
711956

957+
/// <summary>
958+
/// Formats the default schema so that it can be
959+
/// place right before the identity that it is qualifying
960+
/// </summary>
961+
protected string GetPreIndentDefaultSchema()
962+
{
963+
string defaultSchema = GetDefaultSchema();
964+
return string.IsNullOrEmpty(defaultSchema) ? string.Empty : defaultSchema + ".";
965+
}
966+
712967
/// <remarks>
713968
/// This function does not escape special characters from column names so those might lead to errors
714969
/// </remarks>

0 commit comments

Comments
 (0)