toddlers/tests/utils/test_pdc.py
Michal Konečný a8dca5908f Fix flake8 errors
Signed-off-by: Michal Konečný <mkonecny@redhat.com>
2022-03-21 13:44:45 +01:00

333 lines
8.4 KiB
Python

"""
Unit tests for `toddlers.utils.pdc`.
"""
from unittest.mock import call, MagicMock
import toddlers.utils.pdc as pdc
class TestPdcSetPdc:
"""
Test class for `toddlers.utils.pdc.set_pdc` function.
"""
def test_set_pdc(self):
"""
Test initialization of PDC module.
"""
config = {
"pdc_config": {
"server": "https://pdc.fedoraproject.org/rest_api/v1/",
"ssl_verify": False,
"token": "token",
}
}
pdc_client = pdc.set_pdc(config)
assert pdc_client
assert pdc._PDC
class TestPdcPdcClientForConfig:
"""
Test class for `toddlers.utils.pdc.pdc_client_for_config` function.
"""
def test_pdc_client_for_config(self):
"""
Assert that PDC is correctly initialized.
"""
config = {
"pdc_config": {
"server": "https://pdc.fedoraproject.org/rest_api/v1/",
"ssl_verify": False,
"token": "token",
}
}
pdc_client = pdc.pdc_client_for_config(config)
assert pdc_client
class TestPdcGetSla:
"""
Test class for `toddlers.utils.pdc.get_sla` function.
"""
def setup(self):
"""
Setup the PDC module.
"""
pdc._PDC = MagicMock()
def test_get_sla(self):
"""
Assert that correct response is handled.
"""
response = {"count": 1, "results": [{"id": 3}]}
pdc._PDC["component-sla-types"]._.return_value = response
result = pdc.get_sla("rawhide")
assert result == {"id": 3}
def test_get_sla_not_found(self):
"""
Assert that incorrect response is handled.
"""
response = {"count": 0, "results": []}
pdc._PDC["component-sla-types"]._.return_value = response
result = pdc.get_sla("rawhide")
assert not result
class TestPdcNewSLAToBranch:
"""
Test class for `toddlers.utils.pdc.new_sla_to_branch` function.
"""
def setup(self):
"""
Setup the PDC module.
"""
pdc._PDC = MagicMock()
def test_new_sla_to_branch(self):
"""
Assert that correct response is handled.
"""
sla = "sla"
eol = "2020-01-01"
global_component = "global_component"
branch_name = "branch"
branch_type = "branch_type"
pdc.new_sla_to_branch(
sla_name=sla,
eol=eol,
global_component=global_component,
branch=branch_name,
branch_type=branch_type,
)
pdc._PDC["component-branch-slas"]._.assert_called_with(
{
"sla": sla,
"eol": eol,
"branch": {
"global_component": global_component,
"name": branch_name,
"type": branch_type,
},
}
)
class TestPdcGetBranch:
"""
Test class for `toddlers.utils.pdc.get_branch` function.
"""
def setup(self):
"""
Setup the PDC module.
"""
pdc._PDC = MagicMock()
def test_get_branch(self):
"""
Assert that correct response is handled.
"""
response = {"count": 1, "results": [{"id": 3}]}
global_component = "global_component"
name = "branch"
branch_type = "branch_type"
pdc._PDC["component-branches"]._.return_value = response
result = pdc.get_branch(global_component, name, branch_type)
assert result == {"id": 3}
pdc._PDC["component-branches"]._.assert_called_with(
global_component=global_component, name=name, type=branch_type
)
def test_get_branch_not_found(self):
"""
Assert that incorrect response is handled.
"""
response = {"count": 0, "results": []}
global_component = "global_component"
name = "branch"
branch_type = "branch_type"
pdc._PDC["component-branches"]._.return_value = response
result = pdc.get_branch(global_component, name, branch_type)
assert not result
pdc._PDC["component-branches"]._.assert_called_with(
global_component=global_component, name=name, type=branch_type
)
class TestPdcNewBranch:
"""
Test class for `toddlers.utils.pdc.new_branch` function.
"""
def setup(self):
"""
Setup the PDC module.
"""
pdc._PDC = MagicMock()
def test_new_branch_already_exists(self):
"""
Assert that component is not created when it doesn't exist.
"""
response = {"count": 1, "results": [{"id": 3}]}
global_component = "global_component"
branch = "branch"
branch_type = "branch_type"
pdc._PDC["component-branches"]._.return_value = response
pdc.new_branch(global_component, branch, branch_type)
pdc._PDC["component-branches"]._.assert_called_with(
global_component=global_component, name=branch, type=branch_type
)
def test_new_branch_not_found(self):
"""
Assert that component is created when not found.
"""
response = {"count": 0, "results": []}
global_component = "global_component"
branch = "branch"
branch_type = "branch_type"
pdc._PDC["component-branches"]._.return_value = response
pdc.new_branch(global_component, branch, branch_type)
pdc._PDC["component-branches"]._.assert_has_calls(
[
call(global_component=global_component, name=branch, type=branch_type),
call(
{
"global_component": global_component,
"name": branch,
"type": branch_type,
}
),
]
)
class TestPdcGetGlobalComponent:
"""
Test class for `toddlers.utils.pdc.get_global_component` function.
"""
def setup(self):
"""
Setup the PDC module.
"""
pdc._PDC = MagicMock()
def test_get_global_component(self):
"""
Assert that correct response is handled.
"""
response = {"count": 1, "results": [{"id": 3}]}
global_component = "global_component"
pdc._PDC["global_components"]._.return_value = response
result = pdc.get_global_component(global_component)
assert result == {"id": 3}
pdc._PDC["global_components"]._.assert_called_with(
name=global_component,
)
def test_get_global_component_not_found(self):
"""
Assert that incorrect response is handled.
"""
response = {"count": 0, "results": []}
global_component = "global_component"
pdc._PDC["global-components"]._.return_value = response
result = pdc.get_global_component(global_component)
assert not result
pdc._PDC["global-components"]._.assert_called_with(
name=global_component,
)
class TestPdcNewGlobalComponent:
"""
Test class for `toddlers.utils.pdc.new_global_component` function.
"""
def setup(self):
"""
Setup the PDC module.
"""
pdc._PDC = MagicMock()
def test_new_global_component_already_exists(self):
"""
Assert that component is not created when it doesn't exist.
"""
response = {"count": 1, "results": [{"id": 3}]}
global_component = "global_component"
dist_git_url = "https://src.fedoraproject.org/example"
pdc._PDC["global_components"]._.return_value = response
pdc.new_global_component(global_component, dist_git_url)
pdc._PDC["global_components"]._.assert_called_with(
name=global_component,
)
def test_new_global_component_not_found(self):
"""
Assert that component is created when not found.
"""
response = {"count": 0, "results": []}
global_component = "global_component"
dist_git_url = "https://src.fedoraproject.org/example"
pdc._PDC["global-components"]._.return_value = response
pdc.new_global_component(global_component, dist_git_url)
pdc._PDC["global-components"]._.assert_has_calls(
[
call(name=global_component),
call({"name": global_component, "dist_git_web_url": dist_git_url}),
]
)