|
| 1 | +using EpiServer.ContentGraph.UnitTests.QueryTypeObjects; |
| 2 | +using EPiServer.ContentGraph.Api.Filters; |
| 3 | +using EPiServer.ContentGraph.Api.Querying; |
| 4 | +using Xunit; |
| 5 | + |
| 6 | +namespace EpiServer.ContentGraph.UnitTests |
| 7 | +{ |
| 8 | + public class GenerateLinkQueryTests |
| 9 | + { |
| 10 | + private TypeQueryBuilder<RequestTypeObject> typeQueryBuilder; |
| 11 | + public GenerateLinkQueryTests() |
| 12 | + { |
| 13 | + typeQueryBuilder = new TypeQueryBuilder<RequestTypeObject>(); |
| 14 | + } |
| 15 | + |
| 16 | + [Fact] |
| 17 | + public void LinkQueryTests() |
| 18 | + { |
| 19 | + string childQuery = "SubTypeObject(where:{SubProperty:{match: \"test\"}}){items{SubProperty} facets{Property3{name count}}}"; |
| 20 | + string expectedFields = $"items{{Property1 Property2 _link(type:CUSTOMERREFERENCES) {{{childQuery}}}}}"; |
| 21 | + string expectedFacets = @"facets{Property3{NestedProperty{name count}}}"; |
| 22 | + var linkQuery = new LinkQueryBuilder<SubTypeObject>("CUSTOMERREFERENCES") |
| 23 | + .Field(x => x.SubProperty) |
| 24 | + .Where(x => x.SubProperty, new StringFilterOperators().Match("test")) |
| 25 | + .Facet(x => x.Property3); |
| 26 | + |
| 27 | + typeQueryBuilder |
| 28 | + .Field(x => x.Property1) |
| 29 | + .Field(x => x.Property2) |
| 30 | + .Link(linkQuery) |
| 31 | + .Facet(x => x.Property3.NestedProperty); |
| 32 | + GraphQueryBuilder query = typeQueryBuilder.ToQuery(); |
| 33 | + |
| 34 | + Assert.NotNull(query.GetQuery()); |
| 35 | + Assert.Contains(expectedFacets, query.GetQuery().Query); |
| 36 | + Assert.Contains(expectedFields, query.GetQuery().Query); |
| 37 | + Assert.Equal($"RequestTypeObject{{{expectedFields} {expectedFacets}}}", query.GetQuery().Query); |
| 38 | + } |
| 39 | + |
| 40 | + [Fact] |
| 41 | + public void multiple_links_query_should_generate_correct_query() |
| 42 | + { |
| 43 | + string expectedLink1 = "SubTypeObject(where:{SubProperty:{match: \"test1\"}}){items{SubProperty} facets{Property3{name count}}}"; |
| 44 | + string expectedLink2 = "SubTypeObject(where:{Property1:{match: \"test2\"}}){items{Property1} facets{Property3{name count}}}"; |
| 45 | + string expectedFields = $"items{{Property1 Property2 _link(type:CUSTOMERREFERENCES) {{{expectedLink1}}} _link(type:DEFAULT) {{{expectedLink2}}}}}"; |
| 46 | + string expectedFacets = @"facets{Property3{NestedProperty{name count}}}"; |
| 47 | + var linkQuery1 = new LinkQueryBuilder<SubTypeObject>("CUSTOMERREFERENCES") |
| 48 | + .Field(x => x.SubProperty) |
| 49 | + .Where(x => x.SubProperty, new StringFilterOperators().Match("test1")) |
| 50 | + .Facet(x => x.Property3); |
| 51 | + |
| 52 | + var linkQuery2 = new LinkQueryBuilder<SubTypeObject>("DEFAULT") |
| 53 | + .Field(x => x.Property1) |
| 54 | + .Where(x => x.Property1, new StringFilterOperators().Match("test2")) |
| 55 | + .Facet(x => x.Property3); |
| 56 | + |
| 57 | + typeQueryBuilder |
| 58 | + .Field(x => x.Property1) |
| 59 | + .Field(x => x.Property2) |
| 60 | + .Link(linkQuery1) |
| 61 | + .Link(linkQuery2) |
| 62 | + .Facet(x => x.Property3.NestedProperty); |
| 63 | + GraphQueryBuilder query = typeQueryBuilder.ToQuery(); |
| 64 | + |
| 65 | + Assert.Equal(linkQuery1.GetQuery().Query, expectedLink1); |
| 66 | + Assert.Equal(linkQuery2.GetQuery().Query, expectedLink2); |
| 67 | + |
| 68 | + Assert.Contains(expectedFacets, query.GetQuery().Query); |
| 69 | + Assert.Contains(expectedFields, query.GetQuery().Query); |
| 70 | + Assert.Equal($"RequestTypeObject{{{expectedFields} {expectedFacets}}}", query.GetQuery().Query); |
| 71 | + } |
| 72 | + |
| 73 | + [Fact] |
| 74 | + public void nested_links_query_should_generate_nested_link_query() |
| 75 | + { |
| 76 | + string expectedLink1 = "SubTypeObject(where:{SubProperty:{match: \"test1\"}}){items{SubProperty} facets{Property3{name count}}}"; |
| 77 | + string expectedLink2 = "SubTypeObject(where:{Property1:{match: \"test2\"}}){items{Property1 _link(type:CUSTOMERREFERENCES) {" + expectedLink1 + "}} facets{Property3{name count}}}"; |
| 78 | + string expectedFields = $"items{{Property1 Property2 _link(type:DEFAULT) {{{expectedLink2}}}}}"; |
| 79 | + string expectedFacets = @"facets{Property3{NestedProperty{name count}}}"; |
| 80 | + var linkQuery1 = new LinkQueryBuilder<SubTypeObject>() |
| 81 | + .WithLinkType("CUSTOMERREFERENCES") |
| 82 | + .Field(x => x.SubProperty) |
| 83 | + .Where(x => x.SubProperty, new StringFilterOperators().Match("test1")) |
| 84 | + .Facet(x => x.Property3); |
| 85 | + |
| 86 | + var linkQuery2 = new LinkQueryBuilder<SubTypeObject>("DEFAULT") |
| 87 | + .Field(x => x.Property1) |
| 88 | + .Where(x => x.Property1, new StringFilterOperators().Match("test2")) |
| 89 | + .Facet(x => x.Property3) |
| 90 | + .Link(linkQuery1); |
| 91 | + |
| 92 | + typeQueryBuilder |
| 93 | + .Field(x => x.Property1) |
| 94 | + .Field(x => x.Property2) |
| 95 | + .Link(linkQuery2) |
| 96 | + .Facet(x => x.Property3.NestedProperty); |
| 97 | + GraphQueryBuilder query = typeQueryBuilder.ToQuery(); |
| 98 | + |
| 99 | + Assert.Equal(linkQuery1.GetQuery().Query, expectedLink1); |
| 100 | + Assert.Equal(linkQuery2.GetQuery().Query, expectedLink2); |
| 101 | + |
| 102 | + Assert.Contains(expectedFacets, query.GetQuery().Query); |
| 103 | + Assert.Contains(expectedFields, query.GetQuery().Query); |
| 104 | + Assert.Equal($"RequestTypeObject{{{expectedFields} {expectedFacets}}}", query.GetQuery().Query); |
| 105 | + } |
| 106 | + [Fact] |
| 107 | + public void nested_link_query_with_aliases() |
| 108 | + { |
| 109 | + string expectedLink1 = "SubTypeObject(where:{SubProperty:{match: \"test1\"}}){items{SubProperty} facets{Property3{name count}}}"; |
| 110 | + string expectedLink2 = "SubTypeObject(where:{Property1:{match: \"test2\"}}){items{Property1 mylink1:_link(type:CUSTOMERREFERENCES) {" + expectedLink1 + "}} facets{Property3{name count}}}"; |
| 111 | + string expectedFields = $"items{{Property1 Property2 mylink2:_link(type:DEFAULT) {{{expectedLink2}}}}}"; |
| 112 | + string expectedFacets = @"facets{Property3{NestedProperty{name count}}}"; |
| 113 | + var linkQuery1 = new LinkQueryBuilder<SubTypeObject>() |
| 114 | + .WithLinkType("CUSTOMERREFERENCES") |
| 115 | + .Field(x => x.SubProperty) |
| 116 | + .Where(x => x.SubProperty, new StringFilterOperators().Match("test1")) |
| 117 | + .Facet(x => x.Property3); |
| 118 | + |
| 119 | + var linkQuery2 = new LinkQueryBuilder<SubTypeObject>("DEFAULT") |
| 120 | + .Field(x => x.Property1) |
| 121 | + .Where(x => x.Property1, new StringFilterOperators().Match("test2")) |
| 122 | + .Facet(x => x.Property3) |
| 123 | + .Link(linkQuery1, "mylink1"); |
| 124 | + |
| 125 | + typeQueryBuilder |
| 126 | + .Field(x => x.Property1) |
| 127 | + .Field(x => x.Property2) |
| 128 | + .Link(linkQuery2, "mylink2") |
| 129 | + .Facet(x => x.Property3.NestedProperty); |
| 130 | + GraphQueryBuilder query = typeQueryBuilder.ToQuery(); |
| 131 | + |
| 132 | + Assert.Equal(linkQuery1.GetQuery().Query, expectedLink1); |
| 133 | + Assert.Equal(linkQuery2.GetQuery().Query, expectedLink2); |
| 134 | + |
| 135 | + Assert.Contains(expectedFacets, query.GetQuery().Query); |
| 136 | + Assert.Contains(expectedFields, query.GetQuery().Query); |
| 137 | + Assert.Equal($"RequestTypeObject{{{expectedFields} {expectedFacets}}}", query.GetQuery().Query); |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments