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

test_deposit_delete: Migrate to pytest

parent 486f1555
No related branches found
No related tags found
1 merge request!40Migrate most deposit tests to pytest
...@@ -3,111 +3,119 @@ ...@@ -3,111 +3,119 @@
# License: GNU General Public License version 3, or any later version # License: GNU General Public License version 3, or any later version
# See top-level LICENSE file for more information # See top-level LICENSE file for more information
from collections import defaultdict
from django.urls import reverse from django.urls import reverse
from rest_framework import status from rest_framework import status
from rest_framework.test import APITestCase from typing import Mapping
from swh.deposit.config import EDIT_SE_IRI, EM_IRI, ARCHIVE_KEY, METADATA_KEY from swh.deposit.config import (
from swh.deposit.config import DEPOSIT_STATUS_DEPOSITED EDIT_SE_IRI, EM_IRI, ARCHIVE_KEY, METADATA_KEY,
DEPOSIT_STATUS_DEPOSITED
)
from swh.deposit.models import Deposit, DepositRequest from swh.deposit.models import Deposit, DepositRequest
from ..common import BasicTestCase, WithAuthTestCase, CommonCreationRoutine
def count_deposit_request_types(deposit_requests) -> Mapping[str, int]:
class DepositDeleteTest(APITestCase, WithAuthTestCase, BasicTestCase, deposit_request_types = defaultdict(int)
CommonCreationRoutine): for dr in deposit_requests:
deposit_request_types[dr.type] += 1
def test_delete_archive_on_partial_deposit_works(self): return deposit_request_types
"""Removing partial deposit's archive should return a 204 response
""" def test_delete_archive_on_partial_deposit_works(
# given authenticated_client, partial_deposit_with_metadata,
deposit_id = self.create_deposit_partial() deposit_collection):
deposit = Deposit.objects.get(pk=deposit_id) """Removing partial deposit's archive should return a 204 response
deposit_requests = DepositRequest.objects.filter(deposit=deposit)
"""
self.assertEqual(len(deposit_requests), 2) deposit_id = partial_deposit_with_metadata.id
for dr in deposit_requests: deposit = Deposit.objects.get(pk=deposit_id)
if dr.type == ARCHIVE_KEY: deposit_requests = DepositRequest.objects.filter(deposit=deposit)
continue
elif dr.type == METADATA_KEY: # deposit request type: 'archive', 1 'metadata'
continue deposit_request_types = count_deposit_request_types(deposit_requests)
else: assert deposit_request_types == {
self.fail('only archive and metadata type should exist ' ARCHIVE_KEY: 1,
'in this test context') METADATA_KEY: 1
}
# when
update_uri = reverse(EM_IRI, args=[self.collection.name, deposit_id]) # when
response = self.client.delete(update_uri) update_uri = reverse(EM_IRI, args=[deposit_collection.name, deposit_id])
# then response = authenticated_client.delete(update_uri)
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
# then
deposit = Deposit.objects.get(pk=deposit_id) assert response.status_code == status.HTTP_204_NO_CONTENT
requests = list(DepositRequest.objects.filter(deposit=deposit))
deposit = Deposit.objects.get(pk=deposit_id)
self.assertEqual(len(requests), 2) deposit_requests2 = DepositRequest.objects.filter(deposit=deposit)
self.assertEqual(requests[0].type, 'metadata')
self.assertEqual(requests[1].type, 'metadata') deposit_request_types = count_deposit_request_types(deposit_requests2)
assert deposit_request_types == {
def test_delete_archive_on_undefined_deposit_fails(self): METADATA_KEY: 1
"""Delete undefined deposit returns a 404 response }
"""
# when def test_delete_archive_on_undefined_deposit_fails(
update_uri = reverse(EM_IRI, args=[self.collection.name, 999]) authenticated_client, deposit_collection, sample_archive):
response = self.client.delete(update_uri) """Delete undefined deposit returns a 404 response
# then
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) """
# when
def test_delete_archive_on_non_partial_deposit_fails(self): update_uri = reverse(EM_IRI, args=[deposit_collection.name, 999])
"""Delete !partial status deposit should return a 400 response""" response = authenticated_client.delete(update_uri)
deposit_id = self.create_deposit_ready() # then
deposit = Deposit.objects.get(pk=deposit_id) assert response.status_code == status.HTTP_404_NOT_FOUND
self.assertEqual(deposit.status, DEPOSIT_STATUS_DEPOSITED)
# when def test_delete_non_partial_deposit(
update_uri = reverse(EM_IRI, args=[self.collection.name, deposit_id]) authenticated_client, deposit_collection, deposited_deposit):
response = self.client.delete(update_uri) """Delete !partial status deposit should return a 400 response
# then
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) """
deposit = Deposit.objects.get(pk=deposit_id) deposit = deposited_deposit
self.assertIsNotNone(deposit) assert deposit.status == DEPOSIT_STATUS_DEPOSITED
def test_delete_partial_deposit_works(self): # when
"""Delete deposit should return a 204 response update_uri = reverse(EM_IRI, args=[deposit_collection.name, deposit.id])
response = authenticated_client.delete(update_uri)
""" # then
# given assert response.status_code == status.HTTP_400_BAD_REQUEST
deposit_id = self.create_simple_deposit_partial() deposit = Deposit.objects.get(pk=deposit.id)
deposit = Deposit.objects.get(pk=deposit_id) assert deposit is not None
assert deposit.id == deposit_id
# when def test_delete_partial_deposit(
url = reverse(EDIT_SE_IRI, args=[self.collection.name, deposit_id]) authenticated_client, deposit_collection, partial_deposit):
response = self.client.delete(url) """Delete deposit should return a 204 response
# then
self.assertEqual(response.status_code, """
status.HTTP_204_NO_CONTENT) # given
deposit_requests = list(DepositRequest.objects.filter(deposit=deposit)) deposit = partial_deposit
self.assertEqual(deposit_requests, [])
deposits = list(Deposit.objects.filter(pk=deposit_id)) # when
self.assertEqual(deposits, []) url = reverse(EDIT_SE_IRI, args=[deposit_collection.name, deposit.id])
response = authenticated_client.delete(url)
def test_delete_on_edit_se_iri_cannot_delete_non_partial_deposit(self): # then
"""Delete !partial deposit should return a 400 response assert response.status_code == status.HTTP_204_NO_CONTENT
deposit_requests = list(DepositRequest.objects.filter(deposit=deposit))
""" assert deposit_requests == []
# given deposits = list(Deposit.objects.filter(pk=deposit.id))
deposit_id = self.create_deposit_ready() assert deposits == []
deposit = Deposit.objects.get(pk=deposit_id)
assert deposit.id == deposit_id
def test_delete_on_edit_se_iri_cannot_delete_non_partial_deposit(
# when authenticated_client, deposit_collection, complete_deposit):
url = reverse(EDIT_SE_IRI, args=[self.collection.name, deposit_id]) """Delete !partial deposit should return a 400 response
response = self.client.delete(url)
# then """
self.assertEqual(response.status_code, # given
status.HTTP_400_BAD_REQUEST) deposit = complete_deposit
deposit = Deposit.objects.get(pk=deposit_id)
self.assertIsNotNone(deposit) # when
url = reverse(EDIT_SE_IRI, args=[deposit_collection.name, deposit.id])
response = authenticated_client.delete(url)
# then
assert response.status_code == status.HTTP_400_BAD_REQUEST
deposit = Deposit.objects.get(pk=deposit.id)
assert deposit is not None
...@@ -16,7 +16,7 @@ from rest_framework.test import APIClient ...@@ -16,7 +16,7 @@ from rest_framework.test import APIClient
from swh.scheduler.tests.conftest import * # noqa from swh.scheduler.tests.conftest import * # noqa
from swh.deposit.config import ( from swh.deposit.config import (
COL_IRI, DEPOSIT_STATUS_DEPOSITED, DEPOSIT_STATUS_REJECTED, COL_IRI, EDIT_SE_IRI, DEPOSIT_STATUS_DEPOSITED, DEPOSIT_STATUS_REJECTED,
DEPOSIT_STATUS_PARTIAL, DEPOSIT_STATUS_LOAD_SUCCESS DEPOSIT_STATUS_PARTIAL, DEPOSIT_STATUS_LOAD_SUCCESS
) )
from swh.deposit.tests.common import create_arborescence_archive from swh.deposit.tests.common import create_arborescence_archive
...@@ -169,7 +169,7 @@ def create_deposit( ...@@ -169,7 +169,7 @@ def create_deposit(
@pytest.fixture @pytest.fixture
def deposited_deposit( def deposited_deposit(
sample_archive, deposit_collection, authenticated_client): sample_archive, deposit_collection, authenticated_client):
"""Returns a deposit with status deposited. """Returns a deposit with status 'deposited'.
""" """
deposit = create_deposit( deposit = create_deposit(
...@@ -181,7 +181,7 @@ def deposited_deposit( ...@@ -181,7 +181,7 @@ def deposited_deposit(
@pytest.fixture @pytest.fixture
def rejected_deposit(sample_archive, deposit_collection, authenticated_client): def rejected_deposit(sample_archive, deposit_collection, authenticated_client):
"""Returns a deposit with status rejected. """Returns a deposit with status 'rejected'.
""" """
deposit = create_deposit( deposit = create_deposit(
...@@ -195,7 +195,7 @@ def rejected_deposit(sample_archive, deposit_collection, authenticated_client): ...@@ -195,7 +195,7 @@ def rejected_deposit(sample_archive, deposit_collection, authenticated_client):
@pytest.fixture @pytest.fixture
def partial_deposit(sample_archive, deposit_collection, authenticated_client): def partial_deposit(sample_archive, deposit_collection, authenticated_client):
"""Returns a deposit with status rejected. """Returns a deposit with status 'partial'.
""" """
deposit = create_deposit( deposit = create_deposit(
...@@ -208,6 +208,35 @@ def partial_deposit(sample_archive, deposit_collection, authenticated_client): ...@@ -208,6 +208,35 @@ def partial_deposit(sample_archive, deposit_collection, authenticated_client):
return deposit return deposit
@pytest.fixture
def partial_deposit_with_metadata(
sample_archive, deposit_collection, authenticated_client,
atom_dataset):
"""Returns deposit with archive and metadata provided, status 'partial'
"""
# deposit with one archive
deposit = create_deposit(
authenticated_client, deposit_collection.name, sample_archive,
external_id='external-id-partial'
)
deposit.status = DEPOSIT_STATUS_PARTIAL
deposit.save()
assert deposit.status == DEPOSIT_STATUS_PARTIAL
# update the deposit with metadata
response = authenticated_client.post(
reverse(EDIT_SE_IRI, args=[deposit_collection.name, deposit.id]),
content_type='application/atom+xml;type=entry',
data=atom_dataset['entry-data0'] % deposit.external_id.encode('utf-8'),
HTTP_SLUG=deposit.external_id,
HTTP_IN_PROGRESS='true')
assert response.status_code == status.HTTP_201_CREATED
assert deposit.status == DEPOSIT_STATUS_PARTIAL
return deposit
@pytest.fixture @pytest.fixture
def complete_deposit(sample_archive, deposit_collection, authenticated_client): def complete_deposit(sample_archive, deposit_collection, authenticated_client):
"""Returns a completed deposit (load success) """Returns a completed deposit (load success)
......
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