-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[issue-394] add tests for tag_value writer
Signed-off-by: Meret Behrens <meret.behrens@tngtech.com>
- Loading branch information
Showing
9 changed files
with
362 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# SPDX-FileCopyrightText: 2023 spdx contributors | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
from unittest.mock import MagicMock, call, mock_open, patch | ||
|
||
from spdx.writer.tagvalue.annotation_writer import write_annotation | ||
from tests.spdx.fixtures import annotation_fixture | ||
|
||
|
||
def test_annotation_writer(): | ||
annotation = annotation_fixture() | ||
|
||
mock: MagicMock = mock_open() | ||
with patch(f"{__name__}.open", mock, create=True): | ||
with open("foo", "w") as file: | ||
write_annotation(annotation, file) | ||
|
||
mock.assert_called_once_with("foo", "w") | ||
handle = mock() | ||
handle.write.assert_has_calls( | ||
[ | ||
call(f"Annotator: Person: {annotation.annotator.name} ({annotation.annotator.email})\n"), | ||
call("AnnotationDate: 2022-12-01T00:00:00Z\n"), | ||
call(f"AnnotationType: {annotation.annotation_type.name}\n"), | ||
call(f"SPDXREF: {annotation.spdx_id}\n"), | ||
call(f"AnnotationComment: {annotation.annotation_comment}\n"), | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# SPDX-FileCopyrightText: 2023 spdx contributors | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
import pytest | ||
|
||
from spdx.model.checksum import ChecksumAlgorithm | ||
from spdx.writer.tagvalue.checksum_writer import write_checksum_to_tag_value | ||
from tests.spdx.fixtures import checksum_fixture | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"checksum, expected_string", | ||
[ | ||
(checksum_fixture(), "SHA1: 71c4025dd9897b364f3ebbb42c484ff43d00791c"), | ||
(checksum_fixture(algorithm=ChecksumAlgorithm.SHA3_256, value="fdsef"), "SHA3-256: fdsef"), | ||
(checksum_fixture(algorithm=ChecksumAlgorithm.SHA3_384, value="fdsef"), "SHA3-384: fdsef"), | ||
(checksum_fixture(algorithm=ChecksumAlgorithm.SHA3_512, value="fdsef"), "SHA3-512: fdsef"), | ||
(checksum_fixture(algorithm=ChecksumAlgorithm.BLAKE2B_256, value="fdsef"), "BLAKE2b-256: fdsef"), | ||
(checksum_fixture(algorithm=ChecksumAlgorithm.BLAKE2B_384, value="fdsef"), "BLAKE2b-384: fdsef"), | ||
(checksum_fixture(algorithm=ChecksumAlgorithm.BLAKE2B_512, value="fdsef"), "BLAKE2b-512: fdsef"), | ||
], | ||
) | ||
def test_checksum_writer(checksum, expected_string): | ||
checksum_string = write_checksum_to_tag_value(checksum) | ||
|
||
assert checksum_string == expected_string |
28 changes: 28 additions & 0 deletions
28
tests/spdx/writer/tagvalue/test_extracted_licensing_info_writer.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# SPDX-FileCopyrightText: 2023 spdx contributors | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
from unittest.mock import MagicMock, call, mock_open, patch | ||
|
||
from spdx.writer.tagvalue.extracted_licensing_info_writer import write_extracted_licensing_info | ||
from tests.spdx.fixtures import extracted_licensing_info_fixture | ||
|
||
|
||
def test_extracted_licensing_info_writer(): | ||
extracted_licensing_info = extracted_licensing_info_fixture() | ||
|
||
mock: MagicMock = mock_open() | ||
with patch(f"{__name__}.open", mock, create=True): | ||
with open("foo", "w") as file: | ||
write_extracted_licensing_info(extracted_licensing_info, file) | ||
|
||
mock.assert_called_once_with("foo", "w") | ||
handle = mock() | ||
handle.write.assert_has_calls( | ||
[ | ||
call(f"LicenseID: {extracted_licensing_info.license_id}\n"), | ||
call(f"ExtractedText: {extracted_licensing_info.extracted_text}\n"), | ||
call(f"LicenseName: {extracted_licensing_info.license_name}\n"), | ||
call(f"LicenseCrossReference: {extracted_licensing_info.cross_references[0]}\n"), | ||
call(f"LicenseComment: {extracted_licensing_info.comment}\n"), | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# SPDX-FileCopyrightText: 2023 spdx contributors | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
from unittest.mock import MagicMock, call, mock_open, patch | ||
|
||
from spdx.writer.tagvalue.file_writer import write_file | ||
from tests.spdx.fixtures import file_fixture | ||
|
||
|
||
def test_file_writer(): | ||
spdx_file = file_fixture() | ||
|
||
mock: MagicMock = mock_open() | ||
with patch(f"{__name__}.open", mock, create=True): | ||
with open("foo", "w") as file: | ||
write_file(spdx_file, file) | ||
|
||
mock.assert_called_once_with("foo", "w") | ||
handle = mock() | ||
handle.write.assert_has_calls( | ||
[ | ||
call("## File Information\n"), | ||
call(f"FileName: {spdx_file.name}\n"), | ||
call(f"SPDXID: {spdx_file.spdx_id}\n"), | ||
call(f"FileType: {spdx_file.file_types[0].name}\n"), | ||
call("FileChecksum: SHA1: 71c4025dd9897b364f3ebbb42c484ff43d00791c\n"), | ||
call(f"LicenseConcluded: {spdx_file.license_concluded}\n"), | ||
call(f"LicenseInfoInFile: {spdx_file.license_info_in_file[0]}\n"), | ||
call(f"LicenseInfoInFile: {spdx_file.license_info_in_file[1]}\n"), | ||
call(f"LicenseInfoInFile: {spdx_file.license_info_in_file[2]}\n"), | ||
call(f"LicenseComments: {spdx_file.license_comment}\n"), | ||
call(f"FileCopyrightText: {spdx_file.copyright_text}\n"), | ||
call(f"FileComment: {spdx_file.comment}\n"), | ||
call(f"FileNotice: {spdx_file.notice}\n"), | ||
call(f"FileContributor: {spdx_file.contributors[0]}\n"), | ||
call(f"FileAttributionText: {spdx_file.attribution_texts[0]}\n"), | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# SPDX-FileCopyrightText: 2023 spdx contributors | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
from unittest.mock import MagicMock, call, mock_open, patch | ||
|
||
import pytest | ||
|
||
from spdx.model.spdx_no_assertion import SpdxNoAssertion | ||
from spdx.model.spdx_none import SpdxNone | ||
from spdx.writer.tagvalue.relationship_writer import write_relationship | ||
from tests.spdx.fixtures import relationship_fixture | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"relationship, expected_calls", | ||
[ | ||
( | ||
relationship_fixture(), | ||
[ | ||
call("Relationship: SPDXRef-DOCUMENT DESCRIBES SPDXRef-File\n"), | ||
call("RelationshipComment: relationshipComment\n"), | ||
], | ||
), | ||
( | ||
relationship_fixture(related_spdx_element_id=SpdxNoAssertion(), comment=None), | ||
[call("Relationship: SPDXRef-DOCUMENT DESCRIBES NOASSERTION\n")], | ||
), | ||
( | ||
relationship_fixture( | ||
spdx_element_id="DocumentRef-External:SPDXRef-DOCUMENT", | ||
related_spdx_element_id=SpdxNone(), | ||
comment=None, | ||
), | ||
[call("Relationship: DocumentRef-External:SPDXRef-DOCUMENT DESCRIBES NONE\n")], | ||
), | ||
], | ||
) | ||
def test_relationship_writer(relationship, expected_calls): | ||
mock: MagicMock = mock_open() | ||
with patch(f"{__name__}.open", mock, create=True): | ||
with open("foo", "w") as file: | ||
write_relationship(relationship, file) | ||
|
||
mock.assert_called_once_with("foo", "w") | ||
handle = mock() | ||
handle.write.assert_has_calls(expected_calls) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# SPDX-FileCopyrightText: 2023 spdx contributors | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
from unittest.mock import MagicMock, call, mock_open, patch | ||
|
||
from spdx.writer.tagvalue.snippet_writer import write_snippet | ||
from tests.spdx.fixtures import snippet_fixture | ||
|
||
|
||
def test_snippet_writer(): | ||
snippet = snippet_fixture() | ||
|
||
mock: MagicMock = mock_open() | ||
with patch(f"{__name__}.open", mock, create=True): | ||
with open("foo", "w") as file: | ||
write_snippet(snippet, file) | ||
|
||
mock.assert_called_once_with("foo", "w") | ||
handle = mock() | ||
handle.write.assert_has_calls( | ||
[ | ||
call("## Snippet Information\n"), | ||
call(f"SnippetSPDXID: {snippet.spdx_id}\n"), | ||
call(f"SnippetFromFileSPDXID: {snippet.file_spdx_id}\n"), | ||
call("SnippetByteRange: 1:2\n"), | ||
call("SnippetLineRange: 3:4\n"), | ||
call(f"SnippetLicenseConcluded: {snippet.license_concluded}\n"), | ||
call(f"LicenseInfoInSnippet: {snippet.license_info_in_snippet[0]}\n"), | ||
call(f"LicenseInfoInSnippet: {snippet.license_info_in_snippet[1]}\n"), | ||
call(f"LicenseInfoInSnippet: {snippet.license_info_in_snippet[2]}\n"), | ||
call(f"SnippetLicenseComments: {snippet.license_comment}\n"), | ||
call(f"SnippetCopyrightText: {snippet.copyright_text}\n"), | ||
call(f"SnippetComment: {snippet.comment}\n"), | ||
call(f"SnippetName: {snippet.name}\n"), | ||
call(f"SnippetAttributionText: {snippet.attribution_texts[0]}\n"), | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.