Project

General

Profile

0001-csvdatasource-provide-a-id-filter-to-queries-31491.patch

Frédéric Péters, 17 Mar 2019 09:28 AM

Download (2.72 KB)

View differences:

Subject: [PATCH] csvdatasource: provide a ?id= filter to queries (#31491)

 passerelle/apps/csvdatasource/models.py |  6 ++++++
 tests/test_csv_datasource.py            | 23 +++++++++++++++++++++++
 2 files changed, 29 insertions(+)
passerelle/apps/csvdatasource/models.py
369 369
                new_data.append(dict(zip(titles, new_row)))
370 370
            data = new_data
371 371

  
372
        if 'id' in request.GET:
373
            # always provide a ?id= filter.
374
            filters = ["id == %r" % request.GET['id']]
375
            data = [row for new_row, row in stream_expressions(filters, data, kind='filters')
376
                    if new_row[0]]
377

  
372 378
        # allow jsonp queries by select2
373 379
        # filtering is done there after projection because we need a projection named text for
374 380
        # retro-compatibility with previous use of the csvdatasource with select2
tests/test_csv_datasource.py
691 691
    assert response.json['err'] == 0
692 692
    assert len(response.json['data']) == 2
693 693

  
694
def test_query_builtin_id_filter(app, setup, filetype):
695
    csvdata, _url = setup('id,whatever,nom,prenom,sexe', filename=filetype,
696
                         data=get_file_content(filetype))
697
    url = reverse('generic-endpoint', kwargs={
698
        'connector': 'csvdatasource',
699
        'slug': csvdata.slug,
700
        'endpoint': 'query/query-1_/',
701
    })
702

  
703
    query = Query(slug='query-1_', resource=csvdata)
704
    query.save()
705

  
706
    assert len(app.get(url).json['data']) == 20
707
    assert len(app.get(url + '?id=121').json['data']) == 1
708
    assert app.get(url + '?id=121').json['data'][0]['prenom'] == 'Eliot'
709
    assert app.get(url + '?id=525').json['data'][0]['prenom'] == 'Shanone'
710

  
711
    # check when combined with another filter
712
    query.filters = 'sexe == "H"'
713
    query.save()
714
    assert len(app.get(url).json['data']) == 10
715
    assert app.get(url + '?id=121').json['data'][0]['prenom'] == 'Eliot'
716
    assert len(app.get(url + '?id=525').json['data']) == 0
694 717

  
695 718
def test_delete_connector_query(admin_user, app, setup, filetype):
696 719
    csvdata, url = setup('id,whatever,nom,prenom,sexe', filename=filetype, data=get_file_content(filetype))
697
-