mdb_usage_testcase.php

来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· PHP 代码 · 共 1,121 行 · 第 1/4 页

PHP
1,121
字号
        }        // Test currId()        $sequence_name = 'test_currid';        $next = $this->db->nextId($sequence_name);        $curr = $this->db->currId($sequence_name);        if (MDB::isError($curr)) {            $this->assertTrue(false, "Error getting the current value of sequence $sequence_name : ".$curr->getMessage());        } else {            if ($next != $curr) {                if ($next+1 == $curr) {                    $this->assertTrue(false, "Warning: currId() is using nextId() instead of a native implementation");                } else {                    $this->assertEquals($next, $curr, "return value if currId() does not match the previous call to nextId()");                }            }        }        $result = $this->db->dropSequence($sequence_name);        if (MDB::isError($result)) {            $this->assertTrue(false, "Error dropping sequence $sequence_name : ".$result->getMessage());        }    }    /**     * Test replace query     *     * The replace method emulates the replace query of mysql     */    function testReplace() {        if (!$this->supported('Replace')) {            return;        }        $row = 1234;        $data = array();        $data['user_name'] = "user_$row";        $data['user_password'] = 'somepassword';        $data['subscribed'] = (boolean)($row % 2);        $data['user_id'] = $row;        $data['quota'] = strval($row/100);        $data['weight'] = sqrt($row);        $data['access_date'] = MDB_Date::mdbToday();        $data['access_time'] = MDB_Date::mdbTime();        $data['approved'] = MDB_Date::mdbNow();        $fields = array(                      'user_name' => array(                                           'Value' => "user_$row",                                           'Type' => 'text'                                         ),                      'user_password' => array(                                               'Value' => $data['user_password'],                                               'Type' => 'text'                                             ),                      'subscribed' => array(                                            'Value' => $data['subscribed'],                                            'Type' => 'boolean'                                          ),                      'user_id' => array(                                         'Value' => $data['user_id'],                                         'Type' => 'integer',                                         'Key' => 1                                       ),                      'quota' => array(                                       'Value' => $data['quota'],                                       'Type' => 'decimal'                                     ),                      'weight' => array(                                        'Value' => $data['weight'],                                        'Type' => 'float'                                      ),                      'access_date' => array(                                             'Value' => $data['access_date'],                                             'Type' => 'date'                                           ),                      'access_time' => array(                                             'Value' => $data['access_time'],                                             'Type' => 'time'                                           ),                      'approved' => array(                                          'Value' => $data['approved'],                                          'Type' => 'timestamp'                                        )                      );        $support_affected_rows = $this->db->support('AffectedRows');        $result = $this->db->replace('users', $fields);        if (MDB::isError($result)) {            $this->assertTrue(false, 'Replace failed');        }        if ($support_affected_rows) {            $affected_rows = $this->db->affectedRows();            $this->assertEquals(1, $affected_rows, "replacing a row in an empty table returned $affected_rows unlike 1 as expected");        }        $result = $this->db->query('SELECT user_name, user_password, subscribed, user_id, quota, weight, access_date, access_time, approved FROM users', $this->types);        if (MDB::isError($result)) {            $this->assertTrue(false, 'Error selecting from users'.$result->getMessage());        }        $this->verifyFetchedValues($result, 0, $data);        $row = 4321;        $fields['user_name']['Value']     = $data['user_name']     = "user_$row";        $fields['user_password']['Value'] = $data['user_password'] = 'somepassword';        $fields['subscribed']['Value']    = $data['subscribed']    = (boolean)($row % 2);        $fields['quota']['Value']         = $data['quota']         = strval($row/100);        $fields['weight']['Value']        = $data['weight']        = sqrt($row);        $fields['access_date']['Value']   = $data['access_date']   = MDB_Date::mdbToday();        $fields['access_time']['Value']   = $data['access_time']   = MDB_Date::mdbTime();        $fields['approved']['Value']      = $data['approved']      = MDB_Date::mdbNow();        $result = $this->db->replace('users', $fields);        if (MDB::isError($result)) {            $this->assertTrue(false, 'Replace failed');        }        if ($support_affected_rows) {            $affected_rows = $this->db->affectedRows();            $this->assertEquals(2, $affected_rows, "replacing a row in a non empty table returned $affected_rows unlike 2 as expected");        }        $result = $this->db->query('SELECT user_name, user_password, subscribed, user_id, quota, weight, access_date, access_time, approved FROM users', $this->types);        if (MDB::isError($result)) {            $this->assertTrue(false, 'Error selecting from users'.$result->getMessage());        }        $this->verifyFetchedValues($result, 0, $data);        $this->assertTrue($this->db->endOfResult($result), 'the query result did not seem to have reached the end of result as expected');        $this->db->freeResult($result);    }    /**     * Test affected rows methods     */    function testAffectedRows() {        if (!$this->supported('AffectedRows')) {            return;        }        $data = array();        $total_rows = 7;        $prepared_query = $this->db->prepareQuery('INSERT INTO users (user_name, user_password, subscribed, user_id, quota, weight, access_date, access_time, approved) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)', $this->types);        for ($row = 0; $row < $total_rows; $row++) {            $data[$row]['user_name']     = "user_$row";            $data[$row]['user_password'] = 'somepassword';            $data[$row]['subscribed']    = (boolean)($row % 2);            $data[$row]['user_id']       = $row;            $data[$row]['quota']         = sprintf("%.2f",strval(1+($row+1)/100));            $data[$row]['weight']        = sqrt($row);            $data[$row]['access_date']   = MDB_Date::mdbToday();            $data[$row]['access_time']   = MDB_Date::mdbTime();            $data[$row]['approved']      = MDB_Date::mdbNow();            $this->insertTestValues($prepared_query, $data[$row]);            $result = $this->db->executeQuery($prepared_query);            $affected_rows = $this->db->affectedRows();            $this->assertEquals($affected_rows, 1, "Inserting the row $row returned $affected_rows affected row count instead of 1 as expected");            if (MDB::isError($result)) {                $this->assertTrue(false, 'Error executing prepared query'.$result->getMessage());            }        }        $this->db->freePreparedQuery($prepared_query);        $prepared_query = $this->db->prepareQuery('UPDATE users SET user_password=? WHERE user_id < ?', array('text', 'integer'));        for ($row = 0; $row < $total_rows; $row++) {            $this->db->setParamText($prepared_query, 1, "another_password_$row");            $this->db->setParamInteger($prepared_query, 2, $row);            $result = $this->db->executeQuery($prepared_query);            if (MDB::isError($result)) {                $this->assertTrue(false, 'Error executing prepared query'.$result->getMessage());            }            $affected_rows = $this->db->affectedRows();            $this->assertEquals($affected_rows, $row, "Updating the $row rows returned $affected_rows affected row count");        }        $this->db->freePreparedQuery($prepared_query);        $prepared_query = $this->db->prepareQuery('DELETE FROM users WHERE user_id >= ?', array('integer'));        for ($row = $total_rows; $total_rows; $total_rows = $row) {            $this->db->setParamInteger($prepared_query, 1, $row = intval($total_rows / 2));            $result = $this->db->executeQuery($prepared_query);            if (MDB::isError($result)) {                $this->assertTrue(false, 'Error executing prepared query'.$result->getMessage());            }            $affected_rows = $this->db->affectedRows();            $this->assertEquals($affected_rows, ($total_rows - $row), 'Deleting '.($total_rows - $row)." rows returned $affected_rows affected row count");        }        $this->db->freePreparedQuery($prepared_query);    }    /**     * Testing transaction support     */    function testTransactions() {        if (!$this->supported('Transactions')) {            return;        }        $this->db->autoCommit(0);        $row = 0;        $data = array();        $data['user_name']     = "user_$row";        $data['user_password'] = 'somepassword';        $data['subscribed']    = (boolean)($row % 2);        $data['user_id']       = $row;        $data['quota']         = strval($row/100);        $data['weight']        = sqrt($row);        $data['access_date']   = MDB_Date::mdbToday();        $data['access_time']   = MDB_Date::mdbTime();        $data['approved']      = MDB_Date::mdbNow();        $prepared_query = $this->db->prepareQuery('INSERT INTO users (user_name, user_password, subscribed, user_id, quota, weight, access_date, access_time, approved) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)', $this->types);        $this->insertTestValues($prepared_query, $data);        $result = $this->db->executeQuery($prepared_query);        $this->db->rollback();        $result = $this->db->query('SELECT * FROM users');        if (MDB::isError($result)) {            $this->assertTrue(false, 'Error selecting from users'.$result->getMessage());        }        $this->assertTrue($this->db->endOfResult($result), 'Transaction rollback did not revert the row that was inserted');        $this->db->freeResult($result);        $this->insertTestValues($prepared_query, $data);        $result = $this->db->executeQuery($prepared_query);        $this->db->commit();        $result = $this->db->query('SELECT * FROM users');        if (MDB::isError($result)) {            $this->assertTrue(false, 'Error selecting from users'.$result->getMessage());        }        $this->assertTrue(!$this->db->endOfResult($result), 'Transaction commit did not make permanent the row that was inserted');        $this->db->freeResult($result);        $result = $this->db->query('DELETE FROM users');        if (MDB::isError($result)) {            $this->assertTrue(false, 'Error deleting from users'.$result->getMessage());            $this->db->rollback();        }        $autocommit = $this->db->autocommit(1);        $this->assertTrue(!MDB::isError($autocommit), 'Error autocommiting transactions');

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?