Skip to content

Commit 3644f13

Browse files
committed
tests: refactor tests
Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
1 parent 2b69925 commit 3644f13

2 files changed

Lines changed: 114 additions & 105 deletions

File tree

tests/test_factory_license.py

Lines changed: 72 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -17,135 +17,118 @@
1717
# SPDX-License-Identifier: Apache-2.0
1818
# Copyright (c) OWASP Foundation. All Rights Reserved.
1919

20-
import itertools
2120
import unittest
2221
import unittest.mock
23-
from typing import Optional
24-
25-
from ddt import data, ddt, idata, unpack
2622

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

3127

32-
@ddt
3328
class TestFactoryLicense(unittest.TestCase):
3429

35-
@idata(itertools.product(
36-
['MIT'],
37-
['MIT', 'mit', 'MiT'],
38-
[None, unittest.mock.NonCallableMock(spec=AttachedText)],
39-
[None, unittest.mock.NonCallableMock(spec=XsUri)],
40-
))
41-
@unpack
42-
def test_make_from_string_with_id(self, expected_id: str,
43-
id_: str, text: Optional[AttachedText], url: Optional[XsUri]) -> None:
44-
expected = License(spdx_license_id=expected_id, license_text=text, license_url=url)
45-
actual = LicenseFactory().make_from_string(id_, license_text=text, license_url=url)
46-
self.assertEqual(expected, actual)
30+
def test_make_from_string_with_id(self) -> None:
31+
text = unittest.mock.NonCallableMock(spec=AttachedText)
32+
url = unittest.mock.NonCallableMock(spec=XsUri)
33+
expected = License(spdx_license_id='bar', license_text=text, license_url=url)
34+
factory = LicenseFactory()
35+
36+
with unittest.mock.patch('cyclonedx.factory.license.spdx_fixup', return_value='bar'):
37+
actual = factory.make_from_string('foo', license_text=text, license_url=url)
4738

48-
@idata(itertools.product(
49-
['foo bar'],
50-
[None, unittest.mock.NonCallableMock(spec=AttachedText)],
51-
[None, unittest.mock.NonCallableMock(spec=XsUri)],
52-
))
53-
@unpack
54-
def test_make_from_string_with_name(self, name: str, text: Optional[AttachedText], url: Optional[XsUri]) -> None:
55-
expected = License(license_name=name, license_text=text, license_url=url)
56-
actual = LicenseFactory().make_from_string(name, license_text=text, license_url=url)
5739
self.assertEqual(expected, actual)
5840

59-
@idata(itertools.product(
60-
['MIT'],
61-
['MIT', 'mit', 'MiT'],
62-
[None, unittest.mock.NonCallableMock(spec=AttachedText)],
63-
[None, unittest.mock.NonCallableMock(spec=XsUri)],
64-
))
65-
@unpack
66-
def test_make_with_id(self, expected_id: str,
67-
id_: str, text: Optional[AttachedText], url: Optional[XsUri]) -> None:
68-
expected = License(spdx_license_id=expected_id, license_text=text, license_url=url)
69-
actual = LicenseFactory().make_with_id(id_, license_text=text, license_url=url)
41+
def test_make_from_string_with_name(self) -> None:
42+
text = unittest.mock.NonCallableMock(spec=AttachedText)
43+
url = unittest.mock.NonCallableMock(spec=XsUri)
44+
expected = License(license_name='foo', license_text=text, license_url=url)
45+
factory = LicenseFactory()
46+
47+
with unittest.mock.patch('cyclonedx.factory.license.spdx_fixup', return_value=None):
48+
actual = factory.make_from_string('foo', license_text=text, license_url=url)
49+
7050
self.assertEqual(expected, actual)
7151

72-
@data(
73-
'FOO BaR',
74-
)
75-
def test_make_with_id_raises(self, invalid_id: str) -> None:
52+
def test_make_with_id(self) -> None:
53+
text = unittest.mock.NonCallableMock(spec=AttachedText)
54+
url = unittest.mock.NonCallableMock(spec=XsUri)
55+
expected = License(spdx_license_id='bar', license_text=text, license_url=url)
7656
factory = LicenseFactory()
77-
with self.assertRaises(InvalidSpdxLicenseException, msg=invalid_id):
78-
factory.make_with_id(invalid_id)
79-
80-
@idata(itertools.product(
81-
['foo'],
82-
[None, unittest.mock.NonCallableMock(spec=AttachedText)],
83-
[None, unittest.mock.NonCallableMock(spec=XsUri)],
84-
))
85-
@unpack
86-
def test_make_with_name(self, name: str, text: Optional[AttachedText], url: Optional[XsUri]) -> None:
87-
expected = License(license_name=name, license_text=text, license_url=url)
88-
actual = LicenseFactory().make_with_name(name, license_text=text, license_url=url)
57+
58+
with unittest.mock.patch('cyclonedx.factory.license.spdx_fixup', return_value='bar'):
59+
actual = factory.make_with_id('foo', license_text=text, license_url=url)
60+
8961
self.assertEqual(expected, actual)
9062

63+
def test_make_with_id_raises(self) -> None:
64+
factory = LicenseFactory()
65+
with self.assertRaises(InvalidSpdxLicenseException, msg='foo'):
66+
with unittest.mock.patch('cyclonedx.factory.license.spdx_fixup', return_value=None):
67+
factory.make_with_id('foo')
68+
69+
def test_make_with_name(self) -> None:
70+
text = unittest.mock.NonCallableMock(spec=AttachedText)
71+
url = unittest.mock.NonCallableMock(spec=XsUri)
72+
expected = License(license_name='foo', license_text=text, license_url=url)
73+
factory = LicenseFactory()
74+
75+
actual = factory.make_with_name('foo', license_text=text, license_url=url)
9176

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

9879

99-
@ddt
10080
class TestFactoryLicenseChoice(unittest.TestCase):
10181

102-
@idata(VALID_EXPRESSIONS)
103-
def test_make_from_string_with_compound_expression(self, compound_expression: str) -> None:
104-
expected = LicenseChoice(license_expression=compound_expression)
105-
license_factory = unittest.mock.MagicMock(spec=LicenseFactory)
106-
actual = LicenseChoiceFactory(license_factory=license_factory).make_from_string(
107-
compound_expression)
82+
def test_make_from_string_with_compound_expression(self) -> None:
83+
expected = LicenseChoice(license_expression='foo')
84+
factory = LicenseChoiceFactory(license_factory=unittest.mock.MagicMock(spec=LicenseFactory))
85+
86+
with unittest.mock.patch('cyclonedx.factory.license.is_spdx_compound_expression', return_value=True):
87+
actual = factory.make_from_string('foo')
88+
10889
self.assertEqual(expected, actual)
10990

11091
def test_make_from_string_with_license(self) -> None:
11192
license_ = unittest.mock.NonCallableMock(spec=License)
11293
expected = LicenseChoice(license_=license_)
11394
license_factory = unittest.mock.MagicMock(spec=LicenseFactory)
11495
license_factory.make_from_string.return_value = license_
115-
actual = LicenseChoiceFactory(license_factory=license_factory).make_from_string('foo')
96+
factory = LicenseChoiceFactory(license_factory=license_factory)
97+
98+
with unittest.mock.patch('cyclonedx.factory.license.is_spdx_compound_expression', return_value=False):
99+
actual = factory.make_from_string('foo')
100+
116101
self.assertEqual(expected, actual)
117102
self.assertIs(license_, actual.license)
118103
license_factory.make_from_string.assert_called_once_with('foo', license_text=None, license_url=None)
119104

120-
@idata(VALID_EXPRESSIONS)
121-
def test_make_with_compound_expression(self, compound_expression: str) -> None:
122-
expected = LicenseChoice(license_expression=compound_expression)
123-
license_factory = unittest.mock.MagicMock(spec=LicenseFactory)
124-
actual = LicenseChoiceFactory(license_factory=license_factory).make_with_compound_expression(
125-
compound_expression)
105+
def test_make_with_compound_expression(self) -> None:
106+
expected = LicenseChoice(license_expression='foo')
107+
factory = LicenseChoiceFactory(license_factory=unittest.mock.MagicMock(spec=LicenseFactory))
108+
109+
with unittest.mock.patch('cyclonedx.factory.license.is_spdx_compound_expression', return_value=True):
110+
actual = factory.make_with_compound_expression('foo')
111+
126112
self.assertEqual(expected, actual)
127113

128-
@data(
129-
'Foo Bar',
130-
)
131-
def test_make_with_compound_expression_raises(self, invalid_expression: str) -> None:
114+
def test_make_with_compound_expression_raises(self) -> None:
132115
factory = LicenseChoiceFactory(license_factory=unittest.mock.MagicMock(spec=LicenseFactory))
133-
with self.assertRaises(InvalidLicenseExpressionException, msg=invalid_expression):
134-
factory.make_with_compound_expression(invalid_expression)
135-
136-
@idata(itertools.product(
137-
['foo'],
138-
[None, unittest.mock.NonCallableMock(spec=AttachedText)],
139-
[None, unittest.mock.NonCallableMock(spec=XsUri)],
140-
))
141-
@unpack
142-
def test_make_with_license(self, name_or_spdx: str, text: Optional[AttachedText], url: Optional[XsUri]) -> None:
116+
with unittest.mock.patch('cyclonedx.factory.license.is_spdx_compound_expression', return_value=False):
117+
with self.assertRaises(InvalidLicenseExpressionException, msg='foo'):
118+
factory.make_with_compound_expression('foo')
119+
120+
def test_make_with_license(self) -> None:
121+
text = unittest.mock.NonCallableMock(spec=AttachedText)
122+
url = unittest.mock.NonCallableMock(spec=XsUri)
143123
license_ = unittest.mock.NonCallableMock(spec=License)
144124
expected = LicenseChoice(license_=license_)
145125
license_factory = unittest.mock.MagicMock(spec=LicenseFactory)
146126
license_factory.make_from_string.return_value = license_
147-
actual = LicenseChoiceFactory(license_factory=license_factory).make_with_license(
148-
name_or_spdx, license_text=text, license_url=url)
127+
factory = LicenseChoiceFactory(license_factory=license_factory)
128+
129+
with unittest.mock.patch('cyclonedx.factory.license.is_spdx_compound_expression', return_value=False):
130+
actual = factory.make_with_license('foo', license_text=text, license_url=url)
131+
149132
self.assertEqual(expected, actual)
150133
self.assertIs(license_, actual.license)
151-
license_factory.make_from_string.assert_called_once_with(name_or_spdx, license_text=text, license_url=url)
134+
license_factory.make_from_string.assert_called_once_with('foo', license_text=text, license_url=url)

tests/test_spdx.py

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,45 +24,71 @@
2424

2525
from ddt import data, ddt, idata, unpack
2626

27-
from cyclonedx.spdx import fixup_id, is_supported_id
27+
from cyclonedx import spdx
2828

2929
with open(path_join(dirname(__file__), '..', 'cyclonedx', 'schema', 'spdx.schema.json')) as spdx_schema:
30-
SPDX_IDS = json_load(spdx_schema)['enum']
30+
KNOWN_SPDX_IDS = json_load(spdx_schema)['enum']
31+
32+
VALID_COMPOUND_EXPRESSIONS = {
33+
# for valid test data see the spec: https://spdx.github.io/spdx-spec/v2.3/SPDX-license-expressions/
34+
'(MIT WITH Apache-2.0)',
35+
'(BSD-2-Clause OR Apache-2.0)',
36+
}
3137

3238

3339
@ddt
34-
class TestSpdx(TestCase):
40+
class TestSpdxIsSupported(TestCase):
41+
42+
@idata(KNOWN_SPDX_IDS)
43+
def test_positive(self, supported_value: str) -> None:
44+
actual = spdx.is_supported_id(supported_value)
45+
self.assertTrue(actual)
3546

3647
@data(
3748
'something unsupported',
3849
# somehow case-twisted values
3950
'MiT',
4051
'mit',
4152
)
42-
def test_not_supported(self, unsupported_value: str) -> None:
43-
actual = is_supported_id(unsupported_value)
53+
def test_negative(self, unsupported_value: str) -> None:
54+
actual = spdx.is_supported_id(unsupported_value)
4455
self.assertFalse(actual)
4556

46-
@idata(SPDX_IDS)
47-
def test_is_supported(self, supported_value: str) -> None:
48-
actual = is_supported_id(supported_value)
49-
self.assertTrue(actual)
57+
58+
@ddt
59+
class TestSpdxFixup(TestCase):
5060

5161
@idata(chain(
5262
# original value
53-
((v, v) for v in SPDX_IDS),
63+
((v, v) for v in KNOWN_SPDX_IDS),
5464
# somehow case-twisted values
55-
((v.lower(), v) for v in SPDX_IDS),
56-
((v.upper(), v) for v in SPDX_IDS)
65+
((v.lower(), v) for v in KNOWN_SPDX_IDS),
66+
((v.upper(), v) for v in KNOWN_SPDX_IDS)
5767
))
5868
@unpack
59-
def test_fixup(self, fixable: str, expected_fixed: str) -> None:
60-
actual = fixup_id(fixable)
69+
def test_positive(self, fixable: str, expected_fixed: str) -> None:
70+
actual = spdx.fixup_id(fixable)
6171
self.assertEqual(expected_fixed, actual)
6272

6373
@data(
6474
'something unfixable',
6575
)
66-
def test_not_fixup(self, unfixable: str) -> None:
67-
actual = fixup_id(unfixable)
76+
def test_negative(self, unfixable: str) -> None:
77+
actual = spdx.fixup_id(unfixable)
6878
self.assertIsNone(actual)
79+
80+
81+
@ddt
82+
class TestSpdxIsCompoundExpression(TestCase):
83+
84+
@idata(VALID_COMPOUND_EXPRESSIONS)
85+
def test_positive(self, valid_expression: str) -> None:
86+
actual = spdx.is_compound_expression(valid_expression)
87+
self.assertTrue(actual)
88+
89+
@data(
90+
'something invalid',
91+
)
92+
def test_negative(self, invalid_expression: str) -> None:
93+
actual = spdx.is_compound_expression(invalid_expression)
94+
self.assertFalse(actual)

0 commit comments

Comments
 (0)