Skip to content

Commit

Permalink
add tests for new extra-tags function
Browse files Browse the repository at this point in the history
  • Loading branch information
lonvia committed May 4, 2021
1 parent 6cb44ca commit 53228a1
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 6 deletions.
49 changes: 47 additions & 2 deletions src/test/java/de/komoot/photon/elasticsearch/ImporterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import org.junit.Test;

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

import static org.junit.Assert.*;

Expand All @@ -19,15 +22,57 @@ public void tearDown() {
}

@Test
public void addSimpleDoc() throws IOException {
public void testAddSimpleDoc() throws IOException {
setUpES();
Importer instance = new Importer(getClient(), "en", "");
instance.add(new PhotonDoc(1234, "N", 1000, "place", "city"));
instance.add(new PhotonDoc(1234, "N", 1000, "place", "city")
.extraTags(Collections.singletonMap("maxspeed", "100")));
instance.finish();
refresh();

GetResponse response = getById(1234);

assertTrue(response.isExists());

Map<String, Object> source = response.getSource();

assertEquals("N", source.get("osm_type"));
assertEquals(1000, source.get("osm_id"));
assertEquals("place", source.get("osm_key"));
assertEquals("city", source.get("osm_value"));

assertNull(source.get("extra"));
}

@Test
public void testSelectedExtraTagsCanBeIncluded() throws IOException {
setUpES();
Importer instance = new Importer(getClient(), "en", "maxspeed,website");

Map<String, String> extratags = new HashMap<>();
extratags.put("website", "foo");
extratags.put("maxspeed", "100 mph");
extratags.put("source", "survey");

instance.add(new PhotonDoc(1234, "N", 1000, "place", "city").extraTags(extratags));
instance.add(new PhotonDoc(1235, "N", 1001, "place", "city")
.extraTags(Collections.singletonMap("wikidata", "100")));
instance.finish();
refresh();

GetResponse response = getById(1234);
assertTrue(response.isExists());

Map<String, String> extra = (Map<String, String>) response.getSource().get("extra");
assertNotNull(extra);

assertEquals(2, extra.size());
assertEquals("100 mph", extra.get("maxspeed"));
assertEquals("foo", extra.get("website"));

response = getById(1235);
assertTrue(response.isExists());

assertNull(response.getSource().get("extra"));
}
}
33 changes: 33 additions & 0 deletions src/test/java/de/komoot/photon/elasticsearch/UpdaterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.junit.Test;

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

Expand Down Expand Up @@ -73,4 +74,36 @@ public void removeNameFromDoc() throws IOException {
assertFalse(out_names.containsKey("default"));
assertEquals("Enfoo", out_names.get("en"));
}

@Test
public void addExtraTagsToDoc() throws IOException {
Map<String, String> names = new HashMap<>();
names.put("name", "Foo");
PhotonDoc doc = new PhotonDoc(1234, "N", 1000, "place", "city").names(names);

setUpES();
Importer instance = new Importer(getClient(), "en", "website");
instance.add(doc);
instance.finish();
refresh();

GetResponse response = getById(1234);
assertTrue(response.isExists());

assertNull(response.getSource().get("extra"));

doc.extraTags(Collections.singletonMap("website", "http://site.foo"));
Updater updater = new Updater(getClient(), "en,de", "website");
updater.create(doc);
updater.finish();
refresh();

response = getById(1234);
assertTrue(response.isExists());

Map<String, String> extra = (Map<String, String>) response.getSource().get("extra");

assertNotNull(extra);
assertEquals(Collections.singletonMap("website", "http://site.foo"), extra);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.Set;

import static org.junit.Assert.assertEquals;

/**
* Created by Sachin Dole on 2/20/2015.
Expand Down
67 changes: 67 additions & 0 deletions src/test/java/de/komoot/photon/utils/ConvertToJsonTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package de.komoot.photon.utils;

import de.komoot.photon.ESBaseTester;
import de.komoot.photon.PhotonDoc;
import de.komoot.photon.elasticsearch.Importer;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.index.query.QueryBuilders;
import org.json.JSONObject;
import org.junit.After;
import org.junit.Test;
import static org.junit.Assert.*;

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

public class ConvertToJsonTest extends ESBaseTester {

@After
public void tearDown() {
deleteIndex();
shutdownES();
}

private SearchResponse databaseFromDoc(PhotonDoc doc) throws IOException {
setUpES();
Importer instance = new Importer(getClient(), "en", "maxspeed,website");
instance.add(doc);
instance.finish();
refresh();

return getClient().prepareSearch("photon")
.setSearchType(SearchType.QUERY_THEN_FETCH)
.setQuery(QueryBuilders.matchAllQuery())
.execute()
.actionGet();
}

@Test
public void testConvertWithExtraTags() throws IOException {
Map<String, String> extratags = new HashMap<>();
extratags.put("website", "foo");
extratags.put("maxspeed", "100 mph");

SearchResponse response = databaseFromDoc(new PhotonDoc(1234, "N", 1000, "place", "city").extraTags(extratags));

List<JSONObject> json = new ConvertToJson("de").convert(response);

JSONObject extra = json.get(0).getJSONObject("properties").getJSONObject("extra");

assertEquals(2, extra.length());
assertEquals("foo", extra.getString("website"));
assertEquals("100 mph", extra.getString("maxspeed"));
}


@Test
public void testConvertWithoutExtraTags() throws IOException {
SearchResponse response = databaseFromDoc(new PhotonDoc(1234, "N", 1000, "place", "city"));

List<JSONObject> json = new ConvertToJson("de").convert(response);

assertNull(json.get(0).getJSONObject("properties").optJSONObject("extra"));
}
}

0 comments on commit 53228a1

Please sign in to comment.