如何向 Nebula Graph 增加一个测试用例

引子

由于 Nebula Graph 在持续研发过程中会频繁地更改返回数据的格式,这样就会造成了上层应用程序无法正确解析数据。

比如在 v2.0.0 发布的时候,改变了 SHOW JOBS 的格式,造成了 Studio 解析的时候页面崩溃。

为了避免再次发生这样的悲剧问题,可以使用 Nebula Graph 提供的测试框架,方便地增加测试用例。

环境准备

按照文档《源码编译安装》 中所示,我们可以参考以下步骤进行操作:

a. 准备一台开发机,这里给出了开发环境的配置要求。

  • Python3 要求的最低版本为 Python 3.7.
python3 --version

b. 按照文档中步骤编译源代码。选择 master 版本,并拉一个分支

git checkout -b  dev tck

具体步骤可以见 Nebula 文档站中如何贡献中的 GitHub 使用步骤部分

c. 安装 case 运行环境

$ cd nebula-graph/tests/
$ make init-all

如果遇到模块版本过低,可以根据提示升级版本,比如:命令 pip3 install --upgrade keyrings.alt

d. 运行已有case

make test

开发 case

研究一下已有 case

大多数已有 case 都在 https://github.com/vesoft-inc/nebula-graph/tree/master/tests/tck/features 目录下,根据主要功能放在不同目录下。

例如,以最常用的 MATCH 功能举例。打开 GitHub 目录下的 match/MatchById.feature 文件

Feature: Match By Id

  Background:
    Given a graph with space named "basketballplayer"     # 选择一个已有的单元测试数据集 basketballplayer

  Scenario: single node                                   # 给这个 case 取个名字
    When executing query:                              # 以下三引号之间的部分,为发送给 Nebula Graph 的文本。
      """                                                            # 这句话的目的,是获vid为James Harden的点
      MATCH (n) WHERE id(n) == 'James Harden' RETURN n
      """                                                            # Then 表示测试结果匹配                
    Then the result should be, in any order, with relax comparison:
      | n                |                                           # 期望获取的文本结果,应该是这样一个格式。列名为 n
      | ("James Harden") |                                # 内容为 "James Harden", 圆括号表示这是一个点
    When executing query:                             # 开始下一个命令。
      """
      MATCH (n) WHERE id(n) == 'not_exist_vertex' RETURN n
      """
    Then the result should be, in any order, with relax comparison:
      | n |

nebula-test 是基于 BDD 思想撰写的,这些 feature 文件基本是自然语言(gherkin文本),BDD 介绍文章可参考:《基于 BDD 理论的 Nebula 集成测试框架重构(上篇)》

这里简单地解释一下 case 用例模版主要的格式:

  • Feature:介绍整个文本是做什么的;
  • Background:设置一下公共的运行环境,例子中,选用了一个已经存在的数据集 basketballplayer(建立了schema,index,写入了数据);
  • Scenario:一组 When-Then 的组合,是一个最小的并发执行单位;
  • When executing query:向 Nebula Graph 发送一组命令;
  • Then the result should be:从 Nebula Graph 获取结果,和下面的期望结果对比;

这个对比方式可以有以下几种:

  • the result should be:需完全一样 (行、列、单元格内容都必须一模一样);
  • the result should contain:只包含以下内容即可;
  • the result should be, in any order:返回结果的行顺序无关(对于行 1 和行 2 的顺序没有要求);
  • the result should be, in any order, with relax comparison:结果集需要是以下形式,行顺序无关,并且至少包括以下内容(例如,每个单元格至少包括"Tiago Splitter",但可以比这个多);
  • the execution should be successful:返回码为成功;
  • an ExecutionError should be raised at runtime: not existed! 执行失败,抛出异常ExecutionError,且返回错误码为 not existed!;

尝试自己撰写一个 case

假设现在我们的目的是检查一下SHOW JOBS 的返回格式,可以这么操作:在 nebula-graph/tests/tck/features/job/(nebula-graph/tests/tck/features/job at master · vesoft-inc/nebula-graph · GitHub) 下增加一个 JobCommands.feature 的文件

@mintest                                                          #用于调试

Feature: Job compact, flush, rebuild_index

  Background:                                     # 选择数据集
    Given a graph with space named "basketballplayer"

  Scenario: submit job
    When executing query:
      """
      SUBMIT JOB COMPACT;
      """
    Then the result should contain:
      | New Job Id |
      | /\d+/          |              # 用正则表达式检查为正数
    When executing query:
      """
      SUBMIT JOB FLUSH;
      """
     When executing query:
      """
      SUBMIT JOB STATS;

      SHOW JOBS;
      """
    Then the result should contain:   # 检查返回形式
      | Job Id | Command | Status  | Start Time                               | Stop Time                 |
      | /\d+/  | "STATS"      | /\w+/  | /\d+-\d+-\d+T\d+:\d+:\d+/ | /\d+-\d+-\d+T\d+:\d+:\d+/ |

调试 case

nebula-graph/tests> make fmt

检查格式错误,并格式化文件。

nebula-graph/tests>  pytest -m "mintest"

运行刚撰写的 case,通常要来回更改多次。用#号注释。也可以用 @skip 标注跳过整个 Scenario。

nebula-graph/tests>  pytest -n4

如果内存有限,-n4 控制并发度。

更多说明可以参见这里:nebula-graph/tests at master · vesoft-inc/nebula-graph · GitHub

通过 git 向 Nebula Graph repo 增加该 case

如果所有的测试都已经通过,将调试标记 @mintest 去掉,通过 git 指令向 Nebula Graph 仓库提交一个 pull request,参考 如何贡献

交流图数据库技术?加入 Nebula 交流群请先填写下你的 Nebula 名片,Nebula 小助手会拉你进群~~

1 个赞