Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More simple acceptance tests for queries #661

Merged
merged 2 commits into from
Mar 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 52 additions & 25 deletions src/test/java/de/komoot/photon/query/QueryBasicSearchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@ public void testSearchByDefaultName() {
instance.finish();
refresh();

assertEquals(1, search("muffle flu").size());
assertEquals(1, search("flu").size());
assertEquals(1, search("muffle").size());
assertEquals(1, search("mufle flu").size());
assertEquals(1, search("muffle flu 9").size());
assertEquals(0, search("huffle fluff").size());
assertAll("default name",
() -> assertEquals(1, search("muffle flu").size()),
() -> assertEquals(1, search("flu").size()),
() -> assertEquals(1, search("muffle").size()),
() -> assertEquals(1, search("mufle flu").size()),
() -> assertEquals(1, search("muffle flu 9").size()),
() -> assertEquals(0, search("huffle fluff").size())
);
}

@Test
Expand All @@ -67,14 +69,16 @@ public void testSearchByAlternativeNames() {
instance.finish();
refresh();

assertEquals(1, search("original").size());
assertEquals(1, search("alt").size());
assertEquals(1, search("older").size());
assertEquals(1, search("int").size());
assertEquals(1, search("local").size());
assertEquals(1, search("regional").size());
assertEquals(1, search("house").size());
assertEquals(0, search("other").size());
assertAll("altnames",
() -> assertEquals(1, search("original").size()),
() -> assertEquals(1, search("alt").size()),
() -> assertEquals(1, search("older").size()),
() -> assertEquals(1, search("int").size()),
() -> assertEquals(1, search("local").size()),
() -> assertEquals(1, search("regional").size()),
() -> assertEquals(1, search("house").size()),
() -> assertEquals(0, search("other").size())
);
}

@Test
Expand All @@ -92,12 +96,31 @@ public void testSearchByNameAndAddress() {
instance.finish();
refresh();

assertEquals(1, search("castillo").size());
assertEquals(1, search("castillo callino").size());
assertEquals(1, search("castillo quartier madrid").size());
assertEquals(1, search("castillo block montagna estado").size());
assertAll("name and address",
() -> assertEquals(1, search("castillo").size()),
() -> assertEquals(1, search("castillo callino").size()),
() -> assertEquals(1, search("castillo quartier madrid").size()),
() -> assertEquals(1, search("castillo block montagna estado").size()),

assertEquals(0, search("castillo state thing").size());
() -> assertEquals(0, search("castillo state thing").size())
);
}

@Test
public void testSearchMustContainANameTerm() {
Importer instance = makeImporter();
instance.add(createDoc("name", "Palermo").address(Collections.singletonMap("state", "Sicilia")));
instance.finish();
refresh();

assertAll("find names",
() -> assertEquals(1, search("Palermo").size()),
() -> assertEquals(1, search("Paler").size()),
() -> assertEquals(1, search("Palermo Sici").size()),
() -> assertEquals(1, search("Sicilia, Paler").size()),
() -> assertEquals(0, search("Sicilia").size()),
() -> assertEquals(0, search("Sici").size())
);
}

@Test
Expand All @@ -107,10 +130,12 @@ public void testSearchWithHousenumberNamed() {
instance.finish();
refresh();

assertEquals(1, search("hauptstrasse 5").size());
assertEquals(1, search("edeka, hauptstrasse 5").size());
assertEquals(1, search("edeka, hauptstr 5").size());
assertEquals(1, search("edeka, hauptstrasse").size());
assertAll("named housenumber",
() -> assertEquals(1, search("hauptstrasse 5").size()),
() -> assertEquals(1, search("edeka, hauptstrasse 5").size()),
() -> assertEquals(1, search("edeka, hauptstr 5").size()),
() -> assertEquals(1, search("edeka, hauptstrasse").size())
);
}

@Test
Expand All @@ -120,7 +145,9 @@ public void testSearchWithHousenumberUnnamed() {
instance.finish();
refresh();

assertEquals(1, search("hauptstrasse 5").size());
assertEquals(0, search("hauptstrasse").size());
assertAll("unnamed housenumber",
() -> assertEquals(1, search("hauptstrasse 5").size()),
() -> assertEquals(0, search("hauptstrasse").size())
);
}
}
127 changes: 127 additions & 0 deletions src/test/java/de/komoot/photon/query/QueryRelevanceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package de.komoot.photon.query;

import com.vividsolutions.jts.geom.Coordinate;
import de.komoot.photon.ESBaseTester;
import de.komoot.photon.Importer;
import de.komoot.photon.PhotonDoc;
import de.komoot.photon.searcher.PhotonResult;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.*;

/**
* Test that the database backend produces queries that rank the
* results in the expected order.
*/
public class QueryRelevanceTest extends ESBaseTester {

@BeforeEach
public void setup() throws IOException {
setUpES();
}

private PhotonDoc createDoc(String key, String value, long id, String... names) {
Map<String, String> nameMap = new HashMap<>();

for (int i = 0; i < names.length - 1; i += 2) {
nameMap.put(names[i], names[i+1]);
}

return new PhotonDoc(id, "N", id, key, value).names(nameMap);
}

private List<PhotonResult> search(String query) {
return getServer().createSearchHandler(new String[]{"en"}).search(new PhotonRequest(query, "en"));
}

private List<PhotonResult> search(PhotonRequest request) {
return getServer().createSearchHandler(new String[]{"en"}).search(request);
}

@Test
public void testRelevanceByImportance() {
Importer instance = makeImporter();
instance.add(createDoc("amenity", "restuarant", 1001, "name", "New York").importance(0.0));
instance.add(createDoc("place", "city", 2000, "name", "New York").importance(0.5));
instance.finish();
refresh();

List<PhotonResult> results = search("new york");

assertEquals(2, results.size());
assertEquals(2000, results.get(0).get("osm_id"));
}

@Test
public void testFullNameOverPartialName() {
Importer instance = makeImporter();
instance.add(createDoc("place", "hamlet", 1000, "name", "Ham"));
instance.add(createDoc("place", "hamlet", 1001, "name", "Hamburg"));
instance.finish();
refresh();

List<PhotonResult> results = search("ham");

assertEquals(2, results.size());
assertEquals(1000, results.get(0).get("osm_id"));
}

@Test
public void testPartialNameWithImportanceOverFullName() {
Importer instance = makeImporter();
instance.add(createDoc("place", "hamlet", 1000, "name", "Ham").importance(0.1));
instance.add(createDoc("place", "city", 1001, "name", "Hamburg").importance(0.5));
instance.finish();
refresh();

List<PhotonResult> results = search("ham");

assertEquals(2, results.size());
assertEquals(1001, results.get(0).get("osm_id"));
}

@ParameterizedTest
@ValueSource(strings = {"Ham", "Hamm", "Hamburg"})
void testLocationPreferenceForEqualImportance(String placeName) {
Importer instance = makeImporter();
instance.add(createDoc("place", "hamlet", 1000, "name", "Ham")
.centroid(FACTORY.createPoint(new Coordinate(10, 10))));
instance.add(createDoc("place", "hamlet", 1001, "name", placeName)
.centroid(FACTORY.createPoint(new Coordinate(-10, -10))));
instance.finish();
refresh();

List<PhotonResult> results = search(new PhotonRequest("ham", "en")
.setLocationForBias(FACTORY.createPoint(new Coordinate(-9.9, -10))));

assertEquals(2, results.size());
assertEquals(1001, results.get(0).get("osm_id"));
}

@Test
void testLocationPreferenceForHigherImportance() {
Importer instance = makeImporter();
instance.add(createDoc("place", "hamlet", 1000, "name", "Ham")
.importance(0.8)
.centroid(FACTORY.createPoint(new Coordinate(10, 10))));
instance.add(createDoc("place", "hamlet", 1001, "name", "Ham")
.importance(0.01)
.centroid(FACTORY.createPoint(new Coordinate(-10, -10))));
instance.finish();
refresh();

List<PhotonResult> results = search(new PhotonRequest("ham", "en")
.setLocationForBias(FACTORY.createPoint(new Coordinate(-9.9, -10))));

assertEquals(2, results.size());
assertEquals(1000, results.get(0).get("osm_id"));
}
}
70 changes: 70 additions & 0 deletions src/test/java/de/komoot/photon/query/QueryReverseTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package de.komoot.photon.query;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Point;
import de.komoot.photon.ESBaseTester;
import de.komoot.photon.Importer;
import de.komoot.photon.searcher.PhotonResult;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import java.io.IOException;
import java.nio.file.Path;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class QueryReverseTest extends ESBaseTester {
@TempDir
private static Path instanceTestDirectory;

@BeforeAll
public void setup() throws IOException {
setUpES(instanceTestDirectory, "en");

Importer instance = makeImporter();
instance.add(createDoc(10,10, 100, 100, "place", "house"));
instance.add(createDoc(10,10.1, 101, 101, "place", "house"));
instance.add(createDoc(10,10.2, 102, 102, "place", "house"));
instance.add(createDoc(-10,-10, 202, 102, "place", "house"));
instance.finish();
refresh();
}

@AfterAll
@Override
public void tearDown() {
super.tearDown();
}

private List<PhotonResult> reverse(double lon, double lat, double radius, int limit) {
Point pt = FACTORY.createPoint(new Coordinate(lon, lat));
return getServer().createReverseHandler().reverse(new ReverseRequest(pt, "en", radius, "", limit, true));
}

@Test
public void testReverse() {
List<PhotonResult> results = reverse(10, 10, 0.1, 1);

assertEquals(1, results.size());
assertEquals(100, results.get(0).get("osm_id"));
}

@ParameterizedTest
@ValueSource(ints = {2, 3, 10})
public void testReverseMultiple(int limit) {
List<PhotonResult> results = reverse(10, 10, 20, limit);

assertEquals(2, results.size());
assertEquals(100, results.get(0).get("osm_id"));
assertEquals(101, results.get(1).get("osm_id"));
}


}