diff --git a/module/metric/test/lib/metric.unittest.class.php b/module/metric/test/lib/metric.unittest.class.php index eee78a3f1c..8d0c44f900 100644 --- a/module/metric/test/lib/metric.unittest.class.php +++ b/module/metric/test/lib/metric.unittest.class.php @@ -1445,4 +1445,43 @@ class metricTest return $afterCount - $beforeCount; } + + /** + * Test keepLatestRecords method. + * + * @param string $code + * @param array $fields + * @access public + * @return mixed + */ + public function keepLatestRecordsTest($code = '', $fields = array()) + { + global $tester; + + if(empty($code)) return 'invalid_code'; + + // 记录操作前未删除的记录数 + $beforeUndeleted = $tester->dao->select('COUNT(*) as count') + ->from(TABLE_METRICLIB) + ->where('metricCode')->eq($code) + ->andWhere('deleted')->eq('0') + ->fetch('count'); + + // 使用反射来调用protected方法 + $reflection = new ReflectionClass($this->objectTao); + $method = $reflection->getMethod('keepLatestRecords'); + $method->setAccessible(true); + + $method->invoke($this->objectTao, $code, $fields); + if(dao::isError()) return dao::getError(); + + // 记录操作后未删除的记录数 + $afterUndeleted = $tester->dao->select('COUNT(*) as count') + ->from(TABLE_METRICLIB) + ->where('metricCode')->eq($code) + ->andWhere('deleted')->eq('0') + ->fetch('count'); + + return $afterUndeleted - $beforeUndeleted; + } } \ No newline at end of file diff --git a/module/metric/test/tao/keeplatestrecords.php b/module/metric/test/tao/keeplatestrecords.php new file mode 100755 index 0000000000..e36c535b12 --- /dev/null +++ b/module/metric/test/tao/keeplatestrecords.php @@ -0,0 +1,47 @@ +#!/usr/bin/env php +id->range('1-10'); +$table->metricCode->range('test_metric_001{5},test_metric_002{5}'); +$table->system->range('0{10}'); +$table->project->range('1{5},2{5}'); +$table->year->range('2024{10}'); +$table->month->range('01{5},02{5}'); +$table->day->range('01,02,03,04,05'); +$table->value->range('100-110'); +$table->deleted->range('1{10}'); // 所有记录初始都标记为已删除 +$table->date->range('`2024-01-01 10:00:00`,`2024-01-01 11:00:00`,`2024-01-01 12:00:00`,`2024-01-02 10:00:00`,`2024-01-02 11:00:00`,`2024-02-01 10:00:00`,`2024-02-01 11:00:00`,`2024-02-01 12:00:00`,`2024-02-02 10:00:00`,`2024-02-02 11:00:00`'); +$table->gen(10); + +// 3. 用户登录(选择合适角色) +su('admin'); + +// 4. 创建测试实例(变量名与模块名一致) +$metricTest = new metricTest(); + +// 5. 强制要求:必须包含至少5个测试步骤 +r($metricTest->keepLatestRecordsTest('test_metric_001', array('project', 'year'))) && p() && e('1'); // 步骤1:使用正常字段列表保留最新记录 +r($metricTest->keepLatestRecordsTest('test_metric_001', array('year', 'month', 'day'))) && p() && e('4'); // 步骤2:使用包含时间字段的字段列表保留记录 +r($metricTest->keepLatestRecordsTest('test_metric_001', array('project'))) && p() && e('0'); // 步骤3:使用不包含时间字段的字段列表 +r($metricTest->keepLatestRecordsTest('test_metric_001', array())) && p() && e('0'); // 步骤4:传入空字段列表 +r($metricTest->keepLatestRecordsTest('test_metric_002', array('system'))) && p() && e('2'); // 步骤5:使用单个字段进行分组保留记录 \ No newline at end of file