Serialization tests for model classes
This commit is contained in:
parent
c6d2b003b1
commit
fac8936411
@ -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);
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
package com.google.refine.tests.model;
|
||||||
|
|
||||||
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
|
public class ColumnModelTests {
|
||||||
|
@Test
|
||||||
|
public void serializeColumnModel() {
|
||||||
|
String json = "";
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
@ -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}");
|
||||||
|
}
|
||||||
|
}
|
@ -48,8 +48,11 @@ import org.testng.annotations.Test;
|
|||||||
|
|
||||||
import com.google.refine.model.Cell;
|
import com.google.refine.model.Cell;
|
||||||
import com.google.refine.model.Project;
|
import com.google.refine.model.Project;
|
||||||
|
import com.google.refine.model.Recon;
|
||||||
import com.google.refine.model.Row;
|
import com.google.refine.model.Row;
|
||||||
import com.google.refine.tests.RefineTest;
|
import com.google.refine.tests.RefineTest;
|
||||||
|
import com.google.refine.tests.util.TestUtils;
|
||||||
|
import com.google.refine.util.Pool;
|
||||||
|
|
||||||
public class RowTests extends RefineTest {
|
public class RowTests extends RefineTest {
|
||||||
|
|
||||||
@ -104,7 +107,7 @@ public class RowTests extends RefineTest {
|
|||||||
Row row = new Row(5);
|
Row row = new Row(5);
|
||||||
row.setCell(0, new Cell("I'm not empty", null));
|
row.setCell(0, new Cell("I'm not empty", null));
|
||||||
row.save(writer, options);
|
row.save(writer, options);
|
||||||
Assert.assertEquals(writer.getBuffer().toString(),
|
TestUtils.equalAsJson(writer.getBuffer().toString(),
|
||||||
"{\"flagged\":false,\"starred\":false,\"cells\":[{\"v\":\"I'm not empty\"}]}");
|
"{\"flagged\":false,\"starred\":false,\"cells\":[{\"v\":\"I'm not empty\"}]}");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -117,11 +120,40 @@ public class RowTests extends RefineTest {
|
|||||||
when(options.containsKey("recordIndex")).thenReturn(true);
|
when(options.containsKey("recordIndex")).thenReturn(true);
|
||||||
when(options.get("recordIndex")).thenReturn(1);
|
when(options.get("recordIndex")).thenReturn(1);
|
||||||
row.save(writer, options);
|
row.save(writer, options);
|
||||||
Assert.assertEquals(
|
TestUtils.equalAsJson(
|
||||||
writer.getBuffer().toString(),
|
writer.getBuffer().toString(),
|
||||||
"{\"flagged\":false,\"starred\":false,\"cells\":[{\"v\":\"I'm not empty\"}],\"i\":0,\"j\":1}");
|
"{\"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
|
@Test
|
||||||
public void toStringTest() {
|
public void toStringTest() {
|
||||||
Row row = new Row(5);
|
Row row = new Row(5);
|
||||||
|
Loading…
Reference in New Issue
Block a user