+ [misc] Add unit tests for metricModel::updateMetricDate() method
🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1083,4 +1083,26 @@ class metricTest
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updateMetricDate method.
|
||||
*
|
||||
* @access public
|
||||
* @return mixed
|
||||
*/
|
||||
public function updateMetricDateTest()
|
||||
{
|
||||
global $tester;
|
||||
|
||||
// 记录更新前有多少条 createdDate 为 null 的记录
|
||||
$beforeCount = $tester->dao->select('count(*)')->from(TABLE_METRIC)->where('createdDate is null')->fetch('count(*)');
|
||||
|
||||
$this->objectModel->updateMetricDate();
|
||||
if(dao::isError()) return dao::getError();
|
||||
|
||||
// 记录更新后有多少条 createdDate 为 null 的记录
|
||||
$afterCount = $tester->dao->select('count(*)')->from(TABLE_METRIC)->where('createdDate is null')->fetch('count(*)');
|
||||
|
||||
return array('before' => $beforeCount, 'after' => $afterCount, 'updated' => $beforeCount - $afterCount);
|
||||
}
|
||||
}
|
||||
+99
@@ -0,0 +1,99 @@
|
||||
#!/usr/bin/env php
|
||||
<?php
|
||||
|
||||
/**
|
||||
|
||||
title=测试 metricModel::updateMetricDate();
|
||||
timeout=0
|
||||
cid=0
|
||||
|
||||
- 步骤1:正常情况 - 更新 createdDate 为 null 的记录属性updated @5
|
||||
- 步骤2:验证更新后无 null 记录属性after @0
|
||||
- 步骤3:再次执行应该无更新记录属性updated @0
|
||||
- 步骤4:空表测试
|
||||
- 属性before @0
|
||||
- 属性after @0
|
||||
- 属性updated @0
|
||||
- 步骤5:混合数据测试属性updated @3
|
||||
|
||||
*/
|
||||
|
||||
// 1. 导入依赖(路径固定,不可修改)
|
||||
include dirname(__FILE__, 5) . '/test/lib/init.php';
|
||||
include dirname(__FILE__, 2) . '/lib/metric.unittest.class.php';
|
||||
|
||||
// 2. zendata数据准备(根据需要配置)
|
||||
// 使用手工创建测试数据方式,因为 zendata 对 NULL 日期字段的处理有问题
|
||||
global $tester;
|
||||
|
||||
// 清理已有数据
|
||||
$tester->dao->delete()->from(TABLE_METRIC)->exec();
|
||||
|
||||
// 手工插入测试数据
|
||||
for($i = 1; $i <= 10; $i++)
|
||||
{
|
||||
$metric = new stdClass();
|
||||
$metric->id = $i;
|
||||
$metric->name = "test_metric_$i";
|
||||
$metric->code = "test_code_$i";
|
||||
$metric->purpose = 'quality';
|
||||
$metric->scope = 'system';
|
||||
$metric->object = 'bug';
|
||||
$metric->createdBy = 'admin';
|
||||
$metric->stage = 'wait';
|
||||
$metric->type = 'php';
|
||||
// 前5条记录的 createdDate 设置为 null,后5条设置为具体时间
|
||||
if($i <= 5)
|
||||
{
|
||||
$metric->createdDate = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
$metric->createdDate = '2023-01-01 10:00:00';
|
||||
}
|
||||
|
||||
$tester->dao->insert(TABLE_METRIC)->data($metric)->exec();
|
||||
}
|
||||
|
||||
// 3. 用户登录(选择合适角色)
|
||||
su('admin');
|
||||
|
||||
// 4. 创建测试实例(变量名与模块名一致)
|
||||
$metricTest = new metricTest();
|
||||
|
||||
// 5. 强制要求:必须包含至少5个测试步骤
|
||||
r($metricTest->updateMetricDateTest()) && p('updated') && e('5'); // 步骤1:正常情况 - 更新 createdDate 为 null 的记录
|
||||
r($metricTest->updateMetricDateTest()) && p('after') && e('0'); // 步骤2:验证更新后无 null 记录
|
||||
r($metricTest->updateMetricDateTest()) && p('updated') && e('0'); // 步骤3:再次执行应该无更新记录
|
||||
|
||||
// 清理数据后测试空表
|
||||
$tester->dao->delete()->from(TABLE_METRIC)->exec();
|
||||
r($metricTest->updateMetricDateTest()) && p('before,after,updated') && e('0,0,0'); // 步骤4:空表测试
|
||||
|
||||
// 重新生成测试数据,测试混合情况
|
||||
$tester->dao->delete()->from(TABLE_METRIC)->exec();
|
||||
for($i = 1; $i <= 5; $i++)
|
||||
{
|
||||
$metric = new stdClass();
|
||||
$metric->id = $i;
|
||||
$metric->name = "metric_test_$i";
|
||||
$metric->code = "test_code_$i";
|
||||
$metric->purpose = 'quality';
|
||||
$metric->scope = 'system';
|
||||
$metric->object = 'bug';
|
||||
$metric->createdBy = 'admin';
|
||||
$metric->stage = 'wait';
|
||||
$metric->type = 'php';
|
||||
// 前3条记录的 createdDate 设置为 null,后2条设置为具体时间
|
||||
if($i <= 3)
|
||||
{
|
||||
$metric->createdDate = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
$metric->createdDate = '2023-01-01 10:00:00';
|
||||
}
|
||||
|
||||
$tester->dao->insert(TABLE_METRIC)->data($metric)->exec();
|
||||
}
|
||||
r($metricTest->updateMetricDateTest()) && p('updated') && e('3'); // 步骤5:混合数据测试
|
||||
@@ -0,0 +1,94 @@
|
||||
---
|
||||
title: 度量项更新创建日期测试数据
|
||||
desc: 为 updateMetricDate 方法提供测试数据,包含不同状态的 metric 记录
|
||||
author: Claude
|
||||
version: 1.0
|
||||
|
||||
fields:
|
||||
- field: id
|
||||
note: 度量项ID
|
||||
range: 1-10
|
||||
|
||||
- field: purpose
|
||||
note: 度量目的
|
||||
range: quality,efficiency,cost{2}
|
||||
|
||||
- field: scope
|
||||
note: 度量范围
|
||||
range: system{5},product{3},project{2}
|
||||
|
||||
- field: object
|
||||
note: 度量对象
|
||||
range: bug{3},story{3},task{2},user{2}
|
||||
|
||||
- field: stage
|
||||
note: 发布阶段
|
||||
range: wait{7},released{3}
|
||||
|
||||
- field: type
|
||||
note: 度量类型
|
||||
range: php{6},sql{4}
|
||||
|
||||
- field: name
|
||||
note: 度量项名称
|
||||
range: '[测试度量项_,度量_]{5}'
|
||||
|
||||
- field: alias
|
||||
note: 度量项别名
|
||||
range: '[test_metric_,metric_alias_]{5}'
|
||||
|
||||
- field: code
|
||||
note: 度量代码
|
||||
range: '[test_code_,metric_code_]{5}'
|
||||
|
||||
- field: unit
|
||||
note: 度量单位
|
||||
range: '[个,次,小时,天,百分比]{2}'
|
||||
|
||||
- field: dateType
|
||||
note: 日期类型
|
||||
range: day{4},week{3},month{2},quarter{1}
|
||||
|
||||
- field: collector
|
||||
note: 采集器配置
|
||||
range: '[{"type":"sql","sql":"SELECT COUNT(*) FROM table"},{"type":"php","class":"testClass"}]{5}'
|
||||
|
||||
- field: desc
|
||||
note: 度量项描述
|
||||
range: '[这是一个测试度量项,用于测试的度量项描述]{5}'
|
||||
|
||||
- field: definition
|
||||
note: 度量定义
|
||||
range: '[该度量项用于计算,测试度量项的定义说明]{5}'
|
||||
|
||||
- field: when
|
||||
note: 计算时机
|
||||
range: '[daily,weekly,monthly]{3.3}'
|
||||
|
||||
- field: event
|
||||
note: 触发事件
|
||||
range: '[create,update,delete]{3.3}'
|
||||
|
||||
- field: cronCFG
|
||||
note: 定时任务配置
|
||||
range: '[0 0 * * *,0 9 * * 1,0 0 1 * *]{3.3}'
|
||||
|
||||
- field: time
|
||||
note: 执行时间
|
||||
range: '[morning,afternoon,evening]{3.3}'
|
||||
|
||||
- field: createdBy
|
||||
note: 创建人
|
||||
range: admin{3},user1{2},user2{2},user3{1},testuser{2}
|
||||
|
||||
- field: createdDate
|
||||
note: 创建日期
|
||||
range: '[]{5},2023-01-01 10:00:00{2},2023-02-15 14:30:00{2},2023-03-20 09:15:00{1}'
|
||||
|
||||
- field: editedBy
|
||||
note: 最后编辑人
|
||||
range: admin{4},user1{3},user2{2},testuser{1}
|
||||
|
||||
- field: editedDate
|
||||
note: 最后编辑时间
|
||||
range: '2023-01-15 10:00:00{3},2023-02-20 14:30:00{3},2023-03-25 09:15:00{4}'
|
||||
Reference in New Issue
Block a user