mdb_usage_testcase.php
来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· PHP 代码 · 共 1,121 行 · 第 1/4 页
PHP
1,121 行
} /** * Test retrieval of result metadata * * This tests the result metadata by executing a prepared_query and * select the data, and checking the result contains the correct * number of columns and that the column names are in the correct order */ function testMetadata() { $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(); $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->freePreparedQuery($prepared_query); if (MDB::isError($result)) { $this->assertTrue(false, 'Error executing prepared query'.$result->getMessage()); } $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()); } $numcols = $this->db->numCols($result); $this->assertEquals($numcols, count($this->fields), "The query result returned a number of $numcols columns unlike ".count($this->fields) .' as expected'); $column_names = $this->db->getColumnNames($result); for ($column = 0; $column < $numcols; $column++) { $this->assertEquals($column_names[$this->fields[$column]], $column, "The query result column \"".$this->fields[$column]."\" was returned in position ".$column_names[$this->fields[$column]]." unlike $column as expected"); } } /** * Test storage and retrieval of nulls * * This tests null storage and retrieval by successively inserting, * selecting, and testing a number of null / not null values */ function testNulls() { $test_values = array( array('test', false), array('NULL', false), array('null', false), array('', false), array(null, true) ); for ($test_value = 0; $test_value <= count($test_values); $test_value++) { if ($test_value == count($test_values)) { $value = 'NULL'; $is_null = true; } else { $value = $this->db->getTextValue($test_values[$test_value][0]); $is_null = $test_values[$test_value][1]; } $this->clearTables(); $result = $this->db->query("INSERT INTO users (user_name,user_password,user_id) VALUES ($value,$value,0)"); if (MDB::isError($result)) { $this->assertTrue(false, 'Error executing insert query'.$result->getMessage()); } $result = $this->db->query('SELECT user_name,user_password FROM users', array('text', 'text')); if (MDB::isError($result)) { $this->assertTrue(false, 'Error executing select query'.$result->getMessage()); } $this->assertTrue(!$this->db->endOfResult($result), 'The query result seems to have reached the end of result earlier than expected'); if ($is_null) { $error_message = 'A query result column is not NULL unlike what was expected'; } else { $error_message = 'A query result column is NULL even though it was expected to be "' . $test_values[$test_value][0] . '"'; } $value = $this->db->resultIsNull($result, 0, 0); $this->assertTrue(($value == $is_null), $error_message); $value = $this->db->resultIsNull($result, 0, 1); $this->assertTrue(($value == $is_null), $error_message); $this->assertTrue($this->db->endOfResult($result), 'the query result did not seem to have reached the end of result as expected after testing only if columns are NULLs'); $this->db->freeResult($result); } } /** * Tests escaping of text values with special characters * */ function testEscapeSequences() { $test_strings = array( "'", "\"", "\\", "%", "_", "''", "\"\"", "\\\\", "\\'\\'", "\\\"\\\"" ); for($string = 0; $string < count($test_strings); $string++) { $this->clearTables(); $value = $this->db->getTextValue($test_strings[$string]); $result = $this->db->query("INSERT INTO users (user_name,user_password,user_id) VALUES ($value,$value,0)"); if (MDB::isError($result)) { $this->assertTrue(false, 'Error executing insert query'.$result->getMessage()); } $result = $this->db->query('SELECT user_name,user_password FROM users', array('text', 'text')); if (MDB::isError($result)) { $this->assertTrue(false, 'Error executing select query'.$result->getMessage()); } $this->assertTrue(!$this->db->endOfResult($result), 'The query result seems to have reached the end of result earlier than expected'); $value = $this->db->fetch($result, 0, 'user_name'); $this->db->freeResult($result); $this->assertEquals($test_strings[$string], rtrim($value), "the value retrieved for field \"user_name\" (\"$value\") doesn't match what was stored (".$test_strings[$string].')'); } } /** * Test paged queries * * Test the use of setSelectedRowRange to return paged queries */ function testRanges() { if (!$this->supported('SelectRowRanges')) { return; } $data = array(); $total_rows = 5; $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); if (MDB::isError($result)) { $this->assertTrue(false, 'Error executing prepared query'.$result->getMessage()); } } $this->db->freePreparedQuery($prepared_query); for ($rows = 2, $start_row = 0; $start_row < $total_rows; $start_row += $rows) { $this->db->setSelectedRowRange($start_row, $rows); $result = $this->db->query('SELECT user_name,user_password,subscribed,user_id,quota,weight,access_date,access_time,approved FROM users ORDER BY user_id', $this->types); if (MDB::isError($result)) { $this->assertTrue(false, 'Error executing select query'.$result->getMessage()); } for ($row = 0; $row < $rows && ($row + $start_row < $total_rows); $row++) { $this->verifyFetchedValues($result, $row, $data[$row + $start_row]); } } $this->assertTrue($this->db->endOfResult($result), "The query result did not seem to have reached the end of result as expected starting row $start_row after fetching upto row $row"); $this->db->freeResult($result); for ($rows = 2, $start_row = 0; $start_row < $total_rows; $start_row += $rows) { $this->db->setSelectedRowRange($start_row, $rows); $result = $this->db->query('SELECT user_name, user_password, subscribed, user_id, quota, weight, access_date, access_time, approved FROM users ORDER BY user_id', $this->types); if (MDB::isError($result)) { $this->assertTrue(false, 'Error executing select query'.$result->getMessage()); } $result_rows = $this->db->numRows($result); $this->assertTrue(($result_rows <= $rows), "expected a result of no more than $rows but the returned number of rows is $result_rows"); for ($row = 0; $row < $result_rows; $row++) { $this->assertTrue(!$this->db->endOfResult($result), "The query result seem to have reached the end of result at row $row that is before $result_rows as expected"); $this->verifyFetchedValues($result, $row, $data[$row + $start_row]); } } $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 the handling of sequences */ function testSequences() { if (!$this->supported('Sequences')) { return; } for ($start_value = 1; $start_value < 4; $start_value++) { $sequence_name = "test_sequence_$start_value"; $result = $this->db->createSequence($sequence_name, $start_value); $this->assertTrue(!MDB::isError($result), "Error creating sequence $sequence_name with start value $start_value"); for ($sequence_value = $start_value; $sequence_value < ($start_value + 4); $sequence_value++) { $value = $this->db->nextId($sequence_name, false); $this->assertEquals($sequence_value, $value, "The returned sequence value is $value and not $sequence_value as expected with sequence start value with $start_value"); } $result = $this->db->dropSequence($sequence_name); if (MDB::isError($result)) { $this->assertTrue(false, "Error dropping sequence $sequence_name : ".$result->getMessage()); } } // Test ondemand creation of sequences $sequence_name = 'test_ondemand'; for ($sequence_value = 1; $sequence_value < 4; $sequence_value++) { $value = $this->db->nextId($sequence_name); $this->assertEquals($sequence_value, $value, "Error in ondemand sequences. The returned sequence value is $value and not $sequence_value as expected"); } $result = $this->db->dropSequence($sequence_name); if (MDB::isError($result)) { $this->assertTrue(false, "Error dropping sequence $sequence_name : ".$result->getMessage());
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?