From 8ecab8f6ed960e5ee3fb7d8029e6def01a7edab2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Fri, 19 Feb 2021 14:58:05 +0100 Subject: [PATCH] add tests --- Jenkinsfile | 42 ++++++++++++++++++++++++++++++++++++++++++ tests/conftest.py | 10 ++++++++++ tests/settings.py | 13 +++++++++++++ tests/test_button.py | 14 ++++++++++++++ tox.ini | 31 +++++++++++++++++++++++++++++++ 5 files changed, 110 insertions(+) create mode 100644 Jenkinsfile create mode 100644 tests/conftest.py create mode 100644 tests/settings.py create mode 100644 tests/test_button.py create mode 100644 tox.ini diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..cd9d2dc --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,42 @@ +@Library('eo-jenkins-lib@main') import eo.Utils + +pipeline { + agent any + options { + disableConcurrentBuilds() + } + stages { + stage('Unit Tests') { + steps { + sh 'tox -rv' + } + post { + always { + mergeJunitResults() + } + } + } + stage('Packaging') { + steps { + script { + if (env.JOB_NAME == 'combo-plugin-imio-townstreet' && env.GIT_BRANCH == 'origin/main') { + sh 'sudo -H -u eobuilder /usr/local/bin/eobuilder combo-plugin-imio-townstreet' + } else if (env.GIT_BRANCH.startsWith('hotfix/')) { + sh "sudo -H -u eobuilder /usr/local/bin/eobuilder --branch ${env.GIT_BRANCH} --hotfix combo-plugin-imio-townstreet" + } + } + } + } + } + post { + always { + script { + utils = new Utils() + utils.mail_notify(currentBuild, env, 'ci+jenkins-combo-plugin-imio-townstreet@entrouvert.org') + } + } + success { + cleanWs() + } + } +} diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..ecb77a2 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,10 @@ +import pytest +import django_webtest + + +@pytest.fixture +def app(request, db): + wtm = django_webtest.WebTestMixin() + wtm._patch_settings() + request.addfinalizer(wtm._unpatch_settings) + return django_webtest.DjangoTestApp() diff --git a/tests/settings.py b/tests/settings.py new file mode 100644 index 0000000..e98b7e0 --- /dev/null +++ b/tests/settings.py @@ -0,0 +1,13 @@ +import os + +INSTALLED_APPS += ('combo_plugin_imio_townstreet',) + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.postgresql_psycopg2', + 'TEST': { + 'NAME': 'combo-plugin-imio-townstreet-test-%s' + % os.environ.get("BRANCH_NAME", "").replace('/', '-')[:40], + }, + } +} diff --git a/tests/test_button.py b/tests/test_button.py new file mode 100644 index 0000000..30f405a --- /dev/null +++ b/tests/test_button.py @@ -0,0 +1,14 @@ +# -*- coding: utf-8 -*- + +from combo.data.models import Page + +from combo_plugin_imio_townstreet.models import TownstreetButton + + +def test_button(app): + page = Page(title='One', slug='index') + page.save() + button = TownstreetButton(page=page, order=0, placeholder='content') + button.save() + resp = app.get('/', status=200) + assert 'Signaler un problème sur l’espace public' in resp diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..e1cb260 --- /dev/null +++ b/tox.ini @@ -0,0 +1,31 @@ +[tox] +envlist = py3-black-junit-coverage +toxworkdir = {env:TMPDIR:/tmp}/tox-{env:USER}/combo-plugin-imio-townstreet/{env:BRANCH_NAME:} + +[testenv] +usedevelop = + coverage: True + nocoverage: False +setenv = + DJANGO_SETTINGS_MODULE=combo.settings + DB_ENGINE=django.db.backends.postgresql_psycopg2 + SETUPTOOLS_USE_DISTUTILS=stdlib + COMBO_SETTINGS_FILE=tests/settings.py + coverage: COVERAGE=--cov-report xml --cov-report html --cov=combo_plugin_imio-townstreet/ + junit: JUNIT=--junitxml=junit-{envname}.xml +deps = + pytest + pytest-cov + pytest-django + django-webtest<1.9.3 + WebTest + mock<4 + psycopg2-binary + psycopg2 + django>=1.11,<2.3 + https://git.entrouvert.org/debian/django-ckeditor.git/snapshot/django-ckeditor-main.tar.gz + https://git.entrouvert.org/combo.git/snapshot/combo-main.tar.gz + pre-commit +commands = + py.test {posargs: {env:JUNIT:} {env:COVERAGE:} tests/} + black: pre-commit run black --all-files --show-diff-on-failure