Skip to content

Commit

Permalink
ad cleanup script
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasprobst committed Dec 13, 2023
1 parent 6fdbfd9 commit e476907
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 35 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ jobs:
ZENODO_SANDBOX_API_TOKEN: ${{ secrets.ZENODO_SANDBOX_API_TOKEN }}
run: pytest --cov --cov-report=xml

- name: Cleanup test repository data
run: python tests/cleanup.py

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
Expand Down
31 changes: 31 additions & 0 deletions tests/cleanup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import requests

from h5rdmtoolbox.repository.zenodo.tokens import get_api_token


def delete_sandbox_deposits():
"""Delete all deposits in the sandbox account."""
r = requests.get(
'https://sandbox.zenodo.org/api/deposit/depositions',
params={'access_token': get_api_token(sandbox=True)}
)
r.raise_for_status()
for deposit in r.json():
try:
# if deposit['title'].startswith('[test]'):
if not deposit['submitted']:
print(f'deleting deposit {deposit["title"]} with id {deposit["id"]}')
r = requests.delete(
'https://sandbox.zenodo.org/api/deposit/depositions/{}'.format(deposit['id']),
params={'access_token': get_api_token(sandbox=True)}
)
else:
print(
f'Cannot delete {deposit["title"]} with id {deposit["id"]} because it is already published."'
)
except Exception as e:
pass


if __name__ == '__main__':
delete_sandbox_deposits()
36 changes: 1 addition & 35 deletions tests/repository/test_zenodo.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import json
import logging
import pathlib
import requests
import unittest
from datetime import datetime

Expand All @@ -13,7 +12,7 @@

logger = logging.getLogger(__name__)

CLEANUP_ZENODO_SANDBOX = True



class TestConfig(unittest.TestCase):
Expand Down Expand Up @@ -137,36 +136,3 @@ def test_ZenodoSandboxDeposit(self):

z.delete()
self.assertFalse(z.exists())

def delete_sandbox_deposits(self):
"""Delete all deposits in the sandbox account."""
r = requests.get(
'https://sandbox.zenodo.org/api/deposit/depositions',
params={'access_token': get_api_token(sandbox=True)}
)
r.raise_for_status()
for deposit in r.json():
try:
# if deposit['title'].startswith('[test]'):
if not deposit['submitted']:
logger.debug(f'deleting deposit {deposit["title"]} with id {deposit["id"]}')
r = requests.delete(
'https://sandbox.zenodo.org/api/deposit/depositions/{}'.format(deposit['id']),
params={'access_token': get_api_token(sandbox=True)}
)
self.assertEqual(204, r.status_code)
else:
logger.debug(
f'Cannot delete {deposit["title"]} with id {deposit["id"]} because it is already published."')
except Exception as e:
pass

def setUp(self) -> None:
"""Delete all deposits in the sandbox account."""
if CLEANUP_ZENODO_SANDBOX:
self.delete_sandbox_deposits()

def tearDown(self) -> None:
"""Delete all deposits in the sandbox account."""
if CLEANUP_ZENODO_SANDBOX:
self.delete_sandbox_deposits()

0 comments on commit e476907

Please sign in to comment.