Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
S
swh-deposit
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Platform
Development
swh-deposit
Commits
19e290ff
Verified
Commit
19e290ff
authored
5 years ago
by
Antoine R. Dumont
Browse files
Options
Downloads
Patches
Plain Diff
test_deposit_delete: Migrate to pytest
parent
486f1555
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!40
Migrate most deposit tests to pytest
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
swh/deposit/tests/api/test_deposit_delete.py
+111
-103
111 additions, 103 deletions
swh/deposit/tests/api/test_deposit_delete.py
swh/deposit/tests/conftest.py
+33
-4
33 additions, 4 deletions
swh/deposit/tests/conftest.py
with
144 additions
and
107 deletions
swh/deposit/tests/api/test_deposit_delete.py
+
111
−
103
View file @
19e290ff
...
...
@@ -3,111 +3,119 @@
# License: GNU General Public License version 3, or any later version
# See top-level LICENSE file for more information
from
collections
import
defaultdict
from
django.urls
import
reverse
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
DEPOSIT_STATUS_DEPOSITED
from
swh.deposit.config
import
(
EDIT_SE_IRI
,
EM_IRI
,
ARCHIVE_KEY
,
METADATA_KEY
,
DEPOSIT_STATUS_DEPOSITED
)
from
swh.deposit.models
import
Deposit
,
DepositRequest
from
..common
import
BasicTestCase
,
WithAuthTestCase
,
CommonCreationRoutine
class
DepositDeleteTest
(
APITestCase
,
WithAuthTestCase
,
BasicTestCase
,
CommonCreationRoutine
):
def
test_delete_archive_on_partial_deposit_works
(
self
):
"""
Removing partial deposit
'
s archive should return a 204 response
"""
# given
deposit_id
=
self
.
create_deposit_partial
()
deposit
=
Deposit
.
objects
.
get
(
pk
=
deposit_id
)
deposit_requests
=
DepositRequest
.
objects
.
filter
(
deposit
=
deposit
)
self
.
assertEqual
(
len
(
deposit_requests
),
2
)
for
dr
in
deposit_requests
:
if
dr
.
type
==
ARCHIVE_KEY
:
continue
elif
dr
.
type
==
METADATA_KEY
:
continue
else
:
self
.
fail
(
'
only archive and metadata type should exist
'
'
in this test context
'
)
# when
update_uri
=
reverse
(
EM_IRI
,
args
=
[
self
.
collection
.
name
,
deposit_id
])
response
=
self
.
client
.
delete
(
update_uri
)
# then
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_204_NO_CONTENT
)
deposit
=
Deposit
.
objects
.
get
(
pk
=
deposit_id
)
requests
=
list
(
DepositRequest
.
objects
.
filter
(
deposit
=
deposit
))
self
.
assertEqual
(
len
(
requests
),
2
)
self
.
assertEqual
(
requests
[
0
].
type
,
'
metadata
'
)
self
.
assertEqual
(
requests
[
1
].
type
,
'
metadata
'
)
def
test_delete_archive_on_undefined_deposit_fails
(
self
):
"""
Delete undefined deposit returns a 404 response
"""
# when
update_uri
=
reverse
(
EM_IRI
,
args
=
[
self
.
collection
.
name
,
999
])
response
=
self
.
client
.
delete
(
update_uri
)
# then
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_404_NOT_FOUND
)
def
test_delete_archive_on_non_partial_deposit_fails
(
self
):
"""
Delete !partial status deposit should return a 400 response
"""
deposit_id
=
self
.
create_deposit_ready
()
deposit
=
Deposit
.
objects
.
get
(
pk
=
deposit_id
)
self
.
assertEqual
(
deposit
.
status
,
DEPOSIT_STATUS_DEPOSITED
)
# when
update_uri
=
reverse
(
EM_IRI
,
args
=
[
self
.
collection
.
name
,
deposit_id
])
response
=
self
.
client
.
delete
(
update_uri
)
# then
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_400_BAD_REQUEST
)
deposit
=
Deposit
.
objects
.
get
(
pk
=
deposit_id
)
self
.
assertIsNotNone
(
deposit
)
def
test_delete_partial_deposit_works
(
self
):
"""
Delete deposit should return a 204 response
"""
# given
deposit_id
=
self
.
create_simple_deposit_partial
()
deposit
=
Deposit
.
objects
.
get
(
pk
=
deposit_id
)
assert
deposit
.
id
==
deposit_id
# when
url
=
reverse
(
EDIT_SE_IRI
,
args
=
[
self
.
collection
.
name
,
deposit_id
])
response
=
self
.
client
.
delete
(
url
)
# then
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_204_NO_CONTENT
)
deposit_requests
=
list
(
DepositRequest
.
objects
.
filter
(
deposit
=
deposit
))
self
.
assertEqual
(
deposit_requests
,
[])
deposits
=
list
(
Deposit
.
objects
.
filter
(
pk
=
deposit_id
))
self
.
assertEqual
(
deposits
,
[])
def
test_delete_on_edit_se_iri_cannot_delete_non_partial_deposit
(
self
):
"""
Delete !partial deposit should return a 400 response
"""
# given
deposit_id
=
self
.
create_deposit_ready
()
deposit
=
Deposit
.
objects
.
get
(
pk
=
deposit_id
)
assert
deposit
.
id
==
deposit_id
# when
url
=
reverse
(
EDIT_SE_IRI
,
args
=
[
self
.
collection
.
name
,
deposit_id
])
response
=
self
.
client
.
delete
(
url
)
# then
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_400_BAD_REQUEST
)
deposit
=
Deposit
.
objects
.
get
(
pk
=
deposit_id
)
self
.
assertIsNotNone
(
deposit
)
def
count_deposit_request_types
(
deposit_requests
)
->
Mapping
[
str
,
int
]:
deposit_request_types
=
defaultdict
(
int
)
for
dr
in
deposit_requests
:
deposit_request_types
[
dr
.
type
]
+=
1
return
deposit_request_types
def
test_delete_archive_on_partial_deposit_works
(
authenticated_client
,
partial_deposit_with_metadata
,
deposit_collection
):
"""
Removing partial deposit
'
s archive should return a 204 response
"""
deposit_id
=
partial_deposit_with_metadata
.
id
deposit
=
Deposit
.
objects
.
get
(
pk
=
deposit_id
)
deposit_requests
=
DepositRequest
.
objects
.
filter
(
deposit
=
deposit
)
# deposit request type: 'archive', 1 'metadata'
deposit_request_types
=
count_deposit_request_types
(
deposit_requests
)
assert
deposit_request_types
==
{
ARCHIVE_KEY
:
1
,
METADATA_KEY
:
1
}
# when
update_uri
=
reverse
(
EM_IRI
,
args
=
[
deposit_collection
.
name
,
deposit_id
])
response
=
authenticated_client
.
delete
(
update_uri
)
# then
assert
response
.
status_code
==
status
.
HTTP_204_NO_CONTENT
deposit
=
Deposit
.
objects
.
get
(
pk
=
deposit_id
)
deposit_requests2
=
DepositRequest
.
objects
.
filter
(
deposit
=
deposit
)
deposit_request_types
=
count_deposit_request_types
(
deposit_requests2
)
assert
deposit_request_types
==
{
METADATA_KEY
:
1
}
def
test_delete_archive_on_undefined_deposit_fails
(
authenticated_client
,
deposit_collection
,
sample_archive
):
"""
Delete undefined deposit returns a 404 response
"""
# when
update_uri
=
reverse
(
EM_IRI
,
args
=
[
deposit_collection
.
name
,
999
])
response
=
authenticated_client
.
delete
(
update_uri
)
# then
assert
response
.
status_code
==
status
.
HTTP_404_NOT_FOUND
def
test_delete_non_partial_deposit
(
authenticated_client
,
deposit_collection
,
deposited_deposit
):
"""
Delete !partial status deposit should return a 400 response
"""
deposit
=
deposited_deposit
assert
deposit
.
status
==
DEPOSIT_STATUS_DEPOSITED
# when
update_uri
=
reverse
(
EM_IRI
,
args
=
[
deposit_collection
.
name
,
deposit
.
id
])
response
=
authenticated_client
.
delete
(
update_uri
)
# then
assert
response
.
status_code
==
status
.
HTTP_400_BAD_REQUEST
deposit
=
Deposit
.
objects
.
get
(
pk
=
deposit
.
id
)
assert
deposit
is
not
None
def
test_delete_partial_deposit
(
authenticated_client
,
deposit_collection
,
partial_deposit
):
"""
Delete deposit should return a 204 response
"""
# given
deposit
=
partial_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_204_NO_CONTENT
deposit_requests
=
list
(
DepositRequest
.
objects
.
filter
(
deposit
=
deposit
))
assert
deposit_requests
==
[]
deposits
=
list
(
Deposit
.
objects
.
filter
(
pk
=
deposit
.
id
))
assert
deposits
==
[]
def
test_delete_on_edit_se_iri_cannot_delete_non_partial_deposit
(
authenticated_client
,
deposit_collection
,
complete_deposit
):
"""
Delete !partial deposit should return a 400 response
"""
# given
deposit
=
complete_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
This diff is collapsed.
Click to expand it.
swh/deposit/tests/conftest.py
+
33
−
4
View file @
19e290ff
...
...
@@ -16,7 +16,7 @@ from rest_framework.test import APIClient
from
swh.scheduler.tests.conftest
import
*
# noqa
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
)
from
swh.deposit.tests.common
import
create_arborescence_archive
...
...
@@ -169,7 +169,7 @@ def create_deposit(
@pytest.fixture
def
deposited_deposit
(
sample_archive
,
deposit_collection
,
authenticated_client
):
"""
Returns a deposit with status deposited.
"""
Returns a deposit with status
'
deposited
'
.
"""
deposit
=
create_deposit
(
...
...
@@ -181,7 +181,7 @@ def deposited_deposit(
@pytest.fixture
def
rejected_deposit
(
sample_archive
,
deposit_collection
,
authenticated_client
):
"""
Returns a deposit with status rejected.
"""
Returns a deposit with status
'
rejected
'
.
"""
deposit
=
create_deposit
(
...
...
@@ -195,7 +195,7 @@ def rejected_deposit(sample_archive, deposit_collection, authenticated_client):
@pytest.fixture
def
partial_deposit
(
sample_archive
,
deposit_collection
,
authenticated_client
):
"""
Returns a deposit with status
rejected
.
"""
Returns a deposit with status
'
partial
'
.
"""
deposit
=
create_deposit
(
...
...
@@ -208,6 +208,35 @@ def partial_deposit(sample_archive, deposit_collection, authenticated_client):
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
def
complete_deposit
(
sample_archive
,
deposit_collection
,
authenticated_client
):
"""
Returns a completed deposit (load success)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment