planetiler/planetiler-basemap/src/test/java/com/onthegomap/planetiler/basemap/GenerateTest.java

229 wiersze
5.7 KiB
Java
Czysty Zwykły widok Historia

package com.onthegomap.planetiler.basemap;
2021-06-11 01:15:27 +00:00
import static com.onthegomap.planetiler.basemap.Generate.parseYaml;
import static com.onthegomap.planetiler.expression.Expression.*;
2021-06-11 01:15:27 +00:00
import static org.junit.jupiter.api.Assertions.assertEquals;
2021-06-12 11:59:44 +00:00
import static org.junit.jupiter.api.DynamicTest.dynamicTest;
2021-06-11 01:15:27 +00:00
2021-06-14 11:04:03 +00:00
import com.fasterxml.jackson.databind.JsonNode;
import com.onthegomap.planetiler.expression.Expression;
import com.onthegomap.planetiler.expression.MultiExpression;
2021-06-14 11:04:03 +00:00
import java.util.LinkedHashMap;
2021-06-12 11:59:44 +00:00
import java.util.List;
2021-06-11 01:15:27 +00:00
import java.util.Map;
2021-06-12 11:59:44 +00:00
import java.util.stream.Stream;
import org.junit.jupiter.api.DynamicTest;
2021-06-11 01:15:27 +00:00
import org.junit.jupiter.api.Test;
2021-06-12 11:59:44 +00:00
import org.junit.jupiter.api.TestFactory;
2021-06-11 01:15:27 +00:00
2022-04-23 10:36:24 +00:00
class GenerateTest {
2021-06-11 01:15:27 +00:00
@Test
2022-04-23 10:36:24 +00:00
void testParseSimple() {
2021-06-14 11:04:03 +00:00
MultiExpression<String> parsed = Generate.generateFieldMapping(parseYaml("""
2021-06-11 01:15:27 +00:00
output:
key: value
key2:
- value2
- '%value3%'
"""));
assertEquals(MultiExpression.of(List.of(
MultiExpression.entry("output", or(
2021-06-11 01:15:27 +00:00
matchAny("key", "value"),
matchAny("key2", "value2", "%value3%")
))
2021-06-11 01:15:27 +00:00
)), parsed);
}
@Test
2022-04-23 10:36:24 +00:00
void testParseAnd() {
2021-06-14 11:04:03 +00:00
MultiExpression<String> parsed = Generate.generateFieldMapping(parseYaml("""
2021-06-11 01:15:27 +00:00
output:
__AND__:
key1: val1
key2: val2
"""));
assertEquals(MultiExpression.of(List.of(
MultiExpression.entry("output", and(
2021-06-11 01:15:27 +00:00
matchAny("key1", "val1"),
matchAny("key2", "val2")
))
2021-06-11 01:15:27 +00:00
)), parsed);
}
@Test
2022-04-23 10:36:24 +00:00
void testParseAndWithOthers() {
2021-06-14 11:04:03 +00:00
MultiExpression<String> parsed = Generate.generateFieldMapping(parseYaml("""
2021-06-11 01:15:27 +00:00
output:
- key0: val0
- __AND__:
key1: val1
key2: val2
"""));
assertEquals(MultiExpression.of(List.of(
MultiExpression.entry("output", or(
2021-06-11 01:15:27 +00:00
matchAny("key0", "val0"),
and(
matchAny("key1", "val1"),
matchAny("key2", "val2")
)
))
2021-06-11 01:15:27 +00:00
)), parsed);
}
@Test
2022-04-23 10:36:24 +00:00
void testParseAndContainingOthers() {
2021-06-14 11:04:03 +00:00
MultiExpression<String> parsed = Generate.generateFieldMapping(parseYaml("""
2021-06-11 01:15:27 +00:00
output:
__AND__:
- key1: val1
- __OR__:
key2: val2
key3: val3
"""));
assertEquals(MultiExpression.of(List.of(
MultiExpression.entry("output", and(
2021-06-11 01:15:27 +00:00
matchAny("key1", "val1"),
or(
matchAny("key2", "val2"),
matchAny("key3", "val3")
)
))
2021-06-11 01:15:27 +00:00
)), parsed);
}
@Test
2022-04-23 10:36:24 +00:00
void testParseContainsKey() {
2021-06-14 11:04:03 +00:00
MultiExpression<String> parsed = Generate.generateFieldMapping(parseYaml("""
2021-06-11 01:15:27 +00:00
output:
key1: val1
key2:
"""));
assertEquals(MultiExpression.of(List.of(
MultiExpression.entry("output", or(
2021-06-11 01:15:27 +00:00
matchAny("key1", "val1"),
matchField("key2")
))
2021-06-11 01:15:27 +00:00
)), parsed);
}
2021-06-12 11:59:44 +00:00
@TestFactory
2022-04-23 10:36:24 +00:00
Stream<DynamicTest> testParseImposm3Mapping() {
2021-06-12 11:59:44 +00:00
record TestCase(String name, String mapping, String require, String reject, Expression expected) {
TestCase(String mapping, Expression expected) {
this(mapping, mapping, null, null, expected);
}
}
2021-09-10 00:46:20 +00:00
return Stream.of(
2021-06-12 11:59:44 +00:00
new TestCase(
"key: val", matchAny("key", "val")
),
new TestCase(
"key: [val1, val2]", matchAny("key", "val1", "val2")
),
new TestCase(
"key: [\"__any__\"]", matchField("key")
),
new TestCase("reject",
"key: val",
"mustkey: mustval",
null,
and(
matchAny("key", "val"),
matchAny("mustkey", "mustval")
)
),
new TestCase("require",
"key: val",
null,
"badkey: badval",
and(
matchAny("key", "val"),
not(matchAny("badkey", "badval"))
)
),
new TestCase("require and reject complex",
"""
key: val
key2:
- val1
- val2
""",
"""
mustkey: mustval
mustkey2:
- mustval1
- mustval2
""",
"""
notkey: notval
notkey2:
- notval1
- notval2
""",
and(
or(
matchAny("key", "val"),
matchAny("key2", "val1", "val2")
),
matchAny("mustkey", "mustval"),
matchAny("mustkey2", "mustval1", "mustval2"),
not(matchAny("notkey", "notval")),
not(matchAny("notkey2", "notval1", "notval2"))
)
)
2021-09-10 00:46:20 +00:00
).map(test -> dynamicTest(test.name, () -> {
2021-06-12 11:59:44 +00:00
Expression parsed = Generate
2021-06-18 09:45:52 +00:00
.parseImposm3MappingExpression("point", parseYaml(test.mapping), new Generate.Imposm3Filters(
2021-06-14 11:04:03 +00:00
parseYaml(test.reject),
parseYaml(test.require)
2021-06-12 11:59:44 +00:00
));
2021-06-18 09:45:52 +00:00
assertEquals(test.expected, parsed.replace(matchType("point"), TRUE).simplify());
2021-06-12 11:59:44 +00:00
}));
}
2021-06-14 11:04:03 +00:00
@Test
2022-04-23 10:36:24 +00:00
void testTypeMappingTopLevelType() {
2021-06-14 11:04:03 +00:00
Expression parsed = Generate
.parseImposm3MappingExpression("point", parseYaml("""
key: val
"""), new Generate.Imposm3Filters(null, null));
assertEquals(and(
matchAny("key", "val"),
2021-06-18 09:45:52 +00:00
matchType("point")
2021-06-14 11:04:03 +00:00
), parsed);
}
@Test
2022-04-23 10:36:24 +00:00
void testTypeMappings() {
2021-06-14 11:04:03 +00:00
Map<String, JsonNode> props = new LinkedHashMap<>();
2021-06-17 12:11:24 +00:00
props.put("points", parseYaml("""
2021-06-14 11:04:03 +00:00
key: val
"""));
2021-06-17 12:11:24 +00:00
props.put("polygons", parseYaml("""
2021-06-14 11:04:03 +00:00
key2: val2
"""));
Expression parsed = Generate
.parseImposm3MappingExpression(new Generate.Imposm3Table(
"geometry",
false,
List.of(),
null,
null,
props,
List.of()
2021-06-14 11:04:03 +00:00
));
assertEquals(or(
and(
matchAny("key", "val"),
2021-06-18 09:45:52 +00:00
matchType("point")
2021-06-14 11:04:03 +00:00
),
and(
matchAny("key2", "val2"),
2021-06-18 09:45:52 +00:00
matchType("polygon")
2021-06-14 11:04:03 +00:00
)
), parsed);
}
2021-06-11 01:15:27 +00:00
}