Skip to content

Commit

Permalink
Merge pull request #167 from gdcc/dataverse-action-tests
Browse files Browse the repository at this point in the history
Add CI/CD pipeline and re-establish existing tests
  • Loading branch information
JR-1991 authored Dec 6, 2023
2 parents 200eb3a + d85b3d5 commit 7614def
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 19 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/test_build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Unit tests
on: [push]

jobs:
custom_test:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [
"3.7",
"3.8",
"3.9",
"3.10",
"3.11"
]
name: Test pyDataverse
env:
PORT: 8080
steps:
- name: "Checkout"
uses: "actions/checkout@v4"
- name: Run Dataverse Action
id: dataverse
uses: gdcc/dataverse-action@main
- name: Setup Python
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install Python Dependencies
run: |
python3 -m pip install --upgrade pip
python3 -m pip install -r ./requirements/tests.txt
pip install -e .
- name: Run tests
env:
API_TOKEN_SUPERUSER: ${{ steps.dataverse.outputs.api_token }}
API_TOKEN: ${{ steps.dataverse.outputs.api_token }}
BASE_URL: ${{ steps.dataverse.outputs.base_url }}
DV_VERSION: ${{ steps.dataverse.outputs.dv_version }}
run: |
python3 -m pytest
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![PyPI](https://img.shields.io/pypi/v/pyDataverse.svg)](https://pypi.org/project/pyDataverse/) [![Build Status](https://travis-ci.com/gdcc/pyDataverse.svg?branch=master)](https://travis-ci.com/gdcc/pyDataverse) [![Coverage Status](https://coveralls.io/repos/github/gdcc/pyDataverse/badge.svg)](https://coveralls.io/github/gdcc/pyDataverse) [![Documentation Status](https://readthedocs.org/projects/pydataverse/badge/?version=latest)](https://pydataverse.readthedocs.io/en/latest) ![PyPI - Python Version](https://img.shields.io/pypi/pyversions/pydataverse.svg) [![GitHub](https://img.shields.io/github/license/gdcc/pydataverse.svg)](https://opensource.org/licenses/MIT) [![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black) [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.4664557.svg)](https://doi.org/10.5281/zenodo.4664557)
[![PyPI](https://img.shields.io/pypi/v/pyDataverse.svg)](https://pypi.org/project/pyDataverse/) [![Build Status](https://github.com/gdcc/pyDataverse/actions/workflows/test_build.yml/badge.svg)](https://travis-ci.com/gdcc/pyDataverse) [![Coverage Status](https://coveralls.io/repos/github/gdcc/pyDataverse/badge.svg)](https://coveralls.io/github/gdcc/pyDataverse) [![Documentation Status](https://readthedocs.org/projects/pydataverse/badge/?version=latest)](https://pydataverse.readthedocs.io/en/latest) ![PyPI - Python Version](https://img.shields.io/pypi/pyversions/pydataverse.svg) [![GitHub](https://img.shields.io/github/license/gdcc/pydataverse.svg)](https://opensource.org/licenses/MIT) [![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black) [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.4664557.svg)](https://doi.org/10.5281/zenodo.4664557)

# pyDataverse

Expand Down
2 changes: 2 additions & 0 deletions src/pyDataverse/api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Dataverse API wrapper for all it's API's."""
import json
import subprocess as sp
from urllib.parse import urljoin

from requests import ConnectionError, Response, delete, get, post, put

Expand Down Expand Up @@ -118,6 +119,7 @@ def get_request(self, url, params=None, auth=False):
params["key"] = str(self.api_token)

try:
url = urljoin(self.base_url_api, url)
resp = get(url, params=params)
if resp.status_code == 401:
error_msg = resp.json()["message"]
Expand Down
28 changes: 14 additions & 14 deletions tests/api/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ def test_api_connect(self, native_api):
assert isinstance(native_api, NativeApi)
assert not native_api.api_token
assert native_api.api_version == "v1"
assert native_api.base_url == os.getenv("BASE_URL")
assert native_api.base_url == os.getenv("BASE_URL").rstrip("/")
assert native_api.base_url_api_native == "{0}/api/{1}".format(
os.getenv("BASE_URL"), native_api.api_version
os.getenv("BASE_URL").rstrip("/"), native_api.api_version
)

def test_api_connect_base_url_wrong(self):
Expand All @@ -51,7 +51,7 @@ def setup_class(cls):
def test_get_request(self, native_api):
"""Test successfull `.get_request()` request."""
# TODO: test params und auth default
base_url = os.getenv("BASE_URL")
base_url = os.getenv("BASE_URL").rstrip("/")
query_str = base_url + "/api/v1/info/server"
resp = native_api.get_request(query_str)
sleep(test_config["wait_time"])
Expand All @@ -72,11 +72,11 @@ class TestApiToken(object):
"""Test user rights."""

def test_token_missing(self):
BASE_URL = os.getenv("BASE_URL")
BASE_URL = os.getenv("BASE_URL").rstrip("/")
api = NativeApi(BASE_URL)
resp = api.get_info_version()
assert resp.json()["data"]["version"] == "4.18.1"
assert resp.json()["data"]["build"] == "267-a91d370"
assert resp.json()["data"]["version"] == os.getenv("DV_VERSION")
# assert resp.json()["data"]["build"] == "267-a91d370"

with pytest.raises(ApiAuthorizationError):
ds = Dataset()
Expand All @@ -90,11 +90,11 @@ def test_token_missing(self):
api.create_dataset(":root", ds.json())

def test_token_empty_string(self):
BASE_URL = os.getenv("BASE_URL")
BASE_URL = os.getenv("BASE_URL").rstrip("/")
api = NativeApi(BASE_URL, "")
resp = api.get_info_version()
assert resp.json()["data"]["version"] == "4.18.1"
assert resp.json()["data"]["build"] == "267-a91d370"
assert resp.json()["data"]["version"] == os.getenv("DV_VERSION")
# assert resp.json()["data"]["build"] == "267-a91d370"

with pytest.raises(ApiAuthorizationError):
ds = Dataset()
Expand All @@ -112,7 +112,7 @@ def test_token_empty_string(self):
# API_TOKEN = os.getenv("API_TOKEN_NO_RIGHTS")
# api = NativeApi(BASE_URL, API_TOKEN)
# resp = api.get_info_version()
# assert resp.json()["data"]["version"] == "4.18.1"
# assert resp.json()["data"]["version"] == os.getenv("DV_VERSION")
# assert resp.json()["data"]["build"] == "267-a91d370"

# with pytest.raises(ApiAuthorizationError):
Expand All @@ -127,15 +127,15 @@ def test_token_empty_string(self):
# api.create_dataset(":root", ds.json())

def test_token_right_create_dataset_rights(self):
BASE_URL = os.getenv("BASE_URL")
BASE_URL = os.getenv("BASE_URL").rstrip("/")
api_su = NativeApi(BASE_URL, os.getenv("API_TOKEN_SUPERUSER"))
api_nru = NativeApi(BASE_URL, os.getenv("API_TOKEN_TEST_NO_RIGHTS"))

resp = api_su.get_info_version()
assert resp.json()["data"]["version"] == "4.18.1"
assert resp.json()["data"]["build"] == "267-a91d370"
assert resp.json()["data"]["version"] == os.getenv("DV_VERSION")
# assert resp.json()["data"]["build"] == "267-a91d370"
# resp = api_nru.get_info_version()
# assert resp.json()["data"]["version"] == "4.18.1"
# assert resp.json()["data"]["version"] == os.getenv("DV_VERSION")
# assert resp.json()["data"]["build"] == "267-a91d370"

ds = Dataset()
Expand Down
14 changes: 10 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def test_config():
"invalid_json_strings": invalid_filename_strings,
"invalid_data_format_types": invalid_filename_types,
"invalid_data_format_strings": invalid_filename_strings,
"base_url": os.getenv("BASE_URL"),
"base_url": os.getenv("BASE_URL").rstrip("/"),
"api_token": os.getenv("API_TOKEN"),
"travis": os.getenv("TRAVIS") or False,
"wait_time": 1,
Expand All @@ -85,8 +85,11 @@ def native_api(monkeypatch):
Api object.
"""
monkeypatch.setenv("BASE_URL", "https://demo.dataverse.org")
return NativeApi(os.getenv("BASE_URL"))

BASE_URL = os.getenv("BASE_URL").rstrip("/")

monkeypatch.setenv("BASE_URL", BASE_URL)
return NativeApi(BASE_URL)


def import_dataverse_min_dict():
Expand Down Expand Up @@ -132,7 +135,10 @@ def import_datafile_min_dict():
Minimum Datafile metadata.
"""
return {"pid": "doi:10.11587/EVMUHP", "filename": "tests/data/datafile.txt"}
return {
"pid": "doi:10.11587/EVMUHP",
"filename": "tests/data/datafile.txt",
}


def import_datafile_full_dict():
Expand Down

0 comments on commit 7614def

Please sign in to comment.