database.lib.test.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. require_once(api_get_path(LIBRARY_PATH).'database.lib.php');
  3. require_once(api_get_path(LIBRARY_PATH).'add_course.lib.inc.php');
  4. require_once(api_get_path(LIBRARY_PATH).'course.lib.php');
  5. class TestDatabase extends UnitTestCase {
  6. public $dbase;
  7. public function TestDatabase() {
  8. $this->UnitTestCase('Database library - main/inc/lib/database.lib.test.php');
  9. }
  10. public function setUp() {
  11. global $_configuration;
  12. $this->dbase = new Database();
  13. }
  14. public function tearDown() {
  15. $this->dbase = null;
  16. }
  17. public function testAffectedRows() {
  18. $res=$this->dbase->affected_rows();
  19. $this->assertTrue(is_numeric($res));
  20. }
  21. public function testCountRows() {
  22. $table='class';
  23. $res=$this->dbase->count_rows($table);
  24. if(!is_string($res)){
  25. $this->assertTrue(is_numeric($res));
  26. }
  27. }
  28. public function testError() {
  29. $res=$this->dbase->error();
  30. $this->assertTrue(is_string($res));
  31. }
  32. public function testEscapeString() {
  33. $string='Lore"May';
  34. $res=$this->dbase->escape_string($string);
  35. $this->assertTrue(is_string($res));
  36. }
  37. public function testFetchArray() {
  38. $sql = 'select 1';
  39. $res=Database::query($sql);
  40. $resu=$this->dbase->fetch_array($res);
  41. $this->assertTrue(is_array($resu));
  42. $this->assertFalse(is_null($resu));
  43. }
  44. public function testFetchArrayError() {
  45. $sql = 'SELECT 1';
  46. $res=Database::query($sql);
  47. $resu=$this->dbase->fetch_array($res);
  48. $this->assertTrue(is_array($resu));
  49. }
  50. function testFetchObject() {
  51. $sql = 'SELECT 1';
  52. $res=Database::query($sql);
  53. $resu=$this->dbase->fetch_object($res);
  54. $this->assertTrue(is_object($resu));
  55. }
  56. function testFetchRow() {
  57. $sql = 'SELECT 1';
  58. $res=Database::query($sql);
  59. $resu=$this->dbase->fetch_row($res);
  60. $this->assertTrue(is_array($resu));
  61. }
  62. /* // Contains a private unaccessible method, Database::fix_database_parameter().
  63. function testFixDatabaseParameterReturnString() {
  64. $course_info = api_get_course_info();
  65. $database_name= $course_info["dbName"];
  66. $res=$this->dbase->fix_database_parameter($course_info);
  67. $this->assertTrue(is_string($res));
  68. }
  69. */
  70. /* // Contains a private unaccessible method, Database::format_glued_course_table_name().
  71. function testFormatGluedCourseTableName() {
  72. $database_name_with_glue='';
  73. $table='';
  74. $res=$this->dbase->format_glued_course_table_name($database_name_with_glue, $table);
  75. $this->assertTrue(is_string($res));
  76. }
  77. */
  78. /* // Contains a private unaccessible method, Database::format_table_name().
  79. function testFormatTableName() {
  80. $database='';
  81. $table='';
  82. $res=$this->dbase->format_table_name($database, $table);
  83. $this->assertTrue(is_string($res));
  84. }
  85. */
  86. function testGetCourseInfo() {
  87. $course_code='AYDD';
  88. $res=$this->dbase->get_course_info($course_code);
  89. $this->assertTrue(is_array($res));
  90. }
  91. function testGetCourseList() {
  92. $sql_query = "SELECT * FROM $table";
  93. $res=$this->dbase->get_course_list($sql_query);
  94. $this->assertTrue(is_array($res));
  95. }
  96. function testGetCourseTable() {
  97. $short_table_name='';
  98. $database_name='';
  99. $res=$this->dbase->get_course_table($short_table_name,$database_name);
  100. $this->assertTrue(is_string($res));
  101. }
  102. function testGetMainDatabase() {
  103. global $_configuration;
  104. $res=$this->dbase->get_main_database();
  105. $this->assertTrue(is_string($res));
  106. }
  107. function testGetMainTable() {
  108. $short_table_name='';
  109. $res=$this->dbase->get_main_table($short_table_name);
  110. $this->assertTrue(is_string($res));
  111. }
  112. /* // Contains a private unaccessible method, Database::glue_course_database_name().
  113. function testGlueCourseDatabaseName() {
  114. $database_name='';
  115. $res=$this->dbase->glue_course_database_name($database_name);
  116. $this->assertTrue(is_string($res));
  117. $this->assertTrue($res);
  118. }
  119. */
  120. function testInsertId() {
  121. $res = $this->dbase->insert_id();
  122. $this->assertTrue(is_numeric($res));
  123. }
  124. function testNumRows() {
  125. $sql = 'SELECT * FROM user';
  126. $res = Database::query($sql);
  127. $resul=Database::num_rows($res);
  128. $this->assertTrue(is_numeric($resul));
  129. }
  130. function testQuery() {
  131. $sql = 'SELECT 1';
  132. $res = Database::query($sql);
  133. $this->assertTrue(is_resource($res));
  134. }
  135. function testResult() {
  136. $sql = 'SELECT email FROM user';
  137. $resource = Database::query($sql);
  138. $res = Database::result($resource, 1);
  139. $this->assertTrue(is_string($res));
  140. }
  141. function testStoreResult(){
  142. $sql = 'SELECT 1';
  143. $resource = $this->dbase->query($sql);
  144. $res = $this->dbase->store_result($resource);
  145. $this->assertTrue(is_array($res));
  146. }
  147. }
  148. ?>