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