Skip to content

Commit

Permalink
tests for backwards compatibility of #365
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Kowalleck <[email protected]>
  • Loading branch information
jkowalleck committed Oct 11, 2023
1 parent 6770786 commit 755895f
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 4 deletions.
47 changes: 47 additions & 0 deletions tests/_data/own/json/1.2/bom_with_mixed_licenses.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions tests/_data/own/json/1.3/bom_with_mixed_licenses.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions tests/_data/own/json/1.4/bom_with_mixed_licenses.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 32 additions & 3 deletions tests/test_deserialize_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@


from json import loads as json_loads
from os.path import join
from typing import Any, Callable
from unittest import TestCase
from unittest.mock import patch

from ddt import ddt, named_data
from ddt import data, ddt, named_data

from cyclonedx.model.bom import Bom
from cyclonedx.model.license import DisjunctiveLicense, LicenseExpression, LicenseRepository
from cyclonedx.schema import OutputFormat, SchemaVersion
from tests import DeepCompareMixin, SnapshotMixin, mksname, uuid_generator
from tests import OWN_DATA_DIRECTORY, DeepCompareMixin, SnapshotMixin, mksname, uuid_generator
from tests._data.models import all_get_bom_funct_valid, all_get_bom_funct_with_incomplete_deps


Expand All @@ -35,11 +37,38 @@ class TestDeserializeJson(TestCase, SnapshotMixin, DeepCompareMixin):
@named_data(*all_get_bom_funct_valid)
@patch('cyclonedx.model.ThisTool._version', 'TESTING')
@patch('cyclonedx.model.bom_ref.uuid4', side_effect=uuid_generator(0, version=4))
def test(self, get_bom: Callable[[], Bom], *_: Any, **__: Any) -> None:
def test_prepared(self, get_bom: Callable[[], Bom], *_: Any, **__: Any) -> None:
# only latest schema will have all data populated in serialized form
snapshot_name = mksname(get_bom, SchemaVersion.V1_4, OutputFormat.JSON)
expected = get_bom()
json = json_loads(self.readSnapshot(snapshot_name))
bom = Bom.from_json(json)
self.assertBomDeepEqual(expected, bom,
fuzzy_deps=get_bom in all_get_bom_funct_with_incomplete_deps)

@data(SchemaVersion.V1_4, SchemaVersion.V1_3, SchemaVersion.V1_2)
def test_mixed_licenses_before15(self, sv: SchemaVersion) -> None:
# before CDX 1.5 it was allowed to mix `expression` and `license`
def test(ls: LicenseRepository) -> None:
self.assertEqual(3, len(ls))
expression: LicenseExpression = next((
li for li in ls if isinstance(li, LicenseExpression)
), None)
with_id: DisjunctiveLicense = next((
li for li in ls if isinstance(li, DisjunctiveLicense) and li.id is not None
), None)
with_name: DisjunctiveLicense = next((
li for li in ls if isinstance(li, DisjunctiveLicense) and li.name is not None
), None)
self.assertEqual('MIT OR Apache-2.0', expression.value)
self.assertEqual('MIT', with_id.id)
self.assertEqual('foo license', with_name.name)

json_file = join(OWN_DATA_DIRECTORY, 'json', sv.to_version(), 'bom_with_mixed_licenses.json')
with open(json_file) as f:
json = json_loads(f.read())
bom: Bom = Bom.from_json(json)
test(bom.metadata.licenses)
test(bom.metadata.component.licenses)
test(list(bom.components)[0].licenses)
test(list(bom.services)[0].licenses)
2 changes: 1 addition & 1 deletion tests/test_deserialize_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class TestDeserializeXml(TestCase, SnapshotMixin, DeepCompareMixin):
@named_data(*all_get_bom_funct_valid)
@patch('cyclonedx.model.ThisTool._version', 'TESTING')
@patch('cyclonedx.model.bom_ref.uuid4', side_effect=uuid_generator(0, version=4))
def test(self, get_bom: Callable[[], Bom], *_: Any, **__: Any) -> None:
def test_prepared(self, get_bom: Callable[[], Bom], *_: Any, **__: Any) -> None:
# only latest schema will have all data populated in serialized form
snapshot_name = mksname(get_bom, SchemaVersion.V1_4, OutputFormat.XML)
expected = get_bom()
Expand Down

0 comments on commit 755895f

Please sign in to comment.