Skip to content

test: add recent search engine unit tests#328

Merged
Johnson-zs merged 1 commit into
linuxdeepin:semantic-searchfrom
Johnson-zs:semantic-search
Jun 26, 2026
Merged

test: add recent search engine unit tests#328
Johnson-zs merged 1 commit into
linuxdeepin:semantic-searchfrom
Johnson-zs:semantic-search

Conversation

@Johnson-zs

Copy link
Copy Markdown
Contributor

Added comprehensive unit tests for the RecentSearchEngine functionality
to ensure proper operation of recent file search features. The tests
cover various scenarios including:

  1. Empty fetch handling
  2. No filter conditions
  3. Keyword filtering
  4. File extension filtering
  5. Time range filtering
  6. Combined filters
  7. DBus failure handling
  8. Detailed result attributes
  9. Result count limitation
  10. Result found signals

The test file tst_recent_search_engine.cpp was created with extensive
test cases using the stubext library for mocking DBus calls. The
RecentSearchStrategy was made testable by changing fetchRecentItems()
access to protected to allow test subclasses to mock this function.

test: 添加最近文件搜索引擎单元测试

为最近文件搜索功能添加了全面的单元测试,确保其各项功能正常工作。测试覆盖
了以下多种场景:

  1. 空获取处理
  2. 无过滤条件
  3. 关键词过滤
  4. 文件扩展名过滤
  5. 时间范围过滤
  6. 组合过滤条件
  7. DBus调用失败处理
  8. 详细结果属性
  9. 结果数量限制
  10. 结果找到信号

测试文件tst_recent_search_engine.cpp创建了详细的测试用例,使用stubext库
模拟DBus调用。通过将RecentSearchStrategy中的fetchRecentItems()方法访问权
限改为protected,使得测试子类可以模拟该方法以实现测试目的。

Added comprehensive unit tests for the RecentSearchEngine functionality
to ensure proper operation of recent file search features. The tests
cover various scenarios including:
1. Empty fetch handling
2. No filter conditions
3. Keyword filtering
4. File extension filtering
5. Time range filtering
6. Combined filters
7. DBus failure handling
8. Detailed result attributes
9. Result count limitation
10. Result found signals

The test file tst_recent_search_engine.cpp was created with extensive
test cases using the stubext library for mocking DBus calls. The
RecentSearchStrategy was made testable by changing fetchRecentItems()
access to protected to allow test subclasses to mock this function.

test: 添加最近文件搜索引擎单元测试

为最近文件搜索功能添加了全面的单元测试,确保其各项功能正常工作。测试覆盖
了以下多种场景:
1. 空获取处理
2. 无过滤条件
3. 关键词过滤
4. 文件扩展名过滤
5. 时间范围过滤
6. 组合过滤条件
7. DBus调用失败处理
8. 详细结果属性
9. 结果数量限制
10. 结果找到信号

测试文件tst_recent_search_engine.cpp创建了详细的测试用例,使用stubext库
模拟DBus调用。通过将RecentSearchStrategy中的fetchRecentItems()方法访问权
限改为protected,使得测试子类可以模拟该方法以实现测试目的。

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @Johnson-zs, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Johnson-zs

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

★ 总体评分:95分

■ 【总体评价】

代码实现了RecentSearchStrategy的全面单元测试覆盖,设计合理且无安全风险
逻辑严密、用例充分,因测试需要破坏封装性仅作轻微扣分

■ 【详细分析】

  • 1.语法逻辑(完全正确)✓

测试代码语法无误,断言使用准确。通过stubext框架对fetchRecentItems进行打桩,成功隔离了DBus依赖。QSignalSpy正确捕获了searchFinished和resultFound信号,验证了管道各阶段的输出结果,包括空结果集、交集过滤和截断逻辑。
建议:维持当前的测试逻辑结构,无需修改。

  • 2.代码质量(良好)✓

测试用例命名遵循了“场景_条件_预期结果”的规范,可读性极高。提取了createTestItems和resultPaths辅助函数,有效消除了测试数据构建和结果断言的重复代码。为了允许打桩,将recentsearchstrategy.h中的fetchRecentItems访问权限从private放宽至protected,这是一种常见的测试妥协手段,注释也清晰说明了原因。
潜在问题:修改生产代码的访问权限以适应测试框架,在一定程度上破坏了面向对象的封装性。
建议:考虑在测试专属宏控制下使用friend class注入测试友元,避免在正式发布头文件中暴露protected方法,从而保持接口的最小权限原则。

  • 3.代码性能(无性能问题)✓

单元测试代码仅在全量构建和测试阶段执行,不参与生产环境运行。测试内部通过值捕获传递模拟数据,无额外的系统调用或资源消耗瓶颈。
建议:保持现状即可。

  • 4.代码安全(存在0个安全漏洞)✓

漏洞对比统计:新增漏洞 0 个,减少漏洞 0 个,持平 0 个
本次变更为纯测试代码新增及为了测试暴露的生产代码接口调整,不涉及任何网络请求、文件读写、命令执行或用户输入处理,无攻击面,整体安全。

  • 建议:继续保持测试代码与生产安全边界的隔离,避免在测试用例中硬编码真实的敏感系统路径。

■ 【改进建议代码示例】

// recentsearchstrategy.h
// 使用友元类替代修改访问权限,保持封装性
class RecentSearchStrategy : public BaseSearchStrategy
{
    Q_OBJECT

#ifdef DFM_SEARCH_BUILD_TESTS
    friend class TestableRecentStrategy;
#endif

public:
    explicit RecentSearchStrategy(SearchOptions &options, QObject *parent = nullptr);
    ~RecentSearchStrategy() override;

    void search(const SearchQuery &query) override;
    void cancel() override;

private:
    // 调用 DBus 获取最近使用记录;失败时返回空列表并记录 warning。
    QList<RecentItem> fetchRecentItems();

    // ── 过滤管道(每个阶段纯函数,便于测试与组合) ──
    /** 
     * ...
     */
};

// tst_recent_search_engine.cpp
// 测试类无需使用 VADDR 宏,直接调用私有方法打桩(取决于stubext对友元和私有方法的支持情况)
class TestableRecentStrategy : public RecentSearchStrategy
{
public:
    using RecentSearchStrategy::RecentSearchStrategy;
    
    // 若 stubext 支持友元访问,可直接提供 this 指针与函数名字符串进行打桩
    // 而不需要依赖 protected 权限暴露成员函数指针地址
};

@Johnson-zs Johnson-zs merged commit d39b5bb into linuxdeepin:semantic-search Jun 26, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants