Added unit tests for correlated subqueries - select, from, where.

git-svn-id: http://xe-core.googlecode.com/svn/branches/1.5.0-DB@8556 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
ucorina 2011-06-30 15:36:03 +00:00
parent 5d1eb1c21e
commit 1353ade0c2
41 changed files with 661 additions and 256 deletions

View file

@ -1,7 +1,7 @@
<?php
class Helper {
function cleanQuery($query){
static function cleanQuery($query){
$query = trim(preg_replace('/\s+/', ' ',$query));
$query = str_replace(" , ", ', ', $query);
$query = str_replace("( ", '(', $query);
@ -9,6 +9,12 @@
$query = strtolower($query);
return $query;
}
static function getXmlObject($xml_file){
$xmlParser = XmlQueryParser::getInstance();
return $xmlParser->getXmlFileContent($xml_file);
}
}
?>

View file

@ -12,8 +12,8 @@
return _XE_PATH_ . $type ."/".$name."/queries/" . $query_name . ".xml";
}
function getNewParserOutput($xml_file, $escape_char = '"', $db_type = NULL){
$newXmlQueryParser = new XmlQueryParser($db_type);
function getNewParserOutput($xml_file){
$newXmlQueryParser = new XmlQueryParser();
$xml_obj = $newXmlQueryParser->getXmlFileContent($xml_file);
$parser = new QueryParser($xml_obj->query);
@ -58,9 +58,9 @@
.'</pre>';
}
function getNewParserOutputString($xml_file, $escape_char, $argsString, $db_type = NULL){
function getNewParserOutputString($xml_file, $argsString){
$outputString = '';
$outputString = $this->getNewParserOutput($xml_file, $escape_char, $db_type);
$outputString = $this->getNewParserOutput($xml_file);
$outputString = $this->cleanOutputAndAddArgs($outputString, $argsString);
return $outputString;
}

View file

@ -0,0 +1,134 @@
<?php
require(_XE_PATH_ . 'test-phpUnit/config/config.inc.php');
/**
* Test class for TableTag.
*/
class TableTagTest extends CubridTest {
var $xmlPath = "data/";
function TableTagTest(){
$this->xmlPath = str_replace('TableTagTest.php', '', str_replace('\\', '/', __FILE__)) . $this->xmlPath;
}
/**
* Tests a simple <table> tag:
* <table name="modules" />
*/
function testTableTagWithName(){
$xml_file = $this->xmlPath . "table_name.xml";
$xml_obj = Helper::getXmlObject($xml_file);
$tag = new TableTag($xml_obj->table);
$expected = "new Table('\"xe_modules\"', '\"modules\"')";
$actual = $tag->getTableString();
$this->assertEquals($expected, $actual);
}
/**
* Tests a <table> tag with name and alias
* <table name="modules" alias="mod" />
*/
function testTableTagWithNameAndAlias(){
$xml_file = $this->xmlPath . "table_name_alias.xml";
$xml_obj = Helper::getXmlObject($xml_file);
$tag = new TableTag($xml_obj->table);
$expected = "new Table('\"xe_modules\"', '\"mod\"')";
$actual = $tag->getTableString();
$this->assertEquals($expected, $actual);
}
/**
* Tests a <table> tag used for joins
* <table name="module_categories" alias="module_categories" type="left join">
* <conditions>
* <condition operation="equal" column="module_categories.module_category_srl" default="modules.module_category_srl" />
* </conditions>
* </table>
*
*/
function testTableTagWithJoinCondition(){
$xml_file = $this->xmlPath . "table_name_alias_type.xml";
$xml_obj = Helper::getXmlObject($xml_file);
$tag = new TableTag($xml_obj->table);
$actual = $tag->getTableString();
$expected = 'new JoinTable(\'"xe_module_categories"\', \'"module_categories"\', "left join", array(
new ConditionGroup(array(
new Condition(\'"module_categories"."module_category_srl"\',\'"modules"."module_category_srl"\',"equal")
))
))';
$actual = Helper::cleanQuery($actual);
$expected = Helper::cleanQuery($expected);
$this->assertEquals($expected, $actual);
}
/**
* If a table tag has the type attribute and condition children
* it means it is meant to be used inside a join
*/
function testTagWithTypeIsJoinTable(){
$xml_file = $this->xmlPath . "table_name_alias_type.xml";
$xml_obj = Helper::getXmlObject($xml_file);
$tag = new TableTag($xml_obj->table);
$this->assertEquals(true, $tag->isJoinTable());
}
/**
* Tests that a simple table tag is not a join table
*/
function testTagWithoutTypeIsNotJoinTable(){
$xml_file = $this->xmlPath . "table_name_alias.xml";
$xml_obj = Helper::getXmlObject($xml_file);
$tag = new TableTag($xml_obj->table);
$this->assertEquals(false, $tag->isJoinTable());
}
/**
* If no alias is specified, test that table name is used
*/
function testTableAliasWhenAliasNotSpecified(){
$xml_file = $this->xmlPath . "table_name.xml";
$xml_obj = Helper::getXmlObject($xml_file);
$tag = new TableTag($xml_obj->table);
$this->assertEquals("modules", $tag->getTableAlias());
}
/**
* If alias is specified, test that it is used
*/
function testTableAliasWhenAliasSpecified(){
$xml_file = $this->xmlPath . "table_name_alias.xml";
$xml_obj = Helper::getXmlObject($xml_file);
$tag = new TableTag($xml_obj->table);
$this->assertEquals("mod", $tag->getTableAlias());
}
/**
* Table name propery should returned unescaped and unprefixed table name
* (The one in the XML file)
*/
function testTableName(){
$xml_file = $this->xmlPath . "table_name_alias.xml";
$xml_obj = Helper::getXmlObject($xml_file);
$tag = new TableTag($xml_obj->table);
$this->assertEquals("modules", $tag->getTableName());
}
}

View file

@ -0,0 +1 @@
<table name="modules" />

View file

@ -0,0 +1 @@
<table name="modules" alias="mod" />

View file

@ -0,0 +1,5 @@
<table name="module_categories" alias="module_categories" type="left join">
<conditions>
<condition operation="equal" column="module_categories.module_category_srl" default="modules.module_category_srl" />
</conditions>
</table>

View file

@ -1,17 +1,20 @@
<?php
error_reporting(E_ALL ^ E_NOTICE);
define('_XE_PATH_', str_replace('test-phpUnit/config.inc.php', '', str_replace('\\', '/', __FILE__)));
//define('_TEST_PATH_', substr(_XE_PATH_,0,strrpos(substr(_XE_PATH_,0.-1),'/')));
define('_XE_PATH_', str_replace('test-phpUnit/config/config.inc.php', '', str_replace('\\', '/', __FILE__)));
define('_TEST_PATH_', _XE_PATH_ . 'test-phpUnit/');
if(!defined('__DEBUG__')) define('__DEBUG__', 4);
require_once(_XE_PATH_.'test-phpUnit/Helper.class.php');
require_once(_XE_PATH_.'test-phpUnit/QueryTester.class.php');
require_once(_XE_PATH_.'test-phpUnit/db/DBTest.php');
require_once(_XE_PATH_.'test-phpUnit/db/CubridTest.php');
require_once(_XE_PATH_.'classes/object/Object.class.php');
require_once(_XE_PATH_.'classes/handler/Handler.class.php');
require_once(_XE_PATH_.'classes/context/Context.class.php');
require_once(_XE_PATH_.'classes/file/FileHandler.class.php');
require_once('QueryTester.class.php');
require_once(_XE_PATH_.'classes/xml/XmlParser.class.php');
require_once(_XE_PATH_.'classes/xml/XmlQueryParser.class.php');
@ -35,5 +38,8 @@
require_once(_XE_PATH_.'classes/db/queryparts/order/OrderByColumn.class.php');
require_once(_XE_PATH_.'classes/db/queryparts/limit/Limit.class.php');
require_once(_XE_PATH_.'classes/db/queryparts/Query.class.php');
require_once(_XE_PATH_.'classes/db/queryparts/Subquery.class.php');
require_once(_XE_PATH_.'classes/xml/xmlquery/tags/table/TableTag.class.php');
?>

View file

@ -0,0 +1,24 @@
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
class CubridTest extends DBTest {
protected function setUp() {
$oContext = &Context::getInstance();
$db_info->db_type = 'cubrid';
$db_info->db_table_prefix = 'xe';
$oContext->setDbInfo($db_info);
}
protected function tearDown() {
unset($GLOBALS['__DB__']);
XmlQueryParser::setDBParser(null);
}
}
?>

View file

@ -0,0 +1,54 @@
<?php
class DBTest extends PHPUnit_Framework_TestCase {
function _testQuery($xml_file, $argsString, $expected, $methodName){
$tester = new QueryTester();
$outputString = $tester->getNewParserOutputString($xml_file, $argsString);
$output = eval($outputString);
if(!is_a($output, 'Query')){
if(!$output->toBool()) $querySql = "Date incorecte! Query-ul nu a putut fi executat.";
}else {
$db = &DB::getInstance();
$querySql = $db->{$methodName}($output);
// Remove whitespaces, tabs and all
$querySql = Helper::cleanQuery($querySql);
$expected = Helper::cleanQuery($expected);
}
$this->assertEquals($expected, $querySql);
}
function _testPreparedQuery($xml_file, $argsString, $expected, $methodName, $expectedArgs = NULL){
$tester = new QueryTester();
$outputString = $tester->getNewParserOutputString($xml_file, $argsString);
$output = eval($outputString);
if(!is_a($output, 'Query')){
if(!$output->toBool()) $querySql = "Date incorecte! Query-ul nu a putut fi executat.";
}else {
$db = &DB::getInstance();
$querySql = $db->{$methodName}($output);
$queryArguments = $output->getArguments();
// Remove whitespaces, tabs and all
$querySql = Helper::cleanQuery($querySql);
$expected = Helper::cleanQuery($expected);
}
// Test
$this->assertEquals($expected, $querySql);
// Test query arguments
$argCount = count($expectedArgs);
for($i = 0; $i < $argCount; $i++){
//echo "$i: $expectedArgs[$i] vs $queryArguments[$i]->getValue()";
$this->assertEquals($expectedArgs[$i], $queryArguments[$i]->getValue());
}
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
?>

View file

@ -1,5 +1,5 @@
<?php
require(_XE_PATH_ . 'test-phpUnit/config.inc.php');
require(_XE_PATH_ . 'test-phpUnit/config/config.inc.php');
class ExpressionParserTest extends PHPUnit_Framework_TestCase {
/* Escape char for:

View file

@ -0,0 +1,24 @@
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
class MssqlTest extends DBTest {
protected function setUp() {
$oContext = &Context::getInstance();
$db_info->db_type = 'mssql';
$db_info->db_table_prefix = 'xe';
$oContext->setDbInfo($db_info);
}
protected function tearDown() {
unset($GLOBALS['__DB__']);
XmlQueryParser::setDBParser(null);
}
}
?>

View file

@ -1,28 +1,10 @@
<?php
require(_XE_PATH_ . 'test-phpUnit/config.inc.php');
require(_XE_PATH_ . 'test-phpUnit/db/xml_query/cubrid/config.cubrid.inc.php');
require(_XE_PATH_ . 'test-phpUnit/config/config.inc.php');
class CubridDeleteTest extends PHPUnit_Framework_TestCase {
class CubridDeleteTest extends CubridTest {
function _test($xml_file, $argsString, $expected){
$tester = new QueryTester();
$outputString = $tester->getNewParserOutputString($xml_file, '"', $argsString);
$output = eval($outputString);
if(!is_a($output, 'Query')){
if(!$output->toBool()) $querySql = "Date incorecte! Query-ul nu a putut fi executat.";
}else {
$db = &DB::getInstance();
var_dump($db);
$querySql = $db->getDeleteSql($output);
// Remove whitespaces, tabs and all
$querySql = Helper::cleanQuery($querySql);
$expected = Helper::cleanQuery($expected);
}
// Test
$this->assertEquals($expected, $querySql);
$this->_testQuery($xml_file, $argsString, $expected, 'getDeleteSql');
}
function test_module_deleteActionForward(){

View file

@ -1,28 +1,12 @@
<?php
require(_XE_PATH_ . 'test-phpUnit/config.inc.php');
require(_XE_PATH_ . 'test-phpUnit/db/xml_query/cubrid/config.cubrid.inc.php');
require(_XE_PATH_ . 'test-phpUnit/config/config.inc.php');
class CubridInsertTest extends PHPUnit_Framework_TestCase {
class CubridInsertTest extends CubridTest {
function _test($xml_file, $argsString, $expected){
$tester = new QueryTester();
$outputString = $tester->getNewParserOutputString($xml_file, '"', $argsString);
$output = eval($outputString);
if(!is_a($output, 'Query')){
if(!$output->toBool()) $querySql = "Date incorecte! Query-ul nu a putut fi executat.";
}else {
$db = &DB::getInstance();
$querySql = $db->getInsertSql($output);
// Remove whitespaces, tabs and all
$querySql = Helper::cleanQuery($querySql);
$expected = Helper::cleanQuery($expected);
}
// Test
$this->assertEquals($expected, $querySql);
$this->_testQuery($xml_file, $argsString, $expected, 'getInsertSql');
}
/**
* Note: this test can fail when comaparing regdate from the $args with

View file

@ -1,30 +1,10 @@
<?php
require(_XE_PATH_ . 'test-phpUnit/config.inc.php');
require(_XE_PATH_ . 'test-phpUnit/db/xml_query/cubrid/config.cubrid.inc.php');
require(_XE_PATH_ . 'test-phpUnit/config/config.inc.php');
class CubridSelectTest extends PHPUnit_Framework_TestCase {
class CubridSelectTest extends CubridTest {
function _test($xml_file, $argsString, $expected){
$tester = new QueryTester();
$outputString = $tester->getNewParserOutputString($xml_file, '"', $argsString);
//echo $outputString;
$output = eval($outputString);
if(!is_a($output, 'Query')){
if(!$output->toBool()) $querySql = "Date incorecte! Query-ul nu a putut fi executat.";
}else {
//$db = new DBCubrid();
$db = &DB::getInstance();
$querySql = $db->getSelectSql($output);
// Remove whitespaces, tabs and all
$querySql = Helper::cleanQuery($querySql);
$expected = Helper::cleanQuery($expected);
}
// Test
$this->assertEquals($expected, $querySql);
function _test($xml_file, $argsString, $expected){
$this->_testQuery($xml_file, $argsString, $expected, 'getSelectSql');
}
function testSelectStar(){

View file

@ -0,0 +1,77 @@
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
class CubridSubqueryTest extends CubridTest {
var $xmlPath = 'data/';
function CubridSubqueryTest(){
$this->xmlPath = str_replace('CubridSubqueryTest.php', '', str_replace('\\', '/', __FILE__)) . $this->xmlPath;
}
function _test($xml_file, $argsString, $expected){
$this->_testQuery($xml_file, $argsString, $expected, 'getSelectSql');
}
function testSelectUncorrelated1(){
$xml_file = $this->xmlPath . "select_uncorrelated1.xml";
echo $xml_file;
$argsString = '$args->user_id = 4;
';
$expected = 'select "column_a" as "value_a"
, (select max("column_b") as "count"
from "xe_table_b" as "table_b"
) as "value_b"
from "xe_table_a" as "table_a"
where "column_a" = 4';
$this->_test($xml_file, $argsString, $expected);
}
function testSelectUncorrelated2(){
$xml_file = $this->xmlPath . "select_uncorrelated2.xml";
echo $xml_file;
$argsString = '$args->user_id = 4;
$args->user_name = 7;
';
$expected = 'SELECT "column_a" as "value_a"
, "column_b" as "value_b"
, "column_c" as "value_c"
, (SELECT max("column_b") as "count"
FROM "xe_table_b" as "table_b"
WHERE "column_ab" = 7) as "value_b"
FROM "xe_table_a" as "table_a"
WHERE "column_a" = 4';
$this->_test($xml_file, $argsString, $expected);
}
function testFromUncorrelated(){
$xml_file = $this->xmlPath . "from_uncorrelated1.xml";
echo $xml_file;
$argsString = '$args->user_id = 4;
$args->user_name = 7;
';
$expected = 'select max("documentcountbymember"."count") as "maxcount"
from (
select "member_srl" as "member_srl"
, count(*) as "count"
from "xe_documents" as "documents"
group by "member_srl"
) as "documentcountbymember"';
$this->_test($xml_file, $argsString, $expected);
}
function testWhereUncorrelated(){
$xml_file = $this->xmlPath . "where_uncorrelated1.xml";
echo $xml_file;
$argsString = '';
$expected = 'select * from
"xe_member" as "member"
where "regdate" = (select max("regdate") as "maxregdate"
from "xe_documents" as "documents")';
$this->_test($xml_file, $argsString, $expected);
}
}
?>

View file

@ -1,26 +1,10 @@
<?php
require(_XE_PATH_ . 'test-phpUnit/config.inc.php');
require(_XE_PATH_ . 'test-phpUnit/db/xml_query/cubrid/config.cubrid.inc.php');
require(_XE_PATH_ . 'test-phpUnit/config/config.inc.php');
class CubridUpdateTest extends PHPUnit_Framework_TestCase {
class CubridUpdateTest extends CubridTest {
function _test($xml_file, $argsString, $expected){
$tester = new QueryTester();
$outputString = $tester->getNewParserOutputString($xml_file, '"', $argsString);
$output = eval($outputString);
if(!is_a($output, 'Query')){
if(!$output->toBool()) $querySql = "Date incorecte! Query-ul nu a putut fi executat.";
}else {
$db = &DB::getInstance();
$querySql = $db->getUpdateSql($output);
// Remove whitespaces, tabs and all
$querySql = Helper::cleanQuery($querySql);
$expected = Helper::cleanQuery($expected);
}
// Test
$this->assertEquals($expected, $querySql);
function _test($xml_file, $argsString, $expected){
$this->_testQuery($xml_file, $argsString, $expected, 'getUpdateSql');
}
function test_module_updateModule(){

View file

@ -1,8 +0,0 @@
<?php
$oContext = &Context::getInstance();
$db_info->db_type = 'cubrid';
$db_info->db_table_prefix = 'xe';
$oContext->setDbInfo($db_info);
?>

View file

@ -0,0 +1,19 @@
<query id="getMemberInfo" action="select">
<tables>
<query alias="documentCountByMember">
<tables>
<table name="documents" alias="documents" />
</tables>
<columns>
<column name="member_srl" alias="member_srl" />
<column name="count(*)" alias="count" />
</columns>
<groups>
<group column="member_srl" />
</groups>
</query>
</tables>
<columns>
<column name="MAX(documentCountByMember.count)" alias="maxCount" />
</columns>
</query>

View file

@ -0,0 +1,19 @@
<query id="select_uncorrelated" action="select">
<tables>
<table name="table_a" />
</tables>
<columns>
<column name="column_a" alias="value_a" />
<query alias="value_b">
<tables>
<table name="table_b" />
</tables>
<columns>
<column name="max(column_b)" alias="count" />
</columns>
</query>
</columns>
<conditions>
<condition operation="equal" column="column_a" var="user_id" notnull="notnull" />
</conditions>
</query>

View file

@ -0,0 +1,24 @@
<query id="select_uncorrelated" action="select">
<tables>
<table name="table_a" />
</tables>
<columns>
<column name="column_a" alias="value_a" />
<column name="column_b" alias="value_b" />
<query alias="value_b">
<tables>
<table name="table_b" />
</tables>
<columns>
<column name="max(column_b)" alias="count" />
</columns>
<conditions>
<condition operation="equal" column="column_ab" var="user_name" notnull="notnull" />
</conditions>
</query>
<column name="column_c" alias="value_c" />
</columns>
<conditions>
<condition operation="equal" column="column_a" var="user_id" notnull="notnull" />
</conditions>
</query>

View file

@ -0,0 +1,18 @@
<query id="getMemberInfo" action="select">
<tables>
<table name="member" />
</tables>
<columns>
<column name="*" />
</columns>
<conditions>
<query operation="equal" column="regdate" alias="documentMaxRegdate">
<tables>
<table name="documents" />
</tables>
<columns>
<column name="max(regdate)" alias="maxregdate" />
</columns>
</query>
</conditions>
</query>

View file

@ -1,36 +1,10 @@
<?php
require(_XE_PATH_ . 'test-phpUnit/config.inc.php');
require(_XE_PATH_ . 'test-phpUnit/db/xml_query/mssql/config.mssql.inc.php');
require(_XE_PATH_ . 'test-phpUnit/config/config.inc.php');
class MssqlSelectTest extends PHPUnit_Framework_TestCase {
class MssqlSelectTest extends MssqlTest {
function _test($xml_file, $argsString, $expected, $expectedArgs = NULL){
$tester = new QueryTester();
$outputString = $tester->getNewParserOutputString($xml_file, '[', $argsString);
//echo $outputString;
$output = eval($outputString);
if(!is_a($output, 'Query')){
if(!$output->toBool()) $querySql = "Date incorecte! Query-ul nu a putut fi executat.";
}else {
$db = &DB::getInstance();
$querySql = $db->getSelectSql($output);
$queryArguments = $output->getArguments();
// Remove whitespaces, tabs and all
$querySql = Helper::cleanQuery($querySql);
$expected = Helper::cleanQuery($expected);
}
// Test
$this->assertEquals($expected, $querySql);
// Test query arguments
$argCount = count($expectedArgs);
for($i = 0; $i < $argCount; $i++){
//echo "$i: $expectedArgs[$i] vs $queryArguments[$i]->getValue()";
$this->assertEquals($expectedArgs[$i], $queryArguments[$i]->getValue());
}
$this->_testPreparedQuery($xml_file, $argsString, $expected, 'getSelectSql', $expectedArgs = NULL);
}
function testSelectStar(){

View file

@ -1,8 +0,0 @@
<?php
$oContext = &Context::getInstance();
$db_info->db_type = 'mssql';
$db_info->db_table_prefix = 'xe';
$oContext->setDbInfo($db_info);
?>

View file

@ -1,5 +1,6 @@
<?php
require('config.inc.php');
require('config/config.inc.php');
$oDB = &DB::getInstance('mssql');
//$oDB = &DB::getInstance();
$dbParser = $oDB->getParser();