00001 <?php
00024 $config = Config::read("environment.ini");
00025
00026 $db = db::raw_connect();
00027
00028 $db->debug = true;
00029 $total_time = 0;
00030
00031 $test = true;
00032 $test_name = "CREAR Y BORRAR UNA TABLA";
00033 $init_time = $start_benchmark = microtime(true);
00034 try {
00035 $value1 = $db->drop_table("kumbia_test");
00036 if(!$value1){
00037 throw new DbException("No se pudo crear la tabla de prueba (1)");
00038 }
00039 $value2 = $db->create_table("kumbia_test", array(
00040 "id" => array(
00041 "type" => db::TYPE_INTEGER,
00042 "not_null" => true,
00043 "primary" => true,
00044 "auto" => true
00045 ),
00046 "texto" => array(
00047 "type" => db::TYPE_VARCHAR,
00048 "not_null" => true,
00049 "size" => 40
00050 ),
00051 "fecha" => array(
00052 "type" => db::TYPE_DATE,
00053 ),
00054 "email" => array(
00055 "type" => db::TYPE_VARCHAR,
00056 "size" => 70
00057 ),
00058 "numero" => array(
00059 "type" => db::TYPE_INTEGER,
00060 )
00061 ));
00062 if($value2===false){
00063 throw new DbException("No se pudo crear la tabla de prueba (2)");
00064 }
00065 if(!$db->table_exists("kumbia_test")){
00066 throw new DbException("No se pudo comprobar la existencia de la tabla de prueba (3)");
00067 }
00068
00069
00070 eval("class KumbiaTest extends ActiveRecord {
00071
00072 function __construct(){
00073 \$this->validates_numericality_of('numero');
00074 \$this->validates_presence_of('numero');
00075 \$this->validates_email_in('email');
00076 \$this->validates_date_in('fecha');
00077 \$this->validates_uniqueness_of('texto');
00078 }
00079
00080 } ");
00081 unset($_SESSION['KUMBIA_META_DATA'][$_SESSION['KUMBIA_PATH']]["kumbia_test"]);
00082 $model = new KumbiaTest();
00083 if(!is_subclass_of($model, "ActiveRecord")){
00084 throw new DbException("No se pudo crear el modelo de prueba (3)");
00085 }
00086
00087 }
00088 catch(Exception $e){
00089 $test = false;
00090 print "<div style='background:#FFBBBB;border:1px solid red'>";
00091 print "Test '$test_name' (FALLÓ) con mensaje: ({$e->getMessage()})";
00092 print "</div>";
00093 }
00094 if($test){
00095 $end_benckmark = microtime(true) - $start_benchmark;
00096 print "<div style='background:#CCFF99;border:1px solid green'>";
00097 print "Test '$test_name' (OK) con tiempo: ({$end_benckmark})";
00098 print "</div>";
00099 }
00100
00101
00102 $test = true;
00103 $test_name = "INSERTAR DATOS DE PRUEBA EN EL MODELO";
00104 $init_time = $start_benchmark = microtime(true);
00105 try {
00106 $model->debug = true;
00107 for($i=1;$i<=20;$i++){
00108 $model->texto = "Texto ".$i;
00109 $model->fecha = "2007-02-".sprintf("%02d", rand(1, 10));
00110 $model->email = "kumbia@com";
00111 $model->numero = rand(0, 5);
00112 $model->create();
00113 }
00114 }
00115 catch(Exception $e){
00116 $test = false;
00117 print "<div style='background:#FFBBBB;border:1px solid red'>";
00118 print "Test '$test_name' (FALLÓ) con mensaje: ({$e->getMessage()})";
00119 print "</div>";
00120 }
00121 if($test){
00122 $end_benckmark = microtime(true) - $start_benchmark;
00123 print "<div style='background:#CCFF99;border:1px solid green'>";
00124 print "Test '$test_name' (OK) con tiempo: ({$end_benckmark})";
00125 print "</div>";
00126 }
00127
00128 $test = true;
00129 $test_name = "ACTUALIZAR DATOS DE PRUEBA EN EL MODELO";
00130 $start_benchmark = microtime(true);
00131 try {
00132 for($i=1;$i<=20;$i+=5){
00133 $model = $model->find($i);
00134 if($model){
00135 $model->numero = "100";
00136 $model->update();
00137 } else {
00138 throw new DbException("No Devolvio el objeto para id = $i");
00139 }
00140 }
00141 $model->update_all("email = 'hello@com'");
00142 $model->update_all("texto = 'otro texto'", "id <= 10");
00143 }
00144 catch(Exception $e){
00145 $test = false;
00146 print "<div style='background:#FFBBBB;border:1px solid red'>";
00147 print "Test '$test_name' (FALLÓ) con mensaje: ({$e->getMessage()})";
00148 print "</div>";
00149 }
00150 if($test){
00151 $end_benckmark = microtime(true) - $start_benchmark;
00152 print "<div style='background:#CCFF99;border:1px solid green'>";
00153 print "Test '$test_name' (OK) con tiempo: ({$end_benckmark})";
00154 print "</div>";
00155 }
00156
00157
00158 $test = true;
00159 $test_name = "CONSULTAR DATOS DE PRUEBA EN EL MODELO";
00160 $start_benchmark = microtime(true);
00161 try {
00162 $model = new KumbiaTest();
00163 $model->debug = true;
00164 $model->find();
00165 if($model->count!=20){
00166 throw new DbException("No devolvio el numero correcto de registros en la tabla (1)");
00167 }
00168 $model->find_first(11);
00169 if($model->numero!=100){
00170 throw new DbException("No devolvio el registro correcto para id = 11 (2)");
00171 }
00172 $otro_model = $model->find_first(11);
00173 if($otro_model->numero!=100){
00174 throw new DbException("No devolvio el registro correcto para id = 11 (3)");
00175 }
00176 $model->find("numero = 100");
00177 if($model->count!=4){
00178 throw new DbException("No devolvio el numero correcto de registros en la tabla (4)");
00179 }
00180 $results = $model->find("numero = 100", "order: id desc");
00181 if($results[0]->id!=16){
00182 throw new DbException("No devolvio el registro correcto al ordenar (5)");
00183 }
00184 if(count($results)!=4){
00185 throw new DbException("No devolvio el numero de registros correcto al ordenar (6)");
00186 }
00187 $results = $model->find("conditions: numero = 100", "limit: 1", "order: id asc");
00188 if(count($results)!=1){
00189 throw new DbException("No devolvio el registro correcto cuando se uso limit y ordenamiento (7)");
00190 }
00191 if($results[0]->id!=1){
00192 throw new DbException("No devolvio el registro correcto cuando se uso limit y ordenamiento {$results[0]->id} (8)");
00193 }
00194 $min = $model->minimum("id", "conditions: numero = 100");
00195 if($min!=1){
00196 throw new DbException("No devolvio el minimum correcto (9)");
00197 }
00198 $max = $model->maximum("id", "conditions: numero = 100");
00199 if($max!=16){
00200 throw new DbException("No devolvio el maximum correcto (10)");
00201 }
00202 $sum = $model->sum("id", "conditions: numero = 100");
00203 if($sum!=34){
00204 throw new DbException("No devolvio el sum correcto (11)");
00205 }
00206 $avg = $model->average("id", "conditions: numero = 100");
00207 if($avg!=8.5){
00208 throw new DbException("No devolvio el avg correcto (12)");
00209 }
00210 $model->find_first("numero = 100");
00211 if($model->id!=1){
00212 throw new DbException("find_first con condicion fallo (13)");
00213 }
00214 $model->find_first(15);
00215 if($model->id!=15){
00216 throw new DbException("find_first a llave primaria (14)");
00217 }
00218 $model2 = $model->find_first("id > 10");
00219 if($model2->id!=11){
00220 throw new DbException("find_first a condicion (15)");
00221 }
00222 if($model->count()!=20){
00223 throw new DbException("count sin parametros (16)");
00224 }
00225 if($model->count("numero = 100")!=4){
00226 throw new DbException("count con parametros (17)");
00227 }
00228 if(count($model->distinct("id", "conditions: numero = 100"))!=4){
00229 throw new DbException("fallo distinct (18)");
00230 }
00231 $rows = $model->find_all_by_sql("SELECT * FROM kumbia_test WHERE id > 11 AND id < 14 ORDER BY 1");
00232 if($rows[0]->id!=12){
00233 throw new DbException("fallo find_all_by_sql (19)");
00234 }
00235 $row = $model->find_by_sql("SELECT * FROM kumbia_test WHERE id > 11 AND id < 13 ORDER BY 1");
00236 if($row->id!=12){
00237 throw new DbException("fallo find_by_sql (20)");
00238 }
00239 if(count($model->find_all_by_numero(100))!=4){
00240 throw new DbException("fallo find_all_by_numero (21)");
00241 }
00242 $model->find_by_id(16);
00243 if($model->id!=16){
00244 throw new DbException("fallo find_by_id (22)");
00245 }
00246 $num = $model->count_by_numero(100);
00247 if($model->id!=16){
00248 throw new DbException("fallo find_by_id (22)");
00249 }
00250 }
00251 catch(Exception $e){
00252 $test = false;
00253 print "<div style='background:#FFBBBB;border:1px solid red'>";
00254 print "Test '$test_name' (FALLÓ) con mensaje: ({$e->getMessage()})";
00255 print "</div>";
00256 return;
00257 }
00258 if($test){
00259 $end_benckmark = microtime(true) - $start_benchmark;
00260 print "<div style='background:#CCFF99;border:1px solid green'>";
00261 print "Test '$test_name' (OK) con tiempo: ({$end_benckmark})";
00262 print "</div>";
00263 }
00264
00265 $test = true;
00266 $test_name = "ELIMINAR REGISTROS DE PRUEBA EN EL MODELO";
00267 $start_benchmark = microtime(true);
00268 try {
00269 $model->delete(18);
00270 $model->delete_all("id < 10");
00271 $model->delete_all();
00272 }
00273 catch(Exception $e){
00274 $test = false;
00275 print "<div style='background:#FFBBBB;border:1px solid red'>";
00276 print "Test '$test_name' (FALLÓ) con mensaje: ({$e->getMessage()})";
00277 print "</div>";
00278 }
00279 if($test){
00280 $end_benckmark = microtime(true) - $start_benchmark;
00281 print "<div style='background:#CCFF99;border:1px solid green'>";
00282 print "Test '$test_name' (OK) con tiempo: ({$end_benckmark})";
00283 print "</div>";
00284 }
00285
00286
00287 print "<div style='background:#CCFF99;border:1px solid green'>";
00288 print "<strong>Tiempo total de los Test ".(microtime(true) - $init_time)."</strong>";
00289 print "</div>";
00290
00291 ?>