test_sql.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. __sql::set_login_data('localhost', 'root', '', 'pquery_test');
  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::$login_data = array();
  21. __sql::assert_login_data_exist();
  22. }
  23. function test_query_getter() {
  24. $sql = _sql('foobar');
  25. $this->assertEqual($sql->variable, 'foobar');
  26. $this->assertEqual($sql->query, 'foobar');
  27. }
  28. function test_variable_query() {
  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. $sql = _sql("select id from foo where bar = '[bar]'")
  40. ->set(array('bar' => "select id from foo where bar = 'test1'"));
  41. $this->assertNotEqual($sql->query, "select id from foo where bar = 'select id from foo where bar = 'test1''");
  42. }
  43. function test_select_simple() {
  44. $sql = _sql("select bar from foo where id = 1");
  45. $result = $sql->fetch('object');
  46. $this->assertEqual($result->bar, 'test1');
  47. $this->assertIdentical($sql->fetch(), false);
  48. }
  49. function test_result_count() {
  50. $sql = _sql("select bar from foo where id in (1, 2)");
  51. $this->assertEqual($sql->result_count(), 2);
  52. }
  53. }
  54. ?>