From da028dd3cc1653e9af793239d9df441bb76d1fa4 Mon Sep 17 00:00:00 2001 From: Arne H Juul Date: Wed, 15 Dec 2021 13:49:54 +0000 Subject: configurable rendering of "position" structs --- .../document/DocumentTypeManagerTestCase.java | 14 +++++++- .../yahoo/document/json/JsonReaderTestCase.java | 39 ++++++++++++++++++++++ .../yahoo/document/json/JsonWriterTestCase.java | 5 +++ 3 files changed, 57 insertions(+), 1 deletion(-) (limited to 'document/src/test/java/com/yahoo') diff --git a/document/src/test/java/com/yahoo/document/DocumentTypeManagerTestCase.java b/document/src/test/java/com/yahoo/document/DocumentTypeManagerTestCase.java index 0aa5aec4b85..b89ed2b6b08 100644 --- a/document/src/test/java/com/yahoo/document/DocumentTypeManagerTestCase.java +++ b/document/src/test/java/com/yahoo/document/DocumentTypeManagerTestCase.java @@ -114,7 +114,7 @@ public class DocumentTypeManagerTestCase { assertSame(docType2, manager.getDocumentType(new DataTypeName("foo1"))); assertSame(docType3, manager.getDocumentType(new DataTypeName("foo2"))); assertSame(docType4, manager.getDocumentType(new DataTypeName("foo3"))); - + assertEquals(manager.getDocumentTypes().size(), 5); assertNotNull(manager.getDocumentTypes().get(new DataTypeName("document"))); assertEquals(manager.getDocumentTypes().get(new DataTypeName("foo0")), docType1); @@ -587,6 +587,18 @@ search annotationsimplicitstruct { assertFalse(docType.hasImportedField("a_missing_imported_field")); } + @Test + public void position_type_is_recognized_as_v8() { + var manager = DocumentTypeManager.fromFile("src/test/document/documentmanager.testv8pos.cfg"); + var docType = manager.getDocumentType("foobar"); + var simplepos = docType.getField("simplepos").getDataType(); + assertTrue(simplepos instanceof StructDataType); + var arraypos = docType.getField("arraypos").getDataType(); + assertTrue(arraypos instanceof ArrayDataType); + var array = (ArrayDataType) arraypos; + assertTrue(array.getNestedType() instanceof StructDataType); + } + // TODO test clone(). Also fieldSets not part of clone()..! // TODO add imported field to equals()/hashCode() for DocumentType? fieldSets not part of this... diff --git a/document/src/test/java/com/yahoo/document/json/JsonReaderTestCase.java b/document/src/test/java/com/yahoo/document/json/JsonReaderTestCase.java index 66ff7a7d4cd..ab4af5e722e 100644 --- a/document/src/test/java/com/yahoo/document/json/JsonReaderTestCase.java +++ b/document/src/test/java/com/yahoo/document/json/JsonReaderTestCase.java @@ -31,6 +31,7 @@ import com.yahoo.document.datatypes.StringFieldValue; import com.yahoo.document.datatypes.Struct; import com.yahoo.document.datatypes.TensorFieldValue; import com.yahoo.document.datatypes.WeightedSet; +import com.yahoo.document.internal.GeoPosType; import com.yahoo.document.json.readers.DocumentParseInfo; import com.yahoo.document.json.readers.VespaJsonDocumentReader; import com.yahoo.document.serialization.DocumentSerializer; @@ -149,6 +150,7 @@ public class JsonReaderTestCase { DocumentType x = new DocumentType("testsinglepos"); DataType d = PositionDataType.INSTANCE; x.addField(new Field("singlepos", d)); + x.addField(new Field("geopos", new GeoPosType(8))); types.registerDocumentType(x); } { @@ -611,6 +613,43 @@ public class JsonReaderTestCase { assertEquals(63429722, PositionDataType.getYValue(f).getInteger()); } + @Test + public void testPositionGeoPos() throws IOException { + Document doc = docFromJson(inputJson("{ 'put': 'id:unittest:testsinglepos::bamf',", + " 'fields': {", + " 'geopos': 'N63.429722;E10.393333' }}")); + FieldValue f = doc.getFieldValue(doc.getField("geopos")); + assertSame(Struct.class, f.getClass()); + assertEquals(10393333, PositionDataType.getXValue(f).getInteger()); + assertEquals(63429722, PositionDataType.getYValue(f).getInteger()); + assertEquals(f.getDataType(), PositionDataType.INSTANCE); + } + + @Test + public void testPositionOldGeoPos() throws IOException { + Document doc = docFromJson(inputJson("{ 'put': 'id:unittest:testsinglepos::bamf',", + " 'fields': {", + " 'geopos': {'x':10393333,'y':63429722} }}")); + FieldValue f = doc.getFieldValue(doc.getField("geopos")); + assertSame(Struct.class, f.getClass()); + assertEquals(10393333, PositionDataType.getXValue(f).getInteger()); + assertEquals(63429722, PositionDataType.getYValue(f).getInteger()); + assertEquals(f.getDataType(), PositionDataType.INSTANCE); + } + + @Test + public void testGeoPositionGeoPos() throws IOException { + Document doc = docFromJson(inputJson("{ 'put': 'id:unittest:testsinglepos::bamf',", + " 'fields': {", + " 'geopos': {'lat':63.429722,'lng':10.393333} }}")); + FieldValue f = doc.getFieldValue(doc.getField("geopos")); + assertSame(Struct.class, f.getClass()); + assertEquals(10393333, PositionDataType.getXValue(f).getInteger()); + assertEquals(63429722, PositionDataType.getYValue(f).getInteger()); + assertEquals(f.getDataType(), PositionDataType.INSTANCE); + assertEquals(PositionDataType.INSTANCE, f.getDataType()); + } + @Test public void testPositionNegative() throws IOException { Document doc = docFromJson(inputJson("{ 'put': 'id:unittest:testsinglepos::bamf',", diff --git a/document/src/test/java/com/yahoo/document/json/JsonWriterTestCase.java b/document/src/test/java/com/yahoo/document/json/JsonWriterTestCase.java index 29703eadfce..7573aba519f 100644 --- a/document/src/test/java/com/yahoo/document/json/JsonWriterTestCase.java +++ b/document/src/test/java/com/yahoo/document/json/JsonWriterTestCase.java @@ -22,6 +22,7 @@ import com.yahoo.document.TensorDataType; import com.yahoo.document.WeightedSetDataType; import com.yahoo.document.datatypes.ReferenceFieldValue; import com.yahoo.document.datatypes.TensorFieldValue; +import com.yahoo.document.internal.GeoPosType; import com.yahoo.document.json.readers.DocumentParseInfo; import com.yahoo.document.json.readers.VespaJsonDocumentReader; import com.yahoo.tensor.TensorType; @@ -93,6 +94,7 @@ public class JsonWriterTestCase { DocumentType x = new DocumentType("testmultipos"); DataType d = new ArrayDataType(PositionDataType.INSTANCE); x.addField(new Field("multipos", d)); + x.addField(new Field("geopos", new ArrayDataType(new GeoPosType(8)))); types.registerDocumentType(x); } @@ -100,6 +102,7 @@ public class JsonWriterTestCase { DocumentType x = new DocumentType("testsinglepos"); DataType d = PositionDataType.INSTANCE; x.addField(new Field("singlepos", d)); + x.addField(new Field("geopos", new GeoPosType(8))); types.registerDocumentType(x); } @@ -202,11 +205,13 @@ public class JsonWriterTestCase { @Test public void singlePosTest() throws IOException { roundTripEquality("id:unittest:testsinglepos::bamf", "{ \"singlepos\": \"N60.222333;E10.12\" }"); + roundTripEquality("id:unittest:testsinglepos::bamf", "{ \"geopos\": { \"lat\": 60.222333, \"lng\": 10.12 } }"); } @Test public void multiPosTest() throws IOException { roundTripEquality("id:unittest:testmultipos::bamf", "{ \"multipos\": [ \"N0.0;E0.0\", \"S1.1;W1.1\", \"N10.2;W122.2\" ] }"); + roundTripEquality("id:unittest:testmultipos::bamf", "{ \"geopos\": [ { \"lat\": -1.5, \"lng\": -1.5 }, { \"lat\": 63.4, \"lng\": 10.4 }, { \"lat\": 0.0, \"lng\": 0.0 } ] }"); } @Test -- cgit v1.2.3