Example #1
0
def test_count(context, app):
    consume(
        context.push([
            {
                'type': 'rinkimai/:source/json',
                'id': 1,
                'pavadinimas': 'Rinkimai 1',
            },
            {
                'type': 'rinkimai/:source/json',
                'id': 2,
                'pavadinimas': 'Rinkimai 2',
            },
        ]))

    app.authorize(['spinta_rinkimai_source_json_getall'])
    resp = app.get('/rinkimai/:source/json/:count',
                   headers={'accept': 'text/html'})
    assert resp.status_code == 200

    resp.context.pop('request')
    assert resp.context['header'] == ['count']
    assert resp.context['data'] == [[{
        'color': None,
        'link': None,
        'value': 2
    }]]
Example #2
0
def test_denorm(store, responses):
    responses.add(
        GET,
        'http://example.com/orgs.csv',
        status=200,
        content_type='text/plain; charset=utf-8',
        body=('govid,org,kodas,šalis\n'
              '1,Org1,lt,Lietuva\n'
              '2,Org2,lt,Lietuva\n'
              '3,Org3,lv,Latvija'),
    )

    assert consume(store.pull('denorm')) == 5
    assert consume(store.pull('denorm')) == 0

    lt = '552c4c243ec8c98c313255ea9bf16ee286591f8c'
    lv = 'b5dcb86880816fb966cdfbbacd1f3406739464f4'

    assert list(store.getall('country')) == []
    assert sorted([(x['id'], x['title'])
                   for x in store.getall('country', {'source': 'denorm'})
                   ]) == [
                       (lt, 'Lietuva'),
                       (lv, 'Latvija'),
                   ]

    assert list(store.getall('org')) == []
    assert sorted([(x['id'], x['title'], x['country'])
                   for x in store.getall('org', {'source': 'denorm'})],
                  key=itemgetter(1)) == [
                      ('23fcdb953846e7c709d2967fb549de67d975c010', 'Org1', lt),
                      ('6f9f652eb6dae29e4406f1737dd6043af6142090', 'Org2', lt),
                      ('11a0764da48b674ce0c09982e7c43002b510d5b5', 'Org3', lv),
                  ]
Example #3
0
def test_count(store, app):
    consume(
        store.push([
            {
                'type': 'rinkimai/:source/json',
                'id': 1,
                'pavadinimas': 'Rinkimai 1',
            },
            {
                'type': 'rinkimai/:source/json',
                'id': 2,
                'pavadinimas': 'Rinkimai 2',
            },
        ]))

    resp = app.get('/rinkimai/:source/json/:count')
    assert resp.status_code == 200

    resp.context.pop('request')
    assert resp.context['header'] == ['count']
    assert resp.context['data'] == [[{
        'color': None,
        'link': None,
        'value': 2
    }]]
Example #4
0
def test_changes(store):
    data, = list(
        store.push([{
            'type': 'country',
            'code': 'lt',
            'title': 'Lithuania'
        }]))
    consume(
        store.push([{
            'id': data['id'],
            'type': 'country',
            'title': "Lietuva"
        }]))
    consume(
        store.push([{
            'id': data['id'],
            'type': 'country',
            'code': 'lv',
            'title': "Latvia"
        }]))

    backend = store.config.backends['default']
    txn = backend.tables['internal']['transaction'].main
    changes = backend.tables['default']['country'].changes
    with backend.transaction() as transaction:
        c = transaction.connection
        assert len(c.execute(sa.select([txn.c.id])).fetchall()) == 3
        assert list(
            map(
                dict,
                c.execute(
                    sa.select([
                        changes.c.id,
                        changes.c.action,
                        changes.c.change,
                    ]).order_by(changes.c.transaction_id)).fetchall())) == [
                        {
                            'id': data['id'],
                            'action': 'insert',
                            'change': {
                                'code': 'lt',
                                'title': 'Lithuania'
                            }
                        },
                        {
                            'id': data['id'],
                            'action': 'update',
                            'change': {
                                'title': 'Lietuva'
                            }
                        },
                        {
                            'id': data['id'],
                            'action': 'update',
                            'change': {
                                'code': 'lv',
                                'title': 'Latvia'
                            }
                        },
                    ]
Example #5
0
def test_export_json(store, app, mocker):
    mocker.patch('spinta.backends.postgresql.dataset.utcnow',
                 return_value=datetime.datetime(2019, 3, 6, 16, 15, 0, 816308))

    consume(
        store.push([
            {
                'type': 'country/:source/csv',
                'id': 1,
                'code': 'lt',
                'title': 'Lithuania',
            },
            {
                'type': 'country/:source/csv',
                'id': 2,
                'code': 'lv',
                'title': 'LATVIA',
            },
            {
                'type': 'country/:source/csv',
                'id': 2,
                'code': 'lv',
                'title': 'Latvia',
            },
        ]))

    assert app.get('country/:source/csv/:format/jsonl').text == (
        '{"type":"country\\/:source\\/csv","id":"025685077bbcf6e434a95b65b9a6f5fcef046861","code":"lv","title":"Latvia"}\n'
        '{"type":"country\\/:source\\/csv","id":"69a33b149af7a7eeb25026c8cdc09187477ffe21","code":"lt","title":"Lithuania"}\n'
    )
Example #6
0
def test_nested_dataset(store, app):
    consume(
        store.push([
            {
                'type': 'deeply/nested/model/name/:source/nested/dataset/name',
                'id': '42',
                'name': 'Nested One',
            },
        ]))

    resp = app.get('deeply/nested/model/name/:source/nested/dataset/name')
    assert resp.status_code == 200

    resp.context.pop('request')
    assert resp.context == {
        'location': [
            ('root', '/'),
            ('deeply', '/deeply'),
            ('nested', '/deeply/nested'),
            ('model', '/deeply/nested/model'),
            ('name', '/deeply/nested/model/name'),
            (':source/nested/dataset/name', None),
            (':changes',
             '/deeply/nested/model/name/:source/nested/dataset/name/:changes'),
        ],
        'items': [],
        'datasets': [],
        'header': ['id', 'name'],
        'data': [
            [
                {
                    'color': None,
                    'link':
                    '/deeply/nested/model/name/e2ff1ff0f7d663344abe821582b0908925e5b366/:source/nested/dataset/name',
                    'value': 'e2ff1ff0'
                },
                {
                    'color': None,
                    'link': None,
                    'value': 'Nested One'
                },
            ],
        ],
        'row': [],
        'formats': [
            ('CSV',
             '/deeply/nested/model/name/:source/nested/dataset/name/:format/csv'
             ),
            ('JSON',
             '/deeply/nested/model/name/:source/nested/dataset/name/:format/json'
             ),
            ('JSONL',
             '/deeply/nested/model/name/:source/nested/dataset/name/:format/jsonl'
             ),
            ('ASCII',
             '/deeply/nested/model/name/:source/nested/dataset/name/:format/asciitable'
             ),
        ],
    }
Example #7
0
def test_csv(store, responses):
    responses.add(
        GET,
        'http://example.com/continents.csv',
        status=200,
        stream=True,
        content_type='text/plain; charset=utf-8',
        body='id,continent\n1,Europe\n',
    )

    responses.add(
        GET,
        'http://example.com/continents/1/countries.csv',
        status=200,
        stream=True,
        content_type='text/plain; charset=utf-8',
        body='id,country\n1,Lithuania\n',
    )

    responses.add(
        GET,
        'http://example.com/continents/1/countries/1/captials.csv',
        status=200,
        stream=True,
        content_type='text/plain; charset=utf-8',
        body='id,capital\n1,Vilnius\n',
    )

    assert consume(store.pull('dependencies', {'models': ['continent']})) == 1
    assert consume(store.pull('dependencies', {'models': ['country']})) == 1
    assert consume(store.pull('dependencies', {'models': ['capital']})) == 1

    assert sorted(store.getall('continent', {'source': 'dependencies'})) == [
        {
            'type': 'continent/:source/dependencies',
            'id': '23fcdb953846e7c709d2967fb549de67d975c010',
            'title': 'Europe',
            'continent_id': '1',
        },
    ]
    assert sorted(store.getall('country', {'source': 'dependencies'})) == [
        {
            'type': 'country/:source/dependencies',
            'id': '23fcdb953846e7c709d2967fb549de67d975c010',
            'title': 'Lithuania',
            'continent': '23fcdb953846e7c709d2967fb549de67d975c010',
            'country_id': '1',
        },
    ]
    assert sorted(store.getall('capital', {'source': 'dependencies'})) == [
        {
            'type': 'capital/:source/dependencies',
            'id': '23fcdb953846e7c709d2967fb549de67d975c010',
            'title': 'Vilnius',
            'country': '23fcdb953846e7c709d2967fb549de67d975c010',
        },
    ]
Example #8
0
def test_export_ascii(context, app, mocker):
    mocker.patch('spinta.backends.postgresql.dataset.utcnow',
                 return_value=datetime.datetime(2019, 3, 6, 16, 15, 0, 816308))

    consume(
        context.push([
            {
                'type': 'country/:source/csv',
                'id': 1,
                'code': 'lt',
                'title': 'Lithuania',
            },
            {
                'type': 'country/:source/csv',
                'id': 2,
                'code': 'lv',
                'title': 'LATVIA',
            },
            {
                'type': 'country/:source/csv',
                'id': 2,
                'code': 'lv',
                'title': 'Latvia',
            },
        ]))

    app.authorize([
        'spinta_country_source_csv_getall',
        'spinta_country_source_csv_changes',
    ])

    assert app.get('country/:source/csv/:format/ascii').text == (
        '\n\n'
        'Table: country/:source/csv\n'
        '                   id                      code     title  \n'
        '===========================================================\n'
        '025685077bbcf6e434a95b65b9a6f5fcef046861   lv     Latvia   \n'
        '69a33b149af7a7eeb25026c8cdc09187477ffe21   lt     Lithuania')

    changes = context.changes('country', dataset='csv')
    ids = [c['change_id'] for c in changes]
    txn = [c['transaction_id'] for c in changes]
    assert app.get('country/:source/csv/:changes/:format/ascii').text == (
        'change_id   transaction_id                      id                               datetime            action   change.code   change.title\n'
        '========================================================================================================================================\n'
        f'{ids[0]:<3}         {txn[0]:<3}              69a33b149af7a7eeb25026c8cdc09187477ffe21   2019-03-06 16:15:00.816308   insert   lt            Lithuania   \n'
        f'{ids[1]:<3}         {txn[0]:<3}              025685077bbcf6e434a95b65b9a6f5fcef046861   2019-03-06 16:15:00.816308   insert   lv            LATVIA      \n'
        f'{ids[2]:<3}         {txn[0]:<3}              025685077bbcf6e434a95b65b9a6f5fcef046861   2019-03-06 16:15:00.816308   update   None          Latvia      '
    )
Example #9
0
def test_dataset(context, app):
    consume(
        context.push([
            {
                'type': 'rinkimai/:source/json',
                'id': 'Rinkimai 1',
                'pavadinimas': 'Rinkimai 1',
            },
        ]))

    app.authorize(['spinta_rinkimai_source_json_getall'])
    resp = app.get('/rinkimai/:source/json', headers={'accept': 'text/html'})
    assert resp.status_code == 200

    resp.context.pop('request')
    assert resp.context == {
        'location': [
            ('root', '/'),
            ('rinkimai', '/rinkimai'),
            (':source/json', None),
            (':changes', '/rinkimai/:source/json/:changes'),
        ],
        'items': [],
        'datasets': [],
        'header': ['id', 'pavadinimas'],
        'data': [
            [
                {
                    'color': None,
                    'link':
                    '/rinkimai/df6b9e04ac9e2467690bcad6d9fd673af6e1919b/:source/json',
                    'value': 'df6b9e04'
                },
                {
                    'color': None,
                    'link': None,
                    'value': 'Rinkimai 1'
                },
            ],
        ],
        'row': [],
        'formats': [
            ('CSV', '/rinkimai/:source/json/:format/csv'),
            ('JSON', '/rinkimai/:source/json/:format/json'),
            ('JSONL', '/rinkimai/:source/json/:format/jsonl'),
            ('ASCII', '/rinkimai/:source/json/:format/ascii'),
        ],
    }
Example #10
0
def test_json(store, responses):
    responses.add(
        GET,
        'http://example.com/data.json',
        status=200,
        content_type='application/json; charset=utf-8',
        body=(pathlib.Path(__file__).parents[2] /
              'data/data.json').read_bytes(),
    )

    assert consume(store.pull('json')) == 10
    assert sorted(
        store.getall('rinkimai', {'source': 'json'}),
        key=operator.itemgetter('id')
    )[:2] == [
        {
            'type':
            'rinkimai/:source/json',
            'id':
            '1b704eeff5f38eeeb84ebcecdddb989e53a4a709',
            'pavadinimas':
            '2017 m. balandžio 23 d. nauji savivaldybių tarybų narių – merų rinkimai '
            'Jonavos ir Šakių rajonų savivaldybėse',
        },
        {
            'type':
            'rinkimai/:source/json',
            'id':
            '2a9d25cbf1e8ee6402433d6fc5535f79b5701481',
            'pavadinimas':
            '2017 m. balandžio 23 d. nauji rinkimai į Lietuvos Respublikos Seimą vienmandatėje '
            'Anykščių-Panevėžio rinkimų apygardoje Nr. 49',
        },
    ]
Example #11
0
def test_xml(store, responses):
    responses.add(
        GET,
        'http://example.com/data.xml',
        status=200,
        content_type='application/xml; charset=utf-8',
        body=(pathlib.Path(__file__).parents[2] /
              'data/data.xml').read_bytes(),
        stream=True,
    )

    assert consume(store.pull('xml')) == 8
    assert sorted(store.getall('tenure', {'source': 'xml'}),
                  key=operator.itemgetter('id'))[:2] == [
                      {
                          'type': 'tenure/:source/xml',
                          'id': '11a0764da48b674ce0c09982e7c43002b510d5b5',
                          'title': '1996–2000 metų kadencija',
                          'since': '1996-11-25',
                          'until': '2000-10-18',
                      },
                      {
                          'type': 'tenure/:source/xml',
                          'id': '1cc7ac9d26603972f6c471a284ff37b9868854d9',
                          'title': '2016–2020 metų kadencija',
                          'since': '2016-11-14',
                          'until': '',
                      },
                  ]
Example #12
0
def test_export_csv(context, app, mocker):
    mocker.patch('spinta.backends.postgresql.dataset.utcnow',
                 return_value=datetime.datetime(2019, 3, 6, 16, 15, 0, 816308))

    consume(
        context.push([
            {
                'type': 'country/:source/csv',
                'id': 1,
                'code': 'lt',
                'title': 'Lithuania',
            },
            {
                'type': 'country/:source/csv',
                'id': 2,
                'code': 'lv',
                'title': 'LATVIA',
            },
            {
                'type': 'country/:source/csv',
                'id': 2,
                'code': 'lv',
                'title': 'Latvia',
            },
        ]))

    app.authorize([
        'spinta_country_source_csv_getall',
        'spinta_country_source_csv_changes',
    ])

    assert app.get('country/:source/csv/:format/csv').text == (
        'type,id,code,title\r\n'
        'country/:source/csv,025685077bbcf6e434a95b65b9a6f5fcef046861,lv,Latvia\r\n'
        'country/:source/csv,69a33b149af7a7eeb25026c8cdc09187477ffe21,lt,Lithuania\r\n'
    )

    changes = context.changes('country', dataset='csv')
    ids = [c['change_id'] for c in changes]
    txn = [c['transaction_id'] for c in changes]
    assert app.get('country/:source/csv/:changes/:format/csv').text == (
        'change_id,transaction_id,id,datetime,action,change.code,change.title\r\n'
        f'{ids[0]},{txn[0]},69a33b149af7a7eeb25026c8cdc09187477ffe21,2019-03-06 16:15:00.816308,insert,lt,Lithuania\r\n'
        f'{ids[1]},{txn[1]},025685077bbcf6e434a95b65b9a6f5fcef046861,2019-03-06 16:15:00.816308,insert,lv,LATVIA\r\n'
        f'{ids[2]},{txn[2]},025685077bbcf6e434a95b65b9a6f5fcef046861,2019-03-06 16:15:00.816308,update,,Latvia\r\n'
    )
Example #13
0
def test_show_with_joins(store):
    consume(
        store.push([
            {
                'type': 'continent/:source/dependencies',
                'id': '1',
                'title': 'Europe',
            },
            {
                'type': 'country/:source/dependencies',
                'id': 1,
                'title': 'Lithuania',
                'continent': '1',
            },
            {
                'type': 'capital/:source/dependencies',
                'id': 1,
                'title': 'Vilnius',
                'country': '1',
            },
        ]))

    result = store.getall(
        'capital', {
            'source': 'dependencies',
            'show': [
                'id',
                'title',
                'country.title',
                'country.continent.title',
            ],
        })

    assert list(result) == [
        {
            'country.continent.title': 'Europe',
            'country.title': 'Lithuania',
            'title': 'Vilnius',
            'id': '69a33b149af7a7eeb25026c8cdc09187477ffe21',
        },
    ]
Example #14
0
def test_show_with_joins(context):
    consume(
        context.push([
            {
                'type': 'continent/:source/dependencies',
                'id': '1',
                'title': 'Europe',
            },
            {
                'type': 'country/:source/dependencies',
                'id': 1,
                'title': 'Lithuania',
                'continent': '1',
            },
            {
                'type': 'capital/:source/dependencies',
                'id': 1,
                'title': 'Vilnius',
                'country': '1',
            },
        ]))

    result = context.getall('capital',
                            dataset='dependencies',
                            show=[
                                'id',
                                'title',
                                'country.title',
                                'country.continent.title',
                            ])

    assert result == [
        {
            'country.continent.title': 'Europe',
            'country.title': 'Lithuania',
            'title': 'Vilnius',
            'id': '69a33b149af7a7eeb25026c8cdc09187477ffe21',
        },
    ]
Example #15
0
def test_csv(store, responses):
    responses.add(
        GET,
        'http://example.com/countries.csv',
        status=200,
        content_type='text/plain; charset=utf-8',
        body=('kodas,šalis\n'
              'lt,Lietuva\n'
              'lv,Latvija\n'
              'ee,Estija'),
    )

    assert consume(store.pull('csv')) == 3
    assert consume(store.pull('csv')) == 0

    assert sorted([(x['code'], x['title'])
                   for x in store.getall('country')]) == []
    assert sorted([(x['code'], x['title'])
                   for x in store.getall('country', {'source': 'csv'})]) == [
                       ('ee', 'Estija'),
                       ('lt', 'Lietuva'),
                       ('lv', 'Latvija'),
                   ]
Example #16
0
def test_streaming_response(context, app):
    consume(
        context.push([
            {
                'type': 'country',
                'code': 'fi',
                'title': 'Finland',
            },
            {
                'type': 'country',
                'code': 'lt',
                'title': 'Lithuania',
            },
        ]))

    app.authorize(['spinta_country_getall'])
    resp = app.get('/country').json()
    data = resp['data']
    data = sorted((x['code'], x['title']) for x in data)
    assert data == [
        ('fi', 'Finland'),
        ('lt', 'Lithuania'),
    ]
Example #17
0
def test_dataset_key(store, app):
    consume(
        store.push([
            {
                'type': 'rinkimai/:source/json',
                'id': 'Rinkimai 1',
                'pavadinimas': 'Rinkimai 1',
            },
        ]))

    resp = app.get(
        '/rinkimai/df6b9e04ac9e2467690bcad6d9fd673af6e1919b/:source/json')
    assert resp.status_code == 200

    resp.context.pop('request')
    assert resp.context == {
        'location': [
            ('root', '/'),
            ('rinkimai', '/rinkimai'),
            (':source/json', '/rinkimai/:source/json'),
            ('df6b9e04', None),
            (':changes',
             '/rinkimai/df6b9e04ac9e2467690bcad6d9fd673af6e1919b/:source/json/:changes'
             ),
        ],
        'formats': [
            ('CSV',
             '/rinkimai/df6b9e04ac9e2467690bcad6d9fd673af6e1919b/:source/json/:format/csv'
             ),
            ('JSON',
             '/rinkimai/df6b9e04ac9e2467690bcad6d9fd673af6e1919b/:source/json/:format/json'
             ),
            ('JSONL',
             '/rinkimai/df6b9e04ac9e2467690bcad6d9fd673af6e1919b/:source/json/:format/jsonl'
             ),
            ('ASCII',
             '/rinkimai/df6b9e04ac9e2467690bcad6d9fd673af6e1919b/:source/json/:format/asciitable'
             ),
        ],
        'datasets': [],
        'items': [],
        'header': [],
        'data': [],
        'row': [
            ('type', {
                'color': None,
                'link': None,
                'value': 'rinkimai/:source/json'
            }),
            ('id', {
                'color': None,
                'link':
                '/rinkimai/df6b9e04ac9e2467690bcad6d9fd673af6e1919b/:source/json',
                'value': 'df6b9e04ac9e2467690bcad6d9fd673af6e1919b',
            }),
            ('pavadinimas', {
                'color': None,
                'link': None,
                'value': 'Rinkimai 1'
            }),
        ],
    }
Example #18
0
def test_export_json(context, app, mocker):
    mocker.patch('spinta.backends.postgresql.dataset.utcnow',
                 return_value=datetime.datetime(2019, 3, 6, 16, 15, 0, 816308))

    consume(
        context.push([
            {
                'type': 'country/:source/csv',
                'id': 1,
                'code': 'lt',
                'title': 'Lithuania',
            },
            {
                'type': 'country/:source/csv',
                'id': 2,
                'code': 'lv',
                'title': 'LATVIA',
            },
            {
                'type': 'country/:source/csv',
                'id': 2,
                'code': 'lv',
                'title': 'Latvia',
            },
        ]))

    app.authorize([
        'spinta_country_source_csv_getall',
        'spinta_country_source_csv_getone',
        'spinta_country_source_csv_changes',
    ])

    assert app.get('country/:source/csv/:format/json').text == (
        '{"data":['
        '{"type":"country\\/:source\\/csv","id":"025685077bbcf6e434a95b65b9a6f5fcef046861","code":"lv","title":"Latvia"},'
        '{"type":"country\\/:source\\/csv","id":"69a33b149af7a7eeb25026c8cdc09187477ffe21","code":"lt","title":"Lithuania"}'
        ']}')

    assert app.get(
        'country/69a33b149af7a7eeb25026c8cdc09187477ffe21/:source/csv/:format/json'
    ).text == (
        '{"type":"country\\/:source\\/csv","id":"69a33b149af7a7eeb25026c8cdc09187477ffe21","code":"lt","title":"Lithuania"}'
    )

    changes = app.get(
        'country/:source/csv/:changes/:format/json').json()['data']
    assert changes == [
        {
            'change_id': changes[0]['change_id'],
            'transaction_id': changes[0]['transaction_id'],
            'datetime': 1551888900,
            'action': 'insert',
            'id': '69a33b149af7a7eeb25026c8cdc09187477ffe21',
            'change': {
                'code': 'lt',
                'title': 'Lithuania'
            },
        },
        {
            'change_id': changes[1]['change_id'],
            'transaction_id': changes[1]['transaction_id'],
            'datetime': 1551888900,
            'action': 'insert',
            'id': '025685077bbcf6e434a95b65b9a6f5fcef046861',
            'change': {
                'code': 'lv',
                'title': 'LATVIA'
            },
        },
        {
            'change_id': changes[2]['change_id'],
            'transaction_id': changes[2]['transaction_id'],
            'datetime': 1551888900,
            'action': 'update',
            'id': '025685077bbcf6e434a95b65b9a6f5fcef046861',
            'change': {
                'title': 'Latvia'
            },
        },
    ]

    assert app.get('country/:source/csv/:changes/100/:format/json').json() == {
        'data': []
    }
Example #19
0
def test_xlsx(store, responses):
    responses.add(
        GET, 'http://example.com/data.xlsx',
        status=200, content_type='application/vnd.ms-excel',
        body=(pathlib.Path(__file__).parents[2] / 'data/data.xlsx').read_bytes(),
    )

    rinkimai = '2e12c8b9c3e6028b6712fe308876f0321578db4c'
    turas = '1c89290ef09005b332e84dcaba7873951c19216f'
    apygarda = '025685077bbcf6e434a95b65b9a6f5fcef046861'
    apylinke = '629f0976c1a04dbe6cf3e71b3085ec555d3f63bf'

    assert consume(store.pull('xlsx')) > 0
    assert list(store.getall('rinkimai', {'source': 'xlsx'})) == [
        {
            'type': 'rinkimai/:source/xlsx',
            'id': rinkimai,
            'data': '1992-10-25T00:00:00',
            'rusis': 'Seimo rinkimai',
            'pavadinimas': '1992 m. spalio 25 d. Lietuvos Respublikos Seimo rinkimai',
        },
    ]
    assert list(store.getall('rinkimai/turas', {'source': 'xlsx'})) == [
        {
            'type': 'rinkimai/turas/:source/xlsx',
            'id': turas,
            'turas': 1,
            'rinkimai': rinkimai,
        },
    ]
    assert list(store.getall('rinkimai/apygarda', {'source': 'xlsx'})) == [
        {
            'type': 'rinkimai/apygarda/:source/xlsx',
            'id': apygarda,
            'numeris': 2,
            'pavadinimas': 'Senamiesčio',
            'rinkimai': rinkimai,
            'turas': turas,
        },
    ]
    assert list(store.getall('rinkimai/apylinke', {'source': 'xlsx'})) == [
        {
            'type': 'rinkimai/apylinke/:source/xlsx',
            'id': apylinke,
            'numeris': 0,
            'pavadinimas': 'Balsai, suskaičiuoti apygardos rinkimų komisijoje',
            'rinkimai': rinkimai,
            'turas': turas,
            'apygarda': apygarda,
        },
    ]
    assert list(store.getall('rinkimai/kandidatas', {'source': 'xlsx'})) == [
        {
            'type': 'rinkimai/kandidatas/:source/xlsx',
            'id': '8159cf47118c31114c71a75ed06aa66b0476ad7a',
            'vardas': 'NIJOLĖ',
            'pavarde': 'VAITIEKŪNIENĖ',
            'lytis': 'Moteris',
            'tautybe': '',
            'kas_iskele_kandidata': 'Lietuvos Respublikos piliečių chartija',
            'gauti_balsai_is_anksto': 243,
            'gauti_balsai_is_viso': 243,
            'gauti_balsai_rinkimu_diena': 0,
            'gimimo_data': '1954-03-31T00:00:00',
            'rinkimai': rinkimai,
            'turas': turas,
            'apygarda': apygarda,
            'apylinke': apylinke,
        },
    ]
Example #20
0
def test_changes_object_list(context, app, mocker):
    mocker.patch('spinta.backends.postgresql.dataset.utcnow',
                 return_value=datetime.datetime(2019, 3, 6, 16, 15, 0, 816308))

    consume(
        context.push([
            {
                'type': 'rinkimai/:source/json',
                'id': 'Rinkimai 1',
                'pavadinimas': 'Rinkimai 1',
            },
        ]))
    consume(
        context.push([
            {
                'type': 'rinkimai/:source/json',
                'id': 'Rinkimai 1',
                'pavadinimas': 'Rinkimai 2',
            },
        ]))

    app.authorize(['spinta_rinkimai_source_json_changes'])
    resp = app.get('/rinkimai/:source/json/:changes',
                   headers={'accept': 'text/html'})
    assert resp.status_code == 200

    resp.context.pop('request')
    assert resp.context == {
        'location': [
            ('root', '/'),
            ('rinkimai', '/rinkimai'),
            (':source/json', '/rinkimai/:source/json'),
            (':changes', None),
        ],
        'formats': [
            ('CSV', '/rinkimai/:source/json/:changes/:format/csv'),
            ('JSON', '/rinkimai/:source/json/:changes/:format/json'),
            ('JSONL', '/rinkimai/:source/json/:changes/:format/jsonl'),
            ('ASCII', '/rinkimai/:source/json/:changes/:format/ascii'),
        ],
        'datasets': [],
        'items': [],
        'header': [
            'change_id',
            'transaction_id',
            'datetime',
            'action',
            'id',
            'pavadinimas',
        ],
        'data': [
            [
                {
                    'color': None,
                    'link': None,
                    'value': resp.context['data'][0][0]['value']
                },
                {
                    'color': None,
                    'link': None,
                    'value': resp.context['data'][0][1]['value']
                },
                {
                    'color': None,
                    'link': None,
                    'value': '2019-03-06T16:15:00.816308'
                },
                {
                    'color': None,
                    'link': None,
                    'value': 'update'
                },
                {
                    'color': None,
                    'link':
                    '/rinkimai/df6b9e04ac9e2467690bcad6d9fd673af6e1919b/:source/json',
                    'value': 'df6b9e04'
                },
                {
                    'color': '#B2E2AD',
                    'link': None,
                    'value': 'Rinkimai 2'
                },
            ],
            [
                {
                    'color': None,
                    'link': None,
                    'value': resp.context['data'][1][0]['value']
                },
                {
                    'color': None,
                    'link': None,
                    'value': resp.context['data'][1][1]['value']
                },
                {
                    'color': None,
                    'link': None,
                    'value': '2019-03-06T16:15:00.816308'
                },
                {
                    'color': None,
                    'link': None,
                    'value': 'insert'
                },
                {
                    'color': None,
                    'link':
                    '/rinkimai/df6b9e04ac9e2467690bcad6d9fd673af6e1919b/:source/json',
                    'value': 'df6b9e04'
                },
                {
                    'color': '#B2E2AD',
                    'link': None,
                    'value': 'Rinkimai 1'
                },
            ],
        ],
        'row': [],
    }
Example #21
0
def test_nested_dataset(context, app):
    consume(
        context.push([
            {
                'type': 'deeply/nested/model/name/:source/nested/dataset/name',
                'id': '42',
                'name': 'Nested One',
            },
        ]))

    app.authorize(
        ['spinta_deeply_nested_model_name_source_neste168fff41_getall'])
    resp = app.get('deeply/nested/model/name/:source/nested/dataset/name',
                   headers={'accept': 'text/html'})
    assert resp.status_code == 200

    resp.context.pop('request')
    assert resp.context == {
        'location': [
            ('root', '/'),
            ('deeply', '/deeply'),
            ('nested', '/deeply/nested'),
            ('model', '/deeply/nested/model'),
            ('name', '/deeply/nested/model/name'),
            (':source/nested/dataset/name', None),
            (':changes',
             '/deeply/nested/model/name/:source/nested/dataset/name/:changes'),
        ],
        'items': [],
        'datasets': [],
        'header': ['id', 'name'],
        'data': [
            [
                {
                    'color': None,
                    'link':
                    '/deeply/nested/model/name/e2ff1ff0f7d663344abe821582b0908925e5b366/:source/nested/dataset/name',
                    'value': 'e2ff1ff0'
                },
                {
                    'color': None,
                    'link': None,
                    'value': 'Nested One'
                },
            ],
        ],
        'row': [],
        'formats': [
            ('CSV',
             '/deeply/nested/model/name/:source/nested/dataset/name/:format/csv'
             ),
            ('JSON',
             '/deeply/nested/model/name/:source/nested/dataset/name/:format/json'
             ),
            ('JSONL',
             '/deeply/nested/model/name/:source/nested/dataset/name/:format/jsonl'
             ),
            ('ASCII',
             '/deeply/nested/model/name/:source/nested/dataset/name/:format/ascii'
             ),
        ],
    }