Skip to content

Commit

Permalink
chore: add playwright to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
smotornyuk committed Jan 17, 2025
1 parent bc20521 commit ccfbb2a
Show file tree
Hide file tree
Showing 23 changed files with 185 additions and 106 deletions.
2 changes: 1 addition & 1 deletion ckanext/toolbelt/cli/ckan/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ def clean(yes: bool, keep: tuple[str]):
if "alembic_version" in table.name:
continue

connection.execute('truncate "%s" cascade' % table.name)
connection.execute(f'truncate "{table.name}" cascade')
model.repo.session.commit()
click.secho("Database table data deleted", fg="green")
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,5 @@ coverage.xml

# Sphinx documentation
docs/_build/
# mkdocs documentation
site/
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,6 @@ ifeq ($(dirty-server),)
else
ckan -c test.ini run -t
endif

test-frontend: ## run e2e tests
pytest -m playwright
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from __future__ import annotations

from typing import Any

import pytest
from playwright.sync_api import expect

expect.set_options(timeout=1000)


@pytest.fixture
def browser_context_args(
browser_context_args: dict[str, Any], ckan_config: dict[str, Any]
):
"""Modify playwright's standard configuration of browser's context."""
browser_context_args["base_url"] = ckan_config["ckan.site_url"]
return browser_context_args
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""Tests for UI."""

from __future__ import annotations

import pytest
from playwright.sync_api import Page

import ckan.plugins.toolkit as tk


@pytest.mark.playwright
def test_homepage(page: Page):
"""Homepage shows about link."""
page.goto(tk.url_for("home.index"))
assert page.get_by_text("about").count()
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ site_url: https://{{ cookiecutter.github_user_name }}.github.io/{{ cookiecutter.
repo_url: https://github.com/{{ cookiecutter.github_user_name }}/{{ cookiecutter.project }}

markdown_extensions:
- admonition
- pymdownx.snippets
- pymdownx.highlight
- pymdownx.inlinehilite
Expand All @@ -26,7 +27,7 @@ plugins:
python:
options:
show_root_full_path: false
show_root_toc_entry: false
show_root_heading: true
show_symbol_type_heading: true
show_source: false
docstring_section_style: spacy
Expand All @@ -35,6 +36,7 @@ plugins:

watch:
- README.md
- ckanext/{{ cookiecutter.project_shortname }}

theme:
name: material
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Homepage = "https://github.com/{{ cookiecutter.github_user_name }}/{{ cookiecutt
[options.extras_require]
test = ["pytest-ckan", "pytest-benchmark", "pytest-pretty"]
docs = ["mkdocs", "mkdocs-material", "pymdown-extensions", "mkdocstrings[python]"]
dev = ["pytest-ckan", "pytest-benchmark", "pytest-pretty", "mkdocs", "mkdocs-material", "pymdown-extensions", "mkdocstrings[python]", "pre-commit"]
dev = ["pytest-ckan", "pytest-benchmark", "pytest-pretty", "mkdocs", "mkdocs-material", "pymdown-extensions", "mkdocstrings[python]", "pre-commit", "pytest-playwright"]

[project.entry-points."ckan.plugins"]
{{ cookiecutter.project_shortname }} = "ckanext.{{ cookiecutter.project_shortname }}.plugin:{{ cookiecutter.plugin_class_name }}"
Expand Down Expand Up @@ -131,7 +131,7 @@ ckanext = ["ckanext"]
self = ["ckanext.{{ cookiecutter.project_shortname }}"]

[tool.pytest.ini_options]
addopts = "--ckan-ini test.ini -m 'not benchmark'"
addopts = "--ckan-ini test_config/test.ini -m 'not benchmark and not playwright'"
filterwarnings = [
"ignore::sqlalchemy.exc.SADeprecationWarning",
"ignore::sqlalchemy.exc.SAWarning",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[app:main]
use = config:../../ckan/test-core.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ smtp_server = localhost
error_email_from = ckan@localhost

[app:main]
use = config:../ckan/test-core.ini
ckan.site_url = http://localhost:5000
# 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 = {{ cookiecutter.project_shortname }}
use = config:project.ini

sqlalchemy.url = postgresql://ckan_default:pass@localhost/ckan_test
ckan.datastore.write_url = postgresql://ckan_default:pass@localhost/datastore_test
ckan.datastore.read_url = postgresql://datastore_default:pass@localhost/datastore_test
solr_url = http://127.0.0.1:8983/solr/ckan
ckan.redis.url = redis://localhost:6379/1

ckan.site_url = http://localhost:5000
ckan.storage_path = /tmp/playwright-ckan-storage

# Logging configuration
[loggers]
Expand Down
2 changes: 1 addition & 1 deletion ckanext/toolbelt/cli/make.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import click

from . import make_config, make_gh_action, make_readme, make_template, make_ckanext
from . import make_ckanext, make_config, make_gh_action, make_readme, make_template


@click.group()
Expand Down
27 changes: 19 additions & 8 deletions ckanext/toolbelt/cli/make_ckanext.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
from __future__ import annotations
from configparser import NoSectionError

import contextlib
import os
import git
import click
from configparser import NoSectionError

import click
import git

TPL_DIR = os.path.join(os.path.dirname(__file__), "cookiecutter")
PROJECT_PREFIX = "ckanext-"


@click.group()
def ckanext():
"""Generate CKAN extension"""
"""Generate CKAN extension."""


@ckanext.command()
Expand All @@ -24,7 +25,10 @@ def ckanext():
)
@click.argument("project", default=PROJECT_PREFIX)
@click.option("-d", "--use-defaults", is_flag=True)
def extended(output_dir: str, project: str, use_defaults: bool):
@click.option("--overwrite-existing", is_flag=True)
def extended(
output_dir: str, project: str, use_defaults: bool, overwrite_existing: bool,
):
"""Generate empty extension files to expand CKAN."""
from cookiecutter.main import cookiecutter

Expand Down Expand Up @@ -64,18 +68,25 @@ def extended(output_dir: str, project: str, use_defaults: bool):
default=defaults["author_email"],
)
defaults["github_user_name"] = click.prompt(
"Your Github user or organization name", default=""
"Your Github user or organization name", default="",
)
defaults["description"] = click.prompt(
"Brief description of the project",
default="",
)

cookiecutter(
result = cookiecutter(
template_loc,
no_input=True,
extra_context=defaults,
output_dir=output_dir,
overwrite_if_exists=overwrite_existing,
)

click.echo(f"Written: {output_dir}")
link_path = os.path.join(result, "test_config", "project.ini")
if os.path.exists(link_path):
os.unlink(link_path)

os.symlink("../config/project.ini", link_path)

click.echo(f"Written: {result}")
10 changes: 5 additions & 5 deletions ckanext/toolbelt/cli/make_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def config():
@_shared.option_plugin
@_shared.option_write
def pyproject(plugin: str, write: bool):
"""pyproject.toml"""
"""pyproject.toml."""
_shared.ensure_root()
plugin = _shared.safe_plugin_name(plugin)
data = {
Expand All @@ -33,7 +33,7 @@ def pyproject(plugin: str, write: bool):
@_shared.option_plugin
@_shared.option_write
def pre_commit(plugin: str, write: bool):
"""Pre-commit (https://pre-commit.com/)"""
"""Pre-commit (https://pre-commit.com/)."""
_shared.ensure_root()
_shared.produce(
*_config_files("pre-commit"),
Expand All @@ -46,7 +46,7 @@ def pre_commit(plugin: str, write: bool):
@_shared.option_plugin
@_shared.option_write
def ckanext_makefile(plugin: str, write: bool):
"""Tools for CKAN extension management"""
"""Tools for CKAN extension management."""
_shared.ensure_root()
_shared.produce(
*_config_files("ckanext-makefile"),
Expand All @@ -59,7 +59,7 @@ def ckanext_makefile(plugin: str, write: bool):
@_shared.option_plugin
@_shared.option_write
def deps_makefile(plugin: str, write: bool):
"""CKAN dependency manager (https://github.com/DataShades/ckan-deps-installer)"""
"""CKAN dependency manager (https://github.com/DataShades/ckan-deps-installer)."""
_shared.ensure_root()
_shared.produce(
*_config_files("deps-makefile"),
Expand All @@ -72,7 +72,7 @@ def deps_makefile(plugin: str, write: bool):
@_shared.option_plugin
@_shared.option_write
def gulp_sass(plugin: str, write: bool):
"""Gulpfile for SCSS-based assets"""
"""Gulpfile for SCSS-based assets."""
_shared.ensure_root()
_shared.produce(
*_config_files("gulp-sass"),
Expand Down
1 change: 0 additions & 1 deletion ckanext/toolbelt/cli/make_readme.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ def config(
plugins: tuple[str, ...],
):
"""Print declared config options for the given plugins."""

from ckan.config.declaration import Declaration
from ckan.config.declaration.serialize import handler

Expand Down
16 changes: 8 additions & 8 deletions ckanext/toolbelt/cli/make_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def template():
@_shared.option_plugin
@option_file
def black(plugin: str, file: str | None):
"""Black configuration"""
"""Black configuration."""
_shared.ensure_root()
_shared.produce(
_shared.template_source("black"),
Expand All @@ -37,7 +37,7 @@ def black(plugin: str, file: str | None):
@_shared.option_plugin
@option_file
def git_changelog(plugin: str, file: str | None):
"""git-changelog configuration"""
"""git-changelog configuration."""
_shared.ensure_root()
_shared.produce(
_shared.template_source("git_changelog"),
Expand All @@ -51,7 +51,7 @@ def git_changelog(plugin: str, file: str | None):
@_shared.option_plugin
@option_file
def isort(plugin: str, file: str | None):
"""Isort configuration"""
"""Isort configuration."""
_shared.ensure_root()
_shared.produce(
_shared.template_source("isort"),
Expand All @@ -65,7 +65,7 @@ def isort(plugin: str, file: str | None):
@_shared.option_plugin
@option_file
def coverage(plugin: str, file: str | None):
"""Isort configuration"""
"""Isort configuration."""
_shared.ensure_root()
_shared.produce(
_shared.template_source("coverage"),
Expand All @@ -79,7 +79,7 @@ def coverage(plugin: str, file: str | None):
@_shared.option_plugin
@option_file
def ruff(plugin: str, file: str | None):
"""Ruff configuration"""
"""Ruff configuration."""
_shared.ensure_root()
_shared.produce(
_shared.template_source("ruff"),
Expand All @@ -93,7 +93,7 @@ def ruff(plugin: str, file: str | None):
@_shared.option_plugin
@option_file
def pyright(plugin: str, file: str | None):
"""Pyrigh configuration"""
"""Pyrigh configuration."""
_shared.ensure_root()
_shared.produce(
_shared.template_source("pyright"),
Expand All @@ -107,7 +107,7 @@ def pyright(plugin: str, file: str | None):
@_shared.option_plugin
@option_file
def pytest(plugin: str, file: str | None):
"""Pytest configuration"""
"""Pytest configuration."""
_shared.ensure_root()
_shared.produce(
_shared.template_source("pytest"),
Expand All @@ -121,7 +121,7 @@ def pytest(plugin: str, file: str | None):
@_shared.option_plugin
@option_file
def commitizen(plugin: str, file: str | None):
"""Commitizen configuration"""
"""Commitizen configuration."""
_shared.ensure_root()
_shared.produce(
_shared.template_source("commitizen"),
Expand Down
4 changes: 2 additions & 2 deletions ckanext/toolbelt/plugins/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from ckan import plugins
from ckan import plugins as p


class ToolbeltPlugin(plugins.SingletonPlugin):
class ToolbeltPlugin(p.SingletonPlugin):
pass
Loading

0 comments on commit ccfbb2a

Please sign in to comment.