From fac89364117278b7fa6193a0c36e4d6c2c38a881 Mon Sep 17 00:00:00 2001 From: Antonin Delpeuch Date: Thu, 9 Aug 2018 14:25:09 +0100 Subject: [PATCH] Serialization tests for model classes --- .../google/refine/tests/model/CellTests.java | 79 +++++++++++++++++++ .../refine/tests/model/ColumnGroupTests.java | 42 ++++++++++ .../refine/tests/model/ColumnModelTests.java | 10 +++ .../refine/tests/model/ColumnTests.java | 47 +++++++++++ .../refine/tests/model/RecordModelTests.java | 18 +++++ .../google/refine/tests/model/RowTests.java | 36 ++++++++- 6 files changed, 230 insertions(+), 2 deletions(-) create mode 100644 main/tests/server/src/com/google/refine/tests/model/CellTests.java create mode 100644 main/tests/server/src/com/google/refine/tests/model/ColumnGroupTests.java create mode 100644 main/tests/server/src/com/google/refine/tests/model/ColumnModelTests.java create mode 100644 main/tests/server/src/com/google/refine/tests/model/ColumnTests.java create mode 100644 main/tests/server/src/com/google/refine/tests/model/RecordModelTests.java diff --git a/main/tests/server/src/com/google/refine/tests/model/CellTests.java b/main/tests/server/src/com/google/refine/tests/model/CellTests.java new file mode 100644 index 000000000..cab82c55d --- /dev/null +++ b/main/tests/server/src/com/google/refine/tests/model/CellTests.java @@ -0,0 +1,79 @@ +package com.google.refine.tests.model; + +import org.testng.annotations.Test; + +import static org.junit.Assert.assertNull; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import java.util.Properties; + +import com.google.refine.model.Cell; +import com.google.refine.model.Recon; +import com.google.refine.tests.util.TestUtils; +import com.google.refine.util.Pool; + +public class CellTests { + + String reconJson = "{\"id\":1533649346002675326," + + "\"judgmentHistoryEntry\":1530278634724," + + "\"service\":\"https://tools.wmflabs.org/openrefine-wikidata/en/api\"," + + "\"identifierSpace\":\"http://www.wikidata.org/entity/\"," + + "\"schemaSpace\":\"http://www.wikidata.org/prop/direct/\"," + + "\"j\":\"matched\"," + + "\"m\":{\"id\":\"Q551479\",\"name\":\"La Monnaie\",\"score\":100,\"types\":[\"Q153562\"]}," + + "\"c\":[{\"id\":\"Q551479\",\"name\":\"La Monnaie\",\"score\":100,\"types\":[\"Q153562\"]}]," + + "\"f\":[false,false,34,0],\"judgmentAction\":\"auto\",\"judgmentBatchSize\":1,\"matchRank\":0}"; + + Pool pool = mock(Pool.class); + Recon recon = null; + + @Test + public void serializeCellWithRecon() throws Exception { + recon = Recon.loadStreaming(reconJson, pool); + when(pool.getRecon("1533649346002675326")).thenReturn(recon); + String json = "{\"v\":\"http://www.wikidata.org/entity/Q41522540\",\"r\":\"1533649346002675326\"}"; + + Properties options = mock(Properties.class); + when(options.get("pool")).thenReturn(pool); + Cell c = Cell.loadStreaming(json, pool); + TestUtils.isSerializedTo(c, json, options); + verify(pool, times(1)).pool(recon); + } + + @Test + public void serializeCellWithString() throws Exception { + String json = "{\"v\":\"0000-0002-5022-0488\"}"; + Cell c = Cell.loadStreaming(json, pool); + TestUtils.isSerializedTo(c, json); + } + + @Test + public void serializeNullCell() throws Exception { + String json = "null"; + Cell c = Cell.loadStreaming(json, pool); + assertNull(c); + } + + @Test + public void serializeEmptyStringCell() throws Exception { + String json = "{\"v\":\"\"}"; + Cell c = Cell.loadStreaming(json, pool); + TestUtils.isSerializedTo(c, json); + } + + @Test + public void serializeErrorCell() throws Exception { + String json = "{\"e\":\"HTTP 403\"}"; + Cell c = Cell.loadStreaming(json, pool); + TestUtils.isSerializedTo(c, json); + } + + @Test + public void serializeDateCell() throws Exception { + String json = "{\"v\":\"2018-03-04T08:09:10Z\",\"t\":\"date\"}"; + TestUtils.isSerializedTo(Cell.loadStreaming(json, pool), json); + } +} diff --git a/main/tests/server/src/com/google/refine/tests/model/ColumnGroupTests.java b/main/tests/server/src/com/google/refine/tests/model/ColumnGroupTests.java new file mode 100644 index 000000000..83b6d8761 --- /dev/null +++ b/main/tests/server/src/com/google/refine/tests/model/ColumnGroupTests.java @@ -0,0 +1,42 @@ +package com.google.refine.tests.model; + +import java.util.Properties; + +import org.testng.annotations.Test; + +import com.google.refine.model.ColumnGroup; +import com.google.refine.tests.util.TestUtils; + +public class ColumnGroupTests { + + String json = "{" + + "\"startColumnIndex\":2," + + "\"columnSpan\":3," + + "\"keyColumnIndex\":1" + + "}"; + @Test + public void serializeColumnGroup() throws Exception { + TestUtils.isSerializedTo(ColumnGroup.load(json), json); + } + + @Test + public void serializeColumnGroupWithSubgroups() throws Exception { + ColumnGroup cg = new ColumnGroup(2,3,1); + ColumnGroup subCg = new ColumnGroup(2,2,1); + cg.subgroups.add(subCg); + String fullJson = "{" + + "\"startColumnIndex\":2," + + "\"columnSpan\":3," + + "\"keyColumnIndex\":1," + + "\"subgroups\":[{" + + " \"startColumnIndex\":2," + + " \"columnSpan\":2," + + " \"keyColumnIndex\":1" + + "}]" + + "}"; + Properties options = new Properties(); + options.setProperty("mode", "save"); + TestUtils.isSerializedTo(cg, json, options); + TestUtils.isSerializedTo(cg, fullJson); + } +} diff --git a/main/tests/server/src/com/google/refine/tests/model/ColumnModelTests.java b/main/tests/server/src/com/google/refine/tests/model/ColumnModelTests.java new file mode 100644 index 000000000..89f986165 --- /dev/null +++ b/main/tests/server/src/com/google/refine/tests/model/ColumnModelTests.java @@ -0,0 +1,10 @@ +package com.google.refine.tests.model; + +import org.testng.annotations.Test; + +public class ColumnModelTests { + @Test + public void serializeColumnModel() { + String json = ""; + } +} diff --git a/main/tests/server/src/com/google/refine/tests/model/ColumnTests.java b/main/tests/server/src/com/google/refine/tests/model/ColumnTests.java new file mode 100644 index 000000000..162483a24 --- /dev/null +++ b/main/tests/server/src/com/google/refine/tests/model/ColumnTests.java @@ -0,0 +1,47 @@ +package com.google.refine.tests.model; + +import org.testng.annotations.Test; + +import com.google.refine.model.Column; +import com.google.refine.model.recon.ReconConfig; +import com.google.refine.model.recon.StandardReconConfig; +import com.google.refine.tests.util.TestUtils; + +import edu.mit.simile.butterfly.ButterflyModule; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class ColumnTests { + @Test + public void serializeColumn() throws Exception { + ButterflyModule core = mock(ButterflyModule.class); + when(core.getName()).thenReturn("core"); + ReconConfig.registerReconConfig(core, "standard-service", StandardReconConfig.class); + String json = "{\"cellIndex\":4," + + "\"originalName\":\"name\"," + + "\"name\":\"organization_name\"," + + "\"type\":\"\"," + + "\"format\":\"default\"," + + "\"title\":\"\"," + + "\"description\":\"\"," + + "\"constraints\":\"{}\"," + + "\"reconConfig\":{" + + " \"mode\":\"standard-service\"," + + " \"service\":\"https://tools.wmflabs.org/openrefine-wikidata/en/api\"," + + " \"identifierSpace\":\"http://www.wikidata.org/entity/\"," + + " \"schemaSpace\":\"http://www.wikidata.org/prop/direct/\"," + + " \"type\":{\"id\":\"Q43229\",\"name\":\"organization\"}," + + " \"autoMatch\":true," + + " \"columnDetails\":[" + + " {\"column\":\"organization_country\",\"propertyName\":\"SPARQL: P17/P297\",\"propertyID\":\"P17/P297\"}," + + " {\"column\":\"organization_id\",\"propertyName\":\"SPARQL: P3500|P2427\",\"propertyID\":\"P3500|P2427\"}" + + " ]," + + " \"limit\":0}," + + "\"reconStats\":{" + + " \"nonBlanks\":299," + + " \"newTopics\":0," + + " \"matchedTopics\":222" + + "}}"; + TestUtils.isSerializedTo(Column.load(json), json); + } +} diff --git a/main/tests/server/src/com/google/refine/tests/model/RecordModelTests.java b/main/tests/server/src/com/google/refine/tests/model/RecordModelTests.java new file mode 100644 index 000000000..2ebb9e2a7 --- /dev/null +++ b/main/tests/server/src/com/google/refine/tests/model/RecordModelTests.java @@ -0,0 +1,18 @@ +package com.google.refine.tests.model; + +import org.testng.annotations.Test; + +import com.google.refine.model.Project; +import com.google.refine.tests.RefineTest; +import com.google.refine.tests.util.TestUtils; + +public class RecordModelTests extends RefineTest { + @Test + public void serializeRecordModel() { + Project proj = createCSVProject("key,val\n" + + "34,first\n" + + ",second" + ); + TestUtils.isSerializedTo(proj.recordModel, "{\"hasRecords\":true}"); + } +} diff --git a/main/tests/server/src/com/google/refine/tests/model/RowTests.java b/main/tests/server/src/com/google/refine/tests/model/RowTests.java index 5607319d3..ef0a765e0 100644 --- a/main/tests/server/src/com/google/refine/tests/model/RowTests.java +++ b/main/tests/server/src/com/google/refine/tests/model/RowTests.java @@ -48,8 +48,11 @@ import org.testng.annotations.Test; import com.google.refine.model.Cell; import com.google.refine.model.Project; +import com.google.refine.model.Recon; import com.google.refine.model.Row; import com.google.refine.tests.RefineTest; +import com.google.refine.tests.util.TestUtils; +import com.google.refine.util.Pool; public class RowTests extends RefineTest { @@ -104,7 +107,7 @@ public class RowTests extends RefineTest { Row row = new Row(5); row.setCell(0, new Cell("I'm not empty", null)); row.save(writer, options); - Assert.assertEquals(writer.getBuffer().toString(), + TestUtils.equalAsJson(writer.getBuffer().toString(), "{\"flagged\":false,\"starred\":false,\"cells\":[{\"v\":\"I'm not empty\"}]}"); } @@ -117,10 +120,39 @@ public class RowTests extends RefineTest { when(options.containsKey("recordIndex")).thenReturn(true); when(options.get("recordIndex")).thenReturn(1); row.save(writer, options); - Assert.assertEquals( + TestUtils.equalAsJson( writer.getBuffer().toString(), "{\"flagged\":false,\"starred\":false,\"cells\":[{\"v\":\"I'm not empty\"}],\"i\":0,\"j\":1}"); } + + @Test + public void serializeRowTest() throws Exception { + + String reconJson = "{\"id\":1533649346002675326," + + "\"judgmentHistoryEntry\":1530278634724," + + "\"service\":\"https://tools.wmflabs.org/openrefine-wikidata/en/api\"," + + "\"identifierSpace\":\"http://www.wikidata.org/entity/\"," + + "\"schemaSpace\":\"http://www.wikidata.org/prop/direct/\"," + + "\"j\":\"matched\"," + + "\"m\":{\"id\":\"Q551479\",\"name\":\"La Monnaie\",\"score\":100,\"types\":[\"Q153562\"]}," + + "\"c\":[{\"id\":\"Q551479\",\"name\":\"La Monnaie\",\"score\":100,\"types\":[\"Q153562\"]}]," + + "\"f\":[false,false,34,0],\"judgmentAction\":\"auto\",\"judgmentBatchSize\":1,\"matchRank\":0}"; + Pool pool = mock(Pool.class); + Recon recon = Recon.loadStreaming(reconJson, pool); + when(pool.getRecon("1533649346002675326")).thenReturn(recon); + + String json = "{\"flagged\":false," + + "\"starred\":false," + + "\"cells\":[" + + " {\"v\":\"http://www.wikidata.org/entity/Q41522540\",\"r\":\"1533649346002675326\"}," + + " {\"v\":\"0000-0002-5022-0488\"}," + + " null," + + " {\"v\":\"\"}" + + "]}"; + Row row = Row.load(json, pool); + when(options.get("pool")).thenReturn(pool); + TestUtils.isSerializedTo(row, json, options); + } @Test public void toStringTest() {