Fixing #138
[akelos.git] / test / unit / lib / AkActiveRecord / AkDbAdapter.php
blob39255a0bc73e11b60ef6e23c29aa681998d3ae6a
1 <?php
3 defined('AK_ACTIVE_RECORD_PROTECT_GET_RECURSION') ? null : define('AK_ACTIVE_RECORD_PROTECT_GET_RECURSION', false);
4 defined('AK_TEST_DATABASE_ON') ? null : define('AK_TEST_DATABASE_ON', true);
6 require_once(dirname(__FILE__).'/../../../fixtures/config/config.php');
8 class AkDbAdapter_TestCase extends AkUnitTest
11 function test_should_generate_sequence_ids()
13 $db =& AkDbAdapter::getInstance(array('type'=>'sqlite'),false);
16 function test_should_report_errors()
18 $db =& AkDbAdapter::getInstance();
19 $db->debug();
20 $db->execute('selct wrong sql statement');
21 $this->assertError();
22 //$db->debug(false);
25 function test_execute_should_handle_bindings()
27 $db =& new AkDbAdapter(array()); // no conection details, we're using a Mock
28 Mock::generate('ADOConnection');
29 $connection =& new MockADOConnection();
30 $connection->setReturnValue('Execute',true);
31 $connection->expectAt(0,'Execute',array('SELECT * FROM articles WHERE id=1'));
32 $connection->expectAt(1,'Execute',array('SELECT * FROM articles WHERE id=?',array(1)));
33 $db->connection =& $connection;
34 $db->execute('SELECT * FROM articles WHERE id=1');
35 $db->execute(array('SELECT * FROM articles WHERE id=?',1));
38 function test_should_add_limit_and_offset_mysql_style()
40 $mysql_db =& AkDbAdapter::getInstance(array('type'=>'mysql'),false);
41 $sql = 'SELECT * FROM articles';
42 $mysql_db->addLimitAndOffset($sql,array('limit'=>2,'offset'=>10));
43 $this->assertEqual('SELECT * FROM articles LIMIT 10, 2',$sql);
45 $sql = 'SELECT * FROM articles';
46 $mysql_db->addLimitAndOffset($sql,array('offset'=>10));
47 $this->assertEqual('SELECT * FROM articles',$sql);
49 $sql = 'SELECT * FROM articles';
50 $mysql_db->addLimitAndOffset($sql,array('limit'=>10));
51 $this->assertEqual('SELECT * FROM articles LIMIT 10',$sql);
54 function test_should_add_limit_and_offset_common_style()
56 $mysql_db =& AkDbAdapter::getInstance(array('type'=>'postgre'),false);
57 $sql = 'SELECT * FROM articles';
58 $mysql_db->addLimitAndOffset($sql,array('limit'=>2,'offset'=>10));
59 $this->assertEqual('SELECT * FROM articles LIMIT 2 OFFSET 10',$sql);
61 $sql = 'SELECT * FROM articles';
62 $mysql_db->addLimitAndOffset($sql,array('offset'=>10));
63 $this->assertEqual('SELECT * FROM articles',$sql);
65 $sql = 'SELECT * FROM articles';
66 $mysql_db->addLimitAndOffset($sql,array('limit'=>10));
67 $this->assertEqual('SELECT * FROM articles LIMIT 10',$sql);
70 function test_should_quote_strings_for_mysql()
72 $db =& AkDbAdapter::getInstance();
73 if ($db->type() != 'mysql') return;
75 $this->assertEqual("'Hello'",$db->quote_string('Hello'));
76 $this->assertEqual("'Hel\\\"lo'",$db->quote_string('Hel"lo'));
77 $this->assertEqual("'Hel\'\'lo'",$db->quote_string("Hel''lo"));
78 $this->assertEqual("'Hel\\\lo'",$db->quote_string("Hel\lo"));
79 $this->assertEqual("'Hel\\\lo'",$db->quote_string("Hel\\lo"));
82 function test_should_quote_strings_for_postgre()
84 $db =& AkDbAdapter::getInstance();
85 if ($db->type() != 'postgre') return;
87 $this->assertEqual("'Hello'",$db->quote_string('Hello'));
88 $this->assertEqual("'Hel\"lo'",$db->quote_string('Hel"lo'));
89 $this->assertEqual("'Hel''''lo'",$db->quote_string("Hel''lo"));
90 $this->assertEqual("'Hel''lo'",$db->quote_string("Hel'lo"));
91 $this->assertEqual("'Hel\\\lo'",$db->quote_string("Hel\lo"));
92 $this->assertEqual("'Hel\\\lo'",$db->quote_string("Hel\\lo"));
95 function test_should_quote_strings_for_sqlite()
97 $db =& AkDbAdapter::getInstance();
98 if ($db->type() != 'sqlite') return;
100 $this->assertEqual("'Hello'",$db->quote_string('Hello'));
101 $this->assertEqual("'Hel\"lo'",$db->quote_string('Hel"lo'));
102 $this->assertEqual("'Hel''''lo'",$db->quote_string("Hel''lo"));
103 $this->assertEqual("'Hel''lo'",$db->quote_string("Hel'lo"));
104 $this->assertEqual("'Hel\lo'",$db->quote_string("Hel\lo"));
105 $this->assertEqual("'Hel\lo'",$db->quote_string("Hel\\lo"));
108 function _test_investigate_DBTimeStamp()
110 $db =& AkDbAdapter::getInstance();
112 var_dump($db->DBTimeStamp('2007.11.17'));
113 var_dump($db->DBTimeStamp('2007-11-17'));
114 var_dump($db->DBTimeStamp('2007-11-17 17:40:23'));
115 var_dump($db->DBTimeStamp('2007-11-17 8:40:23'));
116 var_dump($db->DBTimeStamp('17-11-2007'));
117 var_dump($db->DBTimeStamp(time()));
121 ak_test('AkDbAdapter_TestCase',true);