X-Git-Url: https://git.openstreetmap.org./nominatim.git/blobdiff_plain/890d415e1ffd997d11ed35dee04cb01b94ca133f..42c3754dcdf85ddd3b048c64187435c241199a90:/test/php/Nominatim/DBTest.php diff --git a/test/php/Nominatim/DBTest.php b/test/php/Nominatim/DBTest.php index 38874c88..1c6f7637 100644 --- a/test/php/Nominatim/DBTest.php +++ b/test/php/Nominatim/DBTest.php @@ -1,9 +1,17 @@ assertTrue($oDB->connect()); } - public function testDatabaseExists() + public function testCheckConnection() { $oDB = new \Nominatim\DB(''); - $this->assertFalse($oDB->databaseExists()); + $this->assertFalse($oDB->checkConnection()); } public function testErrorHandling() @@ -113,4 +121,108 @@ class DBTest extends \PHPUnit\Framework\TestCase \Nominatim\DB::parseDSN('pgsql:dbname=db1;host=machine1;port=1234;user=john;password=secret') ); } + + public function testGenerateDSN() + { + $this->assertEquals( + 'pgsql:', + \Nominatim\DB::generateDSN(array()) + ); + $this->assertEquals( + 'pgsql:host=machine1;dbname=db1', + \Nominatim\DB::generateDSN(\Nominatim\DB::parseDSN('pgsql:host=machine1;dbname=db1')) + ); + } + + public function testAgainstDatabase() + { + $unit_test_dsn = getenv('UNIT_TEST_DSN') != false ? + getenv('UNIT_TEST_DSN') : + 'pgsql:dbname=nominatim_unit_tests'; + + ## Create the database. + { + $aDSNParsed = \Nominatim\DB::parseDSN($unit_test_dsn); + $sDbname = $aDSNParsed['database']; + $aDSNParsed['database'] = 'postgres'; + + $oDB = new \Nominatim\DB(\Nominatim\DB::generateDSN($aDSNParsed)); + $oDB->connect(); + $oDB->exec('DROP DATABASE IF EXISTS ' . $sDbname); + $oDB->exec('CREATE DATABASE ' . $sDbname); + } + + $oDB = new \Nominatim\DB($unit_test_dsn); + $oDB->connect(); + + $this->assertTrue( + $oDB->checkConnection($sDbname) + ); + + # Tables, Indices + { + $oDB->exec('CREATE TABLE table1 (id integer, city varchar, country varchar)'); + + $this->assertTrue($oDB->tableExists('table1')); + $this->assertFalse($oDB->tableExists('table99')); + $this->assertFalse($oDB->tableExists(null)); + } + + # select queries + { + $oDB->exec( + "INSERT INTO table1 VALUES (1, 'Berlin', 'Germany'), (2, 'Paris', 'France')" + ); + + $this->assertEquals( + array( + array('city' => 'Berlin'), + array('city' => 'Paris') + ), + $oDB->getAll('SELECT city FROM table1') + ); + $this->assertEquals( + array(), + $oDB->getAll('SELECT city FROM table1 WHERE id=999') + ); + + + $this->assertEquals( + array('id' => 1, 'city' => 'Berlin', 'country' => 'Germany'), + $oDB->getRow('SELECT * FROM table1 WHERE id=1') + ); + $this->assertEquals( + false, + $oDB->getRow('SELECT * FROM table1 WHERE id=999') + ); + + + $this->assertEquals( + array('Berlin', 'Paris'), + $oDB->getCol('SELECT city FROM table1') + ); + $this->assertEquals( + array(), + $oDB->getCol('SELECT city FROM table1 WHERE id=999') + ); + + $this->assertEquals( + 'Berlin', + $oDB->getOne('SELECT city FROM table1 WHERE id=1') + ); + $this->assertEquals( + null, + $oDB->getOne('SELECT city FROM table1 WHERE id=999') + ); + + $this->assertEquals( + array('Berlin' => 'Germany', 'Paris' => 'France'), + $oDB->getAssoc('SELECT city, country FROM table1') + ); + $this->assertEquals( + array(), + $oDB->getAssoc('SELECT city, country FROM table1 WHERE id=999') + ); + } + } }