Skip to content

Commit

Permalink
Refine get_expression_as_spdx and add unti test #63
Browse files Browse the repository at this point in the history
Signed-off-by: tdruez <tdruez@nexb.com>
  • Loading branch information
tdruez committed Jun 28, 2024
1 parent 111edd7 commit f334131
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
8 changes: 7 additions & 1 deletion component_catalog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from cyclonedx.model import component as cyclonedx_component
from cyclonedx.model import contact as cyclonedx_contact
from cyclonedx.model import license as cyclonedx_license
from license_expression import ExpressionError
from packageurl import PackageURL
from packageurl.contrib import purl2url
from packageurl.contrib import url2purl
Expand Down Expand Up @@ -220,8 +221,13 @@ def _get_primary_license(self):
primary_license = cached_property(_get_primary_license)

def get_expression_as_spdx(self, expression):
if expression:
if not expression:
return

try:
return get_expression_as_spdx(expression, self.dataspace)
except ExpressionError as e:
return str(e)

@property
def concluded_license_expression_spdx(self):
Expand Down
28 changes: 27 additions & 1 deletion component_catalog/tests/test_license_expression_dje.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,42 @@
import os
from collections import namedtuple
from itertools import zip_longest
from unittest import TestCase

from django.core.exceptions import ValidationError
from django.test import TestCase

from license_expression import ExpressionError
from license_expression import LicenseSymbolLike
from license_expression import ParseError
from license_expression import as_symbols

from component_catalog.license_expression_dje import build_licensing
from component_catalog.license_expression_dje import get_expression_as_spdx
from component_catalog.license_expression_dje import get_license_objects
from component_catalog.license_expression_dje import get_unique_license_keys
from component_catalog.license_expression_dje import normalize_and_validate_expression
from component_catalog.license_expression_dje import parse_expression
from dje.models import Dataspace
from license_library.models import License
from organization.models import Owner

MockLicense = namedtuple("MockLicense", "key aliases is_exception")


class LicenseExpressionDjeTestCase(TestCase):
def setUp(self):
self.dataspace = Dataspace.objects.create(name="Starship")
self.owner = Owner.objects.create(name="Owner", dataspace=self.dataspace)

self.license1 = License.objects.create(
key="apache-2.0",
name="Apache License 2.0",
short_name="Apache 2.0",
spdx_license_key="Apache-2.0",
dataspace=self.dataspace,
owner=self.owner,
)

def test_as_symbols(self):
lic1 = MockLicense("x11", ["X11 License"], True)
lic2 = MockLicense("x11-xconsortium", ["X11 XConsortium"], False)
Expand Down Expand Up @@ -263,6 +281,14 @@ def test_get_unique_license_keys(self):
expected = {"lgpl", "oracle-bcl", "gps-2.0", "classpath"}
self.assertEqual(expected, get_unique_license_keys(expression))

def test_get_expression_as_spdx(self):
expression = "apache-2.0"
expected = "Apache-2.0"
self.assertEqual(expected, get_expression_as_spdx(expression, self.dataspace))

with self.assertRaises(ExpressionError):
get_expression_as_spdx("unknown", self.dataspace)


def _print_sequence_diff(left, right):
for lft, rht in zip_longest(left.split(), right.split()):
Expand Down
23 changes: 23 additions & 0 deletions component_catalog/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,29 @@ def test_component_get_license_expression_spdx_id(self):
expected = "SPDX-1 WITH SPDX-2"
self.assertEqual(expected, self.component1.get_license_expression_spdx_id())

def test_component_model_license_expression_spdx_properties(self):
self.license1.spdx_license_key = "SPDX-1"
self.license1.save()

expression = "{} AND {}".format(self.license1.key, self.license2.key)
self.component1.license_expression = expression
self.component1.declared_license_expression = expression
self.component1.other_license_expression = expression
self.component1.save()

expected = "SPDX-1 AND LicenseRef-dejacode-license2"
self.assertEqual(expected, self.component1.concluded_license_expression_spdx)
self.assertEqual(expected, self.component1.declared_license_expression_spdx)
self.assertEqual(expected, self.component1.other_license_expression_spdx)

self.component1.license_expression = "unknown"
self.component1.save()
expected = "Unknown license key(s): unknown"
self.assertEqual(expected, self.component1.concluded_license_expression_spdx)

def test_component_model_get_expression_as_spdx(self):
pass

def test_get_license_expression_key_as_link_conflict(self):
# self.license1.key is contained in self.license2.key
self.license1.key = "w3c"
Expand Down

0 comments on commit f334131

Please sign in to comment.