Skip to content

Commit

Permalink
[QOLDEV-955] add unit testing for stats reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
ThrawnCA committed Jan 30, 2025
1 parent 82d31e1 commit a7e4959
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .ahoy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ commands:
usage: Run unit tests.
cmd: |
ahoy title 'Run unit tests'
ahoy cli 'pytest --ckan-ini=${CKAN_INI} --cov=ckanext "${APP_DIR}"/ckanext --junit-xml=test/junit/results.xml' || \
ahoy cli 'pytest -vv --ckan-ini=${CKAN_INI} --cov=ckanext "${APP_DIR}"/ckanext --junit-xml=test/junit/results.xml' || \
[ "${ALLOW_UNIT_FAIL:-0}" -eq 1 ]
test-bdd:
Expand Down
14 changes: 7 additions & 7 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,24 @@ jobs:
test:
needs: lint
strategy:
fail-fast: true
fail-fast: false
matrix:
ckan-version: ["2.10"]
solr-version: ["8"]
ckan-version: ['2.10']
solr-version: ['8']
ckan-type: ['vanilla', 'custom']
experimental: [false]
include:
- ckan-version: '2.11'
solr-version: "9"
solr-version: '9'
ckan-type: 'vanilla'
experimental: true
- ckan-version: '2.11'
solr-version: "9"
solr-version: '9'
ckan-type: 'custom'
experimental: false
- ckan-version: 'master'
solr-version: "9"
experimental: true #master is unstable, good to know if we are compatible or not
solr-version: '9'
experimental: true # master is unstable, good to know if we are compatible or not

name: ${{ matrix.experimental && '**Fail_Ignored** ' || '' }} CKAN ${{ matrix.ckan-version }} ${{ matrix.ckan-type }}
runs-on: ubuntu-latest
Expand Down
2 changes: 2 additions & 0 deletions bin/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ set -e
. "${APP_DIR}"/bin/activate
CLICK_ARGS="--yes" ckan_cli db clean
ckan_cli db init
# apply any plugin migrations
ckan_cli db upgrade
2 changes: 1 addition & 1 deletion ckanext/qgov/common/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def resource_report(cls):
resource = table('resource')
group = table('group')
package = table('package')
query = select([group.c.title, package.c.title, resource.c.name, resource.c.url, resource.c.created, resource.c.last_modified, resource.c.format, resource.c.webstore_url, resource.c.resource_type]). \
query = select([group.c.title, package.c.title, resource.c.name, resource.c.url, resource.c.created, resource.c.last_modified, resource.c.format, resource.c.webstore_url if hasattr(resource.c, 'webstore_url') else None, resource.c.resource_type]). \
where(and_(resource.c.package_id == package.c.id,
resource.c.state == 'active',
group.c.id == package.c.owner_org))
Expand Down
88 changes: 88 additions & 0 deletions ckanext/qgov/common/test/test_stats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# encoding: utf-8

'''Tests for the ckanext.qgov extension resource URL filter.
'''

from datetime import datetime
import pytest

from ckan.tests import factories
from ckan.plugins.toolkit import check_ckan_version

from ckanext.qgov.common.stats import Stats


@pytest.fixture()
def migrate_db_for_plugins(migrate_db_for):
if check_ckan_version('2.11'):
migrate_db_for('activity')


@pytest.fixture()
def org(migrate_db_for_plugins):
return factories.Organization()


@pytest.fixture()
def group(migrate_db_for_plugins):
return factories.Group()


@pytest.fixture()
def dataset(org, group):
return factories.Dataset(owner_org=org['id'], groups=[{"id": group['id']}], private=False)


@pytest.fixture()
def resource(dataset):
return factories.Resource(package_id=dataset['id'])


@pytest.mark.usefixtures("with_plugins", "clean_db")
class TestStats():
""" Test our URL validation.
"""

def test_top_groups(self, group, resource):
""" Test that the most-used categories can be retrieved.
"""
# ~ self._create_test_data()
top_categories = Stats().top_categories()
assert len(top_categories) == 1
assert top_categories[0][0].id == group['id']
assert top_categories[0][1] == 1

def test_top_orgs(self, org, resource):
""" Test that the most-used organisations can be retrieved.
"""
# ~ self._create_test_data()
top_orgs = Stats().top_organisations()
assert len(top_orgs) == 1
assert top_orgs[0][0].id == org['id']
assert top_orgs[0][1] == 1

def test_resource_count(self, resource):
""" Test that all resources can be counted.
"""
# ~ self._create_test_data()
assert Stats().resource_count() == 1

def test_resource_report(self, org, dataset, resource):
""" Test that the detailed resource report can be retrieved.
"""
# ~ self._create_test_data()
report = Stats().resource_report()
assert len(report) == 1
created_date = datetime.fromisoformat(resource['created']) if resource['created'] else None
modified_date = datetime.fromisoformat(resource['last_modified']) if resource['last_modified'] else None
assert report[0] == (
org['title'], dataset['title'], resource['name'], resource['url'],
created_date, modified_date,
resource['format'], resource.get('webstore_url'), resource['resource_type']
)

def test_resource_org_count(self, org, resource):
""" Test that the resources in an organisation can be counted.
"""
# ~ self._create_test_data()
assert Stats().resource_org_count(org['id']) == 1
2 changes: 1 addition & 1 deletion test/features/steps/steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def log_in(context):
@when(u'I expand the browser height')
def expand_height(context):
# Work around x=null bug in Selenium set_window_size
context.browser.driver.set_window_rect(x=0, y=0, width=1024, height=3072)
context.browser.driver.set_window_rect(x=0, y=0, width=1366, height=3072)


@when(u'I log in directly')
Expand Down
4 changes: 2 additions & 2 deletions test/features/user_creation.feature
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ Feature: User creation

Scenario: Non logged-in user register to the site.
Given "Unauthenticated" as the persona
When I go to register page
And I expand the browser height
When I expand the browser height
And I go to register page
Then I should see an element with xpath "//input[@name='fullname']"
When I fill in "name" with "publisher_user"
And I fill in "fullname" with "gov user"
Expand Down
4 changes: 2 additions & 2 deletions test/features/users.feature
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ Feature: User APIs

Scenario: Register user password must be 10 characters or longer and contain number, lowercase, capital, and symbol
Given "Unauthenticated" as the persona
When I go to register page
And I expand the browser height
When I expand the browser height
And I go to register page
And I fill in "name" with "name"
And I fill in "fullname" with "fullname"
And I fill in "email" with "[email protected]"
Expand Down

0 comments on commit a7e4959

Please sign in to comment.