Skip to content

Commit

Permalink
updated test suite
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.php.net/repository/pear/packages/SQL_Parser/trunk@263285 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information
Sebastian Mendel committed Jul 23, 2008
1 parent d72c5dd commit ca0ff4a
Show file tree
Hide file tree
Showing 19 changed files with 4,746 additions and 3,000 deletions.
36 changes: 14 additions & 22 deletions tests/AllTests.php
Expand Up @@ -7,8 +7,7 @@
require_once 'PHPUnit/Framework/TestCase.php';
require_once 'PHPUnit/Framework/TestSuite.php';
require_once 'PHPUnit/TextUI/TestRunner.php';
require_once 'SQL/Parser.php';

require_once './Parser.php';

class SQL_Parser_AllTests
{
Expand All @@ -24,24 +23,15 @@ public static function suite()
/*
* test files
*/
$tests = array(
'create' => 'tests/testcases/create.php',
'delete' => 'tests/testcases/delete.php',
'drop' => 'tests/testcases/drop.php',
'employment' => 'tests/testcases/employment.php',
'insert' => 'tests/testcases/insert.php',
'select' => 'tests/testcases/select.php',
'tables' => 'tests/testcases/tables.php',
'update' => 'tests/testcases/update.php',
);
$tests = glob('tests/testcases/*.php');

/*
* add test cases
*/
foreach ($tests as $name => $file) {
foreach ($tests as $file) {
include $file;
foreach ($tests as $nr => $test) {
$test_name = $name . ' #' . ($nr + 1);
$test_name = substr(basename($file), 0, -4) . ' #' . ($nr + 1);
$test_case = new PHPUnit_Framework_TestCase_Sql_Parser($test, $test_name);
$suite->addTest($test_case);
}
Expand Down Expand Up @@ -77,14 +67,16 @@ public function runTest()

// unify line endings in error messages
if (is_string($this->_test['expect']) && is_string($result)) {
//$expected = preg_replace('/[\r\n]+/', "\n", $this->_test['expect']);
//$result = preg_replace('/[\r\n]+/', "\n", $result);
$message = 'SQL still fails to be parsed';
$message .= "\nSQL: " . $this->_test['sql'] . "\n";
$message .= "\nExpected:\n [array with parsed SQL]";
$message .= "\nResult:\n" . $result;
$message .= "\n*********************\n";
$this->fail($message);
if (false === $this->_test['fail']) {
//$expected = preg_replace('/[\r\n]+/', "\n", $this->_test['expect']);
//$result = preg_replace('/[\r\n]+/', "\n", $result);
$message = 'SQL still fails to be parsed';
$message .= "\nSQL: " . $this->_test['sql'] . "\n";
$message .= "\nExpected:\n [array with parsed SQL]";
$message .= "\nResult:\n" . $result;
$message .= "\n*********************\n";
$this->fail($message);
}
} elseif (is_string($this->_test['expect'])) {
// a prior failed test now runs fine
$this->fail('SQL seems to run fine now, please update the expected test result!');
Expand Down
75 changes: 44 additions & 31 deletions tests/generate_testcases.php
Expand Up @@ -17,38 +17,35 @@
// | License along with this library; if not, write to the Free Software |
// | Foundation, Inc., 59 Temple Place, Suite 330,Boston,MA 02111-1307 USA|
// +----------------------------------------------------------------------+
// | Author: Brent Cook <busterb@mail.utexas.edu> |
// | Author: Sebastian Mendel <info@sebastianmendel.e> |
// | Brent Cook <busterb@mail.utexas.edu> |
// +----------------------------------------------------------------------+
//
// $Id$
//

// test functionality of the sql parser
chdir('..');

require_once 'PEAR.php';
require_once 'SQL/Parser.php';
require_once './Parser.php';

$parser = new Sql_Parser();

$progname = basename(array_shift($argv));

$argc = count($argv);
if ($argc < 1 || $argc > 3) {
echo("Usage: generate_testcases.php test_cases.sql <dialect>\n");
if ($argc > 3) {
echo("Usage: generate_testcases.php <test_cases.sql <dialect>>\n");
exit(-1);
}

// Preprocess the input file
$file = $argv[0];
if (!$fd = @fopen($file, 'r')) {
echo("Could not load the SQL source file: $file\n");
exit(-1);
}
$source = '';
while ($data = fread($fd, 2048)) {
$source .= $data;
if ($argc >= 1) {
$files = $argv[0];
} else {
$files = '*.sql';
}
fclose($fd);

// Set the dialect
if ($argc == 2) {
Expand All @@ -63,27 +60,43 @@
exit;
}

$queries = explode(";\n", $source);
foreach (glob('tests/sql/' . $files) as $file) {
echo '.';
$source = file_get_contents($file);
if (! $source) {
echo("Could not load the SQL source file: $file\n");
exit(-1);
}

$queries = explode("-- SQL PARSER TESTCASE", $source);

echo "<?php\n";
echo '$tests = array('."\n";
$testcases = array();

foreach ($queries as $query) {
if ($query) {
echo "array(\n";
echo "'sql' => '".preg_replace("/([\'\\\])/", "\\\\\\1", $query)."',\n";
$results = $parser->parse($query);
echo "'expect' => ";
if (PEAR::isError($results)) {
echo "'".preg_replace("/([\'\\\])/", "\\\\\\1", $results->getMessage())."'\n";
} else {
echo var_export($results, true);
foreach ($queries as $query) {
if ($query) {
echo ':';
if (strpos(trim($query), '-- SQL_PARSER_FLAG_FAIL') === 0) {
$fail = true;
} else {
$fail = false;
}
$results = $parser->parse($query);
//if (PEAR::isError($results)) {
if (false === $results) {
$result = $parser->error_message;
} else {
$result = $results;
}

$testcases[] = array(
'sql' => $query,
'expect' => $result,
'fail' => $fail,
);
}
echo "\n),\n";
}
}

echo ");\n";
echo "?>\n";

$output = "<?php\n\$tests = " . var_export($testcases, true) . ";\n?>\n";
$r = file_put_contents('tests/testcases/' . substr(basename($file), 0, -3) . 'php', $output);
}
?>
84 changes: 53 additions & 31 deletions tests/sql/create.sql
@@ -1,52 +1,74 @@
-- SQL PARSER TESTCASE
CREATE TABLE albums (
name varchar(60),
directory varchar(60),
rating enum (1,2,3,4,5,6,7,8,9,10) NOT NULL,
category set('sexy','\'family time\'',"outdoors",'generic','very weird') NULL,
description text NULL,
id int default 200 PRIMARY KEY
name varchar(60),
directory varchar(60),
rating enum (1,2,3,4,5,6,7,8,9,10) NOT NULL,
category set('sexy','\'family time\'',"outdoors",'generic','very weird') NULL,
description text NULL,
id int default 200 PRIMARY KEY
);

-- SQL PARSER TESTCASE
CREATE TABLE photos (
filename varchar(60) not NULL,
name varchar(60) default 'no name',
album int,
price float (4,2),
description text default 'hello',
id int default 0 primary key not null,
filename varchar(60) not NULL,
name varchar(60) default 'no name',
album int,
price float (4,2),
description text default 'hello',
id int default 0 primary key not null,
);
-- SQL PARSER TESTCASE
create table brent (
filename varchar(10),
description varchar(20),
);
-- SQL PARSER TESTCASE
CREATE TABLE films (
code CHARACTER(5) CONSTRAINT firstkey PRIMARY KEY,
title CHARACTER VARYING(40) NOT NULL,
did DECIMAL(3) NOT NULL,
date_prod DATE,
kind CHAR(10),
len INTERVAL HOUR TO MINUTE
CONSTRAINT production UNIQUE(date_prod)
code CHARACTER(5) CONSTRAINT firstkey PRIMARY KEY,
title CHARACTER VARYING(40) NOT NULL,
did DECIMAL(3) NOT NULL,
date_prod DATE,
kind CHAR(10),
len INTERVAL HOUR TO MINUTE
CONSTRAINT production UNIQUE(date_prod)
);
-- SQL PARSER TESTCASE
CREATE TABLE films (
code CHARACTER(5) CONSTRAINT firstkey PRIMARY KEY,
title CHARACTER VARYING(40) NOT NULL,
did DECIMAL(3) NOT NULL,
date_prod DATE,
kind CHAR(10),
len INTERVAL minute to hour
CONSTRAINT production UNIQUE(date_prod)
code CHARACTER(5) CONSTRAINT firstkey PRIMARY KEY,
title CHARACTER VARYING(40) NOT NULL,
did DECIMAL(3) NOT NULL,
date_prod DATE,
kind CHAR(10),
len INTERVAL minute to hour
CONSTRAINT production UNIQUE(date_prod)
);
-- SQL PARSER TESTCASE
CREATE TABLE distributors (
did DECIMAL(3) PRIMARY KEY DEFAULT NEXTVAL('serial'),
name VARCHAR(40) NOT NULL CHECK (name <> '')
CONSTRAINT con1 CHECK (did > 100 AND name > '')
did DECIMAL(3) PRIMARY KEY DEFAULT NEXTVAL('serial'),
name VARCHAR(40) NOT NULL CHECK (name <> '')
CONSTRAINT con1 CHECK (did > 100 AND name > '')
);
-- SQL PARSER TESTCASE
CREATE TABLE distributors (
did DECIMAL(3) PRIMARY KEY,
name VARCHAR(40)
did DECIMAL(3) PRIMARY KEY,
name VARCHAR(40)
);

-- SQL PARSER TESTCASE
CREATE TABLE msgs ( user_id integer, msg_id integer, msg_text varchar, msg_title varchar(30), msg_date time);

-- SQL PARSER TESTCASE
-- SQL_PARSER_FLAG_FAIL
create table nodefinitions;

-- SQL PARSER TESTCASE
-- SQL_PARSER_FLAG_FAIL
create dogfood;

-- SQL PARSER TESTCASE
-- SQL_PARSER_FLAG_FAIL
create table dunce (name varchar;

-- SQL PARSER TESTCASE
-- SQL_PARSER_FLAG_FAIL
create table dunce (name varchar(2,3));
12 changes: 12 additions & 0 deletions tests/sql/delete.sql
@@ -1,5 +1,17 @@
-- SQL PARSER TESTCASE
delete from dog where cat = 4 and horse <> "dead meat" or mouse = 'furry';

-- SQL PARSER TESTCASE
-- SQL_PARSER_FLAG_FAIL
delete from;

-- SQL PARSER TESTCASE
delete from cat;

-- SQL PARSER TESTCASE
-- SQL_PARSER_FLAG_FAIL
delete from where cat = 53;

-- SQL PARSER TESTCASE
-- SQL_PARSER_FLAG_FAIL
delete from dog where mouse is happy;
16 changes: 16 additions & 0 deletions tests/sql/drop.sql
@@ -1,5 +1,21 @@
-- SQL PARSER TESTCASE
drop table dishes cascade;

-- SQL PARSER TESTCASE
drop table bondage restrict;

-- SQL PARSER TESTCASE
-- SQL_PARSER_FLAG_FAIL
drop table bondage, dishes;

-- SQL PARSER TESTCASE
-- SQL_PARSER_FLAG_FAIL
drop table play cascade restrict;

-- SQL PARSER TESTCASE
-- SQL_PARSER_FLAG_FAIL
drop table cat where mouse = floor;

-- SQL PARSER TESTCASE
-- SQL_PARSER_FLAG_FAIL
drop elephant;

0 comments on commit ca0ff4a

Please sign in to comment.