use of org.molgenis.emx2.Column in project molgenis-emx2 by molgenis.
the class TestQueryJsonGraph method setup.
@BeforeClass
public static void setup() {
db = TestDatabaseFactory.getTestDatabase();
schema = db.dropCreateSchema(TestQueryJsonGraph.class.getSimpleName());
new PetStoreLoader().load(schema, true);
schema.create(table("Person").add(column("name").setPkey()).add(column("father").setType(REF).setRefTable("Person")).add(column("mother").setType(REF).setRefTable("Person")).add(column("children").setType(REF_ARRAY).setRefTable("Person")).add(column("cousins").setType(REF_ARRAY).setRefTable("Person")));
schema.getTable("Person").insert(new Row().set("name", "opa1").set("children", "ma,pa").set("cousins", // sorry for this example
"opa2"), new Row().set("name", "opa2"), new Row().set("name", "oma1"), new Row().set("name", "oma2"), new Row().set("name", "ma").set("father", "opa2").set("mother", "oma2").set("children", "kind"), new Row().set("name", "pa").set("father", "opa1").set("mother", "oma1").set("children", "kind"), new Row().set("name", "kind").set("father", "pa").set("mother", "ma"));
/*
<pre>
name | father | mother | children | cousins
opa1 | | | ma,pa | opa2
opa2
oma1
oma2
ma | opa2 | oma2 | kind
pa | opa1 | opa1 | kind
kind | pa | ma
</pre>
*/
}
use of org.molgenis.emx2.Column in project molgenis-emx2 by molgenis.
the class TestQueryJsonGraph method testGroupBy.
// todo @Test
// we want to refactor this
public void testGroupBy() throws JsonProcessingException {
Schema schema = db.dropCreateSchema(TestQueryJsonGraph.class.getSimpleName() + "_testGroupBy");
schema.create(table("Test", column("id").setPkey(), column("tag"), column("tag_array").setType(STRING_ARRAY), column("tag_array2").setType(STRING_ARRAY)));
schema.getTable("Test").insert(row("id", 1, "tag", "blue", "tag_array", new String[] { "blue", "green" }, "tag_array2", new String[] { "yellow", "red" }), row("id", 2, "tag", "blue", "tag_array", new String[] { "green", "blue" }, "tag_array2", new String[] { "yellow", "red" }), row("id", 3, "tag", "green", "tag_array", new String[] { "blue" }, "tag_array2", new String[] { "yellow", "red" }));
ObjectMapper mapper = new ObjectMapper();
Map<String, Map<String, List<Map<String, Object>>>> result = mapper.readValue(schema.agg("Test").select(s("groupBy", s("count"), s("tag"))).retrieveJSON(), Map.class);
assertEquals(2, result.get("Test_agg").get("groupBy").get(1).get("count"));
result = mapper.readValue(schema.agg("Test").select(s("groupBy", s("count"), s("tag_array"))).retrieveJSON(), Map.class);
assertEquals(2, result.get("Test_agg").get("groupBy").get(0).get("count"));
result = mapper.readValue(schema.agg("Test").select(s("groupBy", s("count"), s("tag_array"), s("tag_array2"))).retrieveJSON(), Map.class);
assertEquals(3, result.get("Test_agg").get("groupBy").get(0).get("count"));
}
use of org.molgenis.emx2.Column in project molgenis-emx2 by molgenis.
the class TestMergeDrop method testDrop.
@Test
public void testDrop() {
SchemaMetadata newSchema = new SchemaMetadata();
newSchema.create(table("Person", column("name")));
schema.migrate(newSchema);
// idempotent
schema.migrate(newSchema);
assertNotNull(schema.getTable("Person"));
newSchema = new SchemaMetadata();
newSchema.create(table("Person"));
newSchema.getTableMetadata("Person").drop();
schema.migrate(newSchema);
// should be idempotent
schema.migrate(newSchema);
assertNull(schema.getTable("Person"));
// now more complex, with dependency
newSchema = new SchemaMetadata();
newSchema.create(table("Person", column("name").setPkey()));
newSchema.create(table("Pet", column("name").setPkey(), column("owner").setType(ColumnType.REF).setRefTable("Person")));
schema.migrate(newSchema);
// should be idempotent so repeat
schema.migrate(newSchema);
assertNotNull(schema.getTable("Person"));
assertNotNull(schema.getTable("Pet"));
schema.getTable("Person").insert(row("name", "Donald"));
schema.getTable("Pet").insert(row("name", "Pluto", "owner", "Donald"));
// should fail
newSchema = new SchemaMetadata();
try {
newSchema.create(table("Person"));
newSchema.getTableMetadata("Person").drop();
schema.migrate(newSchema);
fail("should fail because of foreign key");
} catch (Exception e) {
// fine
}
// should succeed
newSchema.create(table("Pet"));
newSchema.getTableMetadata("Pet").drop();
schema.migrate(newSchema);
// should be idempotent so repeat
schema.migrate(newSchema);
assertNull(schema.getTable("Person"));
assertNull(schema.getTable("Pet"));
}
use of org.molgenis.emx2.Column in project molgenis-emx2 by molgenis.
the class TestMgColumns method testMgDraft.
@Test
public void testMgDraft() {
Table t = schema.create(table("MgDraft", column("id").setPkey(), column("required").setRequired(true), column("notrequired")));
try {
t.insert(row("id", 1, "notrequired", "somevalue1"));
fail("should fail");
} catch (Exception e) {
// ok
}
try {
t.insert(row("id", 1, "notrequired", "somevalue2").setDraft(true));
} catch (Exception e) {
fail("should succeed");
}
// verify
Assert.assertEquals(1, t.retrieveRows().size());
Assert.assertEquals("somevalue2", t.retrieveRows().get(0).getString("notrequired"));
// to make sure also test with subclass
t = schema.create(table("MgDraftSuper", column("id").setPkey()));
t = schema.create(table("MgDraftSub", column("required").setRequired(true), column("notrequired")).setInherit("MgDraftSuper"));
try {
t.insert(row("id", 1, "notrequired", "somevalue1"));
fail("should fail");
} catch (Exception e) {
// ok
}
try {
t.insert(row("id", 1, "notrequired", "somevalue2").setDraft(true));
} catch (Exception e) {
fail("should succeed");
}
}
use of org.molgenis.emx2.Column in project molgenis-emx2 by molgenis.
the class SettingsDataModel method create.
public static void create(Schema schema) {
TableMetadata tm = table("Settings");
tm.add(column("schema").setKey(1));
tm.add(column("key").setKey(1));
tm.add(column("value"));
schema.create(tm);
}
Aggregations