Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Kowalleck <[email protected]>
  • Loading branch information
jkowalleck committed Oct 10, 2023
1 parent ce87320 commit 5549519
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions tests/test_model_license.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,94 @@
#
# SPDX-License-Identifier: Apache-2.0
# Copyright (c) OWASP Foundation. All Rights Reserved.


from random import shuffle
from unittest import TestCase
from unittest.mock import MagicMock

from cyclonedx.exception.model import MutuallyExclusivePropertiesException
from cyclonedx.model import AttachedText, XsUri
from cyclonedx.model.license import DisjunctiveLicense, LicenseExpression
from tests import reorder


class TestModelDisjunctiveLicense(TestCase):
def test_create_complete_id(self) -> None:
text = MagicMock(spec=AttachedText)
url = MagicMock(spec=XsUri)
license = DisjunctiveLicense(id='foo', text=text, url=url)
self.assertEqual('foo', license.id)
self.assertIsNone(license.name)
self.assertIs(text, license.text)
self.assertIs(url, license.url)

def test_update_id_name(self) -> None:
license = DisjunctiveLicense(id='foo')
self.assertEqual('foo', license.id)
self.assertIsNone(license.name)
license.name = 'bar'
self.assertIsNone(license.id)
self.assertEqual('bar', license.name)

def test_create_complete_named(self) -> None:
text = MagicMock(spec=AttachedText)
url = MagicMock(spec=XsUri)
license = DisjunctiveLicense(name='foo', text=text, url=url)
self.assertIsNone(license.id)
self.assertEqual('foo', license.name)
self.assertIs(text, license.text)
self.assertIs(url, license.url)

def test_update_name_id(self) -> None:
license = DisjunctiveLicense(name='foo')
self.assertEqual('foo', license.name)
self.assertIsNone(license.id)
license.id = 'bar'
self.assertIsNone(license.name)
self.assertEqual('bar', license.id)

def test_throws_when_no_id_nor_name(self) -> None:
with self.assertRaises(MutuallyExclusivePropertiesException):
DisjunctiveLicense(id=None, name=None)

def test_prefers_id_over_name(self) -> None:
with self.assertWarnsRegex(
RuntimeWarning,
'Both `id` and `name` have been supplied - `name` will be ignored!'):
license = DisjunctiveLicense(id='foo', name='bar')
self.assertEqual('foo', license.id)
self.assertEqual(None, license.name)

def test_equal(self) -> None:
a = DisjunctiveLicense(id='foo', name='bar')
b = DisjunctiveLicense(id='foo', name='bar')
c = DisjunctiveLicense(id='bar', name='foo')
self.assertEqual(a, b)
self.assertNotEqual(a, c)
self.assertNotEqual(a, 'foo')


class TestModelLicenseExpression(TestCase):
def test_create(self) -> None:
license = LicenseExpression('foo')
self.assertEqual('foo', license.value)

def test_update(self) -> None:
license = LicenseExpression('foo')
self.assertEqual('foo', license.value)
license.value = 'bar'
self.assertEqual('bar', license.value)

def test_equal(self) -> None:
a = LicenseExpression('foo')
b = LicenseExpression('foo')
c = LicenseExpression('bar')
self.assertEqual(a, b)
self.assertNotEqual(a, c)
self.assertNotEqual(a, 'foo')


class TestModelLicense(TestCase):

def test_sort_mixed(self) -> None:
Expand Down

0 comments on commit 5549519

Please sign in to comment.