def test_top_positions(wm_api, p_name, p_id, order, limit, expect):
    pos_in = db_top_positions(p_name,
                              RECENT_DATE,
                              desc=order == 'DESC',
                              limit=limit)
    # get top positions
    body = {
        'portfolioId': p_id,
        'number': limit,
        'viewType': 'MONEY',
        'order': {
            'name': 'value',
            'direction': order
        }
    }

    pos_out = wm_api['common'].post(json.dumps(body), url_param='position.top')
    for pos_db, pos_api in zip(pos_in, pos_out):
        expect(
            pos_db['instrument'] == pos_api['name'],
            'Fail: Instrument name: api %s != %s db' %
            (pos_api['name'], pos_db['instrument']))
        expect(
            round(pos_db['nav'], 2) == round(pos_api['value'], 2),
            'Fail: Instrument value: api %s != %s db' %
            (pos_api['value'], pos_db['nav']))
        expect(
            round(pos_db['percentage'], 2) == round(pos_api['percentage'], 2),
            'Fail: Instrument percentage: api %s != %s db' %
            (pos_api['percentage'], pos_db['percentage']))
def test_credit_top_positions_security(wm_api, p_name, p_id, order, limit, expect):
    pos_in = db_top_positions(p_name, RECENT_DATE, order_by='percentage_per_class', desc=order == 'DESC', limit=limit,
                              asset_class=class_name)
    # get top positions
    body = {'portfolioId': p_id, 'number': limit, 'filter': {'type': 'AssetClass', 'id': class_id},
            'order': {'name': 'name', 'direction': order}}
    pos_out = wm_api['common'].post(json.dumps(body), url_param='position.top')

    for pos_db, pos_api in zip(pos_in, pos_out):
        expect(pos_db['instrument'] == pos_api['name'],
               'Fail: Instruments name: api %s != %s db' % (pos_api['name'], pos_db['instrument']))
        expect(round(pos_db['nav'], 2) == round(pos_api['value'], 2),
               'Fail: Instrument %s value: api %s != %s db' % (pos_db['instrument'], pos_api['value'], pos_db['nav']))
        expect(round(pos_db['percentage_per_class'], 2) == round(pos_api['percentage'], 2),
               'Fail: Instrument %s percentage: api %s != %s db' % (pos_db['instrument'], pos_api['percentage'], pos_db['percentage_per_class']))