use of org.molgenis.emx2.Query in project molgenis-emx2 by molgenis.
the class TestQueryJsonGraph method testQuery.
@Test
public void testQuery() {
StopWatch.print("begin");
Query s = schema.getTable("Pet").query();
s.select(s("name"), s("status"), s("category", s("name")));
String result = s.retrieveJSON();
System.out.println(result);
assertTrue(result.contains("spike"));
s = schema.getTable("Person").query();
s.select(s("name"), s("father", s("name"), s("father", s("name")), s("mother", s("name"))), s("mother", s("name"), s("father", s("name")), s("mother", s("name"))), s("children", s("name"), s("children", s("name"))), s("children_agg", s("count")), s("cousins", s("name"), s("cousins", s("name"))));
result = s.retrieveJSON();
System.out.println(result);
assertTrue(result.contains("\"children\": [{\"name\": \"kind\"}]}"));
// smoke test limit offset
s = schema.getTable("Person").query();
s.limit(2).offset(1).retrieveJSON();
StopWatch.print("complete");
}
use of org.molgenis.emx2.Query in project molgenis-emx2 by molgenis.
the class TestQueryJsonGraph method testSearch.
@Test
public void testSearch() {
Query s = this.schema.getTable("Person").query();
s.select(s("name"));
s.search("opa");
String result = s.retrieveJSON();
System.out.println("search for 'opa':\n " + result);
assertTrue(result.contains("opa"));
s = schema.getTable("Person").query();
s.select(s("name"), s("children", s("name"), s("children", s("name"))), s("father", s("name")));
s.search("opa");
result = s.retrieveJSON();
System.out.println("search for 'opa' also in grandparents:\n " + result);
assertTrue(result.contains("opa"));
StopWatch.print("complete");
s.where(f("name", EQUALS, "opa1"), f("children", f("name", EQUALS, "ma"), f("children", f("name", EQUALS, "kind"))));
result = s.retrieveJSON();
System.out.println(result);
assertTrue(result.contains("opa1"));
s.where(f("children", f("children", f("name", EQUALS, "kind"))));
result = s.retrieveJSON();
System.out.println(result);
assertTrue(result.contains("kind"));
//
// s.search("opa");
// {father : {name: { eq:[opa2]}, mother: {name: {eq: oma1}, father: {name: {eq: opa2}}
//
// s.where(
// "name",
// eq("pa"),
// "father",
// Map.of("name", eq("opa2"), "mother", Map.of("name", eq("oma2"))));
// s.where("father", "name")
// .eq("opa1")
// .and("father", "mother", "name")
// .eq("opa2")
// .and("father", "father", "name")
// .eq("opa2");
StopWatch.print("complete");
s = schema.query("Person");
s.select(s("name"));
s.where(f("name", TRIGRAM_SEARCH, "opa"));
result = s.retrieveJSON();
System.out.println();
assertTrue(result.contains("opa"));
}
use of org.molgenis.emx2.Query in project molgenis-emx2 by molgenis.
the class TestRowLevelSecurity method testRls.
@Test
public void testRls() {
try {
// create schema
Schema s = database.dropCreateSchema(TEST_RLS);
// create two users
database.addUser(TEST_RLS_HAS_NO_PERMISSION);
assertEquals(true, database.hasUser(TEST_RLS_HAS_NO_PERMISSION));
database.addUser(TESTRLS_HAS_RLS_VIEW);
// grant both owner on TestRLS schema so can add row level security
s.addMember("testrls1", Privileges.OWNER.toString());
s.addMember("testrls2", Privileges.OWNER.toString());
s.addMember(TESTRLS_HAS_RLS_VIEW, // can view table but only rows with right RLS
Privileges.VIEWER.toString());
// let one user create the table
database.setActiveUser("testrls1");
database.tx(db -> {
db.getSchema(TEST_RLS).create(table(TEST_RLS).add(column("col1").setPkey()));
});
// let the other user add RLS
database.setActiveUser("testrls2");
database.tx(db -> {
db.getSchema(TEST_RLS).getTable(TEST_RLS).getMetadata().enableRowLevelSecurity();
});
// let the first add a row (checks if admin permissions are setup correctly)
database.setActiveUser("testrls1");
database.tx(db -> {
db.getSchema(TEST_RLS).getTable(TEST_RLS).insert(new Row().setString("col1", "Hello World").set(MG_EDIT_ROLE, TESTRLS_HAS_RLS_VIEW), new Row().setString("col1", "Hello World2").set(MG_EDIT_ROLE, TEST_RLS_HAS_NO_PERMISSION));
});
// let the second admin see it
database.setActiveUser("testrls2");
database.tx(db -> {
assertEquals(2, db.getSchema(TEST_RLS).getTable(TEST_RLS).retrieveRows().size());
});
// have RLS user query and see one row
database.setActiveUser(TESTRLS_HAS_RLS_VIEW);
database.tx(db -> {
assertEquals(1, db.getSchema(TEST_RLS).getTable(TEST_RLS).retrieveRows().size());
});
database.becomeAdmin();
database.removeUser(TESTRLS_HAS_RLS_VIEW);
assertEquals(false, database.hasUser(TESTRLS_HAS_RLS_VIEW));
} finally {
database.becomeAdmin();
}
}
use of org.molgenis.emx2.Query in project molgenis-emx2 by molgenis.
the class TestCrossSchemaForeignKeysAndInheritance method testRefArray.
@Test
public void testRefArray() {
Query q = schema2.getTable("PetLover").select(s("name"), s("pets", s("name"), s("species")));
assertEquals("dog", q.retrieveRows().get(1).getString("pets-species"));
System.out.println(q.retrieveJSON());
assertTrue(q.retrieveJSON().contains("dog"));
}
use of org.molgenis.emx2.Query in project packages-jpl by SWI-Prolog.
the class TestJUnit method testGetSolution1.
public void testGetSolution1() {
Query q = new Query("fail");
q.open();
q.getSolution();
assertTrue("an opened query on which getSolution has failed once is closed", !q.isOpen());
}
Aggregations