00001 <?php 00024 $config = Config::read("environment.ini"); 00025 00026 $user = "root"; 00027 $password = "hea101"; 00028 if(isset($config->database->pdo)){ 00029 $dsn = $config->database->dsn; 00030 $db = new Db($dsn, $user, $password); 00031 } else { 00032 $host = "localhost"; 00033 $dbname = "test"; 00034 $db = new Db($host, $user, $password, $dbname); 00035 } 00036 00037 00038 $db->debug = true; 00039 $total_time = 0; 00040 00041 $test = true; 00042 $test_name = "CREAR Y BORRAR UNA TABLA"; 00043 $init_time = $start_benchmark = microtime(true); 00044 try { 00045 $value1 = $db->drop_table("kumbia_test"); 00046 if(!$value1){ 00047 throw new DbException("No se pudo crear la tabla de prueba (1)"); 00048 } 00049 $value2 = $db->create_table("test.kumbia_test", array( 00050 "id" => array( 00051 "type" => db::TYPE_INTEGER, 00052 "not_null" => true, 00053 "primary" => true, 00054 "auto" => true 00055 ), 00056 "texto" => array( 00057 "type" => db::TYPE_VARCHAR, 00058 "not_null" => true, 00059 "size" => 40 00060 ), 00061 "fecha" => array( 00062 "type" => db::TYPE_DATE, 00063 ) 00064 )); 00065 if($value2===false){ 00066 throw new DbException("No se pudo crear la tabla de prueba (2)"); 00067 } 00068 if(!$db->table_exists("kumbia_test")){ 00069 throw new DbException("No se pudo comprobar la existencia de la tabla de prueba (3)"); 00070 } 00071 } 00072 catch(Exception $e){ 00073 $test = false; 00074 print "<div style='background:#FFBBBB;border:1px solid red'>"; 00075 print "Primer Test '$test_name' (FALLÓ) con mensaje: ({$e->getMessage()})"; 00076 print "</div>"; 00077 } 00078 if($test){ 00079 $end_benckmark = microtime(true) - $start_benchmark; 00080 print "<div style='background:#CCFF99;border:1px solid green'>"; 00081 print "Primer Test '$test_name' (OK) con tiempo: ({$end_benckmark})"; 00082 print "</div>"; 00083 } 00084 00085 $test = true; 00086 $test_name = "INSERTAR DATOS EN UNA TABLA DE PRUEBA"; 00087 $start_benchmark = microtime(true); 00088 try { 00089 $value1 = $db->insert("kumbia_test", array("4", "'Hello'", "'2005-04-04'")); 00090 if(!$value1){ 00091 throw new DbException("No se puede insertar en la tabla de prueba (1)"); 00092 } 00093 $value2 = $db->insert("kumbia_test", array("2", "'Hello'", "'2005-02-04'"), array("id", "texto", "fecha")); 00094 if(!$value2){ 00095 throw new DbException("No se puede insertar en la tabla de prueba (2)"); 00096 } 00097 $value3 = $db->insert("kumbia_test", array("'Hello'", "'2005-02-04'"), array("texto", "fecha")); 00098 if(!$value3){ 00099 throw new DbException("No se puede insertar en la tabla de prueba (3)"); 00100 } 00101 if($db->affected_rows()!=1){ 00102 throw new DbException("No se puede insertar en la tabla de prueba (4)"); 00103 } 00104 Flash::notice($db->last_insert_id("kumbia_test", "id")); 00105 } 00106 catch(Exception $e){ 00107 $test = false; 00108 print "<div style='background:#FFBBBB;border:1px solid red'>"; 00109 print "Test '$test_name' (FALLÓ) con mensaje: ({$e->getMessage()})"; 00110 print "</div>"; 00111 } 00112 if($test){ 00113 $end_benckmark = microtime(true) - $start_benchmark; 00114 print "<div style='background:#CCFF99;border:1px solid green'>"; 00115 print "Test '$test_name' (OK) con tiempo: ({$end_benckmark})"; 00116 print "</div>"; 00117 } 00118 00119 $test = true; 00120 $test_name = "ACTUALIZAR DATOS EN UNA TABLA DE PRUEBA"; 00121 $start_benchmark = microtime(true); 00122 try { 00123 $value1 = $db->update("kumbia_test", array("texto"), array("'Esto es un Texto'")); 00124 if(!$value1){ 00125 throw new DbException("No se puede actualizar en la tabla de prueba (1)"); 00126 } 00127 $value2 = $db->update("kumbia_test", array("texto", "fecha"), array("'Esto es otro Texto'", "'2007-02-02'"), "id = 1"); 00128 if($value2===false){ 00129 throw new DbException("No se puede actualizar en la tabla de prueba (2)"); 00130 } 00131 } 00132 catch(Exception $e){ 00133 print "<div style='background:#FFBBBB;border:1px solid red'>"; 00134 print "Test '$test_name' (FALLÓ) con mensaje: ({$e->getMessage()})"; 00135 print "</div>"; 00136 } 00137 if($test){ 00138 $end_benckmark = microtime(true) - $start_benchmark; 00139 print "<div style='background:#CCFF99;border:1px solid green'>"; 00140 print "Test '$test_name' (OK) con tiempo: ({$end_benckmark})"; 00141 print "</div>"; 00142 } 00143 00144 $test = true; 00145 $test_name = "BORRAR DATOS EN UNA TABLA DE PRUEBA"; 00146 $start_benchmark = microtime(true); 00147 try { 00148 $value1 = $db->delete("kumbia_test", "id = 4"); 00149 if(!$value1){ 00150 throw new DbException("No se puede borrar en la tabla de prueba (1)"); 00151 } 00152 } 00153 catch(Exception $e){ 00154 print "<div style='background:#FFBBBB;border:1px solid red'>"; 00155 print "Test '$test_name' (FALLÓ) con mensaje: ({$e->getMessage()})"; 00156 print "</div>"; 00157 } 00158 if($test){ 00159 $end_benckmark = microtime(true) - $start_benchmark; 00160 print "<div style='background:#CCFF99;border:1px solid green'>"; 00161 print "Test '$test_name' (OK) con tiempo: ({$end_benckmark})"; 00162 print "</div>"; 00163 } 00164 00165 $test = true; 00166 $test_name = "CONSULTAR DATOS EN UNA TABLA DE PRUEBA"; 00167 $start_benchmark = microtime(true); 00168 try { 00169 $value1 = $db->query("SELECT * FROM kumbia_test ORDER BY id"); 00170 if(!$value1){ 00171 throw new DbException("No se puede consultar en la tabla de prueba (1)"); 00172 } 00173 while($row = $db->fetch_array()){ 00174 if($row['id']!=0 && $row['id']!=1 && $row['id']!=2 && $row['id']!=4 && $row['id']!=5){ 00175 throw new DbException("No se puede consultar en la tabla de prueba {$row['id']} (2)"); 00176 } 00177 } 00178 if(!isset($config->database->pdo)){ 00179 if($db->num_rows()!=2){ 00180 throw new DbException("No se puede consultar en la tabla de prueba (3)"); 00181 } 00182 } 00183 $value2 = $db->fetch_one("SELECT * FROM kumbia_test {$db->limit(1)}"); 00184 if(!is_array($value2)){ 00185 throw new DbException("No se puede consultar en la tabla de prueba (4)"); 00186 } 00187 $value3 = $db->fetch_all("SELECT * FROM kumbia_test"); 00188 if(count($value3)!=2){ 00189 throw new DbException("No se puede consultar en la tabla de prueba (5)"); 00190 } 00191 $value4 = $db->in_query_assoc("SELECT * FROM kumbia_test {$db->limit(1)}"); 00192 if(count($value4[0])!=3){ 00193 throw new DbException("No se puede consultar en la tabla de prueba (6)"); 00194 } 00195 $value5 = $db->in_query_num("SELECT * FROM kumbia_test {$db->limit(1)}"); 00196 if(count($value5[0])!=3){ 00197 throw new DbException("No se puede consultar en la tabla de prueba (7)"); 00198 } 00199 $value6 = $db->in_query("SELECT * FROM kumbia_test {$db->limit(1)}"); 00200 if(count($value6[0])!=6){ 00201 throw new DbException("No se puede consultar en la tabla de prueba (8)"); 00202 } 00203 if(!isset($config->database->pdo)){ 00204 $value7 = $db->data_seek(1, $value1); 00205 if(!$value7){ 00206 throw new DbException("No se puede consultar en la tabla de prueba (9)"); 00207 } 00208 } 00209 $value8 = $db->fetch_array($value1); 00210 if($value8['id']!=5 && $value8['id']!=2 && $value8['id']!=0){ 00211 throw new DbException("No se puede consultar en la tabla de prueba {$value8['id']} (10)"); 00212 } 00213 } 00214 catch(Exception $e){ 00215 print "<div style='background:#FFBBBB;border:1px solid red'>"; 00216 print "Test '$test_name' (FALLÓ) con mensaje: ({$e->getMessage()})"; 00217 print "</div>"; 00218 } 00219 if($test){ 00220 $end_benckmark = microtime(true) - $start_benchmark; 00221 print "<div style='background:#CCFF99;border:1px solid green'>"; 00222 print "Test '$test_name' (OK) con tiempo: ({$end_benckmark})"; 00223 print "</div>"; 00224 } 00225 00226 $test = true; 00227 $test_name = "COMPROBAR NOMBRE DE CAMPO"; 00228 $start_benchmark = microtime(true); 00229 try { 00230 $value1 = $db->query("SELECT * FROM kumbia_test"); 00231 if(!$value1){ 00232 throw new DbException("No se comprobar nombre del campo (1)"); 00233 } 00234 $value2 = $db->field_name(1); 00235 if($value2!='texto'){ 00236 throw new DbException("No se comprobar nombre del campo (2)"); 00237 } 00238 } 00239 catch(Exception $e){ 00240 print "<div style='background:#FFBBBB;border:1px solid red'>"; 00241 print "Test '$test_name' (FALLÓ) con mensaje: ({$e->getMessage()})"; 00242 print "</div>"; 00243 } 00244 if($test){ 00245 $end_benckmark = microtime(true) - $start_benchmark; 00246 print "<div style='background:#CCFF99;border:1px solid green'>"; 00247 print "Test '$test_name' (OK) con tiempo: ({$end_benckmark})"; 00248 print "</div>"; 00249 } 00250 00251 $test = true; 00252 $test_name = "INSTRUCCIONES TRANSACCIONALES"; 00253 $start_benchmark = microtime(true); 00254 try { 00255 $db->begin(); 00256 $db->query("delete from kumbia_test"); 00257 $db->rollback(); 00258 $db->begin(); 00259 $db->query("delete from kumbia_test"); 00260 $db->commit(); 00261 } 00262 catch(Exception $e){ 00263 print "<div style='background:#FFBBBB;border:1px solid red'>"; 00264 print "Test '$test_name' (FALLÓ) con mensaje: ({$e->getMessage()})"; 00265 print "</div>"; 00266 } 00267 if($test){ 00268 $end_benckmark = microtime(true) - $start_benchmark; 00269 print "<div style='background:#CCFF99;border:1px solid green'>"; 00270 print "Test '$test_name' (OK) con tiempo: ({$end_benckmark})"; 00271 print "</div>"; 00272 } 00273 00274 $test = true; 00275 $test_name = "CONTAR TABLAS Y DESCRIBIR TABLA TEST"; 00276 $start_benchmark = microtime(true); 00277 try { 00278 Flash::notice("HAY ".count($db->list_tables())." TABLA(S) EN LA BASE DE DATOS"); 00279 print_r($db->describe_table("kumbia_test")); 00280 } 00281 catch(Exception $e){ 00282 print "<div style='background:#FFBBBB;border:1px solid red'>"; 00283 print "Test '$test_name' (FALLÓ) con mensaje: ({$e->getMessage()})"; 00284 print "</div>"; 00285 } 00286 if($test){ 00287 $end_benckmark = microtime(true) - $start_benchmark; 00288 print "<div style='background:#CCFF99;border:1px solid green'>"; 00289 print "Test '$test_name' (OK) con tiempo: ({$end_benckmark})"; 00290 print "</div>"; 00291 } 00292 00293 $test = true; 00294 $test_name = "CERRAR LA CONEXION A LA BASE DE DATOS"; 00295 $start_benchmark = microtime(true); 00296 try { 00297 $value1 = $db->close(); 00298 if(!$value1){ 00299 throw new DbException("No se puede cerrar la conexion (1)"); 00300 } 00301 } 00302 catch(Exception $e){ 00303 print "<div style='background:#FFBBBB;border:1px solid red'>"; 00304 print "Test '$test_name' (FALLÓ) con mensaje: ({$e->getMessage()})"; 00305 print "</div>"; 00306 } 00307 if($test){ 00308 $end_benckmark = microtime(true) - $start_benchmark; 00309 print "<div style='background:#CCFF99;border:1px solid green'>"; 00310 print "Test '$test_name' (OK) con tiempo: ({$end_benckmark})"; 00311 print "</div>"; 00312 } 00313 00314 00315 print "<div style='background:#CCFF99;border:1px solid green'>"; 00316 print "<strong>Tiempo total de los Test ".(microtime(true) - $init_time)."</strong>"; 00317 print "</div>"; 00318 00319 00320 ?>