test_sql.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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_result_count() {
  65. __sql::set_login_data('localhost', 'root', '', 'pquery_test');
  66. $sql = _sql("select bar from foo where id in (1, 2)");
  67. $this->assertEqual($sql->result_count(), 2);
  68. }
  69. static function set_login_data() {
  70. __sql::set_login_data('localhost', 'root', '', 'pquery_test');
  71. }
  72. }
  73. ?>