8000 [Issue 305] add new json parser by meretp · Pull Request #366 · spdx/tools-python · GitHub
[go: up one dir, main page]

Skip to content

[Issue 305] add new json parser #366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Dec 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
296816b
[issue-305] add new parser
meretp Nov 25, 2022
cd095e2
[issue-305, refactor] add method to construct an object and raise SPD…
meretp Dec 14, 2022
f2f91fd
[issue-305, refactor] annotation_parser: extract methods to improve r…
meretp Dec 14, 2022
190cd5a
[issue-305, refactor] add methods to parse required/ optional fields …
meretp Dec 14, 2022
2834000
[issue-305, refactor] relationship_parser: extract dict to invert rel…
meretp Dec 14, 2022
6297673
[issue-305, refactor] add method to raise error if logger has message…
meretp Dec 14, 2022
4741a43
[issue-305, review] refactor methods in dict_parsing_functions.py, sm…
meretp Dec 15, 2022
080d848
[issue-305, refactor] json_parser
meretp Dec 15, 2022
5826922
[issue-305, reformat]
meretp Dec 19, 2022
e6332cb
[issue-305] add testcases and update license_expression parser
meretp Dec 19, 2022
1f6d5b6
[issue-305, refactor] delete duplicated check for error type
meretp Dec 20, 2022
fc980b1
[issue-305, review] fix messages, naming, type hints
meretp Dec 21, 2022
3fe3e11
[issue-305, review] refactor relationship_parser
meretp Dec 21, 2022
0be1780
[issue-305, review] refactor snippet_parser
meretp Dec 21, 2022
c5b8d3c
[issue-305, review] make naming consistent
meretp Dec 21, 2022
03cce38
[issue-305, review] add test for dict parsing functions and catch Val…
meretp Dec 21, 2022
2dcd125
[issue-305, review] add None handling for required fields
meretp Dec 21, 2022
50c3038
[issue-305, review] make error messages consistent, add test for json…
meretp Dec 28, 2022
562f288
[issue-305, review] add tests, change test data, naming of tests and …
meretp Dec 22, 2022
a722036
[issue-305, review] add method to parse fields that can be SpdxNone o…
meretp Dec 22, 2022
c8851d8
[issue-305, review] refactor parse_field_or_log_error
meretp Dec 22, 2022
347051a
[issue-305, review] reformat, type hints, fix typos, error messages
meretp Dec 28, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[issue-305] add testcases and update license_expression parser
Signed-off-by: Meret Behrens <meret.behrens@tngtech.com>
  • Loading branch information
meretp committed Dec 28, 2022
commit e6332cbcc2fc38b832833cec3c84e4a6b45e4127
36 changes: 28 additions & 8 deletions src/parser/json/license_expression_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,37 @@
from src.model.license_expression import LicenseExpression
from src.model.spdx_no_assertion import SpdxNoAssertion
from src.model.spdx_none import SpdxNone
from src.parser.json.dict_parsing_functions import construct_or_raise_parsing_error
from src.parser.error import SPDXParsingError
from src.parser.json.dict_parsing_functions import construct_or_raise_parsing_error, append_parsed_field_or_log_error, \
raise_parsing_error_if_logger_has_messages
from src.parser.logger import Logger


class LicenseExpressionParser:
def parse_license_expression(self, license_expression: Union[str, List[str]]) -> Union[LicenseExpression, SpdxNoAssertion, SpdxNone, List[LicenseExpression]]:
if license_expression == SpdxNone().__str__():
def parse_license_expression(self, license_expression_str_or_list: Union[str, List[str]]) -> Union[
LicenseExpression, SpdxNoAssertion, SpdxNone, List[LicenseExpression]]:
if license_expression_str_or_list == SpdxNone().__str__():
return SpdxNone()
if license_expression == SpdxNoAssertion().__str__():
if license_expression_str_or_list == SpdxNoAssertion().__str__():
return SpdxNoAssertion()
elif isinstance(license_expression, str):
license_expression = construct_or_raise_parsing_error(LicenseExpression, dict(expression_string=license_expression))
elif isinstance(license_expression_str_or_list, list):
return self.parse_license_expressions(license_expression_str_or_list)

else:
license_expression = construct_or_raise_parsing_error(LicenseExpression,
dict(
expression_string=license_expression_str_or_list))
return license_expression
elif isinstance(license_expression, list):
return list(map(self.parse_license_expression, license_expression))

def parse_license_expressions(self, license_expression_str_or_list: List[str]) -> List[LicenseExpression]:
license_expressions = []
logger = Logger()
for license_expression_str in license_expression_str_or_list:
try:
license_expressions = append_parsed_field_or_log_error(logger, license_expressions,
license_expression_str,
self.parse_license_expression)
except SPDXParsingError as err:
logger.append(err.get_messages())
raise_parsing_error_if_logger_has_messages(logger)
return license_expressions
29 changes: 21 additions & 8 deletions tests/parser/test_actor_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,34 @@
from src.parser.json.actor_parser import ActorParser


def test_actor_parser():
@pytest.mark.parametrize("actor_string,expected_type,expected_name,expected_mail", [
("Person: Jane Doe (jane.doe@example.com)", ActorType.PERSON, "Jane Doe", "jane.doe@example.com"),
("Organization: Example organization (organization@exaple.com)", ActorType.ORGANIZATION, "Example organization",
"organization@exaple.com"),
("Organization: Example organization ( )", ActorType.ORGANIZATION, "Example organization", None),
("Tool: Example tool ", ActorType.TOOL, "Example tool", None)])
def test_actor_parser(actor_string, expected_type, expected_name, expected_mail):
actor_parser = ActorParser()
actor_string = "Person: Jane Doe (jane.doe@example.com)"

actor = actor_parser.parse_actor(actor_string)

assert actor.actor_type == ActorType.PERSON
assert actor.name == "Jane Doe"
assert actor.email == "jane.doe@example.com"
assert actor.actor_type == expected_type
assert actor.name == expected_name
assert actor.email == expected_mail

def test_invalid_actor():

@pytest.mark.parametrize("actor_string,expected_message", [
("Perso: Jane Doe (jane.doe@example.com)",
"Actor Perso: Jane Doe (jane.doe@example.com) doesn't match any of person, organization or tool."),
("Toole Example Tool ()",
"Actor Toole Example Tool () doesn't match any of person, organization or tool.")
])
def test_invalid_actor(actor_string, expected_message):
actor_parser = ActorParser()
actor_string = "Perso: Jane Doe (jane.doe@example.com)"
actor_string = actor_string

with pytest.raises(SPDXParsingError) as err:
_ = actor_parser.parse_actor(actor_string)
assert err.typename == 'SPDXParsingError'
assert err.value.messages[0] == "Actor Perso: Jane Doe (jane.doe@example.com) doesn't match any of person, organization or tool."
assert err.value.messages[
0] == expected_message
11 changes: 11 additions & 0 deletions tests/parser/test_checksum_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,14 @@ def test_invalid_checksum():
assert err.typename == 'SPDXParsingError'
assert err.value.messages[0] == "Error while parsing Checksum: ['Algorithm SHA not valid for checksum.']"

def test_incomplete_checksum():
checksum_parser = ChecksumParser()
checksum_dict= {
"algorithm": "SHA1"
}

with pytest.raises(SPDXParsingError) as err:
_ = checksum_parser.parse_checksum(checksum_dict)

assert err.type == SPDXParsingError
assert err.value.messages == ["Error while constructing Checksum: ['SetterError Checksum: type of argument \"value\" must be str; got NoneType instead: None']"]
46 changes: 41 additions & 5 deletions tests/parser/test_license_expression_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,70 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import pytest

from src.model.license_expression import LicenseExpression
from src.model.spdx_no_assertion import SpdxNoAssertion
from src.model.spdx_none import SpdxNone
from src.parser.error import SPDXParsingError
from src.parser.json.license_expression_parser import LicenseExpressionParser


def test_license_expression_parser():
license_expression_parser = LicenseExpressionParser()
license_expression_str= "License-Ref1"
license_expression_str = "License-Ref1"

license_expression = license_expression_parser.parse_license_expression(license_expression=license_expression_str)
license_expression = license_expression_parser.parse_license_expression(
license_expression_str_or_list=license_expression_str)

assert license_expression.expression_string == "License-Ref1"


def test_license_expression_no_assert():
license_expression_parser = LicenseExpressionParser()
license_expression_str= "NOASSERTION"
license_expression_str = "NOASSERTION"

spdx_no_assertion = license_expression_parser.parse_license_expression(license_expression=license_expression_str)
spdx_no_assertion = license_expression_parser.parse_license_expression(
license_expression_str_or_list=license_expression_str)

assert type(spdx_no_assertion) == SpdxNoAssertion


def test_license_expression_none():
license_expression_parser = LicenseExpressionParser()
license_expression_str = "NONE"

spdx_none = license_expression_parser.parse_license_expression(
license_expression=license_expression_str)
license_expression_str_or_list=license_expression_str)

assert type(spdx_none) == SpdxNone


@pytest.mark.parametrize("invalid_license_expression,expected_message",
[(56, ["Error while constructing LicenseExpression: ['SetterError LicenseExpression: "
'type of argument "expression_string" must be str; got int instead: 56\']']
),
(["First Expression", 4, 6],
["Error while constructing LicenseExpression: ['SetterError LicenseExpression: "
'type of argument "expression_string" must be str; got int instead: 4\']',
"Error while constructing LicenseExpression: ['SetterError LicenseExpression: "
'type of argument "expression_string" must be str; got int instead: 6\']'])])
def test_invalid_license_expression(invalid_license_expression, expected_message):
license_expression_parser = LicenseExpressionParser()

with pytest.raises(SPDXParsingError) as err:
_ = license_expression_parser.parse_license_expression(invalid_license_expression)

assert err.type == SPDXParsingError
assert err.value.messages == expected_message


def test_license_expressions():
license_expression_parser = LicenseExpressionParser()
license_expressions_list = ["First License", "Second License", "Third License"]

license_expressions = license_expression_parser.parse_license_expression(license_expressions_list)

assert len(license_expressions) == 3
assert license_expressions == [LicenseExpression("First License"), LicenseExpression("Second License"),
LicenseExpression("Third License")]
0