Changeset 122

Show
Ignore:
Timestamp:
06/29/09 22:09:25 (14 months ago)
Author:
seanja
Message:

Some more unit tests for the smysqlquery class (found some bugs and fixed them too!)

Files:
2 modified

Legend:

Unmodified
Added
Removed
  • library/classes/smysqlquery.class.php

    r111 r122  
    145145         */ 
    146146        public function setFrom($table) { 
    147                 return $this->query->from('`'.$this->escape($table).'`'); 
     147                return $this->query->setFrom('`'.$this->escape($table).'`'); 
    148148        } 
    149149 
     
    153153        public function from() { 
    154154                $tables = func_get_args(); 
    155                 $tables = $tables[0]; 
    156                 return $this->query->from($tables); 
     155                foreach($tables as $table){ 
     156                        $this->query->from('`'.$this->escape($table).'`'); 
     157                } 
     158                return true; 
    157159        } 
    158160 
     
    326328                        $sql = "( \n /*begin subselect*/ \n".$queryObject->getSelect()."\n /*end subselect*/ \n)"; 
    327329                } else { 
    328                         $sql= 'SELECT '; 
     330                        $sql= 'SELECT'; 
    329331                        $sql .= $this->getColumnString($queryObject); 
    330332                        $sql .= ' FROM '.$this->getTableString($queryObject); 
     
    388390         */ 
    389391        public function getCount() { 
    390                 $sql  = "SELECT "; 
     392                $sql  = 'SELECT'; 
    391393                $sql .= ' COUNT(*) ' . "\n"; 
    392394                $sql .= ' FROM '.$this->getTableString(); 
  • tests/library/classes/sMYSQLQueryTest.php

    r111 r122  
    5555    { 
    5656                $this->assertEquals( 
     57                        true, 
     58                        $this->object->connect() 
     59                ); 
     60                $this->assertEquals( 
     61                        true, 
     62                        $this->object->connect() 
     63                ); 
     64                $this->assertEquals( 
    5765                        $this->object->connect(), 
    58                         true 
    59                 ); 
    60                 $this->assertEquals( 
    61                         $this->object->connect(), 
    62                         true 
     66                        $this->object->connect() 
    6367                ); 
    6468    } 
     
    7175                $this->object->connect(); 
    7276        $this->assertEquals( 
    73                         $this->object->disconnect(), 
    74                         true 
     77                        true, 
     78                        $this->object->disconnect() 
    7579                ); 
    7680                //you can only disconnect once 
    7781                //but you are disconnected 
    7882                $this->assertEquals( 
     83                        true, 
     84                        $this->object->disconnect() 
     85                ); 
     86                $this->assertEquals( 
    7987                        $this->object->disconnect(), 
    80                         true 
     88                        $this->object->disconnect() 
    8189                ); 
    8290    } 
     
    8896    { 
    8997        // Remove the following lines when you implement this test. 
    90         $this->markTestIncomplete( 
    91           'This test has not been implemented yet.' 
    92         ); 
     98                $this->assertEquals( 
     99                        array(), 
     100                        $this->object->queryDb("SELECT * FROM test WHERE id = 'asdf'") 
     101                ); 
     102                $this->assertEquals( 
     103                        array( 
     104                                array 
     105                                ( 
     106                                        'id' => 1, 
     107                                        'value' => 'one' 
     108                                ) 
     109                        ) 
     110                        , 
     111                        $this->object->queryDb("SELECT * FROM test WHERE id = 1") 
     112                ); 
     113                $this->assertEquals( 
     114                        array( 
     115                                array 
     116                                ( 
     117                                        'id' => 1, 
     118                                        'value' => 'one' 
     119                                ), 
     120                                array 
     121                                ( 
     122                                        'id' => 2, 
     123                                        'value' => 'two' 
     124                                ) 
     125                        ) 
     126                        , 
     127                        $this->object->queryDb("SELECT * FROM test") 
     128                ); 
    93129    } 
    94130 
     
    99135    { 
    100136        $this->assertEquals( 
    101                         $this->object->newQuery(), 
    102                         true 
     137                        true, 
     138                        $this->object->newQuery() 
    103139                ); 
    104140    } 
     
    113149                        $this->fail('Should throw exception'); 
    114150                } catch(Exception $e) { 
    115                         print_r($e->getMessage()); 
    116151                        $this->assertEquals( 
    117                                 strpos($e->getMessage(), 'Not implemented'), 
    118                                 0 
     152                                0, 
     153                                strpos($e->getMessage(), 'Not implemented') 
    119154                        ); 
    120155                } 
     
    126161    public function testGetNumRows() 
    127162    { 
    128         // Remove the following lines when you implement this test. 
    129         $this->markTestIncomplete( 
    130           'This test has not been implemented yet.' 
    131         ); 
     163                $this->object->queryDb("SELECT * FROM test WHERE id = 'asdf'"); 
     164                $this->assertEquals( 
     165                        0, 
     166                        $this->object->getNumRows() 
     167                ); 
     168                $this->object->queryDb("SELECT * FROM test WHERE id = 1"); 
     169                $this->assertEquals( 
     170                        1, 
     171                        $this->object->getNumRows() 
     172                ); 
     173                $this->object->queryDb("SELECT * FROM test WHERE id IN (1, 2)"); 
     174                $this->assertEquals( 
     175                        2, 
     176                        $this->object->getNumRows() 
     177                ); 
    132178    } 
    133179 
     
    137183    public function testSetFrom() 
    138184    { 
    139         // Remove the following lines when you implement this test. 
    140         $this->markTestIncomplete( 
    141           'This test has not been implemented yet.' 
    142         ); 
     185                 
     186                try{ 
     187                        $this->object->newQuery(); 
     188                        $this->object->getSelect(); 
     189                        $this->fail('Should throw exception'); 
     190                } catch(Exception $e) { 
     191                        $this->assertEquals( 
     192                                0, 
     193                                strpos($e->getMessage(), 'You have to set a table before running this query') 
     194                        ); 
     195                } 
     196                 
     197                $this->object->newQuery(); 
     198                $this->object->setFrom('test'); 
     199                $select = $this->object->getSelect(); 
     200                $select = str_replace("\n", '', $select); 
     201                $this->assertEquals( 
     202                        "SELECT * FROM `test`", 
     203                        $select 
     204                ); 
     205                 
     206                $this->object->setFrom('not_test'); 
     207                $select = $this->object->getSelect(); 
     208                $select = str_replace("\n", '', $select); 
     209                $this->assertEquals( 
     210                        "SELECT * FROM `not_test`\n", 
     211                        $this->object->getSelect() 
     212                ); 
    143213    } 
    144214 
     
    148218    public function testFrom() 
    149219    { 
    150         // Remove the following lines when you implement this test. 
    151         $this->markTestIncomplete( 
    152           'This test has not been implemented yet.' 
    153         ); 
     220        $this->object->newQuery(); 
     221                $this->object->setFrom('test'); 
     222                $select = $this->object->getSelect(); 
     223                $select = str_replace("\n", '', $select); 
     224                $this->assertEquals( 
     225                        "SELECT * FROM `test`", 
     226                        $select 
     227                ); 
     228 
     229                $this->object->newQuery(); 
     230                $this->object->from('test'); 
     231                $select = $this->object->getSelect(); 
     232                $select = str_replace("\n", '', $select); 
     233                $this->assertEquals( 
     234                        "SELECT * FROM `test`", 
     235                        $select 
     236                ); 
     237 
     238                $this->object->newQuery(); 
     239                $this->object->from('test', 'not_test'); 
     240                $select = $this->object->getSelect(); 
     241                $select = str_replace("\n", '', $select); 
     242                $this->assertEquals( 
     243                        "SELECT * FROM `test`, `not_test`", 
     244                        $select 
     245                ); 
     246 
     247                $this->object->newQuery(); 
     248                $this->object->from('test'); 
     249                $this->object->from('not_test'); 
     250                $select = $this->object->getSelect(); 
     251                $select = str_replace("\n", '', $select); 
     252                $this->assertEquals( 
     253                        "SELECT * FROM `test`, `not_test`", 
     254                        $select 
     255                ); 
     256 
    154257    } 
    155258