Skip to content
This repository has been archived by the owner on Jul 18, 2023. It is now read-only.

Commit

Permalink
Refactored some query generation tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Giorgi committed Mar 27, 2017
1 parent fe0b8bc commit 2c2eba1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 34 deletions.
50 changes: 16 additions & 34 deletions GraphQLinq.Tests/QueryGenerationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,79 +15,63 @@ 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]
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]
public void SelectingNavigationPropertyQueryIncludesPropertiesOfNavigationProperty()
{
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]
public void SelectingListOfStringNavigationPropertyQueryDoesNotIncludesPropertiesOfNavigationProperty()
{
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]
public void SelectingPrimitiveAndNavigationPropertyQueryIncludesPropertiesOfNavigationProperty()
{
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]
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]
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]
public void IncludingNavigationPropertyQueryIncludesIncludedNavigationPropertyWithNestedProperties()
{
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)));
}
Expand All @@ -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<string, object>("openSoon", true));
});
}

[Test]
Expand Down Expand Up @@ -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"));
}
}
}
3 changes: 3 additions & 0 deletions GraphQLinq/GraphQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public override string ToString()
return lazyQuery.Value.FullQuery;
}

public string Query => lazyQuery.Value.Query;
public IReadOnlyDictionary<string, object> QueryVariables => lazyQuery.Value.Variables;

protected GraphQuery<TR> Clone<TR>()
{
var genericQueryType = GetType().GetGenericTypeDefinition();
Expand Down

0 comments on commit 2c2eba1

Please sign in to comment.