From 2c2eba14ba3cfdd2004d48d62dab166702d16d40 Mon Sep 17 00:00:00 2001 From: Giorgi Dalakishvili Date: Tue, 28 Mar 2017 00:12:17 +0400 Subject: [PATCH] Refactored some query generation tests. --- GraphQLinq.Tests/QueryGenerationTests.cs | 50 ++++++++---------------- GraphQLinq/GraphQuery.cs | 3 ++ 2 files changed, 19 insertions(+), 34 deletions(-) diff --git a/GraphQLinq.Tests/QueryGenerationTests.cs b/GraphQLinq.Tests/QueryGenerationTests.cs index 635a5ca..9e648ba 100644 --- a/GraphQLinq.Tests/QueryGenerationTests.cs +++ b/GraphQLinq.Tests/QueryGenerationTests.cs @@ -15,9 +15,7 @@ public void SelectingSinglePropertyQueryIncludesSelectedProperty() { var locations = context.Locations().Select(l => l.city); - var query = locations.ToString(); - - Assert.That(query, Does.Contain("city")); + Assert.That(locations.Query, Does.Contain("city")); } [Test] @@ -25,9 +23,7 @@ public void SelectingMultiplePropertiesQueryIncludesSelectedProperties() { var locations = context.Locations().Select(l => new { l.city, l.region }); - var query = locations.ToString(); - - Assert.That(query, Does.Contain("city").And.Contains("region")); + Assert.That(locations.Query, Does.Contain("city").And.Contains("region")); } [Test] @@ -35,9 +31,7 @@ public void SelectingNavigationPropertyQueryIncludesPropertiesOfNavigationProper { var locations = context.Locations().Select(l => l.salesPhone); - var query = locations.ToString(); - - Assert.That(query, Does.Contain("number").And.Contains("label")); + Assert.That(locations.Query, Does.Contain("number").And.Contains("label")); } [Test] @@ -45,9 +39,7 @@ public void SelectingListOfStringNavigationPropertyQueryDoesNotIncludesPropertie { var locations = context.Locations().Select(l => l.locationType); - var query = locations.ToString(); - - Assert.That(query, Does.Not.Contain("length").And.Not.Contains("chars")); + Assert.That(locations.Query, Does.Not.Contain("length").And.Not.Contains("chars")); } [Test] @@ -55,9 +47,7 @@ public void SelectingPrimitiveAndNavigationPropertyQueryIncludesPropertiesOfNavi { var locations = context.Locations().Select(l => new { l.salesPhone, l.city }); - var query = locations.ToString(); - - Assert.That(query, Does.Contain("number").And.Contains("label").And.Contains("city")); + Assert.That(locations.Query, Does.Contain("number").And.Contains("label").And.Contains("city")); } [Test] @@ -65,9 +55,7 @@ public void SelectingPropertyQueryIncludesPropertyInCamelCase() { var locations = context.Locations().Select(l => l.Country); - var query = locations.ToString(); - - Assert.That(query, Does.Contain("country")); + Assert.That(locations.Query, Does.Contain("country")); } [Test] @@ -75,9 +63,7 @@ public void SelectingAllPropertiesQueryDoesNotIncludeNavigationProperties() { var locations = context.Locations(); - var query = locations.ToString(); - - Assert.That(query, Does.Not.Contain(nameof(Location.salesPhone)).And.Not.Contains(nameof(Location.emails))); + Assert.That(locations.Query, Does.Not.Contain(nameof(Location.salesPhone)).And.Not.Contains(nameof(Location.emails))); } [Test] @@ -85,9 +71,7 @@ public void IncludingNavigationPropertyQueryIncludesIncludedNavigationPropertyWi { var locations = context.Locations().Include(l => l.salesPhone); - var query = locations.ToString(); - - Assert.That(query, Does.Contain(nameof(Location.salesPhone)) + Assert.That(locations.Query, Does.Contain(nameof(Location.salesPhone)) .And.Contains(nameof(Phone.label)) .And.Contains(nameof(Phone.number))); } @@ -97,21 +81,21 @@ public void IncludingNavigationPropertyWithSpecificPropertyQueryIncludesIncluded { var locations = context.Locations().Include(l => l.salesPhone.Select(p => p.number)); - var query = locations.ToString(); - - Assert.That(query, Does.Contain(nameof(Location.salesPhone)) + Assert.That(locations.Query, Does.Contain(nameof(Location.salesPhone)) .And.Not.Contains(nameof(Phone.label)) .And.Contains(nameof(Phone.number))); } - + [Test] public void FilteringQueryWithScalarParameterGeneratedQueryIncludesPassedParameter() { var locations = context.Locations(openSoon: true).Select(l => l.city); - var query = locations.ToString(); - - Assert.That(query, Does.Contain("openSoon\":true")); + Assert.Multiple(() => + { + Assert.That(locations.Query, Does.Contain("$openSoon: Boolean").And.Contain("openSoon: $openSoon")); + CollectionAssert.Contains(locations.QueryVariables, new KeyValuePair("openSoon", true)); + }); } [Test] @@ -149,9 +133,7 @@ public void SelectingListOfListNestedPropertyQueryShouldNotIncludeListProperties { var agency = hslGraphContext.Agency("232919").Include(a => a.routes.Select(route => route.trips.Select(trip => trip.geometry))); - var query = agency.ToString(); - - Assert.That(query, Does.Not.Contain("capacity").And.Not.Contain("count").And.Not.Contain("item")); + Assert.That(agency.Query, Does.Not.Contain("capacity").And.Not.Contain("count").And.Not.Contain("item")); } } } diff --git a/GraphQLinq/GraphQuery.cs b/GraphQLinq/GraphQuery.cs index 0f5f34f..ea7d069 100644 --- a/GraphQLinq/GraphQuery.cs +++ b/GraphQLinq/GraphQuery.cs @@ -30,6 +30,9 @@ public override string ToString() return lazyQuery.Value.FullQuery; } + public string Query => lazyQuery.Value.Query; + public IReadOnlyDictionary QueryVariables => lazyQuery.Value.Variables; + protected GraphQuery Clone() { var genericQueryType = GetType().GetGenericTypeDefinition();