From febf1fe7059240a8c931424474d2c3b635a7ef53 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Tue, 7 Dec 2021 09:30:22 +0100
Subject: [PATCH 1/5] wcs: change data logic in tests (#58833)

---
 tests/test_wcs.py | 141 +++++++++++++++++++++++-----------------------
 1 file changed, 71 insertions(+), 70 deletions(-)

diff --git a/tests/test_wcs.py b/tests/test_wcs.py
index 8e86c9fd..9df0f9c8 100644
--- a/tests/test_wcs.py
+++ b/tests/test_wcs.py
@@ -120,32 +120,46 @@ WCS_CARDDEFS_DATA = [
     {'title': 'Card Model 3', 'slug': 'card_model_3'},
 ]
 
-WCS_CARDS_DATA = [
-    {
-        'id': 11,
-        'display_id': '10-11',
-        'display_name': 'Card Model 1 - n°10-11',
-        'digest': 'a a a',
-        'text': 'aa',
-        'url': '/backoffice/data/card_model_1/11/',
-    },
-    {
-        'id': 12,
-        'display_id': '10-12',
-        'display_name': 'Card Model 1 - n°10-12',
-        'digest': 'b b b',
-        'text': 'bb',
-        'url': '/backoffice/data/card_model_1/12/',
-    },
-    {
-        'id': 13,
-        'display_id': '10-13',
-        'display_name': 'Card Model 1 - n°10-13',
-        'digest': 'c c c',
-        'text': 'cc',
-        'url': '/backoffice/data/card_model_1/13/',
-    },
-]
+WCS_CARDS_DATA = {
+    'card_model_1': [
+        {
+            'id': 11,
+            'display_id': '10-11',
+            'display_name': 'Card Model 1 - n°10-11',
+            'digest': 'a a a',
+            'text': 'aa',
+            'url': '/backoffice/data/card_model_1/11/',
+            'fields': {
+                'fielda': 'a',
+                'fieldb': True,
+                'fieldc': '2020-09-28',
+                'fieldd': {'filename': 'file.pdf', 'url': 'http://127.0.0.1:8999/download?f=42'},
+                'fielde': 'lorem<strong>ipsum\n\nhello world',
+                'fieldf': 'lorem<strong>ipsum\n\nhello world',
+                'fieldg': 'test@localhost',
+                'related': 'Foo Bar',
+                'related_raw': 42,
+                'related_structured': {'id': 42, 'text': 'blah'},
+            },
+        },
+        {
+            'id': 12,
+            'display_id': '10-12',
+            'display_name': 'Card Model 1 - n°10-12',
+            'digest': 'b b b',
+            'text': 'bb',
+            'url': '/backoffice/data/card_model_1/12/',
+        },
+        {
+            'id': 13,
+            'display_id': '10-13',
+            'display_name': 'Card Model 1 - n°10-13',
+            'digest': 'c c c',
+            'text': 'cc',
+            'url': '/backoffice/data/card_model_1/13/',
+        },
+    ],
+}
 
 WCS_CARDS_CUSTOM_VIEW_DATA = [
     {
@@ -166,43 +180,24 @@ WCS_CARDS_CUSTOM_VIEW_DATA = [
     },
 ]
 
-WCS_CARDDEF_SCHEMA = {
-    'name': 'Card Model 1',
-    'fields': [
-        {'label': 'Field A', 'varname': 'fielda', 'type': 'string'},
-        {'label': 'Field B', 'varname': 'fieldb', 'type': 'bool'},
-        {'label': 'Field C', 'varname': 'fieldc', 'type': 'date'},
-        {'label': 'Field D', 'varname': 'fieldd', 'type': 'file'},
-        {'label': 'Field E', 'varname': 'fielde', 'type': 'text'},
-        {'label': 'Field F', 'varname': 'fieldf', 'type': 'text', 'pre': True},
-        {'label': 'Field G', 'varname': 'fieldg', 'type': 'email'},
-        {'label': 'Related', 'varname': 'related', 'type': 'item'},
-        {'label': 'Page', 'type': 'page'},
-        {'label': 'Comment', 'type': 'comment'},
-        {'label': 'Title', 'type': 'title'},
-        {'label': 'Subtitle', 'type': 'subtitle'},
-        {'label': 'Empty', 'varname': None, 'type': 'string'},
-    ],
-}
-
-WCS_CARD_DATA = {
-    'id': 11,
-    'display_id': '10-11',
-    'display_name': 'Card Model 1 - n°10-11',
-    'digest': 'a a a',
-    'text': 'aa',
-    'url': '/backoffice/data/card_model_1/11/',
-    'fields': {
-        'fielda': 'a',
-        'fieldb': True,
-        'fieldc': '2020-09-28',
-        'fieldd': {'filename': 'file.pdf', 'url': 'http://127.0.0.1:8999/download?f=42'},
-        'fielde': 'lorem<strong>ipsum\n\nhello world',
-        'fieldf': 'lorem<strong>ipsum\n\nhello world',
-        'fieldg': 'test@localhost',
-        'related': 'Foo Bar',
-        'related_raw': 42,
-        'related_structured': {'id': 42, 'text': 'blah'},
+WCS_CARDDEF_SCHEMAS = {
+    'card_model_1': {
+        'name': 'Card Model 1',
+        'fields': [
+            {'label': 'Field A', 'varname': 'fielda', 'type': 'string'},
+            {'label': 'Field B', 'varname': 'fieldb', 'type': 'bool'},
+            {'label': 'Field C', 'varname': 'fieldc', 'type': 'date'},
+            {'label': 'Field D', 'varname': 'fieldd', 'type': 'file'},
+            {'label': 'Field E', 'varname': 'fielde', 'type': 'text'},
+            {'label': 'Field F', 'varname': 'fieldf', 'type': 'text', 'pre': True},
+            {'label': 'Field G', 'varname': 'fieldg', 'type': 'email'},
+            {'label': 'Related', 'varname': 'related', 'type': 'item'},
+            {'label': 'Page', 'type': 'page'},
+            {'label': 'Comment', 'type': 'comment'},
+            {'label': 'Title', 'type': 'title'},
+            {'label': 'Subtitle', 'type': 'subtitle'},
+            {'label': 'Empty', 'varname': None, 'type': 'string'},
+        ],
     },
 }
 
@@ -247,14 +242,20 @@ def get_data_from_url(url):
         return WCS_USER_DRAFTS_DATA
     if '/api/cards/@list' in url:
         return WCS_CARDDEFS_DATA
-    if '/api/cards/card_model_1/@schema' in url:
-        return WCS_CARDDEF_SCHEMA
-    if 'api/cards/card_model_1/11/' in url:
-        return WCS_CARD_DATA
+    m_schema = re.match(r'/api/cards/([a-z0-9_]+)/@schema', url)
+    if m_schema:
+        return WCS_CARDDEF_SCHEMAS.get(m_schema.group(1)) or {}
+    m_card = re.match(r'/api/cards/([a-z0-9_]+)/(\d+)/', url)
+    if m_card:
+        try:
+            return [d for d in WCS_CARDS_DATA[m_card.group(1)] if d['id'] == int(m_card.group(2))][0]
+        except IndexError:
+            return {}
     if 'api/cards/card_model_1/list/foo' in url:
         return WCS_CARDS_CUSTOM_VIEW_DATA
-    if 'api/cards/card_model_1/list' in url:
-        return WCS_CARDS_DATA
+    m_list = re.match(r'/api/cards/([a-z0-9_]+)/list', url)
+    if m_list:
+        return WCS_CARDS_DATA[m_list.group(1)]
     return []
 
 
@@ -2114,7 +2115,7 @@ def test_card_cell_render_identifier(mock_send, nocache, app):
         for i in range(0, 3):
             assert '/api/cards/card_model_1/list' in mock_send.call_args_list[i * 2 + 1][0][0].url
             assert (
-                '/api/cards/card_model_1/%s/' % WCS_CARDS_DATA[i]['id']
+                '/api/cards/card_model_1/%s/' % WCS_CARDS_DATA['card_model_1'][i]['id']
                 in mock_send.call_args_list[i * 2 + 2][0][0].url
             )
 
-- 
2.33.0

