Skip to content

Commit

Permalink
tests: refactor tests
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
  • Loading branch information
jkowalleck committed Sep 14, 2022
1 parent 2b69925 commit 3644f13
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 105 deletions.
161 changes: 72 additions & 89 deletions tests/test_factory_license.py
Expand Up @@ -17,135 +17,118 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright (c) OWASP Foundation. All Rights Reserved.

import itertools
import unittest
import unittest.mock
from typing import Optional

from ddt import data, ddt, idata, unpack

from cyclonedx.exception.factory import InvalidLicenseExpressionException, InvalidSpdxLicenseException
from cyclonedx.factory.license import LicenseChoiceFactory, LicenseFactory
from cyclonedx.model import AttachedText, License, LicenseChoice, XsUri


@ddt
class TestFactoryLicense(unittest.TestCase):

@idata(itertools.product(
['MIT'],
['MIT', 'mit', 'MiT'],
[None, unittest.mock.NonCallableMock(spec=AttachedText)],
[None, unittest.mock.NonCallableMock(spec=XsUri)],
))
@unpack
def test_make_from_string_with_id(self, expected_id: str,
id_: str, text: Optional[AttachedText], url: Optional[XsUri]) -> None:
expected = License(spdx_license_id=expected_id, license_text=text, license_url=url)
actual = LicenseFactory().make_from_string(id_, license_text=text, license_url=url)
self.assertEqual(expected, actual)
def test_make_from_string_with_id(self) -> None:
text = unittest.mock.NonCallableMock(spec=AttachedText)
url = unittest.mock.NonCallableMock(spec=XsUri)
expected = License(spdx_license_id='bar', license_text=text, license_url=url)
factory = LicenseFactory()

with unittest.mock.patch('cyclonedx.factory.license.spdx_fixup', return_value='bar'):
actual = factory.make_from_string('foo', license_text=text, license_url=url)

@idata(itertools.product(
['foo bar'],
[None, unittest.mock.NonCallableMock(spec=AttachedText)],
[None, unittest.mock.NonCallableMock(spec=XsUri)],
))
@unpack
def test_make_from_string_with_name(self, name: str, text: Optional[AttachedText], url: Optional[XsUri]) -> None:
expected = License(license_name=name, license_text=text, license_url=url)
actual = LicenseFactory().make_from_string(name, license_text=text, license_url=url)
self.assertEqual(expected, actual)

@idata(itertools.product(
['MIT'],
['MIT', 'mit', 'MiT'],
[None, unittest.mock.NonCallableMock(spec=AttachedText)],
[None, unittest.mock.NonCallableMock(spec=XsUri)],
))
@unpack
def test_make_with_id(self, expected_id: str,
id_: str, text: Optional[AttachedText], url: Optional[XsUri]) -> None:
expected = License(spdx_license_id=expected_id, license_text=text, license_url=url)
actual = LicenseFactory().make_with_id(id_, license_text=text, license_url=url)
def test_make_from_string_with_name(self) -> None:
text = unittest.mock.NonCallableMock(spec=AttachedText)
url = unittest.mock.NonCallableMock(spec=XsUri)
expected = License(license_name='foo', license_text=text, license_url=url)
factory = LicenseFactory()

with unittest.mock.patch('cyclonedx.factory.license.spdx_fixup', return_value=None):
actual = factory.make_from_string('foo', license_text=text, license_url=url)

self.assertEqual(expected, actual)

@data(
'FOO BaR',
)
def test_make_with_id_raises(self, invalid_id: str) -> None:
def test_make_with_id(self) -> None:
text = unittest.mock.NonCallableMock(spec=AttachedText)
url = unittest.mock.NonCallableMock(spec=XsUri)
expected = License(spdx_license_id='bar', license_text=text, license_url=url)
factory = LicenseFactory()
with self.assertRaises(InvalidSpdxLicenseException, msg=invalid_id):
factory.make_with_id(invalid_id)

@idata(itertools.product(
['foo'],
[None, unittest.mock.NonCallableMock(spec=AttachedText)],
[None, unittest.mock.NonCallableMock(spec=XsUri)],
))
@unpack
def test_make_with_name(self, name: str, text: Optional[AttachedText], url: Optional[XsUri]) -> None:
expected = License(license_name=name, license_text=text, license_url=url)
actual = LicenseFactory().make_with_name(name, license_text=text, license_url=url)

with unittest.mock.patch('cyclonedx.factory.license.spdx_fixup', return_value='bar'):
actual = factory.make_with_id('foo', license_text=text, license_url=url)

self.assertEqual(expected, actual)

def test_make_with_id_raises(self) -> None:
factory = LicenseFactory()
with self.assertRaises(InvalidSpdxLicenseException, msg='foo'):
with unittest.mock.patch('cyclonedx.factory.license.spdx_fixup', return_value=None):
factory.make_with_id('foo')

def test_make_with_name(self) -> None:
text = unittest.mock.NonCallableMock(spec=AttachedText)
url = unittest.mock.NonCallableMock(spec=XsUri)
expected = License(license_name='foo', license_text=text, license_url=url)
factory = LicenseFactory()

actual = factory.make_with_name('foo', license_text=text, license_url=url)

VALID_EXPRESSIONS = {
# for valid test data see the spec: https://spdx.github.io/spdx-spec/v2.3/SPDX-license-expressions/
'(MIT WITH Apache-2.0)',
'(BSD-2-Clause OR Apache-2.0)',
}
self.assertEqual(expected, actual)


@ddt
class TestFactoryLicenseChoice(unittest.TestCase):

@idata(VALID_EXPRESSIONS)
def test_make_from_string_with_compound_expression(self, compound_expression: str) -> None:
expected = LicenseChoice(license_expression=compound_expression)
license_factory = unittest.mock.MagicMock(spec=LicenseFactory)
actual = LicenseChoiceFactory(license_factory=license_factory).make_from_string(
compound_expression)
def test_make_from_string_with_compound_expression(self) -> None:
expected = LicenseChoice(license_expression='foo')
factory = LicenseChoiceFactory(license_factory=unittest.mock.MagicMock(spec=LicenseFactory))

with unittest.mock.patch('cyclonedx.factory.license.is_spdx_compound_expression', return_value=True):
actual = factory.make_from_string('foo')

self.assertEqual(expected, actual)

def test_make_from_string_with_license(self) -> None:
license_ = unittest.mock.NonCallableMock(spec=License)
expected = LicenseChoice(license_=license_)
license_factory = unittest.mock.MagicMock(spec=LicenseFactory)
license_factory.make_from_string.return_value = license_
actual = LicenseChoiceFactory(license_factory=license_factory).make_from_string('foo')
factory = LicenseChoiceFactory(license_factory=license_factory)

with unittest.mock.patch('cyclonedx.factory.license.is_spdx_compound_expression', return_value=False):
actual = factory.make_from_string('foo')

self.assertEqual(expected, actual)
self.assertIs(license_, actual.license)
license_factory.make_from_string.assert_called_once_with('foo', license_text=None, license_url=None)

@idata(VALID_EXPRESSIONS)
def test_make_with_compound_expression(self, compound_expression: str) -> None:
expected = LicenseChoice(license_expression=compound_expression)
license_factory = unittest.mock.MagicMock(spec=LicenseFactory)
actual = LicenseChoiceFactory(license_factory=license_factory).make_with_compound_expression(
compound_expression)
def test_make_with_compound_expression(self) -> None:
expected = LicenseChoice(license_expression='foo')
factory = LicenseChoiceFactory(license_factory=unittest.mock.MagicMock(spec=LicenseFactory))

with unittest.mock.patch('cyclonedx.factory.license.is_spdx_compound_expression', return_value=True):
actual = factory.make_with_compound_expression('foo')

self.assertEqual(expected, actual)

@data(
'Foo Bar',
)
def test_make_with_compound_expression_raises(self, invalid_expression: str) -> None:
def test_make_with_compound_expression_raises(self) -> None:
factory = LicenseChoiceFactory(license_factory=unittest.mock.MagicMock(spec=LicenseFactory))
with self.assertRaises(InvalidLicenseExpressionException, msg=invalid_expression):
factory.make_with_compound_expression(invalid_expression)

@idata(itertools.product(
['foo'],
[None, unittest.mock.NonCallableMock(spec=AttachedText)],
[None, unittest.mock.NonCallableMock(spec=XsUri)],
))
@unpack
def test_make_with_license(self, name_or_spdx: str, text: Optional[AttachedText], url: Optional[XsUri]) -> None:
with unittest.mock.patch('cyclonedx.factory.license.is_spdx_compound_expression', return_value=False):
with self.assertRaises(InvalidLicenseExpressionException, msg='foo'):
factory.make_with_compound_expression('foo')

def test_make_with_license(self) -> None:
text = unittest.mock.NonCallableMock(spec=AttachedText)
url = unittest.mock.NonCallableMock(spec=XsUri)
license_ = unittest.mock.NonCallableMock(spec=License)
expected = LicenseChoice(license_=license_)
license_factory = unittest.mock.MagicMock(spec=LicenseFactory)
license_factory.make_from_string.return_value = license_
actual = LicenseChoiceFactory(license_factory=license_factory).make_with_license(
name_or_spdx, license_text=text, license_url=url)
factory = LicenseChoiceFactory(license_factory=license_factory)

with unittest.mock.patch('cyclonedx.factory.license.is_spdx_compound_expression', return_value=False):
actual = factory.make_with_license('foo', license_text=text, license_url=url)

self.assertEqual(expected, actual)
self.assertIs(license_, actual.license)
license_factory.make_from_string.assert_called_once_with(name_or_spdx, license_text=text, license_url=url)
license_factory.make_from_string.assert_called_once_with('foo', license_text=text, license_url=url)
58 changes: 42 additions & 16 deletions tests/test_spdx.py
Expand Up @@ -24,45 +24,71 @@

from ddt import data, ddt, idata, unpack

from cyclonedx.spdx import fixup_id, is_supported_id
from cyclonedx import spdx

with open(path_join(dirname(__file__), '..', 'cyclonedx', 'schema', 'spdx.schema.json')) as spdx_schema:
SPDX_IDS = json_load(spdx_schema)['enum']
KNOWN_SPDX_IDS = json_load(spdx_schema)['enum']

VALID_COMPOUND_EXPRESSIONS = {
# for valid test data see the spec: https://spdx.github.io/spdx-spec/v2.3/SPDX-license-expressions/
'(MIT WITH Apache-2.0)',
'(BSD-2-Clause OR Apache-2.0)',
}


@ddt
class TestSpdx(TestCase):
class TestSpdxIsSupported(TestCase):

@idata(KNOWN_SPDX_IDS)
def test_positive(self, supported_value: str) -> None:
actual = spdx.is_supported_id(supported_value)
self.assertTrue(actual)

@data(
'something unsupported',
# somehow case-twisted values
'MiT',
'mit',
)
def test_not_supported(self, unsupported_value: str) -> None:
actual = is_supported_id(unsupported_value)
def test_negative(self, unsupported_value: str) -> None:
actual = spdx.is_supported_id(unsupported_value)
self.assertFalse(actual)

@idata(SPDX_IDS)
def test_is_supported(self, supported_value: str) -> None:
actual = is_supported_id(supported_value)
self.assertTrue(actual)

@ddt
class TestSpdxFixup(TestCase):

@idata(chain(
# original value
((v, v) for v in SPDX_IDS),
((v, v) for v in KNOWN_SPDX_IDS),
# somehow case-twisted values
((v.lower(), v) for v in SPDX_IDS),
((v.upper(), v) for v in SPDX_IDS)
((v.lower(), v) for v in KNOWN_SPDX_IDS),
((v.upper(), v) for v in KNOWN_SPDX_IDS)
))
@unpack
def test_fixup(self, fixable: str, expected_fixed: str) -> None:
actual = fixup_id(fixable)
def test_positive(self, fixable: str, expected_fixed: str) -> None:
actual = spdx.fixup_id(fixable)
self.assertEqual(expected_fixed, actual)

@data(
'something unfixable',
)
def test_not_fixup(self, unfixable: str) -> None:
actual = fixup_id(unfixable)
def test_negative(self, unfixable: str) -> None:
actual = spdx.fixup_id(unfixable)
self.assertIsNone(actual)


@ddt
class TestSpdxIsCompoundExpression(TestCase):

@idata(VALID_COMPOUND_EXPRESSIONS)
def test_positive(self, valid_expression: str) -> None:
actual = spdx.is_compound_expression(valid_expression)
self.assertTrue(actual)

@data(
'something invalid',
)
def test_negative(self, invalid_expression: str) -> None:
actual = spdx.is_compound_expression(invalid_expression)
self.assertFalse(actual)

0 comments on commit 3644f13

Please sign in to comment.