File: test_parse_from_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 (70 lines) | stat: -rw-r--r-- 2,727 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
# SPDX-FileCopyrightText: 2022 spdx contributors
#
# SPDX-License-Identifier: Apache-2.0

import os

import pytest

from spdx_tools.spdx.model import Document
from spdx_tools.spdx.parser.json import json_parser
from spdx_tools.spdx.parser.rdf import rdf_parser
from spdx_tools.spdx.parser.tagvalue import tagvalue_parser
from spdx_tools.spdx.parser.xml import xml_parser
from spdx_tools.spdx.parser.yaml import yaml_parser


@pytest.mark.parametrize(
    "parser, format_name, extension",
    [
        (json_parser, "JSON", ".json"),
        (xml_parser, "XML", ".xml"),
        (yaml_parser, "YAML", ".yaml"),
        (rdf_parser, "Rdf", ".rdf.xml"),
        (tagvalue_parser, "Tag", ""),
    ],
)
class TestParseFromFile:
    def test_parse_from_file_not_found(self, parser, format_name, extension):
        with pytest.raises(FileNotFoundError) as err:
            wrong_file_path = os.path.join(os.path.dirname(__file__), f"hnjfkjsedhnflsiafg.spdx{extension}")
            parser.parse_from_file(wrong_file_path)

        assert err.value.args[1] == "No such file or directory"

    def test_parse_from_file_with_2_3_example(self, parser, format_name, extension):
        doc = parser.parse_from_file(
            os.path.join(os.path.dirname(__file__), f"../../data/SPDX{format_name}Example-v2.3.spdx{extension}")
        )
        assert isinstance(doc, Document)
        assert len(doc.annotations) == 5
        assert len(doc.files) == 5
        assert len(doc.packages) == 4
        assert len(doc.snippets) == 1
        assert len(doc.relationships) == 13
        assert len(doc.extracted_licensing_info) == 5

    def test_parse_from_file_with_2_2_example(self, parser, format_name, extension):
        doc = parser.parse_from_file(
            os.path.join(os.path.dirname(__file__), f"../../data/SPDX{format_name}Example-v2.2.spdx{extension}")
        )
        assert isinstance(doc, Document)
        assert len(doc.annotations) == 5
        assert len(doc.files) == 4
        assert len(doc.packages) == 4
        assert len(doc.snippets) == 1
        assert len(doc.relationships) == 11
        assert len(doc.extracted_licensing_info) == 5

    def test_parse_from_file_with_encoding_example(self, parser, format_name, extension):
        doc = parser.parse_from_file(
            os.path.join(os.path.dirname(__file__), f"../../data/SPDX{format_name}Example-UTF-16.spdx{extension}"),
            "utf-16",
        )
        assert isinstance(doc, Document)
        assert len(doc.annotations) == 5
        assert len(doc.files) == 4
        assert len(doc.packages) == 4
        assert len(doc.snippets) == 1
        assert len(doc.relationships) == 11
        assert len(doc.extracted_licensing_info) == 5