From bfc1105b108e265ff9164dae527e84f44ab56994 Mon Sep 17 00:00:00 2001 From: duskobogdanovski Date: Fri, 5 Mar 2021 18:02:33 +0100 Subject: [PATCH] Setup tests --- .github/workflows/ci.yml | 135 ++++++++++++++++++ README.md | 2 +- bin/setup-ckan.bash | 62 ++++++++ .../resourcedictionary/tests/test_plugin.py | 53 ------- .../tests/views/test_resource_dictionary.py | 61 +++++++- conftest.py | 2 + test.ini | 2 +- 7 files changed, 257 insertions(+), 60 deletions(-) create mode 100755 bin/setup-ckan.bash delete mode 100644 ckanext/resourcedictionary/tests/test_plugin.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e69de29..fe5a9b2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -0,0 +1,135 @@ +name: CI + +on: + push: + branches: main + tags: + - 'v*' + pull_request: + branches: main + +env: + CKANVERSION: 2.9 + +jobs: + code_quality: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + + - name: Setup Python + uses: actions/setup-python@v2 + with: + python-version: '3.8' + + - name: Install flake8 + run: | + python -m pip install --upgrade pip + pip install flake8 + + - name: Lint with flake8 + run: | + flake8 . --count --max-complexity=10 --max-line-length=127 --statistics --exclude ckan,ckanext-resourcedictionary + + test: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: [ '3.6', '3.7', '3.8' ] + name: Python ${{ matrix.python-version }} extension test + + services: + postgresql: + image: postgres + env: + POSTGRES_PASSWORD: postgres + options: >- + --health-cmd pg_isready + --health-interval 10s + --health-timeout 5s + --health-retries 5 + ports: + - 5432:5432 + + redis: + image: redis + options: >- + --health-cmd "redis-cli ping" + --health-interval 10s + --health-timeout 5s + --health-retries 5 + ports: + - 6379:6379 + + ckan-solr: + # Workflow level env variables are not addressable on job level, only on steps level + # image: ghcr.io/keitaroinc/ckan-solr-dev:{{ env.CKANVERSION }} + image: ghcr.io/keitaroinc/ckan-solr-dev:2.9 + ports: + - 8983:8983 + + steps: + - uses: actions/checkout@v2 + + - name: Setup Python + uses: actions/setup-python@v2 + with: + python-version: ${{ matrix.python-version }} + architecture: x64 + + - name: Install pytest + run: | + python -m pip install --upgrade pip + pip install pytest + + - name: Setup CKAN + env: + PGPASSWORD: postgres + run: | + bash bin/setup-ckan.bash + + - name: Test with pytest + run: | + pytest --ckan-ini=subdir/test.ini --cov=ckanext.resourcedictionary --disable-warnings ckanext/resourcedictionary/tests + + - name: Coveralls + uses: AndreMiras/coveralls-python-action@develop + with: + parallel: true + flag-name: Python ${{ matrix.python-version }} Unit Test + + publish: + needs: test + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + + - name: Setup Python + uses: actions/setup-python@v2 + with: + python-version: '3.8' + + - name: Install setup requirements + run: | + python -m pip install --upgrade setuptools wheel twine + + - name: Build and package + run: | + python setup.py sdist bdist_wheel + twine check dist/* + + - name: Publish package + if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags') + uses: pypa/gh-action-pypi-publish@release/v1 + with: + user: __token__ + password: ${{ secrets.PYPI_API_TOKEN }} + + coveralls_finish: + needs: test + runs-on: ubuntu-latest + steps: + - name: Coveralls Finished + uses: AndreMiras/coveralls-python-action@develop + with: + parallel-finished: true diff --git a/README.md b/README.md index 3f58621..10f2892 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -[![Tests](https://github.com/keitaroinc/ckanext-resourcedictionary/workflows/Tests/badge.svg?branch=main)](https://github.com/keitaroinc/ckanext-resourcedictionary/actions) +[![Tests](https://github.com/keitaroinc/ckanext-resourcedictionary/workflows/CI/badge.svg?branch=main)](https://github.com/keitaroinc/ckanext-resourcedictionary/actions) # ckanext-resourcedictionary diff --git a/bin/setup-ckan.bash b/bin/setup-ckan.bash new file mode 100755 index 0000000..a6d9f02 --- /dev/null +++ b/bin/setup-ckan.bash @@ -0,0 +1,62 @@ +#!/bin/bash +set -e + +echo "This is setup-ckan.bash..." + +echo "Installing the packages that CKAN requires..." +sudo apt-get update -qq +sudo apt-get install xmlsec1 libxmlsec1-dev + +echo "Installing CKAN and its Python dependencies..." +git clone https://github.com/ckan/ckan +cd ckan +if [ $CKANVERSION == 'master' ] +then + echo "CKAN version: master" +else + CKAN_TAG=$(git tag | grep ^ckan-$CKANVERSION | sort --version-sort | tail -n 1) + git checkout $CKAN_TAG + echo "CKAN version: ${CKAN_TAG#ckan-}" +fi + +# install the recommended version of setuptools +if [ -f requirement-setuptools.txt ] +then + echo "Updating setuptools..." + pip install -r requirement-setuptools.txt +fi + +if [ $CKANVERSION == '2.7' ] +then + echo "Installing setuptools" + pip install setuptools==39.0.1 +fi + +python setup.py develop +pip install -r requirements.txt +pip install -r dev-requirements.txt +cd - + +echo "Creating the PostgreSQL user and database..." +psql -h localhost -U postgres -c "CREATE USER ckan_default WITH PASSWORD 'pass';" +psql -h localhost -U postgres -c "CREATE USER datastore_default WITH PASSWORD 'pass';" +psql -h localhost -U postgres -c 'CREATE DATABASE ckan_test WITH OWNER ckan_default;' +psql -h localhost -U postgres -c 'CREATE DATABASE datastore_default WITH OWNER ckan_default;' + +echo "Setting datastore permissions..." +cd ckan +ckan -c test-core.ini datastore set-permissions + +echo "Initialising the database..." +ckan -c test-core.ini db init +cd - + +echo "Installing ckanext-resourcedictionary and its requirements..." +python setup.py develop +pip install -r dev-requirements.txt + +echo "Moving test.ini into a subdir..." +mkdir subdir +mv test.ini subdir + +echo "setup-ckan.bash is done." diff --git a/ckanext/resourcedictionary/tests/test_plugin.py b/ckanext/resourcedictionary/tests/test_plugin.py deleted file mode 100644 index 97d93e7..0000000 --- a/ckanext/resourcedictionary/tests/test_plugin.py +++ /dev/null @@ -1,53 +0,0 @@ -""" -Tests for plugin.py. - -Tests are written using the pytest library (https://docs.pytest.org), and you -should read the testing guidelines in the CKAN docs: -https://docs.ckan.org/en/2.9/contributing/testing.html - -To write tests for your extension you should install the pytest-ckan package: - - pip install pytest-ckan - -This will allow you to use CKAN specific fixtures on your tests. - -For instance, if your test involves database access you can use `clean_db` to -reset the database: - - import pytest - - from ckan.tests import factories - - @pytest.mark.usefixtures("clean_db") - def test_some_action(): - - dataset = factories.Dataset() - - # ... - -For functional tests that involve requests to the application, you can use the -`app` fixture: - - from ckan.plugins import toolkit - - def test_some_endpoint(app): - - url = toolkit.url_for('myblueprint.some_endpoint') - - response = app.get(url) - - assert response.status_code == 200 - - -To temporary patch the CKAN configuration for the duration of a test you can use: - - import pytest - - @pytest.mark.ckan_config("ckanext.myext.some_key", "some_value") - def test_some_action(): - pass -""" -import ckanext.resourcedictionary.plugin as plugin - -def test_plugin(): - pass diff --git a/ckanext/resourcedictionary/tests/views/test_resource_dictionary.py b/ckanext/resourcedictionary/tests/views/test_resource_dictionary.py index e837c6f..9c7c5f4 100644 --- a/ckanext/resourcedictionary/tests/views/test_resource_dictionary.py +++ b/ckanext/resourcedictionary/tests/views/test_resource_dictionary.py @@ -1,12 +1,63 @@ # encoding: utf-8 +import six import pytest from ckan.tests import factories -from ckan.plugins import toolkit +import ckan.tests.helpers as h +from ckan.lib.helpers import url_for -@pytest.mark.ckan_config("ckan.plugins", "datastore") @pytest.mark.usefixtures(u'clean_db', u'clean_index', - u'clean_datastore', u'with_plugins') -def test_create(self, app): - pass + u'clean_datastore',) +def test_create_new_resource_dictionary_successfully(app): + + user = factories.Sysadmin() + env = {u'REMOTE_USER': six.ensure_str(user[u'name'])} + + context = { + u'user': six.ensure_str(user[u'name']), + u'ignore_auth': True + } + users = [{ + u'name': six.ensure_str(user[u'name']), + u'capacity': 'admin' + }] + + organization = h.call_action(u'organization_create', + context, + name='organization', + users=users) + + package = h.call_action(u'package_create', + context, + name=u'package', + owner_org=organization[u'id']) + + resource = h.call_action(u'resource_create', + context, + name=u'resource', + package_id=package[u'id']) + + post_data = { + u'field__1__id': u'Name', + u'info__1__type': u'text', + u'info__1__type_override': u'', + u'info__1__label': u'Name Label', + u'info__1__notes': u'Name Field Description', + u'field__2__id': u'Lastname', + u'info__2__type': u'text', + u'info__2__type_override': u'', + u'info__2__label': u'Lastname Label', + u'info__2__notes': u'Lastname Field Description', + } + + url = url_for(u'resource_dictionary.dictionary', + id=package[u'id'], + resource_id=resource[u'id']) + + res = app.post( + url, + data=post_data, + extra_environ=env + ) + assert 200 == res.status_code diff --git a/conftest.py b/conftest.py index fae1fc4..61b0c56 100644 --- a/conftest.py +++ b/conftest.py @@ -1,6 +1,8 @@ # -*- coding: utf-8 -*- pytest_plugins = [ + u'ckanext.resourcedictionary.tests.fixtures', + u'ckanext.datastore.tests.conftest', u'ckan.tests.pytest_ckan.ckan_setup', u'ckan.tests.pytest_ckan.fixtures', ] diff --git a/test.ini b/test.ini index f39f28d..49b330f 100644 --- a/test.ini +++ b/test.ini @@ -8,7 +8,7 @@ use = config:../ckan/test-core.ini # Insert any custom config settings to be used when running your extension's # tests here. These will override the one defined in CKAN core's test-core.ini -ckan.plugins = resourcedictionary +ckan.plugins = resourcedictionary datastore # Logging configuration