From 31bbc996f3d3e5d6b155b3271244a19f32b6843f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Tue, 18 Jan 2022 10:36:54 +0100
Subject: [PATCH 6/8] misc: faster tests

---
 tests/backoffice_pages/test_sort.py | 96 ++++++++++++++++++++++-------
 1 file changed, 74 insertions(+), 22 deletions(-)

diff --git a/tests/backoffice_pages/test_sort.py b/tests/backoffice_pages/test_sort.py
index b3a08e1c..a7e2d1a0 100644
--- a/tests/backoffice_pages/test_sort.py
+++ b/tests/backoffice_pages/test_sort.py
@@ -12,7 +12,7 @@ from wcs.qommon.http_request import HTTPRequest
 from wcs.workflows import Workflow, WorkflowCriticalityLevel
 
 from ..utilities import clean_temporary_pub, create_temporary_pub, get_app, login
-from .test_all import create_environment, create_superuser, create_user
+from .test_all import create_superuser
 
 
 @pytest.fixture
@@ -40,17 +40,34 @@ def teardown_module(module):
 
 
 def test_backoffice_listing_order(pub):
+    pub.user_class.wipe()
     create_superuser(pub)
-    create_environment(pub)
-    formdef = FormDef.get_by_urlname('form-title')
+    pub.role_class.wipe()
+    role = pub.role_class(name='test')
+    role.store()
+
+    FormDef.wipe()
+    formdef = FormDef()
+    formdef.name = 'form-title'
+    formdef.fields = []
+    formdef.workflow_roles = {'_receiver': role.id}
+    formdef.store()
+
+    data_class = formdef.data_class()
+    data_class.wipe()
+
     ids = []
-    for i, formdata in enumerate(formdef.data_class().select(order_by='id')):
-        if formdata.status in ('wf-new', 'wf-accepted'):
-            ids.append(formdata.id)
-            formdata.receipt_time = datetime.datetime(2015, 1, 1, 10, i).timetuple()
-            # ordered with odd-numbered ids then even-numbered ids
-            formdata.evolution[-1].time = datetime.datetime(2015, 2, 1, 10 + i % 2, i).timetuple()
-            formdata.store()
+    for i in range(0, 2):
+        formdata = data_class()
+        formdata.data = {}
+        formdata.just_created()
+        formdata.jump_status('new')
+        formdata.store()
+        ids.append(formdata.id)
+        formdata.receipt_time = datetime.datetime(2015, 1, 1, 10, i).timetuple()
+        # ordered with odd-numbered ids then even-numbered ids
+        formdata.evolution[-1].time = datetime.datetime(2015, 2, 1, 10 + i % 2, i).timetuple()
+        formdata.store()
 
     inversed_receipt_time_order = list(reversed([str(x) for x in sorted(ids)]))
     last_update_time_order = [
@@ -59,22 +76,22 @@ def test_backoffice_listing_order(pub):
 
     app = login(get_app(pub))
     resp = app.get('/backoffice/management/form-title/')
-    assert resp.text.count('data-link') == 17
+    assert resp.text.count('data-link') == 2
     ids = [x.strip('/') for x in re.findall(r'data-link="(.*?)"', resp.text)]
     assert ids == inversed_receipt_time_order
 
     resp = app.get('/backoffice/management/form-title/?order_by=receipt_time')
-    assert resp.text.count('data-link') == 17
+    assert resp.text.count('data-link') == 2
     ids = [x.strip('/') for x in re.findall(r'data-link="(.*?)"', resp.text)]
     assert ids == list(reversed(inversed_receipt_time_order))
 
     resp = app.get('/backoffice/management/form-title/?order_by=last_update_time')
-    assert resp.text.count('data-link') == 17
+    assert resp.text.count('data-link') == 2
     ids = [x.strip('/') for x in re.findall(r'data-link="(.*?)"', resp.text)]
     assert ids == last_update_time_order
 
     resp = app.get('/backoffice/management/form-title/?order_by=-last_update_time')
-    assert resp.text.count('data-link') == 17
+    assert resp.text.count('data-link') == 2
     ids = [x.strip('/') for x in re.findall(r'data-link="(.*?)"', resp.text)]
     assert ids == list(reversed(last_update_time_order))
 
@@ -85,7 +102,7 @@ def test_backoffice_listing_order(pub):
             pub.site_options.write(fd)
 
     resp = app.get('/backoffice/management/form-title/')
-    assert resp.text.count('data-link') == 17
+    assert resp.text.count('data-link') == 2
     ids = [x.strip('/') for x in re.findall(r'data-link="(.*?)"', resp.text)]
     assert ids == list(reversed(last_update_time_order))
 
@@ -94,8 +111,11 @@ def test_backoffice_listing_order(pub):
 
 
 def test_backoffice_criticality_in_formdef_listing_order(pub):
-    create_user(pub)
-    create_environment(pub)
+    pub.user_class.wipe()
+    create_superuser(pub)
+    pub.role_class.wipe()
+    role = pub.role_class(name='test')
+    role.store()
 
     wf = Workflow.get_default_workflow()
     wf.id = '2'
@@ -105,10 +125,25 @@ def test_backoffice_criticality_in_formdef_listing_order(pub):
         WorkflowCriticalityLevel(name='red'),
     ]
     wf.store()
-    formdef = FormDef.get_by_urlname('form-title')
+
+    FormDef.wipe()
+    formdef = FormDef()
+    formdef.name = 'form-title'
+    formdef.fields = []
+    formdef.workflow_roles = {'_receiver': role.id}
     formdef.workflow_id = wf.id
     formdef.store()
 
+    data_class = formdef.data_class()
+    data_class.wipe()
+
+    for i in range(0, 4):
+        formdata = data_class()
+        formdata.data = {}
+        formdata.just_created()
+        formdata.jump_status('new')
+        formdata.store()
+
     formdata1, formdata2, formdata3, formdata4 = [
         x for x in formdef.data_class().select() if x.status == 'wf-new'
     ][:4]
@@ -137,9 +172,11 @@ def test_backoffice_criticality_in_formdef_listing_order(pub):
 
 
 def test_backoffice_criticality_in_global_listing_order(pub):
-
-    create_user(pub)
-    create_environment(pub)
+    pub.user_class.wipe()
+    create_superuser(pub)
+    pub.role_class.wipe()
+    role = pub.role_class(name='test')
+    role.store()
 
     wf = Workflow.get_default_workflow()
     wf.id = '2'
@@ -149,10 +186,25 @@ def test_backoffice_criticality_in_global_listing_order(pub):
         WorkflowCriticalityLevel(name='red'),
     ]
     wf.store()
-    formdef = FormDef.get_by_urlname('form-title')
+
+    FormDef.wipe()
+    formdef = FormDef()
+    formdef.name = 'form-title'
+    formdef.fields = []
+    formdef.workflow_roles = {'_receiver': role.id}
     formdef.workflow_id = wf.id
     formdef.store()
 
+    data_class = formdef.data_class()
+    data_class.wipe()
+
+    for i in range(0, 4):
+        formdata = data_class()
+        formdata.data = {}
+        formdata.just_created()
+        formdata.jump_status('new')
+        formdata.store()
+
     formdata1, formdata2, formdata3, formdata4 = [
         x for x in formdef.data_class().select() if x.status == 'wf-new'
     ][:4]
-- 
2.34.1

