From 07d91247c07772be33b420f509689ce6b266e0c1 Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Mon, 5 Dec 2022 18:01:07 +0100
Subject: [PATCH 2/7] toulouse-maelis: sort referentials on text and id
 (#70982)

---
 passerelle/contrib/toulouse_maelis/models.py |  8 ++-
 tests/test_toulouse_maelis.py                | 58 ++++++++++----------
 2 files changed, 35 insertions(+), 31 deletions(-)

diff --git a/passerelle/contrib/toulouse_maelis/models.py b/passerelle/contrib/toulouse_maelis/models.py
index 14011a14..1478ca20 100644
--- a/passerelle/contrib/toulouse_maelis/models.py
+++ b/passerelle/contrib/toulouse_maelis/models.py
@@ -8,16 +8,17 @@
 # This program is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU Affero General Public License for more details.
 #
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+from operator import itemgetter
 from urllib.parse import urljoin
 
 import zeep
 from django.core.cache import cache
 from django.db import models
 from zeep.helpers import serialize_object
 from zeep.wsse.username import UsernameToken
 
@@ -71,24 +72,24 @@ class ToulouseMaelis(BaseResource, HTTPResource):
         assert self.call('Invoice', 'isWSRunning')
 
     def get_referential(self, referential_name):
 
         # local referentials
         if referential_name == 'Complement':
             response = [
                 {'id': 'B', 'text': 'bis'},
-                {'id': 'T', 'text': 'ter'},
                 {'id': 'Q', 'text': 'quater'},
+                {'id': 'T', 'text': 'ter'},
             ]
             return {'list': response, 'dict': {x['id']: x['text'] for x in response}}
         elif referential_name == 'Sex':
             response = [
-                {'id': 'M', 'text': 'Masculin'},
                 {'id': 'F', 'text': 'Féminin'},
+                {'id': 'M', 'text': 'Masculin'},
             ]
             return {'list': response, 'dict': {x['id']: x['text'] for x in response}}
 
         # remote referentials
         cache_key = 'maelis-%s-%s' % (self.pk, referential_name)
         data = cache.get(cache_key)
         if data is None:
             response = self.call('Family', 'read' + referential_name + 'List')
@@ -101,16 +102,19 @@ class ToulouseMaelis(BaseResource, HTTPResource):
                 data_list = []
                 for item in serialize_object(response):
                     item['id'] = item['code']
                     item['text'] = item['label']
                     data_list.append(item)
             else:
                 data_list = [{'id': x.code, 'text': x.libelle} for x in response]
 
+            # sort list on text
+            data_list.sort(key=itemgetter('text', 'id'))
+
             # remove redundant codes from list
             uniq_text = set()
             uniq_data = []
             for item in data_list:
                 item['text'] = item['text'].strip()
                 if item['text'] not in uniq_text:
                     uniq_data.append(item)
                     uniq_text.add(item['text'])
diff --git a/tests/test_toulouse_maelis.py b/tests/test_toulouse_maelis.py
index 18b5598a..5328b89d 100644
--- a/tests/test_toulouse_maelis.py
+++ b/tests/test_toulouse_maelis.py
@@ -324,48 +324,48 @@ def test_read_country_list(mocked_post, mocked_get, con, app):
     mocked_post.return_value = READ_COUNTRY
     url = get_endpoint('read-country-list')
 
     resp = app.get(url)
     assert resp.json['err'] == 0
     assert len(resp.json['data']) == 3
     assert resp.json['data'] == [
         {'id': '212', 'text': 'AFGHANISTAN'},
-        {'id': '99303', 'text': 'AFRIQUE DU SUD'},
-        {'id': '99125', 'text': 'ALBANIE'},
+        {'id': '303', 'text': 'AFRIQUE DU SUD'},
+        {'id': '125', 'text': 'ALBANIE'},
     ]
 
 
 @mock.patch('passerelle.utils.Request.get')
 @mock.patch('passerelle.utils.Request.post')
 def test_read_child_indicator_list(mocked_post, mocked_get, con, app):
     mocked_get.return_value = FAMILY_SERVICE_WSDL
     mocked_post.return_value = READ_CHILD_INDICATOR
     url = get_endpoint('read-child-indicator-list')
 
     resp = app.get(url)
     assert resp.json['err'] == 0
     assert len(resp.json['data']) == 8
     assert resp.json['data'][0:2] == [
-        {
-            'id': 'APPDENTAIRE',
-            'text': 'Port appareil dentaire',
-            'choiceList': [],
-            'code': 'APPDENTAIRE',
-            'label': 'Port appareil dentaire',
-            'typeDesc': 'NONE',
-        },
         {
             'id': 'AUTRE',
             'text': 'Autre',
             'choiceList': [],
             'code': 'AUTRE',
             'label': 'Autre',
             'typeDesc': 'NOTE',
         },
+        {
+            'id': 'AVL',
+            'text': 'Auxiliaire de Vie loisirs',
+            'choiceList': [],
+            'code': 'AVL',
+            'label': 'Auxiliaire de Vie loisirs',
+            'typeDesc': 'NONE',
+        },
     ]
 
 
 @mock.patch('passerelle.utils.Request.get')
 @mock.patch('passerelle.utils.Request.post')
 def test_read_civility_list(mocked_post, mocked_get, con, app):
     mocked_get.return_value = FAMILY_SERVICE_WSDL
     mocked_post.return_value = READ_CIVILITIES
@@ -381,49 +381,49 @@ def test_read_civility_list(mocked_post, mocked_get, con, app):
 
 def test_read_complement_list(con, app):
     url = get_endpoint('read-complement-list')
 
     resp = app.get(url)
     assert resp.json['err'] == 0
     assert resp.json['data'] == [
         {'id': 'B', 'text': 'bis'},
-        {'id': 'T', 'text': 'ter'},
         {'id': 'Q', 'text': 'quater'},
+        {'id': 'T', 'text': 'ter'},
     ]
 
 
 @mock.patch('passerelle.utils.Request.get')
 @mock.patch('passerelle.utils.Request.post')
 def test_read_csp_list(mocked_post, mocked_get, con, app):
     mocked_get.return_value = FAMILY_SERVICE_WSDL
     mocked_post.return_value = READ_CSP
     url = get_endpoint('read-csp-list')
 
     resp = app.get(url)
     assert resp.json['err'] == 0
     assert resp.json['data'] == [
         {'id': '14', 'text': 'AGENT DE MAITRISE'},
         {'id': '1', 'text': 'AGRICULTEUR'},
-        {'id': 'ARTI', 'text': 'ARTISAN'},
+        {'id': 'ART', 'text': 'ARTISAN'},
         {'id': '2', 'text': 'ARTISAN-COMMERCANT'},
         {'id': '15', 'text': 'AUTRES'},
-        {'id': 'CADR', 'text': 'CADRE'},
+        {'id': '4', 'text': 'CADRE'},
         {'id': '13', 'text': 'CADRE SUPERIEUR'},
         {'id': '3', 'text': "CHEF D'ENTREPRISE"},
         {'id': 'CHOM', 'text': 'CHOMEUR'},
-        {'id': 'COM', 'text': 'COMMERCANT'},
+        {'id': '7', 'text': 'COMMERCANT'},
         {'id': '10', 'text': "DEMANDEUR D'EMPLOI"},
         {'id': 'DIV', 'text': 'DIVERS'},
-        {'id': 'EMP', 'text': 'EMPLOYE'},
-        {'id': 'ENS', 'text': 'ENSEIGNANT'},
-        {'id': 'ETU', 'text': 'ETUDIANT'},
+        {'id': '5', 'text': 'EMPLOYE'},
+        {'id': '17', 'text': 'ENSEIGNANT'},
+        {'id': '8', 'text': 'ETUDIANT'},
         {'id': '11', 'text': 'FONCTIONNAIRE'},
         {'id': 'MAIR', 'text': 'MAIRIE DE NICE'},
-        {'id': 'OUV', 'text': 'OUVRIER'},
+        {'id': '6', 'text': 'OUVRIER'},
         {'id': 'PERENS', 'text': 'PERISCO ENSEIGNANT'},
         {'id': 'PEREXT', 'text': 'PERISCO EXTERNE'},
         {'id': 'PERMAI', 'text': 'PERISCO MAIRIE DE NICE'},
         {'id': 'PERANI', 'text': 'PERISCO S.ANIMATION'},
         {'id': '9', 'text': 'PROFESSION LIBERALE'},
         {'id': '12', 'text': 'RETRAITE'},
         {'id': 'RMI', 'text': "REVENU MINIMUM D'INSERTION"},
         {'id': '16', 'text': 'SANS PROFESSION'},
@@ -436,43 +436,43 @@ def test_read_csp_list(mocked_post, mocked_get, con, app):
 def test_read_dietcode_list(mocked_post, mocked_get, con, app):
     mocked_get.return_value = FAMILY_SERVICE_WSDL
     mocked_post.return_value = READ_DIETCODE
     url = get_endpoint('read-dietcode-list')
 
     resp = app.get(url)
     assert resp.json['err'] == 0
     assert resp.json['data'] == [
-        {'id': 'BB', 'text': 'REPAS BEBE'},
-        {'id': 'MSP', 'text': 'REPAS MOYEN SANS PORC'},
-        {'id': 'MSV', 'text': 'REPAS MOYEN SANS VIANDE'},
-        {'id': 'MST', 'text': 'REPAS MOYEN STANDARD'},
         {'id': 'STD', 'text': '1- REPAS STANDARD'},
         {'id': 'RSP', 'text': '2- RÉGIME SANS PORC'},
         {'id': 'RSV', 'text': '3- RÉGIME SANS VIANDE'},
         {'id': 'PAI', 'text': "4- PROTOCOLE D'ACCUEIL INDIVIDUALISÉ"},
+        {'id': 'BB', 'text': 'REPAS BEBE'},
+        {'id': 'MSP', 'text': 'REPAS MOYEN SANS PORC'},
+        {'id': 'MSV', 'text': 'REPAS MOYEN SANS VIANDE'},
+        {'id': 'MST', 'text': 'REPAS MOYEN STANDARD'},
     ]
 
 
 @mock.patch('passerelle.utils.Request.get')
 @mock.patch('passerelle.utils.Request.post')
 def test_read_organ_list(mocked_post, mocked_get, con, app):
     mocked_get.return_value = FAMILY_SERVICE_WSDL
     mocked_post.return_value = READ_ORGAN
     url = get_endpoint('read-organ-list')
 
     resp = app.get(url)
     assert resp.json['err'] == 0
     assert len(resp.json['data']) == 92
     assert resp.json['data'][:5] == [
-        {'id': 'A10004460232', 'text': 'LEVENS'},
-        {'id': 'A10007752822', 'text': 'LA COLLE SUR LOUP'},
-        {'id': 'A10001133770', 'text': 'ASSIM'},
-        {'id': 'A10007751483', 'text': 'EZE'},
-        {'id': 'A10008152785', 'text': 'LUCERAM'},
+        {'id': 'A10000979374', 'text': 'ALC L OLIVIER'},
+        {'id': 'A10000979372', 'text': 'ALVA 06'},
+        {'id': 'A10000980566', 'text': 'ANTIBES'},
+        {'id': 'A10000980388', 'text': 'APAJH'},
+        {'id': 'A10016401771', 'text': 'ASCROS'},
     ]
 
 
 @mock.patch('passerelle.utils.Request.get')
 @mock.patch('passerelle.utils.Request.post')
 def test_read_pai_list(mocked_post, mocked_get, con, app):
     mocked_get.return_value = FAMILY_SERVICE_WSDL
     mocked_post.return_value = READ_PAI
@@ -568,18 +568,18 @@ def test_read_rl_indicator_list(mocked_post, mocked_get, con, app):
 
 
 def test_read_sex_list(con, app):
     url = get_endpoint('read-sex-list')
 
     resp = app.get(url)
     assert resp.json['err'] == 0
     assert resp.json['data'] == [
-        {'id': 'M', 'text': 'Masculin'},
         {'id': 'F', 'text': 'Féminin'},
+        {'id': 'M', 'text': 'Masculin'},
     ]
 
 
 @mock.patch('passerelle.utils.Request.get')
 @mock.patch('passerelle.utils.Request.post')
 def test_read_situation_list(mocked_post, mocked_get, con, app):
     mocked_get.return_value = FAMILY_SERVICE_WSDL
     mocked_post.return_value = READ_SITUATIONS
@@ -606,18 +606,18 @@ def test_read_street_list(mocked_post, mocked_get, con, app):
     mocked_get.return_value = FAMILY_SERVICE_WSDL
     mocked_post.return_value = READ_STREET
     url = get_endpoint('read-street-list')
 
     resp = app.get(url)
     assert resp.json['err'] == 0
     assert len(resp.json['data']) == 3
     assert resp.json['data'] == [
-        {'id': 'AS0264', 'text': 'RUE SAINT FRANCOIS DE PAULE'},
         {'id': 'AP0594', 'text': 'AVENUE PAULIANI'},
+        {'id': 'AS0264', 'text': 'RUE SAINT FRANCOIS DE PAULE'},
         {'id': 'AM0330', 'text': 'TRAVERSE DES MARAICHERS'},
     ]
 
 
 @mock.patch('passerelle.utils.Request.get')
 @mock.patch('passerelle.utils.Request.post')
 def test_read_vaccin_list(mocked_post, mocked_get, con, app):
     mocked_get.return_value = FAMILY_SERVICE_WSDL
-- 
2.30.2

