Skip to content

Commit

Permalink
v2.0.0 (#26)
Browse files Browse the repository at this point in the history
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
f1ana authored Jul 7, 2022
1 parent 9f8e17f commit d94a6c5
Show file tree
Hide file tree
Showing 19 changed files with 409 additions and 132 deletions.
38 changes: 38 additions & 0 deletions src/Nominatim.API.Tests.Integration/AddressLookupTests.cs
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 src/Nominatim.API.Tests.Integration/GeocoderIntegrationTests.cs
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"));
}
}
}
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>
79 changes: 57 additions & 22 deletions src/Nominatim.API.Tests/AddressLookupTests.cs
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)
);
}
63 changes: 0 additions & 63 deletions src/Nominatim.API.Tests/ForwardGeocoderTests.cs

This file was deleted.

Loading

0 comments on commit d94a6c5

Please sign in to comment.