File: test_file.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 (128 lines) | stat: -rw-r--r-- 4,247 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
# SPDX-FileCopyrightText: 2023 spdx contributors
#
# SPDX-License-Identifier: Apache-2.0

from unittest import mock

import pytest

from spdx_tools.spdx.model import Checksum, ChecksumAlgorithm, File, FileType, SpdxNoAssertion, SpdxNone


@mock.patch("spdx_tools.spdx.model.Checksum", autospec=True)
def test_correct_initialization(checksum):
    file = File(
        "name",
        "id",
        [checksum, checksum],
        [FileType.OTHER, FileType.SPDX],
        SpdxNone(),
        [SpdxNoAssertion()],
        "comment on license",
        "copyright",
        "comment",
        "notice",
        ["contributor"],
        ["attribution"],
    )
    assert file.name == "name"
    assert file.spdx_id == "id"
    assert file.checksums == [checksum, checksum]
    assert file.file_types == [FileType.OTHER, FileType.SPDX]
    assert file.license_concluded == SpdxNone()
    assert file.license_info_in_file == [SpdxNoAssertion()]
    assert file.license_comment == "comment on license"
    assert file.copyright_text == "copyright"
    assert file.comment == "comment"
    assert file.notice == "notice"
    assert file.contributors == ["contributor"]
    assert file.attribution_texts == ["attribution"]


@mock.patch("spdx_tools.spdx.model.Checksum", autospec=True)
def test_correct_initialization_with_default_values(checksum):
    file = File("name", "id", [checksum, checksum])
    assert file.name == "name"
    assert file.spdx_id == "id"
    assert file.checksums == [checksum, checksum]
    assert file.file_types == []
    assert file.license_concluded is None
    assert file.license_info_in_file == []
    assert file.license_comment is None
    assert file.copyright_text is None
    assert file.comment is None
    assert file.notice is None
    assert file.contributors == []
    assert file.attribution_texts == []


@mock.patch("spdx_tools.spdx.model.Checksum", autospec=True)
def test_wrong_type_in_name(checksum):
    with pytest.raises(TypeError):
        File(42, "id", [checksum])


@mock.patch("spdx_tools.spdx.model.Checksum", autospec=True)
def test_wrong_type_in_spdx_id(checksum):
    with pytest.raises(TypeError):
        File("name", 42, [checksum])


def test_wrong_type_in_checksum():
    checksum = Checksum(ChecksumAlgorithm.BLAKE2B_256, "value")
    with pytest.raises(TypeError):
        File("name", "id", checksum)


@mock.patch("spdx_tools.spdx.model.Checksum", autospec=True)
def test_wrong_type_in_file_type(checksum):
    with pytest.raises(TypeError):
        File("name", "id", [checksum], file_types=FileType.OTHER)


@mock.patch("spdx_tools.spdx.model.Checksum", autospec=True)
def test_wrong_type_in_license_concluded(checksum):
    with pytest.raises(TypeError):
        File("name", "id", [checksum], license_concluded="NONE")


@mock.patch("spdx_tools.spdx.model.Checksum", autospec=True)
def test_wrong_type_in_license_info_in_file(checksum):
    with pytest.raises(TypeError):
        File("name", "id", [checksum], license_info_in_file=[SpdxNone])


@mock.patch("spdx_tools.spdx.model.Checksum", autospec=True)
def test_wrong_type_in_license_comment(checksum):
    with pytest.raises(TypeError):
        File("name", "id", [checksum], license_comment=42)


@mock.patch("spdx_tools.spdx.model.Checksum", autospec=True)
def test_wrong_type_in_copyright_text(checksum):
    with pytest.raises(TypeError):
        File("name", "id", [checksum], copyright_text=[SpdxNone()])


@mock.patch("spdx_tools.spdx.model.Checksum", autospec=True)
def test_wrong_type_in_comment(checksum):
    with pytest.raises(TypeError):
        File("name", "id", [checksum], comment=42)


@mock.patch("spdx_tools.spdx.model.Checksum", autospec=True)
def test_wrong_type_in_notice(checksum):
    with pytest.raises(TypeError):
        File("name", "id", [checksum], notice=["notice"])


@mock.patch("spdx_tools.spdx.model.Checksum", autospec=True)
def test_wrong_type_in_contributors(checksum):
    with pytest.raises(TypeError):
        File("name", "id", [checksum], contributors="contributor")


@mock.patch("spdx_tools.spdx.model.Checksum", autospec=True)
def test_wrong_type_in_attribution_texts(checksum):
    with pytest.raises(TypeError):
        File("name", "id", [checksum], attribution_texts=[41, 42])