test_sql.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. __p::load_plugin('sql');
  3. class pQuerySqlTest extends UnitTestCase {
  4. function __construct() {
  5. parent::__construct('pQuery MySQL plugin');
  6. }
  7. function tearDown() {
  8. __sql::disconnect();
  9. __sql::$login_data = array();
  10. }
  11. function test_set_login_data() {
  12. __sql::set_login_data('a', 'b', 'c', 'd');
  13. $this->assertEqual(__sql::$login_data['host'], 'a');
  14. $this->assertEqual(__sql::$login_data['username'], 'b');
  15. $this->assertEqual(__sql::$login_data['password'], 'c');
  16. $this->assertEqual(__sql::$login_data['dbname'], 'd');
  17. }
  18. function test_no_login_data() {
  19. $this->expectException('pQueryException');
  20. __sql::assert_login_data_exist();
  21. }
  22. function test_query_getter() {
  23. $sql = _sql('foobar');
  24. $this->assertEqual($sql->variable, 'foobar');
  25. $this->assertEqual($sql->query, 'foobar');
  26. }
  27. function test_variable_query() {
  28. self::set_login_data();
  29. $sql = _sql("select id from foo where bar = '[bar]'")
  30. ->set(array('bar' => 'test1'));
  31. $this->assertEqual($sql->query, "select id from foo where bar = 'test1'");
  32. }
  33. function test_unescaped_query() {
  34. $sql = _sql("select id from foo where bar = '[bar]'")
  35. ->set_unescaped(array('bar' => "select id from foo where bar = 'test1'"));
  36. $this->assertEqual($sql->query, "select id from foo where bar = 'select id from foo where bar = 'test1''");
  37. }
  38. function test_escaped_query() {
  39. self::set_login_data();
  40. $sql = _sql("select id from foo where bar = '[bar]'")
  41. ->set(array('bar' => "select id from foo where bar = 'test1'"));
  42. $this->assertNotEqual($sql->query, "select id from foo where bar = 'select id from foo where bar = 'test1''");
  43. }
  44. function test_constructor_simple() {
  45. self::set_login_data();
  46. $sql = _sql("select id from foo where bar = '[0]'", 'test1');
  47. $this->assertEqual($sql->query, "select id from foo where bar = 'test1'");
  48. }
  49. function test_constructor_advanced() {
  50. self::set_login_data();
  51. $sql = _sql("[0] [bar] [foo] [2]", '1', array('bar' => '2', 'foo' => '3'), '4');
  52. $this->assertEqual($sql->query, "1 2 3 4");
  53. }
  54. function test_num_rows() {
  55. self::set_login_data();
  56. $sql = _sql("select bar from foo where id in (1, 2)");
  57. $this->assertEqual($sql->num_rows(), 2);
  58. }
  59. function test_escape_column_simple() {
  60. $this->assertEqual(__sql::escape_column('foo'), '`foo`');
  61. }
  62. function test_escape_column_escaped() {
  63. $this->assertEqual(__sql::escape_column('`foo`'), '`foo`');
  64. }
  65. function test_escape_column_table() {
  66. $this->assertEqual(__sql::escape_column('foo.bar'), '`foo`.`bar`');
  67. }
  68. function test_escape_column_aggregate() {
  69. $this->assertEqual(__sql::escape_column('count(foo)'), 'COUNT(`foo`)');
  70. }
  71. function test_escape_column_aggregate_escaped() {
  72. $this->assertEqual(__sql::escape_column('count(`foo`)'), 'COUNT(`foo`)');
  73. }
  74. function test_parse_columns_star() {
  75. $sql = __sql::select('foo', '*', '', false);
  76. $this->assertEqual($sql->query, "SELECT * FROM `foo` WHERE 1;");
  77. }
  78. function test_parse_columns_simple() {
  79. $sql = __sql::select('foo', array('id', 'bar'), '', false);
  80. $this->assertEqual($sql->query, "SELECT `id`, `bar` FROM `foo` WHERE 1;");
  81. }
  82. function test_parse_columns_as() {
  83. $sql = __sql::select('foo', array('id' => 'foo_id'), '', false);
  84. $this->assertEqual($sql->query, "SELECT `id` AS `foo_id` FROM `foo` WHERE 1;");
  85. }
  86. function test_parse_constraints_empty() {
  87. $this->assertIdentical(__sql::parse_constraints(null, false), "1");
  88. }
  89. function test_parse_constraints_string() {
  90. $constraints = "foo LIKE '%bar%'";
  91. $this->assertEqual(__sql::parse_constraints($constraints, false), $constraints);
  92. }
  93. function test_parse_constraints_simple() {
  94. $this->assertEqual(__sql::parse_constraints(
  95. array('id' => 1, 'bar' => 'test1'), false),
  96. "`id` = '1' AND `bar` = 'test1'");
  97. }
  98. function test_parse_constraints_value_list() {
  99. $this->assertEqual(__sql::parse_constraints(
  100. array('id' => range(1, 3)), false),
  101. "`id` IN ('1', '2', '3')");
  102. }
  103. function test_select_query() {
  104. $sql = __sql::select('foo', '*', array('bar' => 'test1'), false);
  105. $this->assertEqual($sql->query, "SELECT * FROM `foo` WHERE `bar` = 'test1';");
  106. }
  107. function test_update_query() {
  108. $sql = __sql::update('foo', array('bar' => 'test4'), array('id' => 1), false);
  109. $this->assertEqual($sql->query, "UPDATE `foo` SET `bar` = 'test4' WHERE `id` = '1';");
  110. }
  111. function test_insert_query() {
  112. $sql = __sql::insert_row('foo', array('bar' => 'test3'), false);
  113. $this->assertEqual($sql->query, "INSERT INTO `foo`(`bar`) VALUES('test3');");
  114. }
  115. function test_delete_query() {
  116. $sql = __sql::delete('foo', array('bar' => 'test3'), false);
  117. $this->assertEqual($sql->query, "DELETE FROM `foo` WHERE `bar` = 'test3';");
  118. }
  119. function test_select() {
  120. self::set_login_data();
  121. $sql = _sql("select bar from foo where id = 1");
  122. $result = $sql->fetch('object');
  123. $this->assertEqual($result->bar, 'test1');
  124. $this->assertIdentical($sql->fetch(), false);
  125. }
  126. function test_update() {
  127. self::set_login_data();
  128. $update = __sql::update('foo', array('bar' => 'test4'),
  129. array('id' => 1), false)->execute();
  130. // Do not continue unless the value has been updated
  131. if( !$this->assertIdentical($update->result, true) )
  132. return false;
  133. // Chachge the updated record back to its original state
  134. $update = __sql::update('foo', array('bar' => 'test1'),
  135. array('id' => 1), false)->execute();
  136. $this->assertIdentical($update->result, true);
  137. }
  138. function test_insert_delete() {
  139. self::set_login_data();
  140. $insert = __sql::insert_row('foo', array('bar' => 'test3'))->execute();
  141. // Do not continue unless the value has been inserted
  142. if( !$this->assertIdentical($insert->result, true) )
  143. return false;
  144. // Delete the record that was just inserted
  145. $delete = __sql::delete('foo', array('bar' => 'test3'))->execute();
  146. $this->assertIdentical($delete->result, true);
  147. }
  148. static function set_login_data() {
  149. __sql::set_login_data('localhost', 'root', '', 'pquery_test');
  150. }
  151. }
  152. ?>