Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[issue-397] make fixtures valid and replace valid_defaults.py #409

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 40 additions & 29 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,35 @@
specified unless relevant for the test."""


def creation_info_fixture(spdx_version="spdxVersion", spdx_id="documentId", name="documentName",
namespace="documentNamespace", creators=None, created=datetime(2022, 12, 1),
def actor_fixture(actor_type=ActorType.PERSON, name="actorName", email="[email protected]") -> Actor:
return Actor(actor_type, name, email)


def checksum_fixture(algorithm=ChecksumAlgorithm.SHA1, value="71c4025dd9897b364f3ebbb42c484ff43d00791c") -> Checksum:
return Checksum(algorithm, value)


def package_verification_code_fixture(value="85ed0817af83a24ad8da68c2b5094de69833983c", excluded_files=None) -> PackageVerificationCode:
excluded_files = ["./exclude.py"] if excluded_files is None else excluded_files
return PackageVerificationCode(value, excluded_files)


def creation_info_fixture(spdx_version="SPDX-2.3", spdx_id="SPDXRef-DOCUMENT", name="documentName",
document_namespace="https://some.namespace", creators=None, created=datetime(2022, 12, 1),
creator_comment="creatorComment", data_license="CC0-1.0", external_document_refs=None,
license_list_version=Version(3, 19), document_comment="documentComment") -> CreationInfo:
creators = [Actor(ActorType.PERSON, "creatorName")] if creators is None else creators
creators = [actor_fixture(name="creatorName")] if creators is None else creators
external_document_refs = [
external_document_ref_fixture()] if external_document_refs is None else external_document_refs
return CreationInfo(spdx_version, spdx_id, name, namespace, creators, created, creator_comment, data_license,
return CreationInfo(spdx_version, spdx_id, name, document_namespace, creators, created, creator_comment, data_license,
external_document_refs, license_list_version, document_comment)


def file_fixture(name="fileName", spdx_id="fileId", checksums=None, file_type=None,
def file_fixture(name="./fileName.py", spdx_id="SPDXRef-File", checksums=None, file_type=None,
concluded_license=LicenseExpression("concludedLicenseExpression"), license_info_in_file=None,
license_comment="licenseComment", copyright_text="copyrightText", comment="fileComment",
notice="fileNotice", contributors=None, attribution_texts=None) -> File:
checksums = [Checksum(ChecksumAlgorithm.SHA1, "sha1")] if checksums is None else checksums
checksums = [checksum_fixture()] if checksums is None else checksums
file_type = [FileType.TEXT] if file_type is None else file_type
license_info_in_file = [
LicenseExpression("licenseInfoInFileExpression")] if license_info_in_file is None else license_info_in_file
Expand All @@ -55,20 +68,19 @@ def file_fixture(name="fileName", spdx_id="fileId", checksums=None, file_type=No
contributors=contributors, attribution_texts=attribution_texts)


def package_fixture(spdx_id="packageId", name="packageName", download_location="downloadLocation",
version="packageVersion", file_name="packageFileName",
supplier=Actor(ActorType.PERSON, "supplierName"),
originator=Actor(ActorType.PERSON, "originatorName"), files_analyzed=True,
verification_code=PackageVerificationCode("verificationCode"), checksums=None,
homepage="packageHomepage", source_info="sourceInfo",
def package_fixture(spdx_id="SPDXRef-Package", name="packageName", download_location="https://download.com",
version="12.2", file_name="./packageFileName",
supplier=actor_fixture(name="supplierName"), originator=actor_fixture(name="originatorName"),
files_analyzed=True, verification_code=package_verification_code_fixture(), checksums=None,
homepage="https://homepage.com", source_info="sourceInfo",
license_concluded=LicenseExpression("packageLicenseConcluded"), license_info_from_files=None,
license_declared=LicenseExpression("packageLicenseDeclared"),
license_comment="packageLicenseComment", copyright_text="packageCopyrightText",
summary="packageSummary", description="packageDescription", comment="packageComment",
external_references=None, attribution_texts=None, primary_package_purpose=PackagePurpose.SOURCE,
release_date=datetime(2022, 12, 1), built_date=datetime(2022, 12, 2),
valid_until_date=datetime(2022, 12, 3)) -> Package:
checksums = [Checksum(ChecksumAlgorithm.SHA1, "packageSha1")] if checksums is None else checksums
checksums = [checksum_fixture()] if checksums is None else checksums
license_info_from_files = [
LicenseExpression("licenseInfoFromFile")] if license_info_from_files is None else license_info_from_files
external_references = [external_package_ref_fixture()] if external_references is None else external_references
Expand All @@ -83,20 +95,19 @@ def package_fixture(spdx_id="packageId", name="packageName", download_location="
release_date=release_date, built_date=built_date, valid_until_date=valid_until_date)


def external_document_ref_fixture(document_ref_id="externalDocumentRefId", document_uri="externalDocumentUri",
checksum=Checksum(ChecksumAlgorithm.MD5,
"externalDocumentRefMd5")) -> ExternalDocumentRef:
def external_document_ref_fixture(document_ref_id="DocumentRef-external", document_uri="https://namespace.com",
checksum=checksum_fixture()) -> ExternalDocumentRef:
return ExternalDocumentRef(document_ref_id=document_ref_id, document_uri=document_uri, checksum=checksum)


def external_package_ref_fixture(category=ExternalPackageRefCategory.PACKAGE_MANAGER,
reference_type="externalPackageRefType",
locator="externalPackageRefLocator",
reference_type="maven-central",
locator="org.apache.tomcat:tomcat:9.0.0.M4",
comment="externalPackageRefComment") -> ExternalPackageRef:
return ExternalPackageRef(category=category, reference_type=reference_type, locator=locator, comment=comment)


def snippet_fixture(spdx_id="snippetId", file_spdx_id="snippetFromFileId", byte_range=(1, 2),
def snippet_fixture(spdx_id="SPDXRef-Snippet", file_spdx_id="SPDXRef-File", byte_range=(1, 2),
line_range=(3, 4), concluded_license=LicenseExpression("snippetLicenseConcluded"),
license_info_in_snippet=None, license_comment="snippetLicenseComment",
copyright_text="licenseCopyrightText", comment="snippetComment", name="snippetName",
Expand All @@ -110,20 +121,26 @@ def snippet_fixture(spdx_id="snippetId", file_spdx_id="snippetFromFileId", byte_
attribution_texts=attribution_texts)


def annotation_fixture(spdx_id="annotatedElementId", annotation_type=AnnotationType.REVIEW,
annotator=Actor(ActorType.PERSON, "annotatorName"), annotation_date=datetime(2022, 12, 1),
def annotation_fixture(spdx_id="SPDXRef-File", annotation_type=AnnotationType.REVIEW,
annotator=actor_fixture(name="annotatorName"), annotation_date=datetime(2022, 12, 1),
annotation_comment="annotationComment") -> Annotation:
return Annotation(spdx_id=spdx_id, annotation_type=annotation_type, annotator=annotator,
annotation_date=annotation_date, annotation_comment=annotation_comment)


def extracted_licensing_info_fixture(license_id="licenseId", extracted_text="extractedText", license_name="licenseName",
def extracted_licensing_info_fixture(license_id="LicenseRef-1", extracted_text="extractedText", license_name="licenseName",
cross_references=None, comment="licenseComment") -> ExtractedLicensingInfo:
cross_references = ["crossReference"] if cross_references is None else cross_references
cross_references = ["https://see.also"] if cross_references is None else cross_references
return ExtractedLicensingInfo(license_id=license_id, extracted_text=extracted_text, license_name=license_name,
cross_references=cross_references, comment=comment)


def relationship_fixture(spdx_element_id="SPDXRef-DOCUMENT", relationship_type=RelationshipType.DESCRIBES,
related_spdx_element_id="SPDXRef-File", comment="relationshipComment") -> Relationship:
return Relationship(spdx_element_id=spdx_element_id, relationship_type=relationship_type,
related_spdx_element_id=related_spdx_element_id, comment=comment)


def document_fixture(creation_info=None, packages=None, files=None, snippets=None, annotations=None, relationships=None,
extracted_licensing_info=None) -> Document:
creation_info = creation_info_fixture() if creation_info is None else creation_info
Expand All @@ -137,9 +154,3 @@ def document_fixture(creation_info=None, packages=None, files=None, snippets=Non
return Document(creation_info=creation_info, packages=packages, files=files, snippets=snippets,
annotations=annotations, relationships=relationships,
extracted_licensing_info=extracted_licensing_info)


def relationship_fixture(spdx_element_id="relationshipOriginId", relationship_type=RelationshipType.DESCRIBES,
related_spdx_element_id="relationshipTargetId", comment="relationshipComment") -> Relationship:
return Relationship(spdx_element_id=spdx_element_id, relationship_type=relationship_type,
related_spdx_element_id=related_spdx_element_id, comment=comment)
5 changes: 3 additions & 2 deletions tests/jsonschema/test_document_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -172,7 +172,8 @@ def test_document_describes(converter: DocumentConverter):
described_by_document_relationship = relationship_fixture(related_spdx_element_id=document_id,
relationship_type=RelationshipType.DESCRIBED_BY,
spdx_element_id="describedById")
other_describes_relationship = relationship_fixture(relationship_type=RelationshipType.DESCRIBES)
other_describes_relationship = relationship_fixture(spdx_element_id="DocumentRef-external",
relationship_type=RelationshipType.DESCRIBES)
other_relationship = relationship_fixture(spdx_element_id=document_id, relationship_type=RelationshipType.CONTAINS)
document.relationships = [document_describes_relationship, described_by_document_relationship,
other_describes_relationship, other_relationship]
Expand Down
116 changes: 0 additions & 116 deletions tests/valid_defaults.py

This file was deleted.

8 changes: 4 additions & 4 deletions tests/validation/test_actor_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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", "[email protected]")
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="[email protected]"),
[(actor_fixture(actor_type=ActorType.TOOL, email="[email protected]"),
"email must be None if actor_type is TOOL, but is: [email protected]"),
])
def test_invalid_actor(actor, expected_message):
Expand Down
Loading