File: test_annotation_parser.py

package info (click to toggle)
python-spdx-tools 0.8.3-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,084 kB
  • sloc: python: 18,675; xml: 12,553; sh: 46; makefile: 7
file content (132 lines) | stat: -rw-r--r-- 4,896 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# SPDX-FileCopyrightText: 2022 spdx contributors
#
# SPDX-License-Identifier: Apache-2.0
import datetime
from unittest import TestCase

import pytest

from spdx_tools.spdx.constants import DOCUMENT_SPDX_ID
from spdx_tools.spdx.model import Actor, ActorType, Annotation, AnnotationType
from spdx_tools.spdx.parser.error import SPDXParsingError
from spdx_tools.spdx.parser.jsonlikedict.annotation_parser import AnnotationParser


def test_parse_annotation():
    annotation_parser = AnnotationParser()
    annotation_dict = {
        "annotationDate": "2010-01-29T18:30:22Z",
        "annotationType": "OTHER",
        "annotator": "Person: Jane Doe ()",
        "comment": "Document level annotation",
    }

    annotation = annotation_parser.parse_annotation(annotation_dict, spdx_id=DOCUMENT_SPDX_ID)

    assert annotation.annotator == Actor(ActorType.PERSON, name="Jane Doe")
    assert annotation.annotation_type == AnnotationType.OTHER
    assert annotation.annotation_date == datetime.datetime(2010, 1, 29, 18, 30, 22)
    assert annotation.annotation_comment == "Document level annotation"
    assert annotation.spdx_id == DOCUMENT_SPDX_ID


def test_parse_all_annotations():
    annotation_parser = AnnotationParser()
    doc_dict = {
        "SPDXID": DOCUMENT_SPDX_ID,
        "packages": [
            {
                "SPDXID": "SPDXRef-Package",
                "annotations": [
                    {
                        "annotationDate": "2010-01-29T17:30:22Z",
                        "annotationType": "REVIEW",
                        "annotator": "Person: Mick Doe ()",
                        "comment": "Package level annotation",
                    }
                ],
            }
        ],
        "files": [
            {
                "SPDXID": "SPDXRef-File",
                "annotations": [
                    {
                        "annotationDate": "2010-01-29T18:30:22Z",
                        "annotationType": "OTHER",
                        "annotator": "Person: Jane Doe ()",
                        "comment": "File level annotation",
                    }
                ],
            }
        ],
        "snippets": [
            {
                "SPDXID": "SPDXRef-Snippet",
                "annotations": [
                    {
                        "annotationDate": "2022-01-29T18:30:32Z",
                        "annotationType": "REVIEW",
                        "annotator": "Person: Jonas Rie (jonas@example.com)",
                        "comment": "Snippet level annotation",
                    }
                ],
            }
        ],
        "revieweds": [
            {"reviewDate": "2010-01-29T18:30:22Z", "reviewer": "Person: Jane Doe ()", "comment": "Review annotation"}
        ],
    }

    annotations = annotation_parser.parse_all_annotations(input_doc_dict=doc_dict)

    assert len(annotations) == 4
    test_case = TestCase()
    test_case.maxDiff = None
    test_case.assertCountEqual(
        annotations,
        [
            Annotation(
                spdx_id=DOCUMENT_SPDX_ID,
                annotation_type=AnnotationType.REVIEW,
                annotator=Actor(actor_type=ActorType.PERSON, name="Jane Doe", email=None),
                annotation_date=datetime.datetime(2010, 1, 29, 18, 30, 22),
                annotation_comment="Review annotation",
            ),
            Annotation(
                spdx_id="SPDXRef-Package",
                annotation_type=AnnotationType.REVIEW,
                annotator=Actor(actor_type=ActorType.PERSON, name="Mick Doe", email=None),
                annotation_date=datetime.datetime(2010, 1, 29, 17, 30, 22),
                annotation_comment="Package level annotation",
            ),
            Annotation(
                spdx_id="SPDXRef-File",
                annotation_type=AnnotationType.OTHER,
                annotator=Actor(actor_type=ActorType.PERSON, name="Jane Doe", email=None),
                annotation_date=datetime.datetime(2010, 1, 29, 18, 30, 22),
                annotation_comment="File level annotation",
            ),
            Annotation(
                spdx_id="SPDXRef-Snippet",
                annotation_type=AnnotationType.REVIEW,
                annotator=Actor(actor_type=ActorType.PERSON, name="Jonas Rie", email="jonas@example.com"),
                annotation_date=datetime.datetime(2022, 1, 29, 18, 30, 32),
                annotation_comment="Snippet level annotation",
            ),
        ],
    )


@pytest.mark.parametrize(
    "incomplete_annotation_dict",
    [
        {"annotator": "Person: Jane Doe ()"},
        {"annotationDate": "2010-01-29T18:30:22Z"},
    ],
)
def test_parse_incomplete_annotation(incomplete_annotation_dict):
    annotation_parser = AnnotationParser()

    with pytest.raises(SPDXParsingError):
        annotation_parser.parse_annotation(incomplete_annotation_dict)