<commit_before>/**
 * Copyright (C) 2013 Orbeon, Inc.
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the
 * GNU Lesser General Public License as published by the Free Software Foundation; either version
 * 2.1 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * The full text of the license is available at http://www.gnu.org/copyleft/lesser.html
 */
package org.orbeon.oxf.fr.persistence.ddl

import java.sql.Connection
import org.junit.Test
import org.orbeon.oxf.fr.persistence.DB
import org.orbeon.oxf.test.ResourceManagerTestBase
import org.scalatest.junit.AssertionsForJUnit

/**
 * Test the DDL we provide to create and update databases.
 */
class DDLTest extends ResourceManagerTestBase with AssertionsForJUnit {

    private def withNewDatabase[T](block: Connection ⇒ T): T = {

        try {
            val createUserAndDatabase = Seq(
                "create user orbeon_ddl@localhost identified by 'orbeon_ddl'",
                "create database orbeon_ddl",
                "grant all privileges on orbeon_ddl.* to orbeon_ddl@localhost"
            )
            DB.asRoot(createUserAndDatabase foreach _.createStatement.executeUpdate)
            DB.asOrbeon(block)
        } finally {
            val dropUserAndDatabase = Seq(
                "drop user orbeon_ddl@localhost",
                "drop database orbeon_ddl"
            )
            DB.asRoot(dropUserAndDatabase foreach _.createStatement.executeUpdate)
        }
    }

    // Runs the SQL, and returns the DDL for the tables as defined in the database
    private def sqlToDDL(connection: Connection, sql: Seq[String]): Map[String, String] = {
        val statement = connection.createStatement
        DB.getTableNames(connection).map { tableName ⇒
            val tableResultSet = statement.executeQuery("show create table " + tableName)
            tableResultSet.next()
            val tableDDL = tableResultSet.getString(2)
            (tableName, tableDDL)
        }.toMap
    }

    @Test def createAndUpgradeTest(): Unit = {<commit_msg>Fix: actually run the DDL<commit_after>/**
 * Copyright (C) 2013 Orbeon, Inc.
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the
 * GNU Lesser General Public License as published by the Free Software Foundation; either version
 * 2.1 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * The full text of the license is available at http://www.gnu.org/copyleft/lesser.html
 */
package org.orbeon.oxf.fr.persistence.ddl

import java.sql.Connection
import org.junit.Test
import org.orbeon.oxf.fr.persistence.DB
import org.orbeon.oxf.test.ResourceManagerTestBase
import org.scalatest.junit.AssertionsForJUnit

/**
 * Test the DDL we provide to create and update databases.
 */
class DDLTest extends ResourceManagerTestBase with AssertionsForJUnit {

    private def withNewDatabase[T](block: Connection ⇒ T): T = {

        try {
            val createUserAndDatabase = Seq(
                "create user orbeon_ddl@localhost identified by 'orbeon_ddl'",
                "create database orbeon_ddl",
                "grant all privileges on orbeon_ddl.* to orbeon_ddl@localhost"
            )
            DB.asRoot(createUserAndDatabase foreach _.createStatement.executeUpdate)
            DB.asOrbeon(block)
        } finally {
            val dropUserAndDatabase = Seq(
                "drop user orbeon_ddl@localhost",
                "drop database orbeon_ddl"
            )
            DB.asRoot(dropUserAndDatabase foreach _.createStatement.executeUpdate)
        }
    }

    // Runs the SQL, and returns the DDL for the tables as defined in the database
    private def sqlToDDL(connection: Connection, sql: Seq[String]): Map[String, String] = {
        val statement = connection.createStatement
        sql foreach statement.executeUpdate
        DB.getTableNames(connection).map { tableName ⇒
            val tableResultSet = statement.executeQuery("show create table " + tableName)
            tableResultSet.next()
            val tableDDL = tableResultSet.getString(2)
            (tableName, tableDDL)
        }.toMap
    }

    @Test def createAndUpgradeTest(): Unit = {
{'fraction_non_alphanumeric': 0.07445008460236886, 'fraction_numerical': 0.002961082910321489, 'mean_word_length': 2.8045052292839903, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5324486', 'n_tokens_mistral': 1305, 'n_tokens_neox': 1274, 'n_words': 437}