Skip to content

Commit

Permalink
Modernize tests - NUnit 4
Browse files Browse the repository at this point in the history
  • Loading branch information
maximn committed Oct 9, 2024
1 parent 4c41c6d commit eae8b3a
Show file tree
Hide file tree
Showing 18 changed files with 154 additions and 146 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ packages
\.vs/

GoogleMapsApi/project\.lock\.json

**/appsettings.json
15 changes: 10 additions & 5 deletions GoogleMapsApi.Test/GoogleMapsApi.Test.csproj
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net6.0;</TargetFrameworks>
<TargetFrameworks>net8.0;net6.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<OutputType>Library</OutputType>
<IsPackable>false</IsPackable>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="NUnit" Version="3.13.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="NUnit" Version="4.0.1" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="NUnit.Analyzers" Version="3.10.0" />
<PackageReference Include="coverlet.collector" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
Expand All @@ -18,7 +23,7 @@

<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

Expand Down
58 changes: 29 additions & 29 deletions GoogleMapsApi.Test/IntegrationTests/DirectionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public async Task Directions_SumOfStepDistancesCorrect()
var result = await GoogleMaps.Directions.QueryAsync(request);

AssertInconclusive.NotExceedQuota(result);
Assert.AreEqual(DirectionsStatusCodes.OK, result.Status, result.ErrorMessage);
Assert.Greater(result.Routes.First().Legs.First().Steps.Sum(s => s.Distance.Value), 100);
Assert.That(result.Status, Is.EqualTo(DirectionsStatusCodes.OK), result.ErrorMessage);
Assert.That(result.Routes.First().Legs.First().Steps.Sum(s => s.Distance.Value), Is.GreaterThan(100));
}

[Test]
Expand All @@ -37,9 +37,8 @@ public async Task Directions_ErrorMessage()
var result = await GoogleMaps.Directions.QueryAsync(request);

AssertInconclusive.NotExceedQuota(result);
Assert.AreEqual(DirectionsStatusCodes.REQUEST_DENIED, result.Status);
Assert.IsNotNull (result.ErrorMessage);
Assert.IsNotEmpty (result.ErrorMessage);
Assert.That(result.Status, Is.EqualTo(DirectionsStatusCodes.REQUEST_DENIED));
Assert.That(result.ErrorMessage, Is.Not.Null.And.Not.Empty);
}

[Test]
Expand All @@ -49,11 +48,12 @@ public async Task Directions_WithWayPoints()
var result = await GoogleMaps.Directions.QueryAsync(request);

AssertInconclusive.NotExceedQuota(result);
Assert.AreEqual(DirectionsStatusCodes.OK, result.Status, result.ErrorMessage);
Assert.AreEqual(156097, result.Routes.First().Legs.First().Steps.Sum(s => s.Distance.Value), 10 * 1000);
Assert.That(DirectionsStatusCodes.OK, Is.EqualTo(result.Status), result.ErrorMessage);
Assert.That(156097, Is.EqualTo(result.Routes.First().Legs.First().Steps.Sum(s => s.Distance.Value)).Within(10 * 1000));

StringAssert.Contains("Philadelphia", result.Routes.First().Legs.First().EndAddress);
Assert.That(result.Routes.First().Legs.First().EndAddress, Does.Contain("Philadelphia"));
}

[Test]
public async Task Directions_ExceedingRouteLength()
{
Expand All @@ -79,7 +79,7 @@ public async Task Directions_ExceedingRouteLength()
var result = await GoogleMaps.Directions.QueryAsync(request);

AssertInconclusive.NotExceedQuota(result);
Assert.AreEqual(DirectionsStatusCodes.MAX_ROUTE_LENGTH_EXCEEDED, result.Status, result.ErrorMessage);
Assert.That(DirectionsStatusCodes.MAX_ROUTE_LENGTH_EXCEEDED, Is.EqualTo(result.Status), result.ErrorMessage);
}

[Test]
Expand All @@ -99,9 +99,9 @@ public async Task Directions_Correct_OverviewPath()
OverviewPolyline overviewPath = result.Routes.First().OverviewPath;
OverviewPolyline polyline = result.Routes.First().Legs.First().Steps.First().PolyLine;

Assert.AreEqual(DirectionsStatusCodes.OK, result.Status, result.ErrorMessage);
Assert.AreEqual(122, overviewPath.Points.Count(), 30);
Assert.Greater(polyline.Points.Count(), 1);
Assert.That(DirectionsStatusCodes.OK, Is.EqualTo(result.Status), result.ErrorMessage);
Assert.That(122, Is.EqualTo(overviewPath.Points.Count()).Within(30));
Assert.That(polyline.Points.Count(), Is.GreaterThan(1));
}

[Test]
Expand All @@ -112,8 +112,8 @@ public void DirectionsAsync_SumOfStepDistancesCorrect()
var result = GoogleMaps.Directions.QueryAsync(request).Result;

AssertInconclusive.NotExceedQuota(result);
Assert.AreEqual(DirectionsStatusCodes.OK, result.Status, result.ErrorMessage);
Assert.Greater(result.Routes.First().Legs.First().Steps.Sum(s => s.Distance.Value), 100);
Assert.That(DirectionsStatusCodes.OK, Is.EqualTo(result.Status), result.ErrorMessage);
Assert.That(result.Routes.First().Legs.First().Steps.Sum(s => s.Distance.Value), Is.GreaterThan(100));
}

//The sub_steps differes between google docs documentation and implementation. We use it as google implemented, so we have test to make sure it's not broken.
Expand All @@ -136,7 +136,7 @@ public async Task Directions_VerifysubSteps()
var leg = route.Legs.First();
var step = leg.Steps.First();

Assert.NotNull(step);
Assert.That(step, Is.Not.Null);
}

[Test]
Expand All @@ -156,14 +156,14 @@ public async Task Directions_VerifyBounds()

var route = result.Routes.First();

Assert.NotNull(route);
Assert.NotNull(route.Bounds);
Assert.Greater(route.Bounds.NorthEast.Latitude, 50);
Assert.Greater(route.Bounds.NorthEast.Longitude, 3);
Assert.Greater(route.Bounds.SouthWest.Latitude, 50);
Assert.Greater(route.Bounds.SouthWest.Longitude, 3);
Assert.Greater(route.Bounds.Center.Latitude, 50);
Assert.Greater(route.Bounds.Center.Longitude, 3);
Assert.That(route, Is.Not.Null);
Assert.That(route.Bounds, Is.Not.Null);
Assert.That(route.Bounds.NorthEast.Latitude, Is.GreaterThan(50));
Assert.That(route.Bounds.NorthEast.Longitude, Is.GreaterThan(3));
Assert.That(route.Bounds.SouthWest.Latitude, Is.GreaterThan(50));
Assert.That(route.Bounds.SouthWest.Longitude, Is.GreaterThan(3));
Assert.That(route.Bounds.Center.Latitude, Is.GreaterThan(50));
Assert.That(route.Bounds.Center.Longitude, Is.GreaterThan(3));
}

[Test]
Expand Down Expand Up @@ -192,11 +192,11 @@ public async Task Directions_WithIcons()
var leg = route.Legs.First();
var steps = leg.Steps;

Assert.IsNotEmpty(steps.Where(s =>
Assert.That(steps.Where(s =>
s.TransitDetails?
.Lines?
.Vehicle?
.Icon != null));
.Icon != null), Is.Not.Empty);
}

[Test]
Expand All @@ -219,8 +219,8 @@ public async Task Directions_WithRegionSearch()
DirectionsResponse result = await GoogleMaps.Directions.QueryAsync(request);

AssertInconclusive.NotExceedQuota(result);
Assert.IsNotEmpty(result.Routes);
Assert.True(result.Status.Equals(DirectionsStatusCodes.OK));
Assert.That(result.Routes, Is.Not.Empty);
Assert.That(result.Status.Equals(DirectionsStatusCodes.OK), Is.True);
}

[Test]
Expand All @@ -238,10 +238,10 @@ public async Task Directions_CanGetDurationWithTraffic()
AssertInconclusive.NotExceedQuota(result);

//All legs have duration
Assert.IsTrue(result.Routes.First().Legs.All(l => l.DurationInTraffic != null));
Assert.That(result.Routes.First().Legs.All(l => l.DurationInTraffic != null), Is.True);

//Duration with traffic is usually longer but is not guaranteed
Assert.AreNotEqual(result.Routes.First().Legs.Sum(s => s.Duration.Value.TotalSeconds), result.Routes.First().Legs.Sum(s => s.DurationInTraffic.Value.TotalSeconds));
Assert.That(result.Routes.First().Legs.Sum(s => s.Duration.Value.TotalSeconds), Is.Not.EqualTo(result.Routes.First().Legs.Sum(s => s.DurationInTraffic.Value.TotalSeconds)));
}

[Test]
Expand Down
47 changes: 19 additions & 28 deletions GoogleMapsApi.Test/IntegrationTests/DistanceMatrixTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,12 @@ public async Task ShouldReturnValidValueWhenOneOriginAndOneDestinationsSpeciefed
var result = await GoogleMaps.DistanceMatrix.QueryAsync(request);

AssertInconclusive.NotExceedQuota(result);
Assert.AreEqual(DistanceMatrixStatusCodes.OK, result.Status, result.ErrorMessage);
CollectionAssert.AreEqual(
new [] {"Alter Sirksfelder Weg 10, 23881 Koberg, Germany"},
result.DestinationAddresses);
CollectionAssert.AreEqual(
new[] { "St2154 18, 92726 Waidhaus, Germany" },
result.OriginAddresses);
Assert.AreEqual(DistanceMatrixElementStatusCodes.OK, result.Rows.First().Elements.First().Status);
Assert.IsNotNull(result.Rows.First().Elements.First().Distance);
Assert.IsNotNull(result.Rows.First().Elements.First().Duration);
Assert.That(result.Status, Is.EqualTo(DistanceMatrixStatusCodes.OK), result.ErrorMessage);
Assert.That(result.DestinationAddresses, Is.EqualTo(new[] { "Alter Sirksfelder Weg 10, 23881 Koberg, Germany" }));
Assert.That(result.OriginAddresses, Is.EqualTo(new[] { "St2154 18, 92726 Waidhaus, Germany" }));
Assert.That(result.Rows.First().Elements.First().Status, Is.EqualTo(DistanceMatrixElementStatusCodes.OK));
Assert.That(result.Rows.First().Elements.First().Distance, Is.Not.Null);
Assert.That(result.Rows.First().Elements.First().Duration, Is.Not.Null);
}

[Test]
Expand All @@ -52,16 +48,12 @@ public async Task ShouldReturnValidValueWhenTwoOriginsSpecified()
var result = await GoogleMaps.DistanceMatrix.QueryAsync(request);

AssertInconclusive.NotExceedQuota(result);
Assert.AreEqual(DistanceMatrixStatusCodes.OK, result.Status, result.ErrorMessage);
CollectionAssert.AreEqual(
new[] { "Alter Sirksfelder Weg 10, 23881 Koberg, Germany" },
result.DestinationAddresses);
CollectionAssert.AreEqual(
new[] { "St2154 18, 92726 Waidhaus, Germany", "Böhmerwaldstraße 19, 93444 Bad Kötzting, Germany" },
result.OriginAddresses);
Assert.AreEqual(2, result.Rows.Count());
Assert.AreEqual(DistanceMatrixElementStatusCodes.OK, result.Rows.First().Elements.First().Status);
Assert.AreEqual(DistanceMatrixElementStatusCodes.OK, result.Rows.Last().Elements.First().Status);
Assert.That(DistanceMatrixStatusCodes.OK, Is.EqualTo(result.Status), result.ErrorMessage);
Assert.That(result.DestinationAddresses, Is.EqualTo(new[] { "Alter Sirksfelder Weg 10, 23881 Koberg, Germany" }));
Assert.That(result.OriginAddresses, Is.EqualTo(new[] { "St2154 18, 92726 Waidhaus, Germany", "Böhmerwaldstraße 19, 93444 Bad Kötzting, Germany" }));
Assert.That(2, Is.EqualTo(result.Rows.Count()));
Assert.That(DistanceMatrixElementStatusCodes.OK, Is.EqualTo(result.Rows.First().Elements.First().Status));
Assert.That(DistanceMatrixElementStatusCodes.OK, Is.EqualTo(result.Rows.Last().Elements.First().Status));
}

[Test]
Expand All @@ -78,9 +70,8 @@ public async Task ShouldReturnDurationInTrafficWhenDepartureTimeAndApiKeySpecifi
var result = await GoogleMaps.DistanceMatrix.QueryAsync(request);

AssertInconclusive.NotExceedQuota(result);
Assert.AreEqual(DistanceMatrixStatusCodes.OK, result.Status, result.ErrorMessage);

Assert.IsNotNull(result.Rows.First().Elements.First().DurationInTraffic);
Assert.That(DistanceMatrixStatusCodes.OK, Is.EqualTo(result.Status), result.ErrorMessage);
Assert.That(result.Rows.First().Elements.First().DurationInTraffic, Is.Not.Null);
}

[Test]
Expand Down Expand Up @@ -189,7 +180,7 @@ public async Task ShouldReturnImperialUnitsIfImperialPassedAsParameter()
var result = await GoogleMaps.DistanceMatrix.QueryAsync(request);

AssertInconclusive.NotExceedQuota(result);
Assert.True(result.Rows.First().Elements.First().Distance.Text.Contains("mi"));
Assert.That(result.Rows.First().Elements.First().Distance.Text.Contains("mi"), Is.True);
}

[Test]
Expand All @@ -216,8 +207,8 @@ static Uri onUriCreated(Uri uri)
var result = await GoogleMaps.DistanceMatrix.QueryAsync(request);

AssertInconclusive.NotExceedQuota(result);
Assert.AreEqual(DistanceMatrixStatusCodes.OK, result.Status, result.ErrorMessage);
Assert.AreEqual("1,2", result.OriginAddresses.First());
Assert.That(DistanceMatrixStatusCodes.OK, Is.EqualTo(result.Status), result.ErrorMessage);
Assert.That("1,2", Is.EqualTo(result.OriginAddresses.First()));
}
finally
{
Expand Down Expand Up @@ -246,8 +237,8 @@ public async Task ShouldPassRawDataToOnRawResponseRecivied()
var result = await GoogleMaps.DistanceMatrix.QueryAsync(request);

AssertInconclusive.NotExceedQuota(result);
Assert.AreEqual(DistanceMatrixStatusCodes.OK, result.Status, result.ErrorMessage);
CollectionAssert.IsNotEmpty(rawData);
Assert.That(DistanceMatrixStatusCodes.OK, Is.EqualTo(result.Status), result.ErrorMessage);
Assert.That(rawData, Is.Not.Empty);
}
finally
{
Expand Down
8 changes: 4 additions & 4 deletions GoogleMapsApi.Test/IntegrationTests/ElevationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public async Task Elevation_ReturnsCorrectElevation()
var result = await GoogleMaps.Elevation.QueryAsync(request);

AssertInconclusive.NotExceedQuota(result);
Assert.AreEqual(Entities.Elevation.Response.Status.OK, result.Status);
Assert.AreEqual(16.92, result.Results.First().Elevation, 1.0);
Assert.That(result.Status, Is.EqualTo(Entities.Elevation.Response.Status.OK));
Assert.That(result.Results.First().Elevation, Is.EqualTo(16.92).Within(1.0));
}

[Test]
Expand All @@ -38,8 +38,8 @@ public void ElevationAsync_ReturnsCorrectElevation()
var result = GoogleMaps.Elevation.QueryAsync(request).Result;

AssertInconclusive.NotExceedQuota(result);
Assert.AreEqual(Entities.Elevation.Response.Status.OK, result.Status);
Assert.AreEqual(16.92, result.Results.First().Elevation, 1.0);
Assert.That(Entities.Elevation.Response.Status.OK, Is.EqualTo(result.Status));
Assert.That(16.92, Is.EqualTo(result.Results.First().Elevation).Within(1.0));
}
}
}
Loading

0 comments on commit eae8b3a

Please sign in to comment.