+ [misc] Add unit tests for metricModel::rebuildPrimaryKey() method
🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -381,4 +381,144 @@ class metricTest
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test rebuildPrimaryKey method.
|
||||
*
|
||||
* @param string $testType
|
||||
* @access public
|
||||
* @return mixed
|
||||
*/
|
||||
public function rebuildPrimaryKeyTest($testType = '')
|
||||
{
|
||||
global $tester;
|
||||
|
||||
if(empty($testType))
|
||||
{
|
||||
// 测试空表情况
|
||||
$tester->dao->delete()->from(TABLE_METRICLIB)->exec();
|
||||
$this->objectModel->rebuildPrimaryKey();
|
||||
if(dao::isError()) return dao::getError();
|
||||
return null;
|
||||
}
|
||||
|
||||
if($testType == 'normal')
|
||||
{
|
||||
// 清空表并插入正常数据
|
||||
$tester->dao->delete()->from(TABLE_METRICLIB)->exec();
|
||||
for($i = 1; $i <= 5; $i++)
|
||||
{
|
||||
$record = new stdClass();
|
||||
$record->metricCode = 'test_metric_' . $i;
|
||||
$record->value = $i * 10;
|
||||
$record->year = '2024';
|
||||
$record->month = '01';
|
||||
$record->day = sprintf('%02d', $i);
|
||||
$record->date = '2024-01-' . sprintf('%02d', $i);
|
||||
$tester->dao->insert(TABLE_METRICLIB)->data($record)->exec();
|
||||
}
|
||||
|
||||
$this->objectModel->rebuildPrimaryKey();
|
||||
if(dao::isError()) return dao::getError();
|
||||
|
||||
// 验证ID是否从1开始连续
|
||||
$records = $tester->dao->select('id')->from(TABLE_METRICLIB)->orderBy('id')->fetchAll();
|
||||
for($i = 0; $i < count($records); $i++)
|
||||
{
|
||||
if($records[$i]->id != ($i + 1)) return array('result' => 'failed');
|
||||
}
|
||||
return array('result' => 'success');
|
||||
}
|
||||
|
||||
if($testType == 'discontinuous')
|
||||
{
|
||||
// 清空表并插入不连续ID数据
|
||||
$tester->dao->delete()->from(TABLE_METRICLIB)->exec();
|
||||
$ids = array(2, 5, 8, 12, 15);
|
||||
foreach($ids as $index => $id)
|
||||
{
|
||||
$record = new stdClass();
|
||||
$record->metricCode = 'test_metric_' . $id;
|
||||
$record->value = $id * 10;
|
||||
$record->year = '2024';
|
||||
$record->month = '01';
|
||||
$record->day = sprintf('%02d', $index + 1);
|
||||
$record->date = '2024-01-' . sprintf('%02d', $index + 1);
|
||||
$tester->dao->insert(TABLE_METRICLIB)->data($record)->exec();
|
||||
$tester->dao->update(TABLE_METRICLIB)->set('id')->eq($id)->where('metricCode')->eq('test_metric_' . $id)->exec();
|
||||
}
|
||||
|
||||
$this->objectModel->rebuildPrimaryKey();
|
||||
if(dao::isError()) return dao::getError();
|
||||
|
||||
// 验证ID是否变为连续
|
||||
$records = $tester->dao->select('id')->from(TABLE_METRICLIB)->orderBy('id')->fetchAll();
|
||||
for($i = 0; $i < count($records); $i++)
|
||||
{
|
||||
if($records[$i]->id != ($i + 1)) return array('result' => 'failed');
|
||||
}
|
||||
return array('result' => 'success');
|
||||
}
|
||||
|
||||
if($testType == 'large')
|
||||
{
|
||||
// 清空表并插入大量数据测试
|
||||
$tester->dao->delete()->from(TABLE_METRICLIB)->exec();
|
||||
for($i = 1; $i <= 50; $i++)
|
||||
{
|
||||
$record = new stdClass();
|
||||
$record->metricCode = 'large_test_' . $i;
|
||||
$record->value = $i * 100;
|
||||
$record->year = '2024';
|
||||
$record->month = '01';
|
||||
$record->day = sprintf('%02d', $i % 28 + 1);
|
||||
$record->date = '2024-01-' . sprintf('%02d', $i % 28 + 1);
|
||||
$tester->dao->insert(TABLE_METRICLIB)->data($record)->exec();
|
||||
}
|
||||
|
||||
$this->objectModel->rebuildPrimaryKey();
|
||||
if(dao::isError()) return dao::getError();
|
||||
|
||||
// 验证大量数据的ID连续性
|
||||
$count = $tester->dao->select('COUNT(*) as count')->from(TABLE_METRICLIB)->fetch('count');
|
||||
$maxId = $tester->dao->select('MAX(id) as maxId')->from(TABLE_METRICLIB)->fetch('maxId');
|
||||
if($count == $maxId && $count == 50) return array('result' => 'success');
|
||||
return array('result' => 'failed');
|
||||
}
|
||||
|
||||
if($testType == 'verify')
|
||||
{
|
||||
// 验证AUTO_INCREMENT值设置
|
||||
$tester->dao->delete()->from(TABLE_METRICLIB)->exec();
|
||||
for($i = 1; $i <= 3; $i++)
|
||||
{
|
||||
$record = new stdClass();
|
||||
$record->metricCode = 'verify_test_' . $i;
|
||||
$record->value = $i * 10;
|
||||
$record->year = '2024';
|
||||
$record->month = '01';
|
||||
$record->day = sprintf('%02d', $i);
|
||||
$record->date = '2024-01-' . sprintf('%02d', $i);
|
||||
$tester->dao->insert(TABLE_METRICLIB)->data($record)->exec();
|
||||
}
|
||||
|
||||
$this->objectModel->rebuildPrimaryKey();
|
||||
if(dao::isError()) return dao::getError();
|
||||
|
||||
// 验证下次插入的ID是否正确
|
||||
$record = new stdClass();
|
||||
$record->metricCode = 'auto_increment_test';
|
||||
$record->value = 999;
|
||||
$record->year = '2024';
|
||||
$record->month = '01';
|
||||
$record->day = '04';
|
||||
$record->date = '2024-01-04';
|
||||
$tester->dao->insert(TABLE_METRICLIB)->data($record)->exec();
|
||||
|
||||
$newId = $tester->dao->select('id')->from(TABLE_METRICLIB)->where('metricCode')->eq('auto_increment_test')->fetch('id');
|
||||
return array('autoIncrement' => ($newId == 4));
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
#!/usr/bin/env php
|
||||
<?php
|
||||
|
||||
/**
|
||||
|
||||
title=测试 metricModel::rebuildPrimaryKey();
|
||||
timeout=0
|
||||
cid=0
|
||||
|
||||
- 步骤1:空表重建主键 @0
|
||||
- 步骤2:有数据的表重建主键属性result @success
|
||||
- 步骤3:不连续ID的表重建主键属性result @success
|
||||
- 步骤4:大量数据重建主键属性result @success
|
||||
- 步骤5:验证自增值设置属性autoIncrement @1
|
||||
|
||||
*/
|
||||
|
||||
// 1. 导入依赖(路径固定,不可修改)
|
||||
include dirname(__FILE__, 5) . '/test/lib/init.php';
|
||||
include dirname(__FILE__, 2) . '/lib/metric.unittest.class.php';
|
||||
|
||||
// 2. 用户登录
|
||||
su('admin');
|
||||
|
||||
// 3. 创建测试实例
|
||||
$metricTest = new metricTest();
|
||||
|
||||
// 4. 测试步骤(必须包含至少5个测试步骤)
|
||||
r($metricTest->rebuildPrimaryKeyTest()) && p() && e('0'); // 步骤1:空表重建主键
|
||||
r($metricTest->rebuildPrimaryKeyTest('normal')) && p('result') && e('success'); // 步骤2:有数据的表重建主键
|
||||
r($metricTest->rebuildPrimaryKeyTest('discontinuous')) && p('result') && e('success'); // 步骤3:不连续ID的表重建主键
|
||||
r($metricTest->rebuildPrimaryKeyTest('large')) && p('result') && e('success'); // 步骤4:大量数据重建主键
|
||||
r($metricTest->rebuildPrimaryKeyTest('verify')) && p('autoIncrement') && e('1'); // 步骤5:验证自增值设置
|
||||
Reference in New Issue
Block a user