Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,34 @@ jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
with:
python-version: '3.6'
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
- name: Install requirements
run: pip install flake8 pycodestyle
- name: Check syntax
run: flake8 . --count --select=E901,E999,F821,F822,F823 --show-source --statistics --exclude ckan

test:
needs: lint
name: CKAN 2.9
name: ckanext-versions tests
runs-on: ubuntu-latest
strategy:
matrix:
include:
- ckan-version: "2.10"
solr-image: "2.10"
- ckan-version: 2.9
solr-image: 2.9
- ckan-version: 2.9
solr-image: 2.9-solr8
fail-fast: false
container:
image: openknowledge/ckan-dev:2.9
image: openknowledge/ckan-dev:${{ matrix.ckan-version }}
services:
solr:
image: ckan/ckan-solr-dev:2.9
image: ckan/ckan-solr:${{ matrix.solr-image }}
postgres:
image: ckan/ckan-postgres-dev:2.9
image: ckan/ckan-postgres-dev:${{ matrix.ckan-version }}
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
Expand All @@ -39,14 +47,18 @@ jobs:
CKAN_REDIS_URL: redis://redis:6379/1

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Install requirements
run: |
pip install -r requirements.txt
pip install -r dev-requirements.txt
pip install -e .
# Replace default path to CKAN core config file with the one on the container
sed -i -e 's/use = config:.*/use = config:\/srv\/app\/src\/ckan\/test-core.ini/' test.ini
- name: Remove activity plugin (CKAN 2.9)
if: ${{ matrix.ckan-version == '2.9' }}
run: |
ckan config-tool test.ini "ckan.plugins=versions image_view"
- name: Setup extension
run: |
ckan -c test.ini db init
Expand Down
13 changes: 13 additions & 0 deletions ckanext/versions/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,16 @@ def download_url(resource_url, version_id):
)

return url


@toolkit.side_effect_free
def get_resource_from_activity(activity_id, resource_id):
"""Get the resource dict from the activity object.
"""
context = {"user": toolkit.g.user}
resource = toolkit.get_action("activity_resource_show")(
context, {
"activity_id": activity_id,
"resource_id": resource_id
})
return resource
32 changes: 20 additions & 12 deletions ckanext/versions/logic/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
from ckan.plugins import toolkit
from sqlalchemy.exc import IntegrityError

try:
from ckanext.activity.model import Activity
except ImportError:
# Remove when dropping support for 2.9.x
from ckan.model import Activity

from ckanext.versions.model import Version

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -186,9 +192,9 @@ def resource_version_create(context, data_dict):

creator_user_id = _get_creator_user_id(data_dict, model, context)

activity = model.Session.query(model.Activity). \
activity = model.Session.query(Activity). \
filter_by(object_id=resource.package_id). \
order_by(model.Activity.timestamp.desc()). \
order_by(Activity.timestamp.desc()). \
first()

if not activity:
Expand Down Expand Up @@ -357,15 +363,15 @@ def resource_history(context, data_dict):

result = []
for version in versions_list:
resource = activity_resource_show(
{'user': context['user']},
{
'activity_id': version['activity_id'],
'resource_id': version['resource_id']
}
)
resource['version'] = version
result.append(resource)
resource = activity_resource_show(
{'user': context['user']},
{
'activity_id': version['activity_id'],
'resource_id': version['resource_id']
}
)
resource['version'] = version
result.append(resource)

return result

Expand Down Expand Up @@ -426,7 +432,9 @@ def resource_in_activity(context, data_dict):
'''
user = context.get('user')
if not user:
site_user = toolkit.get_action('get_site_user')({'ignore_auth': True},{})
site_user = toolkit.get_action('get_site_user')(
{'ignore_auth': True}, {}
)
user = site_user['name']

activity_show_context = {
Expand Down
2 changes: 2 additions & 0 deletions ckanext/versions/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def get_actions(self):
'version_show': action.version_show,
'version_delete': action.version_delete,
'resource_view_list': action.resource_view_list,
'activity_resource_show': action.activity_resource_show
}

# IAuthFunctions
Expand All @@ -77,6 +78,7 @@ def get_helpers(self):
'versions_resource_version_from_activity_id': helpers.resource_version_from_activity_id,
'versions_resource_version_current': helpers.resource_version_current,
'versions_download_url': helpers.download_url,
'versions_get_resource_from_activity': helpers.get_resource_from_activity,
}
return helper_functions

Expand Down
39 changes: 21 additions & 18 deletions ckanext/versions/tests/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,9 @@ def test_resource_has_version(self):
user = factories.Sysadmin()
context = get_context(user)

assert False == resource_has_versions(
assert resource_has_versions(
context, {'resource_id': resource['id']}
)
) is False

resource_version_create(
context, {
Expand All @@ -182,9 +182,9 @@ def test_resource_has_version(self):
}
)

assert True == resource_has_versions(
assert resource_has_versions(
context, {'resource_id': resource['id']}
)
) is True

def test_resource_version_create_creator_user_id_parameter(self):
user = factories.User()
Expand Down Expand Up @@ -270,7 +270,7 @@ def test_resource_version_update_overrides_if_not_provided(self):
)

assert version['name'] == '2.0'
assert version['notes'] == None
assert version['notes'] is None
assert version['creator_user_id'] == new_creator['id']

def test_name_must_be_unique(self):
Expand All @@ -292,19 +292,21 @@ def test_name_must_be_unique(self):
resource_id=resource['id'],
name='1.0',
notes='Another version 1.0'
)
)

def test_version_id_is_mandatory_for_update(self):
with pytest.raises(toolkit.ValidationError) as e:
helpers.call_action('resource_version_update', {}, name='2.0')
assert 'Missing value' in str(e)
assert 'version_id' in str(e)
assert 'Missing value' in str(e.value)
assert 'version_id' in str(e.value)

def test_version_name_is_mandatory_for_update(self):
with pytest.raises(toolkit.ValidationError) as e:
helpers.call_action('resource_version_update', {}, version_id='fake-id')
assert 'Missing value' in str(e)
assert 'name' in str(e)
helpers.call_action(
'resource_version_update', {}, version_id='fake-id'
)
assert 'Missing value' in str(e.value)
assert 'name' in str(e.value)


@pytest.mark.usefixtures('clean_db', 'versions_setup')
Expand Down Expand Up @@ -366,14 +368,14 @@ def test_name_must_be_unique(self):
context,
resource_id=resource['id'],
name='1.0'
)
)

def test_version_id_is_mandatory_for_patch(self):
with pytest.raises(toolkit.ValidationError) as e:
helpers.call_action('resource_version_patch', {}, name='2.0')

assert "Missing value" in str(e)
assert "version_id" in str(e)
assert "Missing value" in str(e.value)
assert "version_id" in str(e.value)


@pytest.mark.usefixtures('clean_db', 'versions_setup')
Expand Down Expand Up @@ -544,6 +546,7 @@ def test_resource_version_clear(self):

assert len(resource_version_list(context, {'resource_id': resource['id']})) == 0


@pytest.mark.usefixtures('clean_db', 'versions_setup')
class TestActivityActions(object):

Expand Down Expand Up @@ -684,20 +687,20 @@ def test_resource_in_activity(self):
)
expected_activity_id = version['activity_id']

assert True == resource_in_activity(context, {
assert resource_in_activity(context, {
'activity_id': expected_activity_id,
'resource_id': resource['id']}
)
) is True

resource_2 = factories.Resource(
package_id=dataset['id'],
name='Resource 2'
)

assert False == resource_in_activity(context, {
assert resource_in_activity(context, {
'activity_id': expected_activity_id,
'resource_id': resource_2['id']}
)
) is False


@pytest.mark.usefixtures('clean_db', 'versions_setup')
Expand Down
Loading