PageRenderTime 46ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/core/tests/active_record.php

https://bitbucket.org/gio_jgs/botica
PHP | 291 lines | 252 code | 17 blank | 22 comment | 36 complexity | 19240a4212ba25af98d26df7efa4bc66 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. * Kumbia PHP Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the GNU/GPL that is bundled
  8. * with this package in the file docs/LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://www.kumbia.org/license.txt
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to kumbia@kumbia.org so we can send you a copy immediately.
  14. *
  15. * Este archivo debe ser incluido desde un controlador
  16. * usando include "test/active_record.php"
  17. *
  18. * @category Kumbia
  19. * @package Test
  20. * @copyright Copyright (c) 2005-2007 Andres Felipe Gutierrez (andresfelipe at vagoogle.net)
  21. * @license http://www.kumbia.org/license.txt GNU/GPL
  22. */
  23. $config = Config::read('databases');
  24. $db = db::raw_connect();
  25. $db->debug = true;
  26. $total_time = 0;
  27. $test = true;
  28. $test_name = "CREAR Y BORRAR UNA TABLA";
  29. $init_time = $start_benchmark = microtime(true);
  30. try {
  31. $value1 = $db->drop_table("kumbia_test");
  32. if(!$value1){
  33. throw new DbException("No se pudo crear la tabla de prueba (1)");
  34. }
  35. $value2 = $db->create_table("kumbia_test", array(
  36. "id" => array(
  37. "type" => db::TYPE_INTEGER,
  38. "not_null" => true,
  39. "primary" => true,
  40. "auto" => true
  41. ),
  42. "texto" => array(
  43. "type" => db::TYPE_VARCHAR,
  44. "not_null" => true,
  45. "size" => 40
  46. ),
  47. "fecha" => array(
  48. "type" => db::TYPE_DATE,
  49. ),
  50. "email" => array(
  51. "type" => db::TYPE_VARCHAR,
  52. "size" => 70
  53. ),
  54. "numero" => array(
  55. "type" => db::TYPE_INTEGER,
  56. )
  57. ));
  58. if($value2===false){
  59. throw new DbException("No se pudo crear la tabla de prueba (2)");
  60. }
  61. if(!$db->table_exists("kumbia_test")){
  62. throw new DbException("No se pudo comprobar la existencia de la tabla de prueba (3)");
  63. }
  64. //Crear modelo dinamicamente
  65. eval("class KumbiaTest extends ActiveRecord {
  66. function __construct(){
  67. \$this->validates_numericality_of('numero');
  68. \$this->validates_presence_of('numero');
  69. \$this->validates_email_in('email');
  70. \$this->validates_date_in('fecha');
  71. \$this->validates_uniqueness_of('texto');
  72. }
  73. } ");
  74. unset($_SESSION['KUMBIA_META_DATA'][$_SESSION['KUMBIA_PATH']]["kumbia_test"]);
  75. $model = new KumbiaTest();
  76. if(!is_subclass_of($model, "ActiveRecord")){
  77. throw new DbException("No se pudo crear el modelo de prueba (3)");
  78. }
  79. }
  80. catch(Exception $e){
  81. $test = false;
  82. print "<div style='background:#FFBBBB;border:1px solid red'>";
  83. print "Test '$test_name' (FALL&Oacute;) con mensaje: ({$e->getMessage()})";
  84. print "</div>";
  85. }
  86. if($test){
  87. $end_benckmark = microtime(true) - $start_benchmark;
  88. print "<div style='background:#CCFF99;border:1px solid green'>";
  89. print "Test '$test_name' (OK) con tiempo: ({$end_benckmark})";
  90. print "</div>";
  91. }
  92. $test = true;
  93. $test_name = "INSERTAR DATOS DE PRUEBA EN EL MODELO";
  94. $init_time = $start_benchmark = microtime(true);
  95. try {
  96. $model->debug = true;
  97. for($i=1;$i<=20;$i++){
  98. $model->texto = "Texto ".$i;
  99. $model->fecha = "2007-02-".sprintf("%02d", rand(1, 10));
  100. $model->email = "kumbia@com";
  101. $model->numero = rand(0, 5);
  102. $model->create();
  103. }
  104. }
  105. catch(Exception $e){
  106. $test = false;
  107. print "<div style='background:#FFBBBB;border:1px solid red'>";
  108. print "Test '$test_name' (FALL&Oacute;) con mensaje: ({$e->getMessage()})";
  109. print "</div>";
  110. }
  111. if($test){
  112. $end_benckmark = microtime(true) - $start_benchmark;
  113. print "<div style='background:#CCFF99;border:1px solid green'>";
  114. print "Test '$test_name' (OK) con tiempo: ({$end_benckmark})";
  115. print "</div>";
  116. }
  117. $test = true;
  118. $test_name = "ACTUALIZAR DATOS DE PRUEBA EN EL MODELO";
  119. $start_benchmark = microtime(true);
  120. try {
  121. for($i=1;$i<=20;$i+=5){
  122. $model = $model->find($i);
  123. if($model){
  124. $model->numero = "100";
  125. $model->update();
  126. } else {
  127. throw new DbException("No Devolvio el objeto para id = $i");
  128. }
  129. }
  130. $model->update_all("email = 'hello@com'");
  131. $model->update_all("texto = 'otro texto'", "id <= 10");
  132. }
  133. catch(Exception $e){
  134. $test = false;
  135. print "<div style='background:#FFBBBB;border:1px solid red'>";
  136. print "Test '$test_name' (FALL&Oacute;) con mensaje: ({$e->getMessage()})";
  137. print "</div>";
  138. }
  139. if($test){
  140. $end_benckmark = microtime(true) - $start_benchmark;
  141. print "<div style='background:#CCFF99;border:1px solid green'>";
  142. print "Test '$test_name' (OK) con tiempo: ({$end_benckmark})";
  143. print "</div>";
  144. }
  145. $test = true;
  146. $test_name = "CONSULTAR DATOS DE PRUEBA EN EL MODELO";
  147. $start_benchmark = microtime(true);
  148. try {
  149. $model = new KumbiaTest();
  150. $model->debug = true;
  151. $model->find();
  152. if($model->count!=20){
  153. throw new DbException("No devolvio el numero correcto de registros en la tabla (1)");
  154. }
  155. $model->find_first(11);
  156. if($model->numero!=100){
  157. throw new DbException("No devolvio el registro correcto para id = 11 (2)");
  158. }
  159. $otro_model = $model->find_first(11);
  160. if($otro_model->numero!=100){
  161. throw new DbException("No devolvio el registro correcto para id = 11 (3)");
  162. }
  163. $model->find("numero = 100");
  164. if($model->count!=4){
  165. throw new DbException("No devolvio el numero correcto de registros en la tabla (4)");
  166. }
  167. $results = $model->find("numero = 100", "order: id desc");
  168. if($results[0]->id!=16){
  169. throw new DbException("No devolvio el registro correcto al ordenar (5)");
  170. }
  171. if(count($results)!=4){
  172. throw new DbException("No devolvio el numero de registros correcto al ordenar (6)");
  173. }
  174. $results = $model->find("conditions: numero = 100", "limit: 1", "order: id asc");
  175. if(count($results)!=1){
  176. throw new DbException("No devolvio el registro correcto cuando se uso limit y ordenamiento (7)");
  177. }
  178. if($results[0]->id!=1){
  179. throw new DbException("No devolvio el registro correcto cuando se uso limit y ordenamiento {$results[0]->id} (8)");
  180. }
  181. $min = $model->minimum("id", "conditions: numero = 100");
  182. if($min!=1){
  183. throw new DbException("No devolvio el minimum correcto (9)");
  184. }
  185. $max = $model->maximum("id", "conditions: numero = 100");
  186. if($max!=16){
  187. throw new DbException("No devolvio el maximum correcto (10)");
  188. }
  189. $sum = $model->sum("id", "conditions: numero = 100");
  190. if($sum!=34){
  191. throw new DbException("No devolvio el sum correcto (11)");
  192. }
  193. $avg = $model->average("id", "conditions: numero = 100");
  194. if($avg!=8.5){
  195. throw new DbException("No devolvio el avg correcto (12)");
  196. }
  197. $model->find_first("numero = 100");
  198. if($model->id!=1){
  199. throw new DbException("find_first con condicion fallo (13)");
  200. }
  201. $model->find_first(15);
  202. if($model->id!=15){
  203. throw new DbException("find_first a llave primaria (14)");
  204. }
  205. $model2 = $model->find_first("id > 10");
  206. if($model2->id!=11){
  207. throw new DbException("find_first a condicion (15)");
  208. }
  209. if($model->count()!=20){
  210. throw new DbException("count sin parametros (16)");
  211. }
  212. if($model->count("numero = 100")!=4){
  213. throw new DbException("count con parametros (17)");
  214. }
  215. if(count($model->distinct("id", "conditions: numero = 100"))!=4){
  216. throw new DbException("fallo distinct (18)");
  217. }
  218. $rows = $model->find_all_by_sql("SELECT * FROM kumbia_test WHERE id > 11 AND id < 14 ORDER BY 1");
  219. if($rows[0]->id!=12){
  220. throw new DbException("fallo find_all_by_sql (19)");
  221. }
  222. $row = $model->find_by_sql("SELECT * FROM kumbia_test WHERE id > 11 AND id < 13 ORDER BY 1");
  223. if($row->id!=12){
  224. throw new DbException("fallo find_by_sql (20)");
  225. }
  226. if(count($model->find_all_by_numero(100))!=4){
  227. throw new DbException("fallo find_all_by_numero (21)");
  228. }
  229. $model->find_by_id(16);
  230. if($model->id!=16){
  231. throw new DbException("fallo find_by_id (22)");
  232. }
  233. $num = $model->count_by_numero(100);
  234. if($model->id!=16){
  235. throw new DbException("fallo find_by_id (22)");
  236. }
  237. }
  238. catch(Exception $e){
  239. $test = false;
  240. print "<div style='background:#FFBBBB;border:1px solid red'>";
  241. print "Test '$test_name' (FALL&Oacute;) con mensaje: ({$e->getMessage()})";
  242. print "</div>";
  243. return;
  244. }
  245. if($test){
  246. $end_benckmark = microtime(true) - $start_benchmark;
  247. print "<div style='background:#CCFF99;border:1px solid green'>";
  248. print "Test '$test_name' (OK) con tiempo: ({$end_benckmark})";
  249. print "</div>";
  250. }
  251. $test = true;
  252. $test_name = "ELIMINAR REGISTROS DE PRUEBA EN EL MODELO";
  253. $start_benchmark = microtime(true);
  254. try {
  255. $model->delete(18);
  256. $model->delete_all("id < 10");
  257. $model->delete_all();
  258. }
  259. catch(Exception $e){
  260. $test = false;
  261. print "<div style='background:#FFBBBB;border:1px solid red'>";
  262. print "Test '$test_name' (FALL&Oacute;) con mensaje: ({$e->getMessage()})";
  263. print "</div>";
  264. }
  265. if($test){
  266. $end_benckmark = microtime(true) - $start_benchmark;
  267. print "<div style='background:#CCFF99;border:1px solid green'>";
  268. print "Test '$test_name' (OK) con tiempo: ({$end_benckmark})";
  269. print "</div>";
  270. }
  271. print "<div style='background:#CCFF99;border:1px solid green'>";
  272. print "<strong>Tiempo total de los Test ".(microtime(true) - $init_time)."</strong>";
  273. print "</div>";
  274. ?>