use of com.reprezen.kaizen.oasparser.model3.Schema in project molgenis-emx2 by molgenis.
the class Benchmark method testCopyInAndOut.
public void testCopyInAndOut() {
Database database = TestDatabaseFactory.getTestDatabase();
Schema schema = database.dropCreateSchema(Benchmark.class.getSimpleName());
int aSize = 50;
int bSize = 100000;
Table a = schema.create(table("TableA").add(column("ID").setPkey()));
List<String> values = new ArrayList<>();
Table b = schema.create(table("TableB").add(column("ID").setPkey()).add(column("ref").setType(REF_ARRAY).setRefTable("TableA")));
// Table c =
// schema.create(
// table("TableC")
// .add(column("ID").setPkey())
// .add(column("ref").setType(MREF).setRefTable("TableA")));
StopWatch.start("benchmark started");
List<Row> aRows = new ArrayList<>();
for (int i = 0; i < aSize; i++) {
aRows.add(new Row().set("ID", "row" + i));
values.add("row" + i);
}
StopWatch.start("benchmark1");
a.insert(aRows);
StopWatch.print("inserted primary", aSize);
aRows.clear();
List<Row> bRows = new ArrayList<>();
for (int i = 0; i < bSize; i++) {
bRows.add(new Row().set("ID", "row" + i).set("ref", values));
}
StopWatch.start("benchmark2 started");
b.insert(bRows);
StopWatch.print("inserted ref_array", bSize);
bRows.clear();
// List<Row> cRows = new ArrayList<>();
// for (int i = 0; i < bSize; i++) {
// cRows.add(new Row().set("ID", "row" + i).set("ref", values));
// }
// StopWatch.start("benchmark3 started");
// c.insert(cRows);
// StopWatch.print("inserted mref", bSize);
// cRows.clear();
// StopWatch.print("inserted mref", bSize);
// ref_array
}
use of com.reprezen.kaizen.oasparser.model3.Schema in project molgenis-emx2 by molgenis.
the class TestCompositeForeignKeys method testCompositeRef.
@Test
public void testCompositeRef() {
Schema schema = database.dropCreateSchema(TestCompositeForeignKeys.class.getSimpleName() + "Ref");
schema.create(table("Person", column("firstName").setPkey(), column("lastName").setPkey(), column("uncle", REF).setRefTable("Person")));
Table p = schema.getTable("Person");
p.insert(new Row().setString("firstName", "Donald").setString("lastName", "Duck"));
try {
p.insert(new Row().setString("firstName", "Kwik").setString("lastName", "Duck").setString("uncle.firstName", "Donald").setString("uncle.lastName", "MISSING"));
fail("should have failed on missing foreign key");
} catch (Exception e) {
System.out.println("errored correctly: " + e);
}
p.insert(new Row().setString("firstName", "Kwik").setString("lastName", "Duck").setString("uncle.firstName", "Donald").setString("uncle.lastName", "Duck"));
p.insert(new Row().setString("firstName", "Kwek").setString("lastName", "Duck").setString("uncle.firstName", "Donald").setString("uncle.lastName", "Duck"));
p.insert(new Row().setString("firstName", "Kwak").setString("lastName", "Duck").setString("uncle.firstName", "Donald").setString("uncle.lastName", "Duck"));
try {
p.delete(new Row().setString("firstName", "Donald").setString("lastName", "Duck"));
fail("should have failed on foreign key (Donald is used in foreign key)");
} catch (Exception e) {
System.out.println("errored correctly: " + e);
}
schema.create(table("Student").setInherit("Person"));
Table s = schema.getTable("Student");
s.insert(new Row().setString("firstName", "Mickey").setString("lastName", "Mouse").setString("uncle.firstName", "Kwik").setString("uncle.lastName", "Duck"));
String result = schema.query("Student", s("firstName"), s("lastName"), s("uncle", s("firstName"), s("lastName")), s("uncle", s("firstName"), s("lastName"))).retrieveJSON();
System.out.println(result);
result = schema.query("Student").select(s("firstName"), s("lastName")).where(or(and(f("firstName", EQUALS, "Donald"), f("lastName", EQUALS, "Duck")), and(f("firstName", EQUALS, "Mickey"), f("lastName", EQUALS, "Mouse")))).retrieveJSON();
System.out.println(result);
assertTrue(result.contains("Mouse"));
assertFalse(result.contains("Duck"));
result = schema.query("Person").select(s("firstName"), s("lastName")).where(or(and(f("firstName", EQUALS, "Donald"), f("lastName", EQUALS, "Duck")), and(f("firstName", EQUALS, "Mickey"), f("lastName", EQUALS, "Mouse")))).retrieveJSON();
System.out.println(result);
assertTrue(result.contains("Mouse"));
assertTrue(result.contains("Duck"));
// composite key filter
result = schema.query("Person").select(s("firstName"), s("lastName")).where(f("uncle", or(and(f("firstName", EQUALS, "Donald"), f("lastName", EQUALS, "Duck")), and(f("firstName", EQUALS, "Mickey"), f("lastName", EQUALS, "Mouse"))))).retrieveJSON();
System.out.println(result);
assertTrue(result.contains("Kwik"));
assertFalse(result.contains("Mouse"));
// refback
schema.getTable("Person").getMetadata().add(column("nephew").setType(REFBACK).setRefTable("Person").setRefBack("uncle"));
s.insert(new Row().setString("firstName", "Katrien").setString("lastName", "Duck").setString("nephew.firstName", "Kwik").setString("nephew.lastName", // I know, not true
"Duck"));
assertTrue(List.of(s.query().select(s("firstName"), s("lastName"), s("nephew", s("firstName"), s("lastName")), s("uncle", s("firstName"), s("lastName"))).where(f("firstName", EQUALS, "Katrien")).retrieveRows().get(0).getStringArray("nephew-firstName")).contains("Kwik"));
assertTrue(List.of(p.query().select(s("firstName"), s("lastName"), s("nephew", s("firstName"), s("lastName")), s("uncle", s("firstName"), s("lastName"))).where(f("firstName", EQUALS, "Kwik")).retrieveRows().get(0).getStringArray("uncle-firstName")).contains("Katrien"));
// test order by for refback
p.query().select(s("firstName"), s("lastName"), s("nephew", s("firstName"), s("lastName")), s("uncle", s("firstName"), s("lastName"))).orderBy("nephew").retrieveJSON();
}
use of com.reprezen.kaizen.oasparser.model3.Schema 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 com.reprezen.kaizen.oasparser.model3.Schema in project molgenis-emx2 by molgenis.
the class TestSettings method testSchemaSettings.
@Test
public void testSchemaSettings() {
database.tx(// prevent side effect of user changes on other tests using tx
db -> {
Schema s = db.dropCreateSchema("testSchemaSettings");
// set roles
// viewer should only be able to see, not change
// manager should be able to set values
s.addMember("testsettingseditor", EDITOR.toString());
s.addMember("testsettingsmanager", MANAGER.toString());
db.setActiveUser("testsettingseditor");
try {
// reload schema
s = db.getSchema("testSchemaSettings");
s.getMetadata().setSetting("key", "value");
fail("editors should not be able to change schema settings");
} catch (Exception e) {
// failed correctly
}
db.setActiveUser("testsettingsmanager");
try {
// reload schema
s = db.getSchema("testSchemaSettings");
s.getMetadata().setSetting("key", "value");
} catch (Exception e) {
e.printStackTrace();
fail("managers should be able to change schema settings");
}
assertEquals("key", s.getMetadata().getSettings().get(0).key());
assertEquals("value", s.getMetadata().getSettings().get(0).value());
assertEquals("key", db.getSchema("testSchemaSettings").getMetadata().getSettings().get(0).key());
assertEquals("value", db.getSchema("testSchemaSettings").getMetadata().getSettings().get(0).value());
db.clearCache();
assertEquals("key", db.getSchema("testSchemaSettings").getMetadata().getSettings().get(0).key());
assertEquals("value", db.getSchema("testSchemaSettings").getMetadata().getSettings().get(0).value());
db.becomeAdmin();
});
}
use of com.reprezen.kaizen.oasparser.model3.Schema in project molgenis-emx2 by molgenis.
the class TestSettings method testTableSettings.
@Test
public void testTableSettings() {
database.tx(// prevent side effect of user changes on other tests using tx
db -> {
Schema s = db.dropCreateSchema("testTableSettings");
// set roles
// viewer should only be able to see, not change
// editor should be able to set values
s.addMember("testtablesettingsviewer", VIEWER.toString());
s.addMember("testtablesettingseditor", EDITOR.toString());
s.create(table("test").add(column("test")));
db.setActiveUser("testtablesettingsviewer");
try {
Table t = db.getSchema("testTableSettings").getTable("test");
t.getMetadata().setSetting("key", "value");
fail("viewers should not be able to change schema settings");
} catch (Exception e) {
// failed correctly
}
db.setActiveUser("testtablesettingseditor");
try {
Table t = db.getSchema("testTableSettings").getTable("test");
t.getMetadata().setSetting("key", "value");
} catch (Exception e) {
e.printStackTrace();
fail("managers should be able to change schema settings");
}
db.clearCache();
List<Setting> test = db.getSchema("testTableSettings").getTable("test").getMetadata().getSettings();
assertEquals(1, test.size());
assertEquals("key", test.get(0).key());
assertEquals("value", test.get(0).value());
assertEquals("key", db.getSchema("testTableSettings").getTable("test").getMetadata().getSettings().get(0).key());
assertEquals("value", db.getSchema("testTableSettings").getTable("test").getMetadata().getSettings().get(0).value());
db.becomeAdmin();
});
}
Aggregations