From c757033a56706ffcb48020e64fe1f5f8cce8e34e Mon Sep 17 00:00:00 2001
From: Emmanuel Cazenave <ecazenave@entrouvert.com>
Date: Tue, 2 Jul 2019 18:54:07 +0200
Subject: [PATCH 1/2] tests improvements (#33788)

Move tests in their own directory, add code coverage, run tests using postgres.
---
 Jenkinsfile                                   |  6 ++++-
 test_settings.py                              | 11 ++++++---
 django_journal/tests.py => tests/test_main.py | 23 +++++++++----------
 tox.ini                                       | 10 +++++---
 4 files changed, 31 insertions(+), 19 deletions(-)
 rename django_journal/tests.py => tests/test_main.py (72%)

diff --git a/Jenkinsfile b/Jenkinsfile
index a557988..06e5937 100644
--- a/Jenkinsfile
+++ b/Jenkinsfile
@@ -10,7 +10,11 @@ pipeline {
             }
             post {
                 always {
-                    junit 'junit.xml'
+                    script {
+                        utils = new Utils()
+                        utils.publish_coverage('coverage.xml')
+                    }
+                    mergeJunitResults()
                 }
             }
         }
diff --git a/test_settings.py b/test_settings.py
index fa94ae7..e6aeeec 100644
--- a/test_settings.py
+++ b/test_settings.py
@@ -1,5 +1,10 @@
-INSTALLED_APPS = ('django_journal', 'django.contrib.contenttypes' , 'django.contrib.auth', 'django.contrib.sessions', )
-DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3' }, }
+INSTALLED_APPS = (
+    'django_journal', 'django.contrib.contenttypes', 'django.contrib.auth',
+    'django.contrib.sessions'
+)
+DATABASES = {
+    'default': {'ENGINE': 'django.db.backends.postgresql_psycopg2'}
+}
 SECRET_KEY = "django_tests_secret_key"
-PASSWORD_HASHERS = ( 'django.contrib.auth.hashers.MD5PasswordHasher',)
+PASSWORD_HASHERS = ('django.contrib.auth.hashers.MD5PasswordHasher',)
 ROOT_URLCONF = 'test_urls'
diff --git a/django_journal/tests.py b/tests/test_main.py
similarity index 72%
rename from django_journal/tests.py
rename to tests/test_main.py
index 8bc9c89..6e32647 100644
--- a/django_journal/tests.py
+++ b/tests/test_main.py
@@ -3,14 +3,13 @@ from django.contrib.auth.models import User, Group
 from django.db import transaction
 
 
-from . import record
-from . import actions
-from . import models
+from django_journal import record
+from django_journal.actions import export_as_csv_generator
+from django_journal.models import Journal
 
 
 class JournalTestCase(TestCase):
     def setUp(self):
-        models.JOURNAL_METADATA_CACHE_TIMEOUT = 0
         self.users = []
         self.groups = []
         with transaction.atomic():
@@ -30,27 +29,27 @@ class JournalTestCase(TestCase):
                 record('logout', '{user} logged out', user=self.users[i])
 
     def test_login(self):
-        for i, event in zip(range(20), models.Journal.objects.for_tag('login').order_by('id')):
+        for i, event in zip(range(20), Journal.objects.for_tag('login').order_by('id')):
             self.assertEqual(unicode(event), 'user{0} logged in'.format(i))
 
     def test_groups(self):
-        for i, event in zip(range(40), models.Journal.objects.for_tag('group-changed').order_by('id')):
+        for i, event in zip(range(40), Journal.objects.for_tag('group-changed').order_by('id')):
             self.assertEqual(unicode(event),
                     'user{0} gave group group{0} to user{1}'.format(i, (i+1)%20))
 
     def test_logout(self):
-        for i, event in zip(range(20), models.Journal.objects.for_tag('logout').order_by('id')):
+        for i, event in zip(range(20), Journal.objects.for_tag('logout').order_by('id')):
             self.assertEqual(unicode(event), 'user{0} logged out'.format(i))
 
     def test_export_as_csv(self):
-        qs = models.Journal.objects.all()
-        l = list(actions.export_as_csv_generator(qs))
+        qs = Journal.objects.all()
+        l = list(export_as_csv_generator(qs))
         self.assertEquals(set(l[0]), set(['time', 'tag', 'message', 'group', 'group__id', 'user', 'user__id', 'user1', 'user1__id', 'user2', 'user2__id']))
-        l = list(actions.export_as_csv_generator(qs[:5]))
+        l = list(export_as_csv_generator(qs[:5]))
         self.assertEquals(set(l[0]), set(['time', 'tag', 'message', 'user', 'user__id']))
         for user in self.users:
             user.delete()
-        qs = models.Journal.objects.all()
-        l = list(actions.export_as_csv_generator(qs))
+        qs = Journal.objects.all()
+        l = list(export_as_csv_generator(qs))
         self.assertEquals(l[1]['user'], '<deleted>')
 
diff --git a/tox.ini b/tox.ini
index a4b812a..b6dffb0 100644
--- a/tox.ini
+++ b/tox.ini
@@ -1,14 +1,18 @@
 [tox]
-toxworkdir = /tmp/tox-{env:USER}/combo/{env:BRANCH_NAME:}
-envlist = py2
+toxworkdir = /tmp/tox-{env:USER}/django-journal/{env:BRANCH_NAME:}
+envlist = py2-coverage
 
 
 [testenv]
+basepython = python2
 usedevelop = True
 deps =
+  psycopg2
+  pytest-cov
   pytest
   pytest-django
 setenv =
   DJANGO_SETTINGS_MODULE=test_settings
+  coverage: COVERAGE=--cov-append --cov-report xml --cov-report html --cov=django_journal/
 commands =
-  {posargs:py.test --junitxml=junit.xml django_journal/tests.py}
+  py.test {posargs: {env:COVERAGE:} --junitxml=junit-{envname}.xml tests/}
-- 
2.20.1

