diff --git a/Jenkinsfile b/Jenkinsfile index 68445d0..5d9c7e7 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -2,7 +2,20 @@ pipeline { agent any + options { + disableConcurrentBuilds() + } stages { + stage('Unit Tests') { + steps { + sh 'tox -rv' + } + post { + always { + mergeJunitResults() + } + } + } stage('Packaging') { steps { script { diff --git a/tests/settings.py b/tests/settings.py new file mode 100644 index 0000000..b884068 --- /dev/null +++ b/tests/settings.py @@ -0,0 +1,14 @@ +import os + + +INSTALLED_APPS += ('passerelle_reunion_unicite',) + + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.postgresql_psycopg2', + 'TEST': { + 'NAME': 'passerelle-reunion-unicite-test-%s' % os.environ.get("BRANCH_NAME", "").replace('/', '-')[:63], + } + } +} diff --git a/tests/test_connector.py b/tests/test_connector.py new file mode 100644 index 0000000..c2942f1 --- /dev/null +++ b/tests/test_connector.py @@ -0,0 +1,83 @@ +# -*- coding: utf-8 -*- + +import os + +from django.contrib.contenttypes.models import ContentType +from django.core.cache import cache +import django_webtest +import psycopg2 +from psycopg2 import sql +import pytest + +from passerelle_reunion_unicite.models import UnicityReunionConnector +from passerelle.base.models import ApiUser, AccessRight + + +DB_NAME = 'wcs-reunion-unicite-test-%s' % os.environ.get("BRANCH_NAME", "").replace('/', '-')[:63] +TABLE_NAME = 'test_table' + + +@pytest.fixture +def wcsdb(): + connection = psycopg2.connect(user='', password='', host='', port='', database='') + connection.set_session(autocommit=True) + cursor = connection.cursor() + cursor.execute(sql.SQL('DROP DATABASE IF EXISTS {}').format(sql.Identifier(DB_NAME))) + cursor.execute(sql.SQL('CREATE DATABASE {}').format(sql.Identifier(DB_NAME))) + new_conn = psycopg2.connect(user='', password='', host='', port='', database=DB_NAME) + new_conn.set_session(autocommit=True) + new_cursor = new_conn.cursor() + create_table = ''' +CREATE TABLE {} ( + id char(5) CONSTRAINT xxxx PRIMARY KEY, + f_siren varchar(40) NOT NULL, + f_iban varchar(40) NOT NULL, + status varchar(40) NOT NULL, + annee varchar(40) NOT NULL +) +''' + new_cursor.execute(sql.SQL(create_table).format(sql.Identifier(TABLE_NAME))) + yield new_cursor + new_conn.close() + cursor.execute(sql.SQL('DROP DATABASE {}').format(sql.Identifier(DB_NAME))) + connection.close() + + +@pytest.fixture +def app(request): + wtm = django_webtest.WebTestMixin() + wtm._patch_settings() + cache.clear() + yield django_webtest.DjangoTestApp() + wtm._unpatch_settings() + + +@pytest.fixture +def connector(db): + connector = UnicityReunionConnector.objects.create( + slug='test', form_id=1, db_name=DB_NAME, table_name=TABLE_NAME) + api = ApiUser.objects.create(username='all', keytype='', key='') + obj_type = ContentType.objects.get_for_model(connector) + AccessRight.objects.create( + codename='can_access', apiuser=api, + resource_type=obj_type, resource_pk=connector.pk) + + +def test_unicite_siren_annee(app, connector, wcsdb): + wcsdb.execute( + sql.SQL('INSERT INTO {} VALUES (%s, %s, %s, %s, %s)').format(sql.Identifier(TABLE_NAME)), + ['1', '1', '1', '1', '1'] + ) + resp = app.get('/passerelle-reunion-unicite/test/unicite_siren_annee?siren=1') + assert resp.json['err'] == 0 + assert resp.json['data'] == ['1'] + + +def test_unicite_iban_annee(app, connector, wcsdb): + wcsdb.execute( + sql.SQL('INSERT INTO {} VALUES (%s, %s, %s, %s, %s)').format(sql.Identifier(TABLE_NAME)), + ['1', '1', '1', '1', '1'] + ) + resp = app.get('/passerelle-reunion-unicite/test/unicite_iban_annee?iban=1') + assert resp.json['err'] == 0 + assert resp.json['data'] == ['1'] diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..bffe0f1 --- /dev/null +++ b/tox.ini @@ -0,0 +1,20 @@ +[tox] +toxworkdir = {env:TMPDIR:/tmp}/tox-{env:USER}/passerelle-reunion-unicite/{env:BRANCH_NAME:} +envlist = py2-django111 + +[testenv] +usedevelop = True +basepython = python2 +setenv = + DJANGO_SETTINGS_MODULE=passerelle.settings + PASSERELLE_SETTINGS_FILE=tests/settings.py +deps = + django111: django>=1.11,<1.12 + git+http://git.entrouvert.org/passerelle.git + django-webtest + psycopg2-binary + pytest + pytest-django + xmlschema<1.1 +commands = + django111: py.test {posargs: --junitxml=junit-{envname}.xml tests/}