|
| 1 | +# Copyright (c) 2022 spdx contributors |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# Unless required by applicable law or agreed to in writing, software |
| 7 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 8 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 9 | +# See the License for the specific language governing permissions and |
| 10 | +# limitations under the License. |
| 11 | +import json |
| 12 | +import os |
| 13 | +from datetime import datetime |
| 14 | +from pathlib import Path |
| 15 | + |
| 16 | +import pytest |
| 17 | + |
| 18 | +from src.model.actor import Actor, ActorType |
| 19 | +from src.model.annotation import Annotation, AnnotationType |
| 20 | +from src.model.checksum import ChecksumAlgorithm, Checksum |
| 21 | +from src.model.document import CreationInfo, Document |
| 22 | +from src.model.external_document_ref import ExternalDocumentRef |
| 23 | +from src.model.extracted_licensing_info import ExtractedLicensingInfo |
| 24 | +from src.model.file import File |
| 25 | +from src.model.package import Package |
| 26 | +from src.model.relationship import RelationshipType, Relationship |
| 27 | +from src.model.snippet import Snippet |
| 28 | +from src.model.spdx_none import SpdxNone |
| 29 | +from src.writer.json.json_writer import JsonWriter |
| 30 | + |
| 31 | + |
| 32 | +@pytest.fixture |
| 33 | +def temporary_file_path() -> str: |
| 34 | + temporary_file_path = "temp_test_json_writer_output.json" |
| 35 | + yield temporary_file_path |
| 36 | + os.remove(temporary_file_path) |
| 37 | + |
| 38 | + |
| 39 | +def test_write_json(temporary_file_path: str): |
| 40 | + writer = JsonWriter() |
| 41 | + creation_info = CreationInfo("spdxVersion", "documentId", "documentName", "documentNamespace", |
| 42 | + [Actor(ActorType.TOOL, "tools-python", "tools-python@github.com")], |
| 43 | + datetime(2022, 12, 1), document_comment="comment", data_license="dataLicense", |
| 44 | + external_document_refs=[ExternalDocumentRef("docRefId", "externalDocumentUri", |
| 45 | + Checksum(ChecksumAlgorithm.SHA1, |
| 46 | + "externalRefSha1"))]) |
| 47 | + package = Package("packageId", "packageName", SpdxNone()) |
| 48 | + file = File("fileName", "fileId", [Checksum(ChecksumAlgorithm.SHA1, "fileSha1")]) |
| 49 | + snippet = Snippet("snippetId", "snippetFileId", (1, 2)) |
| 50 | + document = Document(creation_info, annotations=[ |
| 51 | + Annotation("documentId", AnnotationType.REVIEW, Actor(ActorType.PERSON, "reviewerName"), |
| 52 | + datetime(2022, 12, 2), "reviewComment"), |
| 53 | + Annotation("fileId", AnnotationType.OTHER, Actor(ActorType.TOOL, "toolName"), datetime(2022, 12, 3), |
| 54 | + "otherComment")], |
| 55 | + extracted_licensing_info=[ExtractedLicensingInfo("licenseId", "licenseText")], relationships=[ |
| 56 | + Relationship(creation_info.spdx_id, RelationshipType.DESCRIBES, "packageId"), |
| 57 | + Relationship(creation_info.spdx_id, RelationshipType.DESCRIBES, "fileId", "relationshipComment"), |
| 58 | + Relationship("relationshipOriginId", RelationshipType.AMENDS, "relationShipTargetId")], packages=[package], |
| 59 | + files=[file], snippets=[snippet]) |
| 60 | + writer.write_document(document, temporary_file_path) |
| 61 | + |
| 62 | + with open(temporary_file_path) as written_file: |
| 63 | + written_json = json.load(written_file) |
| 64 | + |
| 65 | + with open(os.path.join(os.path.dirname(__file__), 'expected_results', 'expected.json')) as expected_file: |
| 66 | + expected_json = json.load(expected_file) |
| 67 | + |
| 68 | + assert written_json == expected_json |
0 commit comments