Projet

Général

Profil

0001-toulouse_axel-new-endpoint-clae_children_activities_.patch

Lauréline Guérin, 27 février 2020 14:31

Télécharger (6,72 ko)

Voir les différences:

Subject: [PATCH] toulouse_axel: new endpoint clae_children_activities_info
 (#40229)

 passerelle/contrib/toulouse_axel/models.py | 23 +++++-
 tests/test_toulouse_axel.py                | 95 ++++++++++++++++++++++
 2 files changed, 117 insertions(+), 1 deletion(-)
passerelle/contrib/toulouse_axel/models.py
1014 1014
        return {child_id: bool(child.get('ACTIVITE', [])) for child_id, child in children_activities.items()}
1015 1015

  
1016 1016
    @endpoint(
1017
        description=_("Get information about CLAE booking"),
1017
        description=_("Get information about CLAE activities for all children"),
1018
        perm='can_access',
1019
        parameters={
1020
            'NameID': {'description': _('Publik ID')},
1021
            'booking_date': {'description': _('Booking date')},
1022
        })
1023
    def clae_children_activities_info(self, request, NameID, booking_date):
1024
        link = self.get_link(NameID)
1025
        try:
1026
            booking_date = datetime.datetime.strptime(booking_date, utils.json_date_format)
1027
        except ValueError:
1028
            raise APIError('bad date format', err_code='bad-request', http_status=400)
1029

  
1030
        reference_year = utils.get_reference_year_from_date(booking_date)
1031
        children_activities = self.get_children_activities(dui=link.dui, reference_year=reference_year)
1032
        for child in children_activities.values():
1033
            child['id'] = child['IDPERSONNE']
1034
            child['text'] = '{} {}'.format(child['PRENOM'], child['NOM']).strip()
1035
        return {'data': children_activities.values()}
1036

  
1037
    @endpoint(
1038
        description=_("Get information about CLAE booking for a child"),
1018 1039
        perm='can_access',
1019 1040
        parameters={
1020 1041
            'NameID': {'description': _('Publik ID')},
tests/test_toulouse_axel.py
1931 1931
    }
1932 1932

  
1933 1933

  
1934
def test_clae_children_activities_info_endpoint_axel_error(app, resource):
1935
    Link.objects.create(resource=resource, name_id='yyy', dui='XXX', person_id='42')
1936
    with mock.patch('passerelle.contrib.toulouse_axel.models.enfants_activites') as operation:
1937
        operation.side_effect = AxelError('FooBar')
1938
        resp = app.get('/toulouse-axel/test/clae_children_activities_info?NameID=yyy&booking_date=2020-01-20')
1939
    assert resp.json['err_desc'] == "Axel error: FooBar"
1940
    assert resp.json['err'] == 'error'
1941

  
1942

  
1943
@pytest.mark.parametrize('value', ['foo', '20/01/2020', '2020'])
1944
def test_clae_children_activities_info_endpoint_bad_date_format(app, resource, value):
1945
    Link.objects.create(resource=resource, name_id='yyy', dui='XXX', person_id='42')
1946
    resp = app.get('/toulouse-axel/test/clae_children_activities_info?NameID=yyy&booking_date=%s' % value, status=400)
1947
    assert resp.json['err_desc'] == "bad date format"
1948
    assert resp.json['err'] == 'bad-request'
1949

  
1950

  
1951
def test_clae_children_activities_info_endpoint_no_result(app, resource, child_activities_data):
1952
    resp = app.get('/toulouse-axel/test/clae_children_activities_info?NameID=yyy&booking_date=2020-01-20')
1953
    assert resp.json['err_desc'] == "Person not found"
1954
    assert resp.json['err'] == 'not-found'
1955

  
1956

  
1957
def test_clae_children_activities_info_endpoint(app, resource, child_activities_data):
1958
    Link.objects.create(resource=resource, name_id='yyy', dui='XXX', person_id='42')
1959
    filepath = os.path.join(os.path.dirname(__file__), 'data/toulouse_axel/child_activities.xml')
1960
    with open(filepath) as xml:
1961
        content = xml.read()
1962
    with mock_getdata(content, 'EnfantsActivites'):
1963
        resp = app.get('/toulouse-axel/test/clae_children_activities_info?NameID=yyy&booking_date=2020-01-20')
1964
    assert resp.json['err'] == 0
1965
    assert resp.json['data'] == [
1966
        {
1967
            'ACTIVITE': [
1968
                {
1969
                    'COUTREVIENT': '99999',
1970
                    'DATEDEBUT': '2019-08-01',
1971
                    'DATEENTREE': '2019-08-01',
1972
                    'DATEFIN': '2020-07-31',
1973
                    'DATESORTIE': '2020-07-31',
1974
                    'IDACTIVITE': 'A19P1M1',
1975
                    'ISPAI': False,
1976
                    'LIBELLEACTIVITE': 'Temps du matin',
1977
                    'TARIF': '0.42',
1978
                    'TYPEACTIVITE': 'MAT'
1979
                },
1980
                {
1981
                    'COUTREVIENT': '99999',
1982
                    'DATEDEBUT': '2019-08-01',
1983
                    'DATEENTREE': '2019-08-01',
1984
                    'DATEFIN': '2020-07-31',
1985
                    'DATESORTIE': '2020-07-31',
1986
                    'IDACTIVITE': 'A19P1M2',
1987
                    'ISPAI': False,
1988
                    'LIBELLEACTIVITE': 'Temps du midi',
1989
                    'TARIF': '0.43',
1990
                    'TYPEACTIVITE': 'MIDI'
1991
                },
1992
                {
1993
                    'COUTREVIENT': '99999',
1994
                    'DATEDEBUT': '2019-08-01',
1995
                    'DATEENTREE': '2019-08-01',
1996
                    'DATEFIN': '2020-07-31',
1997
                    'DATESORTIE': '2020-07-31',
1998
                    'IDACTIVITE': 'A19P1M3',
1999
                    'ISPAI': False,
2000
                    'LIBELLEACTIVITE': 'Temps du soir',
2001
                    'TARIF': '0.44',
2002
                    'TYPEACTIVITE': 'SOIR'
2003
                },
2004
                {
2005
                    'COUTREVIENT': '99999',
2006
                    'DATEDEBUT': '2019-08-01',
2007
                    'DATEENTREE': '2019-08-01',
2008
                    'DATEFIN': '2020-07-31',
2009
                    'DATESORTIE': '2020-07-31',
2010
                    'IDACTIVITE': 'A19P1M4',
2011
                    'ISPAI': False,
2012
                    'LIBELLEACTIVITE': 'Temps mercredi apr\xe8s midi',
2013
                    'TARIF': '0.45',
2014
                    'TYPEACTIVITE': 'GARD'
2015
                }
2016
            ],
2017
            'DATENAISSANCE': '2019-01-01',
2018
            'IDPERSONNE': '3535',
2019
            'LIBELLEECOLE': 'MATERNELLE 1',
2020
            'NOM': 'foo',
2021
            'PRENOM': 'foo',
2022
            'REGIME': 'SV',
2023
            'id': '3535',
2024
            'text': 'foo foo'
2025
        }
2026
    ]
2027

  
2028

  
1934 2029
def test_clae_booking_info_endpoint_axel_error(app, resource):
1935 2030
    Link.objects.create(resource=resource, name_id='yyy', dui='XXX', person_id='42')
1936 2031
    with mock.patch('passerelle.contrib.toulouse_axel.models.enfants_activites') as operation:
1937
-