|
| 1 | +import os |
| 2 | +import pytest |
| 3 | +import requests |
| 4 | + |
| 5 | + |
| 6 | +@pytest.fixture |
| 7 | +def credentials(): |
| 8 | + """Returns the credentials for the Dataverse server.""" |
| 9 | + BASE_URL = os.environ.get("BASE_URL") |
| 10 | + API_TOKEN = os.environ.get("API_TOKEN") |
| 11 | + |
| 12 | + assert BASE_URL, "BASE_URL environment variable must be set" |
| 13 | + assert API_TOKEN, "API_TOKEN environment variable must be set" |
| 14 | + |
| 15 | + return BASE_URL, API_TOKEN |
| 16 | + |
| 17 | + |
| 18 | +def create_dataset( |
| 19 | + parent: str, |
| 20 | + server_url: str, |
| 21 | + api_token: str, |
| 22 | +): |
| 23 | + """ |
| 24 | + Creates a dataset in a Dataverse. |
| 25 | +
|
| 26 | + Args: |
| 27 | + parent (str): The parent Dataverse identifier. |
| 28 | + server_url (str): The URL of the Dataverse server. |
| 29 | + api_token (str): The API token for authentication. |
| 30 | +
|
| 31 | + Returns: |
| 32 | + str: The persistent identifier of the created dataset. |
| 33 | + """ |
| 34 | + if server_url.endswith("/"): |
| 35 | + server_url = server_url[:-1] |
| 36 | + |
| 37 | + url = f"{server_url}/api/dataverses/{parent}/datasets" |
| 38 | + response = requests.post( |
| 39 | + url=url, |
| 40 | + headers={"X-Dataverse-key": api_token}, |
| 41 | + data=open("./tests/fixtures/create_dataset.json", "rb"), |
| 42 | + ) |
| 43 | + |
| 44 | + response.raise_for_status() |
| 45 | + |
| 46 | + return response.json()["data"]["persistentId"] |
| 47 | + |
| 48 | + |
| 49 | +def create_mock_file( |
| 50 | + directory: str, |
| 51 | + name: str, |
| 52 | + size: int = 100, |
| 53 | +): |
| 54 | + """Create a file with the specified size in megabytes. |
| 55 | +
|
| 56 | + Args: |
| 57 | + directory (str): The directory where the file will be created. |
| 58 | + name (str): The name of the file. |
| 59 | + size (int, optional): Size of the file in megabytes. Defaults to 100. |
| 60 | + """ |
| 61 | + |
| 62 | + path = os.path.join(directory, name) |
| 63 | + size = size * 1024 * 1024 |
| 64 | + |
| 65 | + with open(path, "wb") as f: |
| 66 | + f.seek(size - 1) # 1 GB |
| 67 | + f.write(b"\0") |
| 68 | + |
| 69 | + return path |
0 commit comments