Search in sources :

Example 46 with Database

use of com.google.spanner.admin.database.v1.Database in project molgenis-emx2 by molgenis.

the class TestSettingsAndMembersLoading method testExcelTypesCast.

@Test
public void testExcelTypesCast() {
    Database database = TestDatabaseFactory.getTestDatabase();
    Schema schema = database.dropCreateSchema(TestSettingsAndMembersLoading.class.getSimpleName());
    ClassLoader classLoader = getClass().getClassLoader();
    Path path = new File(classLoader.getResource("settings_and_members.xlsx").getFile()).toPath();
    new ImportExcelTask(path, schema, true).run();
    assertEquals("key1", schema.getTable("table1").getMetadata().getSettings().get(0).key());
    assertEquals("value1", schema.getTable("table1").getMetadata().getSettings().get(0).value());
    assertEquals("key2", schema.getMetadata().getSettings().get(0).key());
    assertEquals("value2", schema.getMetadata().getSettings().get(0).value());
    assertEquals(1, schema.getMembers().size());
    assertEquals("anonymous", schema.getMembers().get(0).getUser());
    assertEquals("Viewer", schema.getMembers().get(0).getRole());
    database.dropSchema(schema.getName());
}
Also used : Path(java.nio.file.Path) Schema(org.molgenis.emx2.Schema) Database(org.molgenis.emx2.Database) File(java.io.File) Test(org.junit.Test)

Example 47 with Database

use of com.google.spanner.admin.database.v1.Database in project molgenis-emx2 by molgenis.

the class MolgenisSessionManager method createSessionListener.

/**
 * takes care of creating and destroying session attributes when Jetty creates/destroys sessions
 */
private EventListener createSessionListener() {
    MolgenisSessionManager _this = this;
    return new HttpSessionListener() {

        public void sessionCreated(HttpSessionEvent httpSessionEvent) {
            logger.info("Initializing session");
            // create private database wrapper to session
            Database database = new SqlDatabase(false);
            // set default use to "anonymous"
            database.setActiveUser("anonymous");
            // create session and add to sessions lists so we can also access all active
            // sessions
            MolgenisSession molgenisSession = new MolgenisSession(database);
            sessions.put(httpSessionEvent.getSession().getId(), molgenisSession);
            logger.info("session created: " + httpSessionEvent.getSession().getId());
            // create listener
            database.setListener(new MolgenisSessionManagerDatabaseListener(_this, molgenisSession));
        }

        public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
            // remove from session pool
            sessions.remove(httpSessionEvent.getSession().getId());
            logger.info("session destroyed: " + httpSessionEvent.getSession().getId());
        }
    };
}
Also used : HttpSessionListener(javax.servlet.http.HttpSessionListener) HttpSessionEvent(javax.servlet.http.HttpSessionEvent) Database(org.molgenis.emx2.Database) SqlDatabase(org.molgenis.emx2.sql.SqlDatabase) SqlDatabase(org.molgenis.emx2.sql.SqlDatabase)

Example 48 with Database

use of com.google.spanner.admin.database.v1.Database in project molgenis-emx2 by molgenis.

the class InitTestDatabaseAndRunNonParallelTests method createDatabase.

@BeforeClass
public static void createDatabase() {
    // we want this run only once and NOT parallel for total test suite
    // AND we want run all other tests in parallel
    // so tests are in molgenis-emx2-sql-it ('integration test')
    // and 'init' only happence once, here
    Database db = new SqlDatabase(true);
    assertTrue(db.getDatabaseVersion() > 0);
}
Also used : Database(org.molgenis.emx2.Database) BeforeClass(org.junit.BeforeClass)

Example 49 with Database

use of com.google.spanner.admin.database.v1.Database in project molgenis-emx2 by molgenis.

the class RunWebApi method main.

public static void main(String[] args) throws IOException {
    // create data source
    HikariDataSource dataSource = new HikariDataSource();
    String url = "jdbc:postgresql:molgenis";
    dataSource.setJdbcUrl(url);
    dataSource.setUsername("molgenis");
    dataSource.setPassword("molgenis");
    dataSource.setPassword("molgenis");
    // setup
    Database db = TestDatabaseFactory.getTestDatabase();
    Schema schema = db.dropCreateSchema("pet store");
    PetStoreExample.create(schema.getMetadata());
    PetStoreExample.populate(schema);
    MolgenisWebservice.start(8080);
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) Schema(org.molgenis.emx2.Schema) Database(org.molgenis.emx2.Database)

Example 50 with Database

use of com.google.spanner.admin.database.v1.Database in project molgenis-emx2 by molgenis.

the class TestTruncate method testTruncate.

@Test
public void testTruncate() {
    Database db = TestDatabaseFactory.getTestDatabase();
    Schema schema = db.dropCreateSchema(TestTruncate.class.getSimpleName());
    // create simple table, add data, and truncate
    Table table1 = schema.create(table("Table1", column("name").setPkey()));
    table1.insert(row("name", "a"));
    Assert.assertEquals(1, table1.retrieveRows().size());
    table1.truncate();
    Assert.assertEquals(0, table1.retrieveRows().size());
    // create with subclass
    Table table2 = schema.create(table("Table2").setInherit("Table1").add(column("col1")));
    table1.insert(row("name", "a"));
    table2.insert(row("name", "b", "col1", "checkb"));
    Assert.assertEquals(2, table1.retrieveRows().size());
    Assert.assertEquals(1, table2.retrieveRows().size());
    table1.truncate();
    Assert.assertEquals(1, table1.retrieveRows().size());
    Assert.assertEquals(1, table2.retrieveRows().size());
    table2.truncate();
    Assert.assertEquals(0, table1.retrieveRows().size());
    Assert.assertEquals(0, table2.retrieveRows().size());
    // create with subclass of a subclass
    Table table3 = schema.create(table("Table3").setInherit("Table2").add(column("col2")));
    table1.insert(row("name", "a"));
    table2.insert(row("name", "b", "col1", "checkb"));
    table3.insert(row("name", "c", "col1", "checkc", "col2", "checkc"));
    Assert.assertEquals(3, table1.retrieveRows().size());
    Assert.assertEquals(2, table2.retrieveRows().size());
    Assert.assertEquals(1, table3.retrieveRows().size());
    // leaves subclass?!!! is this expected behavior?
    table2.truncate();
    Assert.assertEquals(2, table1.retrieveRows().size());
    // !!!
    Assert.assertEquals(1, table2.retrieveRows().size());
    Assert.assertEquals(1, table3.retrieveRows().size());
    table3.truncate();
    Assert.assertEquals(1, table1.retrieveRows().size());
    Assert.assertEquals(0, table2.retrieveRows().size());
    Assert.assertEquals(0, table3.retrieveRows().size());
    table1.truncate();
    Assert.assertEquals(0, table1.retrieveRows().size());
    Assert.assertEquals(0, table2.retrieveRows().size());
    Assert.assertEquals(0, table3.retrieveRows().size());
}
Also used : Table(org.molgenis.emx2.Table) Schema(org.molgenis.emx2.Schema) Database(org.molgenis.emx2.Database) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)53 CreateDatabaseMetadata (com.google.spanner.admin.database.v1.CreateDatabaseMetadata)32 Database (com.google.cloud.spanner.Database)31 ArrayList (java.util.ArrayList)25 AbstractMessage (com.google.protobuf.AbstractMessage)24 ExecutionException (java.util.concurrent.ExecutionException)22 Database (com.google.spanner.admin.database.v1.Database)21 Operation (com.google.longrunning.Operation)18 ByteString (com.google.protobuf.ByteString)16 DatabaseAdminClient (com.google.cloud.spanner.DatabaseAdminClient)13 SpannerException (com.google.cloud.spanner.SpannerException)12 Database (org.molgenis.emx2.Database)12 CreateBackupMetadata (com.google.spanner.admin.database.v1.CreateBackupMetadata)11 DatabaseName (com.google.spanner.admin.database.v1.DatabaseName)10 RestoreDatabaseMetadata (com.google.spanner.admin.database.v1.RestoreDatabaseMetadata)9 Timestamp (com.google.cloud.Timestamp)8 Spanner (com.google.cloud.spanner.Spanner)8 RestoreDatabaseRequest (com.google.spanner.admin.database.v1.RestoreDatabaseRequest)7 UpdateDatabaseDdlMetadata (com.google.spanner.admin.database.v1.UpdateDatabaseDdlMetadata)7 StatusRuntimeException (io.grpc.StatusRuntimeException)7