test_sql.php 5.2 KB

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