Skip to content

Commit

Permalink
[issue-397] replace valid_defaults.py usages with fixtures.py
Browse files Browse the repository at this point in the history
Signed-off-by: Armin Tänzer <armin.taenzer@tngtech.com>
  • Loading branch information
armintaenzertng committed Jan 5, 2023
1 parent f4c48f7 commit 35ed3d8
Show file tree
Hide file tree
Showing 15 changed files with 87 additions and 236 deletions.
2 changes: 1 addition & 1 deletion 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
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", "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):
Expand Down
14 changes: 5 additions & 9 deletions tests/validation/test_annotation_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 == []

Expand All @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion tests/validation/test_checksum_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 6 additions & 12 deletions tests/validation/test_creation_info_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,31 @@
# 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 == []


@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):
Expand Down
9 changes: 2 additions & 7 deletions tests/validation/test_document_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 == []
Expand Down
6 changes: 2 additions & 4 deletions tests/validation/test_external_document_ref_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 == []
12 changes: 5 additions & 7 deletions tests/validation/test_external_package_ref_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 4 additions & 6 deletions tests/validation/test_extracted_licensing_info_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,23 @@

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 == []


# 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):
Expand Down
Loading

0 comments on commit 35ed3d8

Please sign in to comment.