-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use httpclientfactory Add unit tests, separate integration tests Make more friendly to dependency injection Targets .NET Standard 2.0 and .NET 6
- Loading branch information
Showing
19 changed files
with
409 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Nominatim.API.Address; | ||
using Nominatim.API.Interfaces; | ||
using Nominatim.API.Models; | ||
using Nominatim.API.Web; | ||
using NUnit.Framework; | ||
|
||
namespace Nominatim.API.Tests { | ||
public class AddressLookupTests { | ||
private IServiceProvider _serviceProvider; | ||
|
||
[SetUp] | ||
public void Setup() { | ||
var serviceCollection = new ServiceCollection(); | ||
serviceCollection.AddScoped<INominatimWebInterface, NominatimWebInterface>(); | ||
serviceCollection.AddScoped<AddressSearcher>(); | ||
serviceCollection.AddHttpClient(); | ||
_serviceProvider = serviceCollection.BuildServiceProvider(); | ||
} | ||
|
||
[Test] | ||
public async Task TestSuccessfulAddressLookup() { | ||
var addressSearcher = _serviceProvider.GetService<AddressSearcher>(); | ||
|
||
var r = await addressSearcher.Lookup(new AddressSearchRequest { | ||
OSMIDs = new List<string>(new []{ "R146656", "W104393803", "N240109189" }), | ||
BreakdownAddressElements = true, | ||
ShowAlternativeNames = true, | ||
ShowExtraTags = true | ||
}); | ||
|
||
Assert.IsTrue(r.Length > 0); | ||
} | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
src/Nominatim.API.Tests.Integration/GeocoderIntegrationTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Nominatim.API.Geocoders; | ||
using Nominatim.API.Interfaces; | ||
using Nominatim.API.Models; | ||
using Nominatim.API.Web; | ||
using NUnit.Framework; | ||
|
||
namespace Nominatim.API.Tests { | ||
public class GeocoderIntegrationTests { | ||
private IServiceProvider _serviceProvider; | ||
|
||
[SetUp] | ||
public void Setup() { | ||
var serviceCollection = new ServiceCollection(); | ||
serviceCollection.AddScoped<INominatimWebInterface, NominatimWebInterface>(); | ||
serviceCollection.AddScoped<ForwardGeocoder>(); | ||
serviceCollection.AddScoped<ReverseGeocoder>(); | ||
serviceCollection.AddHttpClient(); | ||
_serviceProvider = serviceCollection.BuildServiceProvider(); | ||
} | ||
|
||
[Test] | ||
public async Task TestSuccessfulForwardGeocode() { | ||
var forwardGeocoder = _serviceProvider.GetService<ForwardGeocoder>(); | ||
|
||
var r = await forwardGeocoder.Geocode(new ForwardGeocodeRequest { | ||
queryString = "1600 Pennsylvania Avenue, Washington, DC", | ||
|
||
BreakdownAddressElements = true, | ||
ShowExtraTags = true, | ||
ShowAlternativeNames = true, | ||
ShowGeoJSON = true | ||
}); | ||
|
||
Assert.IsTrue(r.Length > 0); | ||
} | ||
|
||
[Test] | ||
public async Task TestSuccessfulReverseGeocodeBuilding() { | ||
var reverseGeocoder = _serviceProvider.GetService<ReverseGeocoder>(); | ||
|
||
var r2 = await reverseGeocoder.ReverseGeocode(new ReverseGeocodeRequest { | ||
Longitude = -77.0365298, | ||
Latitude = 38.8976763, | ||
|
||
BreakdownAddressElements = true, | ||
ShowExtraTags = true, | ||
ShowAlternativeNames = true, | ||
ShowGeoJSON = true | ||
}); | ||
|
||
Assert.IsTrue(r2.PlaceID > 0); | ||
} | ||
|
||
|
||
[Test] | ||
public async Task TestSuccessfulReverseGeocodeRoad() { | ||
var reverseGeocoder = _serviceProvider.GetService<ReverseGeocoder>(); | ||
|
||
var r3 = await reverseGeocoder.ReverseGeocode(new ReverseGeocodeRequest | ||
{ | ||
Longitude = -58.7051622809683, | ||
Latitude = -34.440723129053, | ||
|
||
BreakdownAddressElements = true, | ||
ShowExtraTags = true, | ||
ShowAlternativeNames = true, | ||
ShowGeoJSON = true | ||
}); | ||
|
||
Assert.IsTrue((r3.PlaceID > 0) && (r3.Category == "highway") && (r3.ClassType == "milestone")); | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/Nominatim.API.Tests.Integration/Nominatim.API.Tests.Integration.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<IsPackable>false</IsPackable> | ||
<Version>2.0.0</Version> | ||
<AssemblyVersion>2.0.0.0</AssemblyVersion> | ||
<FileVersion>2.0.0.0</FileVersion> | ||
<PackageVersion>2.0.0</PackageVersion> | ||
<RootNamespace>Nominatim.API.Tests</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Nominatim.API\Nominatim.API.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Http" Version="6.0.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" /> | ||
<PackageReference Include="NUnit" Version="3.13.3" /> | ||
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,58 @@ | ||
using System.Collections.Generic; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Nominatim.API.Address; | ||
using Nominatim.API.Models; | ||
|
||
namespace Nominatim.API.Tests { | ||
[TestClass] | ||
public class AddressLookupTests { | ||
[TestMethod] | ||
public void TestSuccessfulAddressLookup() { | ||
var x = new AddressSearcher(); | ||
var r = x.Lookup(new AddressSearchRequest { | ||
OSMIDs = new List<string>(new []{ "R146656", "W104393803", "N240109189" }), | ||
BreakdownAddressElements = true, | ||
ShowAlternativeNames = true, | ||
ShowExtraTags = true | ||
}); | ||
r.Wait(); | ||
|
||
Assert.IsTrue(r.Result.Length > 0); | ||
} | ||
} | ||
using Newtonsoft.Json; | ||
using Nominatim.API.Address; | ||
using Nominatim.API.Interfaces; | ||
using Nominatim.API.Models; | ||
using Nominatim.API.Tests.Helpers; | ||
using NSubstitute; | ||
using NUnit.Framework; | ||
|
||
namespace Nominatim.API.Tests; | ||
|
||
[TestFixture] | ||
public class AddressLookupTests { | ||
// Thank you to knom for designing these tests | ||
|
||
private static string baseUrl = @"https://nominatim.openstreetmap.org/lookup"; | ||
|
||
[Test] | ||
public async Task AddressLookupTests_TestSuccessfulAddressLookup() { | ||
// arrange | ||
const string responseJson = "[{\"place_id\":281979440,\"licence\":\"Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright\",\"osm_type\":\"relation\",\"osm_id\":109166,\"boundingbox\":[\"48.1179069\",\"48.3226679\",\"16.181831\",\"16.5775132\"],\"lat\":\"48.2083537\",\"lon\":\"16.3725042\",\"display_name\":\"Vienna, Austria\",\"class\":\"boundary\",\"type\":\"administrative\",\"importance\":0.769412325825045,\"address\":{\"city\":\"Vienna\",\"country\":\"Austria\",\"country_code\":\"at\"},\"extratags\":{\"ele\":\"542\",\"capital\":\"yes\",\"website\":\"https://www.wien.gv.at/\",\"ref:nuts\":\"AT13;AT130\",\"wikidata\":\"Q1741\",\"ISO3166-2\":\"AT-9\",\"wikipedia\":\"de:Wien\",\"population\":\"1897481\",\"ref:at:gkz\":\"90001\",\"ref:nuts:2\":\"AT13\",\"ref:nuts:3\":\"AT130\",\"description\":\"Wien ist die Hauptstadt der Republik Österreich und zugleich eines der neun österreichischen Bundesländer.\",\"linked_place\":\"city\",\"name:prefix:at\":\"Statutarstadt\",\"population:date\":\"2019-01-01\",\"ISO3166-1:alpha2\":\"AT\",\"capital_ISO3166-1\":\"yes\"},\"namedetails\":{\"name\":\"Wien\"}}]"; | ||
var searchRequest = new AddressSearchRequest { | ||
OSMIDs = new List<string>(new[] { "R109166" }), | ||
BreakdownAddressElements = true, | ||
ShowAlternativeNames = true, | ||
ShowExtraTags = true | ||
}; | ||
var expectedSearchDict = new Dictionary<string, string> { | ||
{ "format", "json" }, | ||
{ "addressdetails", "1" }, | ||
{ "namedetails", "1" }, | ||
{ "extratags", "1" }, | ||
{ "osm_ids", "R109166" }, | ||
}; | ||
|
||
var nominatimWebInterface = Substitute.For<INominatimWebInterface>(); | ||
nominatimWebInterface | ||
.GetRequest<AddressLookupResponse[]>( | ||
Arg.Is(baseUrl), | ||
Arg.Is<Dictionary<string, string>>(x => x.IsEquivalentTo(expectedSearchDict))) | ||
.Returns( JsonConvert.DeserializeObject<AddressLookupResponse[]>(responseJson)); | ||
var addressSearcher = new AddressSearcher(nominatimWebInterface); | ||
|
||
// act | ||
var result = await addressSearcher.Lookup(searchRequest); | ||
|
||
// assert | ||
Assert.AreEqual(1, result.Length); | ||
Assert.AreEqual(281979440, result[0].PlaceID); | ||
} | ||
|
||
// https://stackoverflow.com/questions/3804367/testing-for-equality-between-dictionaries-in-c-sharp | ||
private bool areDictsEqual(Dictionary<string, string> d1, Dictionary<string, string> d2) => | ||
d1.Count == d2.Count && d1.All( | ||
(d1KV) => d2.TryGetValue(d1KV.Key, out var d2Value) && ( | ||
d1KV.Value == d2Value || | ||
d1KV.Value?.Equals(d2Value) == true) | ||
); | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.