use of com.helger.genericode.v10.Row 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.helger.genericode.v10.Row in project molgenis-emx2 by molgenis.
the class TestComputedValue method test1.
@Test
public void test1() {
Table t = schema.create(table("Test1", column("id").setPkey(), column("computed").setComputed("1;")));
t.insert(new Row().set("id", 1));
assertEquals(1, (int) t.query().retrieveRows().get(0).getInteger("computed"));
t = schema.create(table("Test2", column("id").setPkey(), column("computed").setComputed("id")));
t.insert(new Row().set("id", 1));
assertEquals(1, (int) t.query().retrieveRows().get(0).getInteger("computed"));
// t = schema.create(table("Test3", column("id").pkey(), column("computed").computed("id")));
// List<Row> rows = new ArrayList<>();
// for (int i = 0; i < 10000; i++) {
// rows.add(new Row().set("id", i));
// }
//
// // perf test;
// StopWatch.start("start");
// t.insert(rows);
// StopWatch.print("complete", 10000);
}
use of com.helger.genericode.v10.Row in project molgenis-emx2 by molgenis.
the class TestExcelStore method testSimple.
@Test
public void testSimple() throws IOException {
List<Row> rows = new ArrayList<Row>();
for (int i = 0; i < 10; i++) {
Row r = new Row();
r.set("name", "test" + i);
r.set("id", i);
rows.add(r);
}
Path tmp = Files.createTempDirectory("TestExcelStore");
tmp.toFile().deleteOnExit();
Path excelFile = tmp.resolve("test.xlsx");
TableStoreForXlsxFile store = new TableStoreForXlsxFile(excelFile);
store.writeTable("test", List.of(), rows);
// and read
store = new TableStoreForXlsxFile(excelFile);
List<Row> rows2 = store.readTable("test");
assertEquals(10, rows2.size());
// empty rows
excelFile = tmp.resolve("test.xlsx");
store = new TableStoreForXlsxFile(excelFile);
store.writeTable("test2", List.of("blaat"), new ArrayList<>());
}
use of com.helger.genericode.v10.Row 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.helger.genericode.v10.Row in project molgenis-emx2 by molgenis.
the class TestMgColumns method testUpdatedOn.
@Test
public void testUpdatedOn() {
Table t = schema.create(table("UpdatedOn", column("id").setPkey()));
t.insert(row("id", 1));
Row r = t.retrieveRows().get(0);
Assert.assertNotNull(r.getDateTime(MG_INSERTEDON));
Assert.assertEquals(r.getDateTime(MG_INSERTEDON), r.getDateTime(MG_UPDATEDON));
Assert.assertNotNull(r.getString(MG_INSERTEDBY));
Assert.assertEquals(r.getString(MG_UPDATEDBY), r.getString(MG_INSERTEDBY));
t.update(r);
r = t.retrieveRows().get(0);
Assert.assertTrue(r.getDateTime(MG_INSERTEDON).compareTo(r.getDateTime(MG_UPDATEDON)) < 0);
// to make sure also test with subclass
t = schema.create(table("UpdatedOnSub").setInherit("UpdatedOn"));
t.insert(row("id", 2));
r = t.retrieveRows().get(0);
Assert.assertNotNull(r.getDateTime(MG_INSERTEDON));
Assert.assertEquals(r.getDateTime(MG_INSERTEDON), r.getDateTime(MG_UPDATEDON));
Assert.assertNotNull(r.getString(MG_INSERTEDBY));
Assert.assertEquals(r.getString(MG_UPDATEDBY), r.getString(MG_INSERTEDBY));
t.update(r);
r = t.retrieveRows().get(0);
Assert.assertTrue(r.getDateTime(MG_INSERTEDON).compareTo(r.getDateTime(MG_UPDATEDON)) < 0);
}
Aggregations