From e47690718185d13eb0d3af4bf5a9bd5a9dd96117 Mon Sep 17 00:00:00 2001 From: Matthias Probst Date: Wed, 13 Dec 2023 22:29:43 +0100 Subject: [PATCH] ad cleanup script --- .github/workflows/tests.yml | 3 +++ tests/cleanup.py | 31 ++++++++++++++++++++++++++++ tests/repository/test_zenodo.py | 36 +-------------------------------- 3 files changed, 35 insertions(+), 35 deletions(-) create mode 100644 tests/cleanup.py diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index a5f34921..7542b964 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -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: diff --git a/tests/cleanup.py b/tests/cleanup.py new file mode 100644 index 00000000..e71ac9cb --- /dev/null +++ b/tests/cleanup.py @@ -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() diff --git a/tests/repository/test_zenodo.py b/tests/repository/test_zenodo.py index a9f7a7c6..ffc848eb 100644 --- a/tests/repository/test_zenodo.py +++ b/tests/repository/test_zenodo.py @@ -1,7 +1,6 @@ import json import logging import pathlib -import requests import unittest from datetime import datetime @@ -13,7 +12,7 @@ logger = logging.getLogger(__name__) -CLEANUP_ZENODO_SANDBOX = True + class TestConfig(unittest.TestCase): @@ -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()