Skip to content
Snippets Groups Projects
Verified Commit 45aeec44 authored by Antoine R. Dumont's avatar Antoine R. Dumont
Browse files

utils: Enforce necessary setup for deposit and associated client

Related T1648
parent 56e90372
No related branches found
No related tags found
No related merge requests found
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
# See top-level LICENSE file for more information # See top-level LICENSE file for more information
import unittest import unittest
import pytest
from unittest.mock import patch from unittest.mock import patch
...@@ -12,7 +13,9 @@ from swh.deposit.models import Deposit, DepositClient ...@@ -12,7 +13,9 @@ from swh.deposit.models import Deposit, DepositClient
def test_origin_url_from(): def test_origin_url_from():
"""With correctly setup-ed deposit, all is fine
"""
for provider_url, external_id in ( for provider_url, external_id in (
('http://somewhere.org', 'uuid'), ('http://somewhere.org', 'uuid'),
('http://overthejungle.org', 'diuu'), ('http://overthejungle.org', 'diuu'),
...@@ -28,6 +31,23 @@ def test_origin_url_from(): ...@@ -28,6 +31,23 @@ def test_origin_url_from():
provider_url.rstrip('/'), external_id) provider_url.rstrip('/'), external_id)
def test_origin_url_from_ko():
"""Badly configured deposit should raise
"""
for provider_url, external_id in (
(None, 'uuid'),
('http://overthejungle.org', None),
):
deposit = Deposit(
client=DepositClient(provider_url=provider_url),
external_id=None
)
with pytest.raises(AssertionError):
utils.origin_url_from(deposit)
class UtilsTestCase(unittest.TestCase): class UtilsTestCase(unittest.TestCase):
"""Utils library """Utils library
......
...@@ -11,17 +11,27 @@ from swh.model.identifiers import normalize_timestamp ...@@ -11,17 +11,27 @@ from swh.model.identifiers import normalize_timestamp
def origin_url_from(deposit): def origin_url_from(deposit):
"""Given a deposit instance, return the associated origin url """Given a deposit instance, return the associated origin url.
This expects a deposit and the associated client to be correctly
configured.
Args: Args:
deposit (Deposit): The deposit from which derives the origin url deposit (Deposit): The deposit from which derives the origin url
Raises:
AssertionError if:
- the client's provider_url field is not configured.
- the deposit's external_id field is not configured.
Returns Returns
The associated origin url The associated origin url
""" """
base_url = deposit.client.provider_url
external_id = deposit.external_id external_id = deposit.external_id
assert external_id is not None
base_url = deposit.client.provider_url
assert base_url is not None
return '%s/%s' % (base_url.rstrip('/'), external_id) return '%s/%s' % (base_url.rstrip('/'), external_id)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment