Skip to content

Commit

Permalink
Merge pull request #152 from Honny1/move-unit-tests-to-dir-unit-tests
Browse files Browse the repository at this point in the history
Move unit tests to the unit tests directory
  • Loading branch information
evgenyz committed Apr 11, 2023
2 parents 89eb900 + 9e25ace commit 8000ab3
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 39 deletions.
41 changes: 2 additions & 39 deletions tests/integration_tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,61 +1,24 @@
# Copyright 2022, Red Hat, Inc.
# SPDX-License-Identifier: LGPL-2.1-or-later

import argparse
import subprocess
import tempfile
from io import BytesIO
from pathlib import Path
from unittest import mock

import pytest

from openscap_report.cli import CommandLineAPI
from openscap_report.scap_results_parser import (ARF_SCHEMAS_PATH,
SCAPResultsParser)
from openscap_report.scap_results_parser import SCAPResultsParser

from ..constants import PATH_TO_ARF, PATH_TO_EMPTY_FILE
from ..test_utils import get_fake_args

PATH_TO_RESULT_FILE = Path(tempfile.gettempdir()) / "oscap-report-tests_result.html"
OSCAP_REPORT_COMMAND = "oscap-report"
CAT_ARF_FILE = ["cat", str(PATH_TO_ARF)]


def get_fake_args():
# pylint: disable=bad-option-value,R1732
input_file = open(PATH_TO_ARF, "r", encoding="utf-8")
output_file = open(PATH_TO_RESULT_FILE, "wb")
return argparse.Namespace(
FILE=input_file, output=output_file,
log_file=None, log_level="WARNING", format="HTML",
debug=[""],
)


@pytest.mark.unit_test
@mock.patch('argparse.ArgumentParser.parse_args',
return_value=get_fake_args())
def test_load_file(mock_args): # pylint: disable=W0613
api = CommandLineAPI()
xml_report = api.load_file()
parser = SCAPResultsParser(xml_report)
assert parser.validate(ARF_SCHEMAS_PATH)
api.close_files()


@pytest.mark.unit_test
@pytest.mark.usefixtures("remove_generated_file")
@mock.patch('argparse.ArgumentParser.parse_args',
return_value=get_fake_args())
def test_store_file(mock_args): # pylint: disable=W0613
api = CommandLineAPI()
data = BytesIO(b'<html><h1>TEST DATA</h1></html>')
api.store_file(data)
api.close_files()
with open(PATH_TO_RESULT_FILE, "r", encoding="utf-8") as result_file:
assert result_file.read() == data.getvalue().decode("utf-8")


@pytest.mark.integration_test
@mock.patch('argparse.ArgumentParser.parse_args',
return_value=get_fake_args())
Expand Down
16 changes: 16 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# Copyright 2022, Red Hat, Inc.
# SPDX-License-Identifier: LGPL-2.1-or-later

import argparse
import tempfile
from dataclasses import replace
from pathlib import Path

try:
from functools import cache
Expand All @@ -22,6 +25,8 @@
PATH_TO_ARF_REPRODUCING_DANGLING_REFERENCE_TO)
from .unit_tests.test_oval_tree_eval import OVAL_TREE_TRUE

PATH_TO_RESULT_FILE = Path(tempfile.gettempdir()) / "oscap-report-tests_result.html"


@cache
def get_xml_data(file_path):
Expand Down Expand Up @@ -104,3 +109,14 @@ def get_dummy_cpe_oval_definition():
"oval:ssg-installed_env_has_zipl_package:def:1": dummy_oval_definition,
"oval:ssg-system_boot_mode_is_uefi:def:1": dummy_oval_definition,
}


def get_fake_args():
# pylint: disable=bad-option-value,R1732
input_file = open(PATH_TO_ARF, "r", encoding="utf-8")
output_file = open(PATH_TO_RESULT_FILE, "wb")
return argparse.Namespace(
FILE=input_file, output=output_file,
log_file=None, log_level="WARNING", format="HTML",
debug=[""],
)
41 changes: 41 additions & 0 deletions tests/unit_tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright 2022, Red Hat, Inc.
# SPDX-License-Identifier: LGPL-2.1-or-later

import tempfile
from io import BytesIO
from pathlib import Path
from unittest import mock

import pytest

from openscap_report.cli import CommandLineAPI
from openscap_report.scap_results_parser import (ARF_SCHEMAS_PATH,
SCAPResultsParser)

from ..test_utils import get_fake_args

PATH_TO_RESULT_FILE = Path(tempfile.gettempdir()) / "oscap-report-tests_result.html"


@pytest.mark.unit_test
@mock.patch('argparse.ArgumentParser.parse_args',
return_value=get_fake_args())
def test_load_file(mock_args): # pylint: disable=W0613
api = CommandLineAPI()
xml_report = api.load_file()
parser = SCAPResultsParser(xml_report)
assert parser.validate(ARF_SCHEMAS_PATH)
api.close_files()


@pytest.mark.unit_test
@pytest.mark.usefixtures("remove_generated_file")
@mock.patch('argparse.ArgumentParser.parse_args',
return_value=get_fake_args())
def test_store_file(mock_args): # pylint: disable=W0613
api = CommandLineAPI()
data = BytesIO(b'<html><h1>TEST DATA</h1></html>')
api.store_file(data)
api.close_files()
with open(PATH_TO_RESULT_FILE, "r", encoding="utf-8") as result_file:
assert result_file.read() == data.getvalue().decode("utf-8")

0 comments on commit 8000ab3

Please sign in to comment.