From e604bd668ea30ec4e8fb52bbdee0fc041d332450 Mon Sep 17 00:00:00 2001 From: liumengyi Date: Thu, 28 Sep 2023 13:20:11 +0800 Subject: [PATCH] * Refactor testsuiteModel::getCanImportCases, and add its unit test. --- module/testsuite/model.php | 11 ++-- .../test/model/getcanimportcases.php | 56 +++++++++++++++++++ module/testsuite/test/testsuite.class.php | 38 +++++++++++-- module/testsuite/test/yaml/case.yaml | 15 +++++ module/testsuite/test/yaml/module.yaml | 11 ++++ module/testsuite/test/yaml/userquery.yaml | 7 +++ 6 files changed, 125 insertions(+), 13 deletions(-) create mode 100644 module/testsuite/test/model/getcanimportcases.php create mode 100644 module/testsuite/test/yaml/case.yaml create mode 100644 module/testsuite/test/yaml/module.yaml create mode 100644 module/testsuite/test/yaml/userquery.yaml diff --git a/module/testsuite/model.php b/module/testsuite/model.php index a3fe3db5d7..d0ea1c7d91 100644 --- a/module/testsuite/model.php +++ b/module/testsuite/model.php @@ -308,20 +308,17 @@ class testsuiteModel extends model } else { - if($this->session->testsuiteQuery == false) $this->session->set('testsuiteQuery', ' 1 = 1'); + if($this->session->testsuiteQuery === false) $this->session->set('testsuiteQuery', ' 1 = 1'); } $query = $this->session->testsuiteQuery; $allLib = "`lib` = 'all'"; - $withAllLib = strpos($query, $allLib) !== false; - if($withAllLib) $query = str_replace($allLib, 1, $query); - if(!$withAllLib) $query .= " AND `lib` = '$libID'"; + $query = strpos($query, $allLib) !== false ? str_replace($allLib, '1 = 1', $query) : "{$query} AND `lib` = '{$libID}'"; } - $this->loadModel('branch'); - $product = $this->loadModel('product')->getById($productID); - $branches = $product->type != 'normal' ? array(BRANCH_MAIN => $this->lang->branch->main) + $this->branch->getPairs($productID, 'active') : array(0); + $product = $this->loadModel('product')->getById($productID); + $branches = $branch === 'all' && $product->type != 'normal' ? array(BRANCH_MAIN => $this->lang->branch->main) + $this->branch->getPairs($productID, 'active') : array($product->type == 'normal' ? 0 : $branch => ''); $canImport = array(); foreach($branches as $branchID => $branchName) $canImport += $this->getCanImportModules($productID, $libID, $branchID); diff --git a/module/testsuite/test/model/getcanimportcases.php b/module/testsuite/test/model/getcanimportcases.php new file mode 100644 index 0000000000..3ef9fa1b88 --- /dev/null +++ b/module/testsuite/test/model/getcanimportcases.php @@ -0,0 +1,56 @@ +#!/usr/bin/env php +gen(45); +zdTable('branch')->gen(20); +zdTable('case')->config('case')->gen(50); +zdTable('module')->config('module')->gen(50); +zdTable('userquery')->config('userquery')->gen(1); +zdTable('user')->gen(1); + +su('admin'); + +/** + +title=测试 testsuiteModel->getCanImportCases(); +cid=1 +pid=1 + + +*/ + +$productIdList = array(1, 41); +$libIdList = array(0, 1); +$branchList = array('all', 1); +$orderByList = array('id_desc', 'id_asc'); +$browseTypeList = array('', 'bysearch'); +$queryIdList = array(0, 1); +$testsuiteQuery = array(false, "`lib` = 'all'"); + +$testsuite = new testsuiteTest(); + +r($testsuite->getCanImportCasesTest($productIdList[0], $libIdList[0], $branchList[0], $orderByList[0])) && p() && e('29,27,25,23,21'); // 测试获取产品 1 用例库 0 分支 all 排序 id_desc 可以导入的用例 +r($testsuite->getCanImportCasesTest($productIdList[0], $libIdList[1], $branchList[0], $orderByList[0])) && p() && e('30,28,26,24,22'); // 测试获取产品 1 用例库 1 分支 all 排序 id_desc 可以导入的用例 +r($testsuite->getCanImportCasesTest($productIdList[0], $libIdList[0], $branchList[1], $orderByList[0])) && p() && e('29,27,25,23,21'); // 测试获取产品 1 用例库 0 分支 1 排序 id_desc 可以导入的用例 +r($testsuite->getCanImportCasesTest($productIdList[0], $libIdList[1], $branchList[1], $orderByList[0])) && p() && e('30,28,26,24,22'); // 测试获取产品 1 用例库 1 分支 1 排序 id_desc 可以导入的用例 +r($testsuite->getCanImportCasesTest($productIdList[0], $libIdList[0], $branchList[0], $orderByList[1])) && p() && e('21,23,25,27,29'); // 测试获取产品 1 用例库 0 分支 all 排序 id_asc 可以导入的用例 +r($testsuite->getCanImportCasesTest($productIdList[0], $libIdList[1], $branchList[1], $orderByList[1])) && p() && e('22,24,26,28,30'); // 测试获取产品 1 用例库 1 分支 1 排序 id_asc 可以导入的用例 + +r($testsuite->getCanImportCasesTest($productIdList[1], $libIdList[0], $branchList[0], $orderByList[0])) && p() && e('29,27,25,23,21'); // 测试获取产品 41 用例库 0 分支 all 排序 id_desc 可以导入的用例 +r($testsuite->getCanImportCasesTest($productIdList[1], $libIdList[1], $branchList[0], $orderByList[0])) && p() && e('30,28,26,24,22'); // 测试获取产品 41 用例库 1 分支 all 排序 id_desc 可以导入的用例 +r($testsuite->getCanImportCasesTest($productIdList[1], $libIdList[0], $branchList[1], $orderByList[0])) && p() && e('29,27,25,23,21'); // 测试获取产品 41 用例库 0 分支 1 排序 id_desc 可以导入的用例 +r($testsuite->getCanImportCasesTest($productIdList[1], $libIdList[1], $branchList[1], $orderByList[0])) && p() && e('30,28,26,24,22'); // 测试获取产品 41 用例库 1 分支 1 排序 id_desc 可以导入的用例 +r($testsuite->getCanImportCasesTest($productIdList[1], $libIdList[0], $branchList[0], $orderByList[1])) && p() && e('21,23,25,27,29'); // 测试获取产品 41 用例库 0 分支 all 排序 id_asc 可以导入的用例 +r($testsuite->getCanImportCasesTest($productIdList[1], $libIdList[1], $branchList[1], $orderByList[1])) && p() && e('22,24,26,28,30'); // 测试获取产品 41 用例库 1 分支 1 排序 id_asc 可以导入的用例 + +r($testsuite->getCanImportCasesTest($productIdList[0], $libIdList[0], $branchList[0], $orderByList[0], $browseTypeList[1], $queryIdList[0], $testsuiteQuery[0])) && p() && e('29,27,25,23,21'); // 测试获取产品 1 用例库 0 分支 all 排序 id_desc browseType bysearch queryID 0 testsuiteQuery false 可以导入的用例 +r($testsuite->getCanImportCasesTest($productIdList[0], $libIdList[0], $branchList[0], $orderByList[0], $browseTypeList[1], $queryIdList[1], $testsuiteQuery[0])) && p() && e('0'); // 测试获取产品 1 用例库 0 分支 all 排序 id_desc browseType bysearch queryID 1 testsuiteQuery false 可以导入的用例 +r($testsuite->getCanImportCasesTest($productIdList[0], $libIdList[0], $branchList[0], $orderByList[0], $browseTypeList[1], $queryIdList[0], $testsuiteQuery[1])) && p() && e('29,27,25,23,21'); // 测试获取产品 1 用例库 0 分支 all 排序 id_desc browseType bysearch queryID 0 testsuiteQuery "`lib` = 'all'" 可以导入的用例 +r($testsuite->getCanImportCasesTest($productIdList[0], $libIdList[0], $branchList[0], $orderByList[0], $browseTypeList[1], $queryIdList[1], $testsuiteQuery[1])) && p() && e('0'); // 测试获取产品 1 用例库 0 分支 all 排序 id_desc browseType bysearch queryID 1 testsuiteQuery "`lib` = 'all'" 可以导入的用例 + +r($testsuite->getCanImportCasesTest($productIdList[0], $libIdList[0], $branchList[1], $orderByList[0], $browseTypeList[1], $queryIdList[0], $testsuiteQuery[0])) && p() && e('29,27,25,23,21'); // 测试获取产品 1 用例库 0 分支 all 排序 id_desc browseType bysearch queryID 0 testsuiteQuery false 可以导入的用例 +r($testsuite->getCanImportCasesTest($productIdList[0], $libIdList[0], $branchList[1], $orderByList[0], $browseTypeList[1], $queryIdList[1], $testsuiteQuery[0])) && p() && e('0'); // 测试获取产品 1 用例库 0 分支 all 排序 id_desc browseType bysearch queryID 1 testsuiteQuery false 可以导入的用例 +r($testsuite->getCanImportCasesTest($productIdList[0], $libIdList[0], $branchList[1], $orderByList[0], $browseTypeList[1], $queryIdList[0], $testsuiteQuery[1])) && p() && e('29,27,25,23,21'); // 测试获取产品 1 用例库 0 分支 all 排序 id_desc browseType bysearch queryID 0 testsuiteQuery "`lib` = 'all'" 可以导入的用例 +r($testsuite->getCanImportCasesTest($productIdList[0], $libIdList[0], $branchList[1], $orderByList[0], $browseTypeList[1], $queryIdList[1], $testsuiteQuery[1])) && p() && e('0'); // 测试获取产品 1 用例库 0 分支 all 排序 id_desc browseType bysearch queryID 1 testsuiteQuery "`lib` = 'all'" 可以导入的用例 diff --git a/module/testsuite/test/testsuite.class.php b/module/testsuite/test/testsuite.class.php index 0e0d776623..a14630da75 100644 --- a/module/testsuite/test/testsuite.class.php +++ b/module/testsuite/test/testsuite.class.php @@ -130,7 +130,7 @@ class testsuiteTest $suite->name = $name; $suite->desc = $desc; $suite->type = $type; - + if(!empty($uid)) { global $tester; @@ -140,13 +140,13 @@ class testsuiteTest $objects = $this->objectModel->update($suite, $uid); if(dao::isError()) return dao::getError(); - + foreach($objects as &$object) { if($object['field'] == 'desc') $object['result'] = $object['new'] == $desc; } - - return $objects; + + return $objects; } /** @@ -185,12 +185,12 @@ class testsuiteTest $objects = $this->objectModel->getLinkedCases($suiteID, $orderBy, $pager, $append); if(dao::isError()) return dao::getError(); - + if(!$append) { foreach($objects as $object) $object->results = 'a'; } - + return $objects; } @@ -280,4 +280,30 @@ class testsuiteTest { return $this->objectModel->deleteCaseBySuiteID($cases, $suiteID); } + + /** + * 测试获取可以导入的用例。 + * Test get can import cases. + * + * @param int $productID + * @param int $libID + * @param int|string $branch + * @param string $orderBy + * @param string $browseType + * @param int $queryID + * @param bool|string $testsuiteQuery + * @access public + * @return string|array + */ + public function getCanImportCasesTest(int $productID, int $libID, int|string $branch, string $orderBy = 'id_desc', string $browseType = '', int $queryID = 0, bool|string $testsuiteQuery = false): string|array + { + global $tester; + $tester->session->set('testsuiteQuery', $testsuiteQuery); + + $objects = $this->objectModel->getCanImportCases($productID, $libID, $branch, $orderBy, null, $browseType, $queryID); + + if(dao::isError()) return dao::getError(); + + return implode(',', array_keys($objects)); + } } diff --git a/module/testsuite/test/yaml/case.yaml b/module/testsuite/test/yaml/case.yaml new file mode 100644 index 0000000000..ea893d80f0 --- /dev/null +++ b/module/testsuite/test/yaml/case.yaml @@ -0,0 +1,15 @@ +title: table zt_testcase +desc: "测试用例" +author: Mengyi Liu +version: "1.0" +fields: + - field: product + range: 1{10},41{10},0{10} + - field: branch + range: 0{10},1{5},0{15} + - field: module + range: 0{5},1{5},0{5},12{5},0{5},23{5} + - field: lib + range: 0,1 + - field: fromCaseID + range: 21-25,0{2 5} diff --git a/module/testsuite/test/yaml/module.yaml b/module/testsuite/test/yaml/module.yaml new file mode 100644 index 0000000000..9e4e2094e9 --- /dev/null +++ b/module/testsuite/test/yaml/module.yaml @@ -0,0 +1,11 @@ +title: table zt_module +desc: "模块" +author: Mengyi Liu +version: "1.0" +fields: + - field: root + range: 1{10},41{10},0{10} + - field: branch + range: 0{10},1{5},0{15} + - field: type + range: case diff --git a/module/testsuite/test/yaml/userquery.yaml b/module/testsuite/test/yaml/userquery.yaml new file mode 100644 index 0000000000..7f979b9cd4 --- /dev/null +++ b/module/testsuite/test/yaml/userquery.yaml @@ -0,0 +1,7 @@ +title: table zt_userquery +desc: "用户查询" +author: Mengyi Liu +version: "1.0" +fields: + - field: sql + range: "(1=1 AND `product` = '1')"