<commit_before><?php
/**
 * DBSteward unit test for mysql5 ddl generation
 *
 * @package DBSteward
 * @license http://www.opensource.org/licenses/bsd-license.php Simplified BSD License
 * @author <NAME> <<EMAIL>>
 */

require_once 'PHPUnit/Framework/TestCase.php';
require_once 'PHPUnit/Framework/TestSuite.php';

require_once __DIR__ . '/../../lib/DBSteward/dbsteward.php';
require_once __DIR__ . '/../mock_output_file_segmenter.php';

/**
 * @group mysql5
 */
class Mysql5BuildDataTest extends PHPUnit_Framework_TestCase {

  public function setUp() {
    dbsteward::set_sql_format('mysql5');
    dbsteward::$quote_schema_names = TRUE;
    dbsteward::$quote_table_names = TRUE;
    dbsteward::$quote_column_names = TRUE;
    dbsteward::$quote_function_names = TRUE;
    dbsteward::$quote_object_names = TRUE;
  }

  public function testSimple() {
    $xml = <<<XML
<dbsteward>
  <database>
    <host>db-host</host>
    <name>dbsteward</name>
    <role>
      <application>dbsteward_phpunit_app</application>
      <owner>deployment</owner>
      <replication/>
      <readonly/>
    </role>
  </database>
  <schema name="public" owner="ROLE_OWNER">
    <table name="result_list" owner="ROLE_OWNER" primaryKey="result_list_id" slonyId="7">
      <column name="result_list_id" type="integer"/>
      <column name="result" type="character varying(50)"/>
      <rows columns="result_list_id, result">
        <row>
          <col>0</col>
          <col>Passed</col>
        </row>
        <row>
          <col>1</col>
          <col>Incomplete</col>
        </row>
        <row>
          <col>2</col>
          <col>Failure</col>
        </row>
        <row>
          <col>3</col>
          <col>Error</col>
        </row>
      </rows>
    </table>
  </schema>
</dbsteward>
XML;
    
    $expected = <<<SQL
INSERT INTO `public`.`result_list` (`result_list_id`, `result`) VALUES (0, 'Passed');
INSERT INTO `public`.`result_list` (`result_list_id`, `result`) VALUES (1, 'Incomplete');
INSERT INTO `public`.`result_list` (`result_list_id`, `result`) VALUES (2, 'Failure');
INSERT INTO `public`.`result_list` (`result_list_id`, `result`) VALUES (3, 'Error');
SQL;

    $this->common($xml, $expected);
  }

  public function testSerials() {
    $xml = <<<XML
<dbsteward>
  <database>
    <host>db-host</host>
    <name>dbsteward</name>
    <role>
      <application>dbsteward_phpunit_app</application>
      <owner>deployment</owner>
      <replication/>
      <readonly/>
    </role>
  </database>
  <schema name="public" owner="ROLE_OWNER">
    <table name="result_list" owner="ROLE_OWNER" primaryKey="result_list_id" slonyId="7">
      <column name="result_list_id" type="serial"/>
      <column name="result" type="character varying(50)"/>
      <rows columns="result_list_id, result">
        <row>
          <col>0</col>
          <col>Passed</col>
        </row>
        <row>
          <col>1</col>
          <col>Incomplete</col>
        </row>
        <row>
          <col>2</col>
          <col>Failure</col>
        </row>
        <row>
          <col>3</col>
          <col>Error</col>
        </row>
      </rows>
    </table>
  </schema>
</dbsteward>
XML;
    
    $expected = <<<SQL
INSERT INTO `public`.`result_list` (`result_list_id`, `result`) VALUES (0, 'Passed');
INSERT INTO `public`.`result_list` (`result_list_id`, `result`) VALUES (1, 'Incomplete');
INSERT INTO `public`.`result_list` (`result_list_id`, `result`) VALUES (2, 'Failure');
INSERT INTO `public`.`result_list` (`result_list_id`, `result`) VALUES (3, 'Error');
SELECT setval('__public_result_list_result_list_id_serial_seq',MAX(`result_list_id`),TRUE) FROM `public`.`result_list`;
SQL;

    $this->common($xml, $expected);
  }

  public function testEmptiesNullsDefaults() {
    $xml = <<<XML
<dbsteward>
  <database>
    <host>db-host</host>
    <name>dbsteward</name>
    <role>
      <application>dbsteward_phpunit_app</application>
      <owner>deployment</owner>
      <replication/>
      <readonly/>
    </role>
  </database>
  <schema name="public" owner="ROLE_OWNER">
    <table name="result_list" owner="ROLE_OWNER" primaryKey="result_list_id" slonyId="7">
      <column name="result_list_id" type="integer"/>
      <column name="result" type="character varying(50)" null="false" default="xyz"/>
      <rows columns="result_list_id, result">
        <row>
          <col>0</col>
          <col></col>
        </row>
        <row>
          <col>1</col>
          <col null="true"></col>
        </row>
        <row>
          <col>2</col>
          <col empty="true"></col>
        </row>
      </rows>
    </table>
  </schema>
</dbsteward>
XML;
    
    $expected = <<<SQL
INSERT INTO `public`.`result_list` (`result_list_id`, `result`) VALUES (0, 'xyz');
INSERT INTO `public`.`result_list` (`result_list_id`, `result`) VALUES (1, NULL);
INSERT INTO `public`.`result_list` (`result_list_id`, `result`) VALUES (2, '');
SQL;

    $this->common($xml, $expected);
  }

  private function common($xml, $expected) {
    $dbs = new SimpleXMLElement($xml);
    $ofs = new mock_output_file_segmenter();

    dbsteward::$new_database = $dbs;
    $table_dependency = xml_parser::table_dependency_order($dbs);

    mysql5::build_data($dbs, $ofs, $table_dependency);

    $actual = $ofs->_get_output();
    
    // get rid of extra whitespace
    $expected = preg_replace("/^ +/m","",$expected);
    $expected = trim(preg_replace("/\n+/","\n",$expected));

    // echo $actual;

    // get rid of comments
    $actual = preg_replace("/\s*-- .*$/m",'',$actual);
    // get rid of extra whitespace
    $actual = preg_replace("/^ +/m","",$actual);
    $actual = trim(preg_replace("/\n+/","\n",$actual));

    $this->assertEquals($expected, $actual);
  }
}
?>
<commit_msg>Fix whitespace issue in Mysql5BuildDataTest<commit_after><?php
/**
 * DBSteward unit test for mysql5 ddl generation
 *
 * @package DBSteward
 * @license http://www.opensource.org/licenses/bsd-license.php Simplified BSD License
 * @author <NAME> <<EMAIL>>
 */

require_once 'PHPUnit/Framework/TestCase.php';
require_once 'PHPUnit/Framework/TestSuite.php';

require_once __DIR__ . '/../../lib/DBSteward/dbsteward.php';
require_once __DIR__ . '/../mock_output_file_segmenter.php';

/**
 * @group mysql5
 */
class Mysql5BuildDataTest extends PHPUnit_Framework_TestCase {

  public function setUp() {
    dbsteward::set_sql_format('mysql5');
    dbsteward::$quote_schema_names = TRUE;
    dbsteward::$quote_table_names = TRUE;
    dbsteward::$quote_column_names = TRUE;
    dbsteward::$quote_function_names = TRUE;
    dbsteward::$quote_object_names = TRUE;
  }

  public function testSimple() {
    $xml = <<<XML
<dbsteward>
  <database>
    <host>db-host</host>
    <name>dbsteward</name>
    <role>
      <application>dbsteward_phpunit_app</application>
      <owner>deployment</owner>
      <replication/>
      <readonly/>
    </role>
  </database>
  <schema name="public" owner="ROLE_OWNER">
    <table name="result_list" owner="ROLE_OWNER" primaryKey="result_list_id" slonyId="7">
      <column name="result_list_id" type="integer"/>
      <column name="result" type="character varying(50)"/>
      <rows columns="result_list_id, result">
        <row>
          <col>0</col>
          <col>Passed</col>
        </row>
        <row>
          <col>1</col>
          <col>Incomplete</col>
        </row>
        <row>
          <col>2</col>
          <col>Failure</col>
        </row>
        <row>
          <col>3</col>
          <col>Error</col>
        </row>
      </rows>
    </table>
  </schema>
</dbsteward>
XML;
    
    $expected = <<<SQL
INSERT INTO `public`.`result_list` (`result_list_id`, `result`) VALUES (0, 'Passed');
INSERT INTO `public`.`result_list` (`result_list_id`, `result`) VALUES (1, 'Incomplete');
INSERT INTO `public`.`result_list` (`result_list_id`, `result`) VALUES (2, 'Failure');
INSERT INTO `public`.`result_list` (`result_list_id`, `result`) VALUES (3, 'Error');
SQL;

    $this->common($xml, $expected);
  }

  public function testSerials() {
    $xml = <<<XML
<dbsteward>
  <database>
    <host>db-host</host>
    <name>dbsteward</name>
    <role>
      <application>dbsteward_phpunit_app</application>
      <owner>deployment</owner>
      <replication/>
      <readonly/>
    </role>
  </database>
  <schema name="public" owner="ROLE_OWNER">
    <table name="result_list" owner="ROLE_OWNER" primaryKey="result_list_id" slonyId="7">
      <column name="result_list_id" type="serial"/>
      <column name="result" type="character varying(50)"/>
      <rows columns="result_list_id, result">
        <row>
          <col>0</col>
          <col>Passed</col>
        </row>
        <row>
          <col>1</col>
          <col>Incomplete</col>
        </row>
        <row>
          <col>2</col>
          <col>Failure</col>
        </row>
        <row>
          <col>3</col>
          <col>Error</col>
        </row>
      </rows>
    </table>
  </schema>
</dbsteward>
XML;
    
    $expected = <<<SQL
INSERT INTO `public`.`result_list` (`result_list_id`, `result`) VALUES (0, 'Passed');
INSERT INTO `public`.`result_list` (`result_list_id`, `result`) VALUES (1, 'Incomplete');
INSERT INTO `public`.`result_list` (`result_list_id`, `result`) VALUES (2, 'Failure');
INSERT INTO `public`.`result_list` (`result_list_id`, `result`) VALUES (3, 'Error');
SELECT setval('__public_result_list_result_list_id_serial_seq', MAX(`result_list_id`), TRUE) FROM `public`.`result_list`;
SQL;

    $this->common($xml, $expected);
  }

  public function testEmptiesNullsDefaults() {
    $xml = <<<XML
<dbsteward>
  <database>
    <host>db-host</host>
    <name>dbsteward</name>
    <role>
      <application>dbsteward_phpunit_app</application>
      <owner>deployment</owner>
      <replication/>
      <readonly/>
    </role>
  </database>
  <schema name="public" owner="ROLE_OWNER">
    <table name="result_list" owner="ROLE_OWNER" primaryKey="result_list_id" slonyId="7">
      <column name="result_list_id" type="integer"/>
      <column name="result" type="character varying(50)" null="false" default="xyz"/>
      <rows columns="result_list_id, result">
        <row>
          <col>0</col>
          <col></col>
        </row>
        <row>
          <col>1</col>
          <col null="true"></col>
        </row>
        <row>
          <col>2</col>
          <col empty="true"></col>
        </row>
      </rows>
    </table>
  </schema>
</dbsteward>
XML;
    
    $expected = <<<SQL
INSERT INTO `public`.`result_list` (`result_list_id`, `result`) VALUES (0, 'xyz');
INSERT INTO `public`.`result_list` (`result_list_id`, `result`) VALUES (1, NULL);
INSERT INTO `public`.`result_list` (`result_list_id`, `result`) VALUES (2, '');
SQL;

    $this->common($xml, $expected);
  }

  private function common($xml, $expected) {
    $dbs = new SimpleXMLElement($xml);
    $ofs = new mock_output_file_segmenter();

    dbsteward::$new_database = $dbs;
    $table_dependency = xml_parser::table_dependency_order($dbs);

    mysql5::build_data($dbs, $ofs, $table_dependency);

    $actual = $ofs->_get_output();
    
    // get rid of extra whitespace
    $expected = preg_replace("/^ +/m","",$expected);
    $expected = trim(preg_replace("/\n+/","\n",$expected));

    // echo $actual;

    // get rid of comments
    $actual = preg_replace("/\s*-- .*$/m",'',$actual);
    // get rid of extra whitespace
    $actual = preg_replace("/^ +/m","",$actual);
    $actual = trim(preg_replace("/\n+/","\n",$actual));

    $this->assertEquals($expected, $actual);
  }
}
?>

{'fraction_non_alphanumeric': 0.17973629424011103, 'fraction_numerical': 0.006332408049965302, 'mean_word_length': 3.104307582769669, 'pattern_counts': {'":': 0, '<': 323, '<?xml version=': 0, '>': 297, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 18}, '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': '6596541', 'n_tokens_mistral': 4060, 'n_tokens_neox': 3978, 'n_words': 744}