Search in sources :

Example 61 with Column

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>
     */
}
Also used : PetStoreLoader(org.molgenis.emx2.datamodels.PetStoreLoader) Row(org.molgenis.emx2.Row) BeforeClass(org.junit.BeforeClass)

Example 62 with Column

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"));
}
Also used : Schema(org.molgenis.emx2.Schema) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 63 with Column

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"));
}
Also used : SchemaMetadata(org.molgenis.emx2.SchemaMetadata) Test(org.junit.Test)

Example 64 with Column

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");
    }
}
Also used : Table(org.molgenis.emx2.Table) Test(org.junit.Test)

Example 65 with Column

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);
}
Also used : TableMetadata(org.molgenis.emx2.TableMetadata)

Aggregations

org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Column (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Column)21 ArrayList (java.util.ArrayList)20 Test (org.junit.Test)20 Row (org.molgenis.emx2.Row)15 Table (org.molgenis.emx2.Table)14 HashMap (java.util.HashMap)11 Column (org.molgenis.emx2.Column)11 List (java.util.List)10 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Bigint (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Bigint)10 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Smallint (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Smallint)10 ColumnType (org.molgenis.emx2.ColumnType)10 Column (com.google.bigtable.v2.Column)9 TableMetadata (org.molgenis.emx2.TableMetadata)9 Family (com.google.bigtable.v2.Family)8 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Tinyint (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Tinyint)8 Schema (org.molgenis.emx2.Schema)8 MolgenisException (org.molgenis.emx2.MolgenisException)7 Collectors (java.util.stream.Collectors)6 Map (java.util.Map)5 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Table (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Table)5