Skip to content
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

[issue-389] allow NONE and NOASSERTION in related_spdx_element_id #390

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 6 additions & 4 deletions src/model/relationship.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from enum import auto, Enum
from typing import Optional
from typing import Optional, Union

from src.model.spdx_no_assertion import SpdxNoAssertion
from src.model.spdx_none import SpdxNone
from src.model.typing.dataclass_with_properties import dataclass_with_properties
from src.model.typing.type_checks import check_types_and_set_values

Expand Down Expand Up @@ -67,9 +69,9 @@ class RelationshipType(Enum):
class Relationship:
spdx_element_id: str
relationship_type: RelationshipType
related_spdx_element_id: str
related_spdx_element_id: Union[str, SpdxNone, SpdxNoAssertion]
comment: Optional[str] = None

def __init__(self, spdx_element_id: str, relationship_type: RelationshipType, related_spdx_element_id: str,
comment: Optional[str] = None):
def __init__(self, spdx_element_id: str, relationship_type: RelationshipType,
related_spdx_element_id: Union[str, SpdxNone, SpdxNoAssertion], comment: Optional[str] = None):
check_types_and_set_values(self, locals())
4 changes: 2 additions & 2 deletions src/parser/json/relationship_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from src.parser.error import SPDXParsingError
from src.parser.json.dict_parsing_functions import raise_parsing_error_if_logger_has_messages, json_str_to_enum_name, \
construct_or_raise_parsing_error, \
parse_field_or_log_error, parse_list_of_elements
parse_field_or_log_error, parse_field_or_no_assertion_or_none
from src.parser.logger import Logger


Expand Down Expand Up @@ -58,7 +58,7 @@ def parse_all_relationships(self, input_doc_dict: Dict) -> List[Relationship]:
def parse_relationship(self, relationship_dict: Dict) -> Relationship:
logger = Logger()
spdx_element_id: Optional[str] = relationship_dict.get("spdxElementId")
related_spdx_element: Optional[str] = relationship_dict.get("relatedSpdxElement")
related_spdx_element: Optional[str] = parse_field_or_no_assertion_or_none(relationship_dict.get("relatedSpdxElement"))
relationship_type: Optional[RelationshipType] = parse_field_or_log_error(logger, relationship_dict.get(
"relationshipType"), self.parse_relationship_type)
relationship_comment: Optional[str] = relationship_dict.get("comment")
Expand Down
14 changes: 9 additions & 5 deletions src/validation/relationship_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import List
from typing import List, Union

from src.model.document import Document
from src.model.relationship import Relationship, RelationshipType
from src.model.spdx_no_assertion import SpdxNoAssertion
from src.model.spdx_none import SpdxNone
from src.validation.spdx_id_validators import validate_spdx_id
from src.validation.validation_message import ValidationMessage, ValidationContext, SpdxElementType

Expand All @@ -30,12 +32,14 @@ def validate_relationship(relationship: Relationship, document: Document, spdx_v
context = ValidationContext(element_type=SpdxElementType.RELATIONSHIP,
full_element=relationship)

first_id: str = relationship.spdx_element_id
second_id: str = relationship.related_spdx_element_id
relationship_type: RelationshipType = relationship.relationship_type

for spdx_id in [first_id, second_id]:
messages: List[str] = validate_spdx_id(spdx_id, document, check_document=True)
messages: List[str] = validate_spdx_id(relationship.spdx_element_id, document, check_document=True)
for message in messages:
validation_messages.append(ValidationMessage(message, context))

if relationship.related_spdx_element_id not in [SpdxNone(), SpdxNoAssertion()]:
messages: List[str] = validate_spdx_id(relationship.related_spdx_element_id, document, check_document=True)
for message in messages:
validation_messages.append(ValidationMessage(message, context))

Expand Down
7 changes: 4 additions & 3 deletions tests/model/test_relationship.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import pytest

from src.model.relationship import Relationship, RelationshipType
from src.model.spdx_no_assertion import SpdxNoAssertion


def test_correct_initialization():
relationship = Relationship("id", RelationshipType.OTHER, "other_id", "comment")
relationship = Relationship("id", RelationshipType.OTHER, SpdxNoAssertion(), "comment")
assert relationship.spdx_element_id == "id"
assert relationship.relationship_type == RelationshipType.OTHER
assert relationship.related_spdx_element_id == "other_id"
assert relationship.related_spdx_element_id == SpdxNoAssertion()
assert relationship.comment == "comment"


def test_wrong_type_in_spdx_element_id():
with pytest.raises(TypeError):
Relationship(42, RelationshipType.OTHER, "other_id")
Relationship(SpdxNoAssertion(), RelationshipType.OTHER, "other_id")


def test_wrong_type_in_relationship_type():
Expand Down
5 changes: 3 additions & 2 deletions tests/parser/test_relationship_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import pytest

from src.model.relationship import RelationshipType, Relationship
from src.model.spdx_no_assertion import SpdxNoAssertion
from src.parser.error import SPDXParsingError
from src.parser.json.relationship_parser import RelationshipParser

Expand All @@ -23,15 +24,15 @@ def test_parse_relationship():
relationship_dict = {
"spdxElementId": "SPDXRef-DOCUMENT",
"relationshipType": "CONTAINS",
"relatedSpdxElement": "SPDXRef-Package",
"relatedSpdxElement": "NOASSERTION",
"comment": "Comment."
}

relationship = relationship_parser.parse_relationship(relationship_dict)

assert relationship.relationship_type == RelationshipType.CONTAINS
assert relationship.spdx_element_id == "SPDXRef-DOCUMENT"
assert relationship.related_spdx_element_id == "SPDXRef-Package"
assert relationship.related_spdx_element_id == SpdxNoAssertion()
assert relationship.comment == "Comment."


Expand Down
8 changes: 6 additions & 2 deletions tests/validation/test_relationship_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,19 @@

from src.model.document import Document
from src.model.relationship import Relationship, RelationshipType
from src.model.spdx_no_assertion import SpdxNoAssertion
from src.model.spdx_none import SpdxNone
from src.validation.relationship_validator import validate_relationship
from src.validation.validation_message import ValidationMessage, SpdxElementType, ValidationContext
from tests.valid_defaults import get_document, get_package, get_relationship, get_file


def test_valid_relationship():
@pytest.mark.parametrize("related_spdx_element",
["SPDXRef-Package", SpdxNoAssertion(), SpdxNone()])
def test_valid_relationship(related_spdx_element):
document: Document = get_document(packages=[get_package(spdx_id="SPDXRef-Package")])

relationship = Relationship("SPDXRef-DOCUMENT", RelationshipType.AMENDS, "SPDXRef-Package", comment="comment")
relationship = Relationship("SPDXRef-DOCUMENT", RelationshipType.AMENDS, related_spdx_element, comment="comment")
validation_messages: List[ValidationMessage] = validate_relationship(relationship, document, "2.3")

assert validation_messages == []
Expand Down