From 35ed3d88ac9e97700d02f893929de1c019069d3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armin=20T=C3=A4nzer?= Date: Wed, 4 Jan 2023 10:43:40 +0100 Subject: [PATCH] [issue-397] replace valid_defaults.py usages with fixtures.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Armin Tänzer --- tests/jsonschema/test_document_converter.py | 2 +- tests/valid_defaults.py | 116 ------------------ tests/validation/test_actor_validator.py | 8 +- tests/validation/test_annotation_validator.py | 14 +-- tests/validation/test_checksum_validator.py | 4 +- .../test_creation_info_validator.py | 18 +-- tests/validation/test_document_validator.py | 9 +- .../test_external_document_ref_validator.py | 6 +- .../test_external_package_ref_validator.py | 12 +- ...test_extracted_licensing_info_validator.py | 10 +- tests/validation/test_file_validator.py | 24 ++-- tests/validation/test_package_validator.py | 33 +++-- .../validation/test_relationship_validator.py | 17 ++- tests/validation/test_snippet_validator.py | 27 ++-- tests/writer/tagvalue/test_package_writer.py | 23 ++-- 15 files changed, 87 insertions(+), 236 deletions(-) delete mode 100644 tests/valid_defaults.py diff --git a/tests/jsonschema/test_document_converter.py b/tests/jsonschema/test_document_converter.py index 5facc4f8c..086712848 100644 --- a/tests/jsonschema/test_document_converter.py +++ b/tests/jsonschema/test_document_converter.py @@ -71,7 +71,7 @@ def test_json_property_names(converter: DocumentConverter, document_property: Do def test_successful_conversion(converter: DocumentConverter): creation_info = creation_info_fixture(spdx_version="spdxVersion", spdx_id="spdxId", name="name", - namespace="namespace", document_comment="comment", data_license="dataLicense", + document_namespace="namespace", document_comment="comment", data_license="dataLicense", external_document_refs=[external_document_ref_fixture()]) document = Document(creation_info, annotations=[ Annotation("annotationId", AnnotationType.REVIEW, Actor(ActorType.PERSON, "reviewerName"), diff --git a/tests/valid_defaults.py b/tests/valid_defaults.py deleted file mode 100644 index a800193f0..000000000 --- a/tests/valid_defaults.py +++ /dev/null @@ -1,116 +0,0 @@ -# Copyright (c) 2022 spdx contributors -# 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 datetime import datetime - -from src.model.actor import Actor, ActorType -from src.model.annotation import AnnotationType, Annotation -from src.model.checksum import Checksum, ChecksumAlgorithm -from src.model.document import CreationInfo, Document -from src.model.external_document_ref import ExternalDocumentRef -from src.model.extracted_licensing_info import ExtractedLicensingInfo -from src.model.file import File -from src.model.package import Package, PackageVerificationCode, ExternalPackageRef, ExternalPackageRefCategory -from src.model.relationship import Relationship, RelationshipType -from src.model.snippet import Snippet -from src.model.spdx_none import SpdxNone - - -def get_actor(actor_type=ActorType.PERSON, name="person name", mail=None) -> Actor: - return Actor(actor_type, name, mail) - - -def get_annotation(spdx_id="SPDXRef-DOCUMENT", annotation_type=AnnotationType.OTHER, annotator=get_actor(), - annotation_date=datetime(2022, 1, 1), annotation_comment="annotation comment") -> Annotation: - return Annotation(spdx_id, annotation_type, annotator, annotation_date, annotation_comment) - - -def get_checksum(algorithm=ChecksumAlgorithm.SHA1, value="85ed0817af83a24ad8da68c2b5094de69833983c") -> Checksum: - return Checksum(algorithm, value) - - -def get_creation_info(spdx_version="SPDX-2.3", spdx_id="SPDXRef-DOCUMENT", name="document_name", - document_namespace="https://some.uri", - creators=None, created=datetime(2022, 1, 1), creator_comment=None, data_license="CC0-1.0", - external_document_refs=None, license_list_version=None, document_comment=None) -> CreationInfo: - if creators is None: - creators = [get_actor()] - - return CreationInfo(spdx_version, spdx_id, name, document_namespace, creators, created, creator_comment, - data_license, external_document_refs, license_list_version, document_comment) - - -def get_document(creation_info=get_creation_info(), packages=None, files=None, snippets=None, annotations=None, - relationships=None, extracted_licensing_info=None) -> Document: - - return Document(creation_info, packages, files, snippets, annotations, relationships, extracted_licensing_info) - - -def get_external_document_ref(document_ref_id="DocumentRef-idstring", document_uri="https://some.uri", - checksum=get_checksum()) -> ExternalDocumentRef: - return ExternalDocumentRef(document_ref_id, document_uri, checksum) - - -def get_extracted_licensing_info(license_id="LicenseRef-1", extracted_text="extracted text", - license_name="license name", cross_references=None, - comment=None, ) -> ExtractedLicensingInfo: - if cross_references is None: - cross_references = ["http://some.url"] - return ExtractedLicensingInfo(license_id, extracted_text, license_name, cross_references, comment) - - -def get_file(name="./file/name.py", spdx_id="SPDXRef-File", checksums=None, file_type=None, concluded_license=None, - license_info_in_file=None, license_comment=None, copyright_text=None, comment=None, notice=None, - contributors=None, attribution_texts=None) -> File: - if checksums is None: - checksums = [get_checksum()] - - return File(name, spdx_id, checksums, file_type, concluded_license, license_info_in_file, license_comment, - copyright_text, comment, notice, contributors, attribution_texts) - - -def get_package_verification_code(value="85ed0817af83a24ad8da68c2b5094de69833983c", - excluded_files=None) -> PackageVerificationCode: - - return PackageVerificationCode(value, excluded_files) - - -def get_external_package_ref(category=ExternalPackageRefCategory.SECURITY, reference_type="cpe22Type", - locator="cpe:/o:canonical:ubuntu_linux:10.04:-:lts", - comment="external package ref comment") -> ExternalPackageRef: - return ExternalPackageRef(category, reference_type, locator, comment) - - -def get_package(spdx_id="SPDXRef-Package", name="package name", download_location=SpdxNone(), version=None, - file_name=None, supplier=None, originator=None, files_analyzed=False, verification_code=None, - checksums=None, homepage=None, source_info=None, license_concluded=None, license_info_from_files=None, - license_declared=None, license_comment=None, copyright_text=None, summary=None, description=None, - comment=None, external_references=None, attribution_texts=None, primary_package_purpose=None, - release_date=None, built_date=None, valid_until_date=None) -> Package: - - return Package(spdx_id, name, download_location, version, file_name, supplier, originator, files_analyzed, - verification_code, checksums, homepage, source_info, license_concluded, license_info_from_files, - license_declared, license_comment, copyright_text, summary, description, comment, - external_references, attribution_texts, primary_package_purpose, release_date, built_date, - valid_until_date) - - -def get_relationship(spdx_element_id="SPDXRef-DOCUMENT", relationship_type=RelationshipType.DESCRIBES, - related_spdx_element_id="SPDXRef-File", comment=None) -> Relationship: - return Relationship(spdx_element_id, relationship_type, related_spdx_element_id, comment) - - -def get_snippet(spdx_id="SPDXRef-Snippet", file_spdx_id="SPDXRef-File", byte_range=(200, 400), line_range=None, - concluded_license=None, license_info_in_snippet=None, license_comment=None, copyright_text=None, - comment=None, name=None, attribution_texts=None) -> Snippet: - - return Snippet(spdx_id, file_spdx_id, byte_range, line_range, concluded_license, license_info_in_snippet, - license_comment, copyright_text, comment, name, attribution_texts) diff --git a/tests/validation/test_actor_validator.py b/tests/validation/test_actor_validator.py index 4215fe7e5..7561f6659 100644 --- a/tests/validation/test_actor_validator.py +++ b/tests/validation/test_actor_validator.py @@ -13,21 +13,21 @@ import pytest -from src.model.actor import ActorType, Actor +from src.model.actor import ActorType from src.validation.actor_validator import validate_actor from src.validation.validation_message import ValidationMessage, ValidationContext, SpdxElementType -from tests.valid_defaults import get_actor +from tests.fixtures import actor_fixture def test_valid_actor_person(): - actor = Actor(ActorType.PERSON, "person name", "mail@mail.com") + actor = actor_fixture() validation_messages: List[ValidationMessage] = validate_actor(actor, "SPDXRef-DOCUMENT") assert validation_messages == [] @pytest.mark.parametrize("actor, expected_message", - [(get_actor(actor_type=ActorType.TOOL, mail="mail@mail.com"), + [(actor_fixture(actor_type=ActorType.TOOL, email="mail@mail.com"), "email must be None if actor_type is TOOL, but is: mail@mail.com"), ]) def test_invalid_actor(actor, expected_message): diff --git a/tests/validation/test_annotation_validator.py b/tests/validation/test_annotation_validator.py index 885693d8b..5ddf87f41 100644 --- a/tests/validation/test_annotation_validator.py +++ b/tests/validation/test_annotation_validator.py @@ -9,23 +9,19 @@ # See the License for the specific language governing permissions and # limitations under the License. -from datetime import datetime from typing import List import pytest -from src.model.annotation import Annotation, AnnotationType +from src.model.annotation import Annotation from src.model.document import Document from src.validation.annotation_validator import validate_annotation from src.validation.validation_message import ValidationMessage, ValidationContext, SpdxElementType -from tests.valid_defaults import get_actor, get_annotation, get_document, get_file +from tests.fixtures import document_fixture, annotation_fixture, file_fixture def test_valid_annotation(): - document: Document = get_document(files=[get_file(spdx_id="SPDXRef-File")]) - - annotation = Annotation("SPDXRef-File", AnnotationType.OTHER, get_actor(), datetime(2022, 1, 1), "comment") - validation_messages: List[ValidationMessage] = validate_annotation(annotation, document) + validation_messages: List[ValidationMessage] = validate_annotation(annotation_fixture(), document_fixture()) assert validation_messages == [] @@ -35,8 +31,8 @@ def test_valid_annotation(): "did not find the referenced spdx_id SPDXRef-File in the SPDX document") ]) def test_invalid_annotation(annotation_id, file_id, expected_message): - annotation: Annotation = get_annotation(spdx_id=annotation_id) - document: Document = get_document(files=[get_file(spdx_id=file_id)]) + annotation: Annotation = annotation_fixture(spdx_id=annotation_id) + document: Document = document_fixture(files=[file_fixture(spdx_id=file_id)]) validation_messages: List[ValidationMessage] = validate_annotation(annotation, document) expected = ValidationMessage(expected_message, diff --git a/tests/validation/test_checksum_validator.py b/tests/validation/test_checksum_validator.py index 661fb110a..22a1b6ffb 100644 --- a/tests/validation/test_checksum_validator.py +++ b/tests/validation/test_checksum_validator.py @@ -16,10 +16,12 @@ from src.model.checksum import Checksum, ChecksumAlgorithm from src.validation.checksum_validator import validate_checksum from src.validation.validation_message import ValidationMessage, ValidationContext, SpdxElementType +from tests.fixtures import checksum_fixture @pytest.mark.parametrize("checksum", - [Checksum(ChecksumAlgorithm.SHA1, "71c4025dd9897b364f3ebbb42c484ff43d00791c"), + [checksum_fixture(), + Checksum(ChecksumAlgorithm.SHA1, "71c4025dd9897b364f3ebbb42c484ff43d00791c"), Checksum(ChecksumAlgorithm.SHA224, "9c9f4e27d957a123cc32d86afe33ae53b1184192cccb23b0f257f588"), Checksum(ChecksumAlgorithm.SHA256, diff --git a/tests/validation/test_creation_info_validator.py b/tests/validation/test_creation_info_validator.py index fb9cc86e9..5de041660 100644 --- a/tests/validation/test_creation_info_validator.py +++ b/tests/validation/test_creation_info_validator.py @@ -9,23 +9,17 @@ # See the License for the specific language governing permissions and # limitations under the License. -from datetime import datetime from typing import List import pytest -from src.model.document import CreationInfo -from src.model.version import Version from src.validation.creation_info_validator import validate_creation_info from src.validation.validation_message import ValidationMessage, ValidationContext, SpdxElementType -from tests.valid_defaults import get_actor, get_external_document_ref, get_creation_info +from tests.fixtures import creation_info_fixture def test_valid_creation_info(): - creation_info = CreationInfo("SPDX-2.3", "SPDXRef-DOCUMENT", "document name", "https://some.uri", - [get_actor(), get_actor()], datetime(2022, 1, 1), "creator_comment", - "CC0-1.0", [get_external_document_ref(), get_external_document_ref()], Version(6, 3), - "doc_comment") + creation_info = creation_info_fixture() validation_messages: List[ValidationMessage] = validate_creation_info(creation_info) assert validation_messages == [] @@ -33,13 +27,13 @@ def test_valid_creation_info(): @pytest.mark.parametrize \ ("creation_info_input, spdx_id, expected_message", - [(get_creation_info(spdx_version="version-2.3"), "SPDXRef-DOCUMENT", + [(creation_info_fixture(spdx_version="version-2.3"), "SPDXRef-DOCUMENT", 'spdx_version must be of the form "SPDX-[major].[minor]" but is: version-2.3'), - (get_creation_info(spdx_id="SPDXRef-doc"), "SPDXRef-doc", + (creation_info_fixture(spdx_id="SPDXRef-doc"), "SPDXRef-doc", 'spdx_id must be "SPDXRef-DOCUMENT", but is: SPDXRef-doc'), - (get_creation_info(data_license="MIT"), "SPDXRef-DOCUMENT", + (creation_info_fixture(data_license="MIT"), "SPDXRef-DOCUMENT", 'data_license must be "CC0-1.0", but is: MIT'), - (get_creation_info(document_namespace="some_namespace"), "SPDXRef-DOCUMENT", + (creation_info_fixture(document_namespace="some_namespace"), "SPDXRef-DOCUMENT", "document_namespace must be a valid URI specified in RFC-3986, but is: some_namespace"), ]) def test_invalid_creation_info(creation_info_input, expected_message, spdx_id): diff --git a/tests/validation/test_document_validator.py b/tests/validation/test_document_validator.py index be66477c0..637c6ad74 100644 --- a/tests/validation/test_document_validator.py +++ b/tests/validation/test_document_validator.py @@ -11,18 +11,13 @@ from typing import List -from src.model.document import Document from src.validation.document_validator import validate_full_spdx_document from src.validation.validation_message import ValidationMessage -from tests.valid_defaults import get_creation_info, get_package, get_file, get_snippet, get_annotation, \ - get_relationship, get_extracted_licensing_info +from tests.fixtures import document_fixture def test_valid_document(): - document = Document(get_creation_info(), [get_package(), get_package()], [get_file(), get_file()], - [get_snippet(), get_snippet()], [get_annotation(), get_annotation()], - [get_relationship(), get_relationship()], - [get_extracted_licensing_info(), get_extracted_licensing_info()]) + document = document_fixture() validation_messages: List[ValidationMessage] = validate_full_spdx_document(document, "2.3") assert validation_messages == [] diff --git a/tests/validation/test_external_document_ref_validator.py b/tests/validation/test_external_document_ref_validator.py index 84ead91ad..611c1e5cb 100644 --- a/tests/validation/test_external_document_ref_validator.py +++ b/tests/validation/test_external_document_ref_validator.py @@ -11,15 +11,13 @@ from typing import List -from src.model.external_document_ref import ExternalDocumentRef from src.validation.external_document_ref_validator import validate_external_document_ref from src.validation.validation_message import ValidationMessage -from tests.valid_defaults import get_checksum +from tests.fixtures import external_document_ref_fixture def test_valid_external_document_ref(): - - external_document_ref = ExternalDocumentRef("DocumentRef-id", "http://some.uri", get_checksum()) + external_document_ref = external_document_ref_fixture() validation_messages: List[ValidationMessage] = validate_external_document_ref(external_document_ref, "parent_id") assert validation_messages == [] diff --git a/tests/validation/test_external_package_ref_validator.py b/tests/validation/test_external_package_ref_validator.py index 7b94e84e6..456dd7acd 100644 --- a/tests/validation/test_external_package_ref_validator.py +++ b/tests/validation/test_external_package_ref_validator.py @@ -13,26 +13,24 @@ import pytest -from src.model.package import ExternalPackageRef, ExternalPackageRefCategory from src.validation.external_package_ref_validator import validate_external_package_ref from src.validation.validation_message import ValidationMessage, ValidationContext, SpdxElementType -from tests.valid_defaults import get_external_package_ref +from tests.fixtures import external_package_ref_fixture def test_valid_external_package_ref(): - - external_package_ref = ExternalPackageRef(ExternalPackageRefCategory.OTHER, "swh", - "swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2", "comment") + external_package_ref = external_package_ref_fixture() validation_messages: List[ValidationMessage] = validate_external_package_ref(external_package_ref, "parent_id") assert validation_messages == [] @pytest.mark.parametrize("external_package_ref, expected_message", - [(get_external_package_ref(), + [(external_package_ref_fixture(), "TBD"), ]) -@pytest.mark.skip("add tests once external package ref validation is implemented: https://github.com/spdx/tools-python/issues/373") +@pytest.mark.skip( + "add tests once external package ref validation is implemented: https://github.com/spdx/tools-python/issues/373") def test_invalid_external_package_ref(external_package_ref, expected_message): parent_id = "SPDXRef-Package" validation_messages: List[ValidationMessage] = validate_external_package_ref(external_package_ref, parent_id) diff --git a/tests/validation/test_extracted_licensing_info_validator.py b/tests/validation/test_extracted_licensing_info_validator.py index 25047cca5..395c3a000 100644 --- a/tests/validation/test_extracted_licensing_info_validator.py +++ b/tests/validation/test_extracted_licensing_info_validator.py @@ -13,15 +13,13 @@ import pytest -from src.model.extracted_licensing_info import ExtractedLicensingInfo from src.validation.extracted_licensing_info_validator import validate_extracted_licensing_info from src.validation.validation_message import ValidationMessage, ValidationContext, SpdxElementType -from tests.valid_defaults import get_extracted_licensing_info +from tests.fixtures import extracted_licensing_info_fixture def test_valid_extracted_licensing_info(): - extracted_licensing_info = ExtractedLicensingInfo("LicenseRef-1", "extracted text", "license name", - ["http://some.url"], "comment") + extracted_licensing_info = extracted_licensing_info_fixture() validation_messages: List[ValidationMessage] = validate_extracted_licensing_info(extracted_licensing_info) assert validation_messages == [] @@ -29,9 +27,9 @@ def test_valid_extracted_licensing_info(): # TODO: tests for licenses not on the SPDX License list (i.e. they must provide id, name and cross-references) @pytest.mark.parametrize("extracted_licensing_info, expected_message", - [(get_extracted_licensing_info(extracted_text=None), + [(extracted_licensing_info_fixture(extracted_text=None), 'extracted_text must be provided if there is a license_id assigned'), - (get_extracted_licensing_info(cross_references=["invalid_url"]), + (extracted_licensing_info_fixture(cross_references=["invalid_url"]), 'cross_reference must be a valid URL, but is: invalid_url') ]) def test_invalid_extracted_licensing_info(extracted_licensing_info, expected_message): diff --git a/tests/validation/test_file_validator.py b/tests/validation/test_file_validator.py index fda40ab36..f76822255 100644 --- a/tests/validation/test_file_validator.py +++ b/tests/validation/test_file_validator.py @@ -14,36 +14,32 @@ import pytest from src.model.checksum import Checksum, ChecksumAlgorithm -from src.model.file import File, FileType -from src.model.spdx_no_assertion import SpdxNoAssertion -from src.model.spdx_none import SpdxNone from src.validation.file_validator import validate_file_within_document from src.validation.validation_message import ValidationMessage, ValidationContext, SpdxElementType -from tests.valid_defaults import get_checksum, get_file, get_document +from tests.fixtures import file_fixture, document_fixture def test_valid_file(): - file = File("./file/name.py", "SPDXRef-File", [get_checksum()], [FileType.OTHER, FileType.SPDX], SpdxNone(), - SpdxNoAssertion(), - "comment on license", "copyright", "comment", "notice", ["contributor"], ["attribution"]) - validation_messages: List[ValidationMessage] = validate_file_within_document(file, get_document()) + file = file_fixture() + validation_messages: List[ValidationMessage] = validate_file_within_document(file, document_fixture()) assert validation_messages == [] @pytest.mark.parametrize("file_input, spdx_id, expected_message", - [(get_file(name="invalid file name"), get_file().spdx_id, + [(file_fixture(name="invalid file name"), file_fixture().spdx_id, 'file name must be a relative path to the file, starting with "./", but is: invalid file name'), - (get_file(checksums=[Checksum(ChecksumAlgorithm.MD2, "d4c41ce30a517d6ce9d79c8c17bb4b66")]), - get_file().spdx_id, - f'checksums must contain a SHA1 algorithm checksum, but only contains: []') + ( + file_fixture(checksums=[Checksum(ChecksumAlgorithm.MD2, "d4c41ce30a517d6ce9d79c8c17bb4b66")]), + file_fixture().spdx_id, + f'checksums must contain a SHA1 algorithm checksum, but only contains: []') ]) def test_invalid_file(file_input, spdx_id, expected_message): - validation_messages: List[ValidationMessage] = validate_file_within_document(file_input, get_document()) + validation_messages: List[ValidationMessage] = validate_file_within_document(file_input, document_fixture()) expected = ValidationMessage(expected_message, ValidationContext(spdx_id=spdx_id, - parent_id=get_document().creation_info.spdx_id, + parent_id=document_fixture().creation_info.spdx_id, element_type=SpdxElementType.FILE, full_element=file_input)) diff --git a/tests/validation/test_package_validator.py b/tests/validation/test_package_validator.py index 8c81c6739..31b86b8fb 100644 --- a/tests/validation/test_package_validator.py +++ b/tests/validation/test_package_validator.py @@ -9,46 +9,43 @@ # See the License for the specific language governing permissions and # limitations under the License. -from datetime import datetime from typing import List import pytest from src.model.license_expression import LicenseExpression -from src.model.package import Package, PackagePurpose from src.model.spdx_no_assertion import SpdxNoAssertion from src.model.spdx_none import SpdxNone from src.validation.package_validator import validate_package_within_document from src.validation.validation_message import ValidationMessage, ValidationContext, SpdxElementType -from tests.valid_defaults import get_checksum, get_external_package_ref, get_actor, get_package_verification_code, \ - get_package, get_document +from tests.fixtures import package_fixture, package_verification_code_fixture, document_fixture def test_valid_package(): - package = Package("SPDXRef-Package", "package name", "www.download.com", "version", "file_name", SpdxNoAssertion(), - get_actor(), True, - get_package_verification_code(), [get_checksum()], "https://homepage.com", "source_info", None, - [LicenseExpression("expression")], - SpdxNone(), "comment on license", "copyright", "summary", "description", "comment", - [get_external_package_ref()], ["text"], PackagePurpose.OTHER, datetime(2022, 1, 1), None, None) - validation_messages: List[ValidationMessage] = validate_package_within_document(package, get_document()) + package = package_fixture() + validation_messages: List[ValidationMessage] = validate_package_within_document(package, document_fixture()) assert validation_messages == [] @pytest.mark.parametrize("package_input, expected_message", - [(get_package(files_analyzed=False, verification_code=get_package_verification_code()), - f'verification_code must be None if files_analyzed is False, but is: {get_package_verification_code()}'), - (get_package(files_analyzed=False, license_info_from_files=SpdxNone()), + [(package_fixture(files_analyzed=False, verification_code=package_verification_code_fixture(), + license_info_from_files=[]), + f'verification_code must be None if files_analyzed is False, but is: {package_verification_code_fixture()}'), + (package_fixture(files_analyzed=False, license_info_from_files=SpdxNone(), + verification_code=None), 'license_info_from_files must be None if files_analyzed is False, but is: NONE'), - (get_package(files_analyzed=False, license_info_from_files=SpdxNoAssertion()), + (package_fixture(files_analyzed=False, license_info_from_files=SpdxNoAssertion(), + verification_code=None), 'license_info_from_files must be None if files_analyzed is False, but is: NOASSERTION'), - (get_package(files_analyzed=False, - license_info_from_files=[LicenseExpression("some_license")]), + (package_fixture(files_analyzed=False, + license_info_from_files=[LicenseExpression("some_license")], + verification_code=None), 'license_info_from_files must be None if files_analyzed is False, but is: [LicenseExpression(expression_string=\'some_license\')]') ]) def test_invalid_package(package_input, expected_message): - validation_messages: List[ValidationMessage] = validate_package_within_document(package_input, get_document()) + validation_messages: List[ValidationMessage] = validate_package_within_document(package_input, + document_fixture(relationships=[])) expected = ValidationMessage(expected_message, ValidationContext(spdx_id=package_input.spdx_id, parent_id="SPDXRef-DOCUMENT", diff --git a/tests/validation/test_relationship_validator.py b/tests/validation/test_relationship_validator.py index 2e2a08b95..706655551 100644 --- a/tests/validation/test_relationship_validator.py +++ b/tests/validation/test_relationship_validator.py @@ -19,16 +19,14 @@ from src.model.spdx_none import SpdxNone from src.validation.relationship_validator import validate_relationship from src.validation.validation_message import ValidationMessage, SpdxElementType, ValidationContext -from tests.valid_defaults import get_document, get_package, get_relationship, get_file +from tests.fixtures import document_fixture, relationship_fixture @pytest.mark.parametrize("related_spdx_element", ["SPDXRef-Package", SpdxNoAssertion(), SpdxNone()]) def test_valid_relationship(related_spdx_element): - document: Document = get_document(packages=[get_package(spdx_id="SPDXRef-Package")]) - - relationship = Relationship("SPDXRef-DOCUMENT", RelationshipType.AMENDS, related_spdx_element, comment="comment") - validation_messages: List[ValidationMessage] = validate_relationship(relationship, document, "2.3") + relationship = Relationship("SPDXRef-DOCUMENT", RelationshipType.DESCRIBES, related_spdx_element, comment="comment") + validation_messages: List[ValidationMessage] = validate_relationship(relationship, document_fixture(), "2.3") assert validation_messages == [] @@ -40,10 +38,9 @@ def test_valid_relationship(related_spdx_element): 'did not find the referenced spdx_id SPDXRef-unknownFile in the SPDX document'), ]) def test_unknown_spdx_id(spdx_element_id, related_spdx_element_id, expected_message): - relationship: Relationship = get_relationship(spdx_element_id=spdx_element_id, - related_spdx_element_id=related_spdx_element_id) - document: Document = get_document(files=[get_file(spdx_id="SPDXRef-File")]) - validation_messages: List[ValidationMessage] = validate_relationship(relationship, document, "2.3") + relationship: Relationship = relationship_fixture(spdx_element_id=spdx_element_id, + related_spdx_element_id=related_spdx_element_id) + validation_messages: List[ValidationMessage] = validate_relationship(relationship, document_fixture(), "2.3") expected = ValidationMessage(expected_message, ValidationContext(element_type=SpdxElementType.RELATIONSHIP, @@ -59,7 +56,7 @@ def test_unknown_spdx_id(spdx_element_id, related_spdx_element_id, expected_mess "SPDXRef-Package"), "RelationshipType.REQUIREMENT_DESCRIPTION_FOR is not supported for SPDX versions below 2.3")]) def test_v2_3_only_types(relationship, expected_message): - document: Document = get_document(packages=[get_package(spdx_id="SPDXRef-Package")]) + document: Document = document_fixture() validation_message: List[ValidationMessage] = validate_relationship(relationship, document, "2.2") diff --git a/tests/validation/test_snippet_validator.py b/tests/validation/test_snippet_validator.py index 9ceb675f6..8b072fc94 100644 --- a/tests/validation/test_snippet_validator.py +++ b/tests/validation/test_snippet_validator.py @@ -13,43 +13,34 @@ import pytest -from src.model.document import Document -from src.model.license_expression import LicenseExpression -from src.model.snippet import Snippet -from src.model.spdx_no_assertion import SpdxNoAssertion from src.validation.snippet_validator import validate_snippet_within_document from src.validation.validation_message import ValidationMessage, ValidationContext, SpdxElementType -from tests.valid_defaults import get_snippet, get_document, get_file +from tests.fixtures import document_fixture, snippet_fixture def test_valid_snippet(): - document: Document = get_document(files=[get_file(spdx_id="SPDXRef-File")]) - - snippet = Snippet("SPDXRef-Snippet", "SPDXRef-File", (200, 400), (20, 40), LicenseExpression("some_license"), - SpdxNoAssertion(), "comment on license", - "copyright", "comment", "name", ["attribution"]) - validation_messages: List[ValidationMessage] = validate_snippet_within_document(snippet, document) + snippet = snippet_fixture() + validation_messages: List[ValidationMessage] = validate_snippet_within_document(snippet, document_fixture()) assert validation_messages == [] @pytest.mark.parametrize("snippet_input, expected_message", - [(get_snippet(byte_range=(-12, 45)), + [(snippet_fixture(byte_range=(-12, 45)), "byte_range values must be greater than or equal to 1, but is: (-12, 45)"), - (get_snippet(byte_range=(45, 23)), + (snippet_fixture(byte_range=(45, 23)), "the first value of byte_range must be less than or equal to the second, but is: (45, 23)"), - (get_snippet(line_range=(-12, 45)), + (snippet_fixture(line_range=(-12, 45)), "line_range values must be greater than or equal to 1, but is: (-12, 45)"), - (get_snippet(line_range=(45, 23)), + (snippet_fixture(line_range=(45, 23)), "the first value of line_range must be less than or equal to the second, but is: (45, 23)") ]) def test_invalid_ranges(snippet_input, expected_message): - validation_messages: List[ValidationMessage] = validate_snippet_within_document(snippet_input, - get_document(files=[get_file()])) + validation_messages: List[ValidationMessage] = validate_snippet_within_document(snippet_input, document_fixture()) expected = ValidationMessage(expected_message, ValidationContext(spdx_id=snippet_input.spdx_id, - parent_id=get_document().creation_info.spdx_id, + parent_id=document_fixture().creation_info.spdx_id, element_type=SpdxElementType.SNIPPET, full_element=snippet_input)) diff --git a/tests/writer/tagvalue/test_package_writer.py b/tests/writer/tagvalue/test_package_writer.py index 052b24dd7..735a983dd 100644 --- a/tests/writer/tagvalue/test_package_writer.py +++ b/tests/writer/tagvalue/test_package_writer.py @@ -11,22 +11,27 @@ from datetime import datetime from unittest.mock import patch, mock_open, call +from src.model.actor import ActorType, Actor +from src.model.checksum import Checksum, ChecksumAlgorithm from src.model.license_expression import LicenseExpression -from src.model.package import PackagePurpose +from src.model.package import PackagePurpose, Package, PackageVerificationCode, ExternalPackageRef, \ + ExternalPackageRefCategory from src.model.spdx_no_assertion import SpdxNoAssertion from src.model.spdx_none import SpdxNone from src.writer.tagvalue.package_writer import write_package -from tests.valid_defaults import get_package, get_package_verification_code, get_actor, get_checksum, \ - get_external_package_ref def test_package_writer(): - package = get_package("SPDXRef-Package", "package name", "www.download.com", "version", "file_name", SpdxNoAssertion(), - get_actor(), True, - get_package_verification_code(), [get_checksum()], "https://homepage.com", "source_info", None, - [LicenseExpression("expression")], + package = Package("SPDXRef-Package", "package name", "www.download.com", "version", "file_name", SpdxNoAssertion(), + Actor(ActorType.PERSON, "person name", "email@mail.com"), True, + PackageVerificationCode("85ed0817af83a24ad8da68c2b5094de69833983c"), + [Checksum(ChecksumAlgorithm.SHA1, "85ed0817af83a24ad8da68c2b5094de69833983c")], + "https://homepage.com", "source_info", None, [LicenseExpression("expression")], SpdxNone(), "comment on license", "copyright", "summary", "description", "comment", - [get_external_package_ref()], ["text"], PackagePurpose.OTHER, datetime(2022, 1, 1), None, None) + [ExternalPackageRef(ExternalPackageRefCategory.SECURITY, "cpe22Type", + "cpe:/o:canonical:ubuntu_linux:10.04:-:lts", + "external package ref comment")], + ["text"], PackagePurpose.OTHER, datetime(2022, 1, 1), None, None) m = mock_open() with patch('{}.open'.format(__name__), m, create=True): @@ -42,7 +47,7 @@ def test_package_writer(): call('PackageVersion: version\n'), call('PackageFileName: file_name\n'), call('PackageSupplier: NOASSERTION\n'), - call('PackageOriginator: Person: person name\n'), + call('PackageOriginator: Person: person name (email@mail.com)\n'), call('PackageDownloadLocation: www.download.com\n'), call('FilesAnalyzed: True\n'), call('PackageVerificationCode: 85ed0817af83a24ad8da68c2b5094de69833983c\n'),