From 4789f1e1ffa06cd260426902f8c7a1f425c9efe3 Mon Sep 17 00:00:00 2001 From: Serghei Mihai Date: Tue, 10 Oct 2023 10:13:45 +0200 Subject: [PATCH] astech: add endpoint to get view data (#82416) --- passerelle/apps/astech/models.py | 26 ++++++++++++++++++++++ tests/test_astech.py | 38 ++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/passerelle/apps/astech/models.py b/passerelle/apps/astech/models.py index ea063be5..6c2f5f07 100644 --- a/passerelle/apps/astech/models.py +++ b/passerelle/apps/astech/models.py @@ -475,3 +475,29 @@ class ASTech(BaseResource, HTTPResource): column['id'] = column['code'] column['text'] = column['des'] return {'data': columns} + + @endpoint( + name='get-view-data', + display_order=3, + description=_('Get view data'), + display_category=_('Referential'), + datasource=True, + parameters={ + 'code': { + 'description': _('View code'), + 'example_value': 'ASTECH_BIENS', + }, + 'id_column': {'description': _('Name of column contaning the id'), 'example_value': 'BIEN_ID'}, + 'text_column': { + 'description': _('Name of column contaning the label'), + 'example_value': 'DESIGNATION', + }, + }, + ) + def get_view_data(self, request, code, id_column, text_column): + endpoint = 'apicli/data/%s/results' % code + results = self.call(endpoint, json={'data': {'filters': []}}) + for result in results: + result['id'] = result[id_column] + result['text'] = result[text_column] + return {'data': results} diff --git a/tests/test_astech.py b/tests/test_astech.py index 0f3eee2a..228814a6 100644 --- a/tests/test_astech.py +++ b/tests/test_astech.py @@ -88,6 +88,11 @@ COLUMNS_RESPONSE = """ {"code":"ANCETREID","des":"Identifiant de l\u0027anc\u00eatre du bien","type":"NUM","length":""}]} """ +RESULTS_RESPONSE = """[{"BIEN_ID": "2219", "CODE_BIEN": "AC-849-YE", "DESIGNATION": "RENAULT KANGOO"}, +{"BIEN_ID": "2220", "CODE_BIEN": "AC-933-EA", "DESIGNATION": "RENAULT MASTER"}, +{"BIEN_ID": "2221", "CODE_BIEN": "AC-955-SE", "DESIGNATION": "RENAULT KANGOO"}] +""" + @mock.patch('passerelle.utils.Request.request') def test_connections(mocked_request, app, setup): @@ -483,3 +488,36 @@ def test_view_columns(mocked_auth, mocked_request, app, setup): for r in response.json['data']: assert 'id' in r assert 'text' in r + + +@mock.patch('passerelle.utils.Request.request') +@mock.patch('passerelle.apps.astech.models.ASTech.get_authorization') +def test_view_data(mocked_auth, mocked_request, app, setup): + mocked_auth.return_value = {'access_token': '4242', 'connection_id': 'TEST'} + + endpoint = reverse( + 'generic-endpoint', + kwargs={'connector': 'astech', 'slug': setup.slug, 'endpoint': 'get-view-data'}, + ) + mocked_request.return_value = tests.utils.FakedResponse(content=RESULTS_RESPONSE, status_code=200) + response = app.get( + endpoint, params={'code': 'ASTECH_BIENS', 'id_column': 'BIEN_ID', 'text_column': 'DESIGNATION'} + ) + assert mocked_request.call_args[0][0] == 'post' + assert mocked_request.call_args[0][1].endswith('apicli/data/ASTECH_BIENS/results') + assert response.json['data'] + for r in response.json['data']: + assert 'id' in r + assert 'text' in r + + response = app.get( + endpoint, + params={'code': 'ASTECH_BIENS', 'id_column': 'BIEN_ID', 'text_column': 'DESIGNATION', 'id': 2221}, + ) + assert len(response.json['data']) == 1 + + response = app.get( + endpoint, + params={'code': 'ASTECH_BIENS', 'id_column': 'BIEN_ID', 'text_column': 'DESIGNATION', 'q': 'KANGOO'}, + ) + assert len(response.json['data']) == 2