From f8abb220af59dbbdca5fa8e2fc664d850b12961b Mon Sep 17 00:00:00 2001 From: gtema Date: Wed, 22 Mar 2023 11:42:43 +0000 Subject: [PATCH] Add unittests for conversion Reviewed-by: Hasko, Vladimir Co-authored-by: gtema Co-committed-by: gtema --- .gitignore | 2 + .stestr.conf | 3 + otc_doc_convertor/convertor.py | 6 +- otc_doc_convertor/tests/unit/__init__.py | 0 .../tests/unit/test_convertor.py | 165 ++++++++++++++++++ zuul.yaml | 1 + 6 files changed, 174 insertions(+), 3 deletions(-) create mode 100644 .stestr.conf create mode 100644 otc_doc_convertor/tests/unit/__init__.py create mode 100644 otc_doc_convertor/tests/unit/test_convertor.py diff --git a/.gitignore b/.gitignore index bd58829e..ce14d2cd 100644 --- a/.gitignore +++ b/.gitignore @@ -134,6 +134,8 @@ dmypy.json **/temp/ **/tmp_result/ +.stestr/ + # Some people build submodule because of not understanding git basics doc-exports diff --git a/.stestr.conf b/.stestr.conf new file mode 100644 index 00000000..7c6f6ed2 --- /dev/null +++ b/.stestr.conf @@ -0,0 +1,3 @@ +[DEFAULT] +test_path=./otc_doc_convertor/tests/unit +top_dir=./ diff --git a/otc_doc_convertor/convertor.py b/otc_doc_convertor/convertor.py index 3a94d7ec..11aa39d3 100644 --- a/otc_doc_convertor/convertor.py +++ b/otc_doc_convertor/convertor.py @@ -109,7 +109,7 @@ class OTCDocConvertor: "Cannot find string for rawization anymore" ) - def streamline_html(self, soup, file_name): + def streamline_html(self, soup, file_name, args=None): # Drop eventual header duplicated anchors fname = file_name.replace(".html", "").lower() page_anchors = set() @@ -227,7 +227,7 @@ class OTCDocConvertor: if th.p.strong: th.p.strong.unwrap() - if self.args.improve_table_headers: + if args and args.improve_table_headers: # Add spaces around "/" for th in soup.body.find_all("th"): if hasattr(th, "p") and th.p.string: @@ -562,7 +562,7 @@ class OTCDocConvertor: content = re.sub(r"≤", "<=", content) content = re.sub(r"≥", ">=", content) soup = bs4.BeautifulSoup(content, "lxml") - proc = self.streamline_html(soup, f.name) + proc = self.streamline_html(soup, f.name, self.args) for lnk in proc.find_all("a"): href = lnk.get("href") diff --git a/otc_doc_convertor/tests/unit/__init__.py b/otc_doc_convertor/tests/unit/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/otc_doc_convertor/tests/unit/test_convertor.py b/otc_doc_convertor/tests/unit/test_convertor.py new file mode 100644 index 00000000..9304d6e5 --- /dev/null +++ b/otc_doc_convertor/tests/unit/test_convertor.py @@ -0,0 +1,165 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +from unittest import TestCase +import bs4 + +from otc_doc_convertor import convertor + + +class TestConvertor(TestCase): + def setUp(self): + self.convertor = convertor.OTCDocConvertor() + + def test_streamline_html_escape_1(self): + test_data = """ +
  • ObjectCreated:*
+ """ + expected = """ +
  • ObjectCreated:*
""" + soup = bs4.BeautifulSoup(test_data, 'lxml') + res = self.convertor.streamline_html(soup, "dummy") + self.assertEqual(str(res.find('ul')), expected.strip()) + + def test_streamline_html_escape_2(self): + test_data = """

s3:*

""" + expected = """

s3:*

""" + soup = bs4.BeautifulSoup(test_data, 'lxml') + res = self.convertor.streamline_html(soup, "dummy") + self.assertEqual(str(res.find('tr')), expected.strip()) + + def test_streamline_html_escape_3(self): + test_data = """ +

If the VPC, subnet, and security group are displayed in the DB + instance list, you need to configure vpc:*:get and + vpc:*:list.

+ """ + expected = """ +

If the VPC, subnet, and security group are displayed in the DB + instance list, you need to configure vpc:``*``:get and + vpc:``*``:list.

+ """ + soup = bs4.BeautifulSoup(test_data, 'lxml') + res = self.convertor.streamline_html(soup, "dummy") + self.assertEqual(str(res.find('td')), expected.strip()) + + def test_streamline_html_escape_4(self): + test_data = """ +
  • You must have VPC-related permissions when creating a n SFS + Turbo instance, including the permissions for verifying VPCs, subnets, + and security groups, creating virtual IP addresses and ports, and + creating security group rules. You must add the following + action:
    • "vpc:*:*"
    + """ + expected = """ +
    • You must have VPC-related permissions when creating a n SFS + Turbo instance, including the permissions for verifying VPCs, subnets, + and security groups, creating virtual IP addresses and ports, and + creating security group rules. You must add the following + action:
      • "vpc:*:*"
    + """ + soup = bs4.BeautifulSoup(test_data, 'lxml') + res = self.convertor.streamline_html(soup, "dummy") + self.assertEqual(str(res.find('td')), expected.strip()) + + def test_streamline_html_escape_5(self): + test_data = """ +
    • "dss:*:get",
    • "dss:*:list",
    • "dss:*:count"
    + """ + expected = """ +
    • "dss:*:get",
    • "dss:*:list",
    • "dss:*:count"
    + """ + soup = bs4.BeautifulSoup(test_data, 'lxml') + res = self.convertor.streamline_html(soup, "dummy") + self.assertEqual(str(res.find('ul')), expected.strip()) + + def test_streamline_html_escape_6(self): + test_data = """ +
    • "vpc:*:*"
    + """ + expected = """ +
    • "vpc:*:*"
    + """ + soup = bs4.BeautifulSoup(test_data, 'lxml') + res = self.convertor.streamline_html(soup, "dummy") + self.assertEqual(str(res.find('ul')), expected.strip()) + + def test_streamline_html_escape_7(self): + test_data = """ +
    • vpc:securityGroups:*
    • vpc:securityGroupRules:*
    + """ + expected = """ +
    • vpc:securityGroups:*
    • vpc:securityGroupRules:*
    + """ + soup = bs4.BeautifulSoup(test_data, 'lxml') + res = self.convertor.streamline_html(soup, "dummy") + self.assertEqual(str(res.find('ul')), expected.strip()) + + def test_streamline_html_escape_8(self): + test_data = """ +

    cce:kubernetes:*

    + """ + expected = """ +

    cce:kubernetes:*

    + """ + soup = bs4.BeautifulSoup(test_data, 'lxml') + res = self.convertor.streamline_html(soup, "dummy") + self.assertEqual(str(res.find('p')), expected.strip()) + + def test_streamline_html_escape_9(self): + test_data = """ +
    • The folder name contains a maximum of 255 characters, and the + full path cannot exceed 1,023 characters.
    • +
    • The folder name cannot be empty.
    • +
    • The folder name cannot contain the following special characters: + /:*?"<>|\;&,'`!{}[]$%+
    • +
    • The value cannot start or end with a period (.).
    • +
    • The spaces at the beginning and end are ignored.
    + """ # noqa + expected = """ +
    • The folder name contains a maximum of 255 characters, and the + full path cannot exceed 1,023 characters.
    • The folder name cannot be empty.
    • The folder name cannot contain the following special characters: /:*?"<>|\\;&,\'`!{}[]$%+
    • The value cannot start or end with a period (.).
    • The spaces at the beginning and end are ignored.
    + """ # noqa + soup = bs4.BeautifulSoup(test_data, 'lxml') + res = self.convertor.streamline_html(soup, "dummy") + self.assertEqual( + str(res.find('ul')).replace('\n', ''), + expected.strip().replace('\n', ''), + ) + + def test_streamline_html_escape_10(self): + test_data = """ +

    ipFilterRules=allow:ip:127.*, allow:name:localhost, deny:ip:*

    + """ # noqa + expected = """ +

    ipFilterRules=allow:ip:127.*, allow:name:localhost, deny:ip:*

    + """ # noqa + soup = bs4.BeautifulSoup(test_data, 'lxml') + res = self.convertor.streamline_html(soup, "dummy") + self.assertEqual( + str(res.find('p')), + expected.strip(), + ) + + def test_streamline_html_escape_11(self): + test_data = """ +

    If the VPC, subnet, and security group are displayed in the DB instance list, you need to configure vpc:*:get and vpc:*:list.

    + """ # noqa + expected = """ +

    If the VPC, subnet, and security group are displayed in the DB instance list, you need to configure vpc:``*``:get and vpc:``*``:list.

    + """ # noqa + soup = bs4.BeautifulSoup(test_data, 'lxml') + res = self.convertor.streamline_html(soup, "dummy") + self.assertEqual( + str(res.find('td')), + expected.strip(), + ) diff --git a/zuul.yaml b/zuul.yaml index 08bf7ada..6a3c78c9 100644 --- a/zuul.yaml +++ b/zuul.yaml @@ -222,6 +222,7 @@ propose_change_git_baseurl: "gitea.eco.tsi-dev.otc-service.com" check: jobs: + - otc-tox-py39 - otc-tox-pep8: nodeset: ubuntu-focal - otc-doc-exports-convert-application