コード例 #1
0
def test_micro_strategy():
    js = JsonWrapper.load(open('tests/micro_strategy/fixtures/fixture.json'))

    # login
    responses.add(
        responses.POST,
        'https://demo.microstrategy.com/MicroStrategyLibrary2/api/auth/login',
        headers={'x-mstr-authtoken': 'x'},
        status=200,
    )

    # cube
    responses.add(
        responses.POST,
        'https://demo.microstrategy.com/MicroStrategyLibrary2/api/cubes/6137E0964C68D84F107816AA694C2209/instances?limit=100&offset=0',  # noqa: E501
        json=js,
        status=200,
    )

    df = mc.get_df(md)
    assert df.shape == (100, 40)

    # report
    responses.add(
        responses.POST,
        'https://demo.microstrategy.com/MicroStrategyLibrary2/api/reports/TDjAuKmfGeKnqbxKr1TPfcFr4vBTlWIKDWDvODTSKsQ/instances?limit=100&offset=0',  # noqa: E501
        json=js,
        status=200,
    )

    df = mc.get_df(mdr)
    assert df.shape == (100, 40)
コード例 #2
0
def test_fill_viewfilter_with_ids():
    results = JsonWrapper.load(open('tests/micro_strategy/fixtures/fixture.json'))
    dfn = get_definition(results)
    viewfilter = {
        'plop': {'attribute': 'Call Center'},
        'plop_id': {'attribute': '8D679D3511D3E4981000E787EC6DE8A4'},
        'plip': {'attribute': 'Call Center@DESC'},
        'plip_id': {'attribute': '8D679D3511D3E4981000E787EC6DE8A4@DESC'},
        'ploup': {'metric': '% Change to Profit'},
        'ploup_id': {'metric': '965C42404FD62829356000B0B955F267'},
        'poulp': {'constant': 42},
    }

    res = fill_viewfilter_with_ids(viewfilter, dfn)
    assert res['plop'] == {'type': 'attribute', 'id': '8D679D3511D3E4981000E787EC6DE8A4'}
    assert res['plop'] == res['plop_id']
    assert res['plip'] == {
        'type': 'form',
        'attribute': {'id': '8D679D3511D3E4981000E787EC6DE8A4'},
        'form': {'id': 'CCFBE2A5EADB4F50941FB879CCF1721C'},
    }
    assert res['plip'] == res['plip_id']
    assert res['ploup'] == {'type': 'metric', 'id': '965C42404FD62829356000B0B955F267'}
    assert res['ploup'] == res['ploup_id']
    assert res['poulp'] == {'type': 'constant', 'dataType': 'Real', 'value': '42'}
コード例 #3
0
def test_json_dump():
    with open(path_dumps, 'w+') as f:
        JsonWrapper.dump(json_json, f)
    with open(path_dumps, 'r') as f:
        result = JsonWrapper.load(f)
        assert json_json == result
    if os.path.exists(path_dumps):
        os.remove(path_dumps)
コード例 #4
0
def test_google_analytics(mocker):
    gac = GoogleAnalyticsConnector(
        type='GoogleAnalytics',
        name='Test',
        credentials={
            'type':
            'test',
            'project_id':
            'test',
            'private_key_id':
            'test',
            'private_key':
            'test',
            'client_email':
            'test',
            'client_id':
            'test',
            'auth_uri':
            'https://accounts.google.com/o/oauth2/auth',
            'token_uri':
            'https://oauth2.googleapis.com/token',
            'auth_provider_x509_cert_url':
            'https://www.googleapis.com/oauth2/v1/certs',
            'client_x509_cert_url':
            'https://www.googleapis.com/robot/v1/metadata/x509/pika.com',
        },
    )

    gads = GoogleAnalyticsDataSource(
        name='Test',
        domain='test',
        report_request={
            'viewId': '0123456789',
            'dateRanges': [{
                'startDate': '2018-06-01',
                'endDate': '2018-07-01'
            }],
        },
    )

    fixture = JsonWrapper.load(
        open('tests/google_analytics/fixtures/reports.json'))
    module = 'toucan_connectors.google_analytics.google_analytics_connector'
    mocker.patch(f'{module}.ServiceAccountCredentials.from_json_keyfile_dict')
    mocker.patch(f'{module}.build')
    mocker.patch(
        f'{module}.get_query_results').return_value = fixture['reports'][0]

    df = gac.get_df(gads)
    assert df.shape == (3, 11)
コード例 #5
0
def test_viewfilter():
    js = JsonWrapper.load(open('tests/micro_strategy/fixtures/fixture.json'))
    expected_viewfilter = {
        'operator': 'Equals',
        'operands': [
            {
                'type': 'form',
                'attribute': {'id': '8D679D3511D3E4981000E787EC6DE8A4'},
                'form': {'id': 'CCFBE2A5EADB4F50941FB879CCF1721C'},
            },
            {'type': 'constant', 'dataType': 'Char', 'value': 'Miami'},
        ],
    }

    # login
    responses.add(
        responses.POST,
        'https://demo.microstrategy.com/MicroStrategyLibrary2/api/auth/login',
        headers={'x-mstr-authtoken': 'x'},
        status=200,
    )

    # get definition
    responses.add(
        responses.POST,
        'https://demo.microstrategy.com/MicroStrategyLibrary2/api/cubes/6137E0964C68D84F107816AA694C2209/instances?limit=0&offset=0',  # noqa: E501
        json=js,
        status=200,
    )

    # get cube data
    responses.add(
        responses.POST,
        'https://demo.microstrategy.com/MicroStrategyLibrary2/api/cubes/6137E0964C68D84F107816AA694C2209/instances?limit=100&offset=0',  # noqa: E501
        json=js,
        status=200,
    )

    df = mc.get_df(md_filtered)
    assert df.shape == (100, 40)

    viewfilter = JsonWrapper.loads(responses.calls[2].request.body)['viewFilter']
    assert viewfilter == expected_viewfilter
コード例 #6
0
def test_search():
    js = JsonWrapper.load(open('tests/micro_strategy/fixtures/fixture_search.json'))

    # login
    responses.add(
        responses.POST,
        'https://demo.microstrategy.com/MicroStrategyLibrary2/api/auth/login',
        headers={'x-mstr-authtoken': 'x'},
        status=200,
    )

    # search
    responses.add(
        responses.GET,
        'https://demo.microstrategy.com/MicroStrategyLibrary2/api/searches/results?type=776&type=768&offset=0&limit=5&name=revenue+analysis',  # noqa: E501
        json=js,
        status=200,
    )
    df = mc.get_df(mds)
    assert df.shape == (5, 15)
コード例 #7
0
import numpy as np
import responses

from toucan_connectors.json_wrapper import JsonWrapper
from toucan_connectors.trello.trello_connector import TrelloConnector, TrelloDataSource

with open('tests/trello/fixtures/fixture.json') as f:
    mock_trello_api_json_responses = JsonWrapper.load(f)

trello_connector = TrelloConnector(name='trello')

baseroute = 'https://api.trello.com/1/boards/dsjhdejbdkeb'
default_param = 'key=&token=&'


@responses.activate
def test_get_board_method():
    responses.add(
        responses.GET,
        f'{baseroute}/lists?fields=name',
        json=mock_trello_api_json_responses['lists'],
        status=200,
    )

    lists = trello_connector.get_board(fields='name', path='dsjhdejbdkeb/lists')

    assert len(lists) == 2
    assert set(lists[0].keys()) == {'id', 'name'}
    assert set(lists[0].values()) == {'5b2775500401ad42967638a8', 'zorro'}

コード例 #8
0
data_result_none = []
data_result_one = [{
    '1 Column Name': 'value',
    '2 Column Name': 'value',
    '3 Column Name': 'value',
    '4 Column Name': 'value',
    '5 Column Name': 'value',
    '6 Column Name': 'value',
    '7 Column Name': 'value',
    '8 Column Name': 'value',
    '9 Column Name': 'value',
    '10 Column Name': 'value',
    '11 Column Name': 'value',
}]
data_result_5 = JsonWrapper.load(
    open('tests/fixtures/fixture_snowflake_common/data_5.json', ))
data_result_all = JsonWrapper.load(
    open('tests/fixtures/fixture_snowflake_common/data_10.json', ))
databases_result_all = [{'name': 'database_1'}, {'name': 'database_2'}]
databases_result_none = []
databases_result_one = [{'name': 'database_1'}]
warehouses_result_all = [{'name': 'warehouse_1'}, {'name': 'warehouse_2'}]
warehouses_result_none = []
warehouses_result_one = [{'name': 'warehouse_1'}]


@patch('snowflake.connector.connect',
       return_value=snowflake.connector.SnowflakeConnection)
@patch('snowflake.connector.cursor.SnowflakeCursor.execute', return_value=None)
@patch('pandas.DataFrame.from_dict',
       return_value=pd.DataFrame(databases_result_all))
コード例 #9
0
        database='database_1',
        warehouse='warehouse_1',
        query='test_query with %(foo)s and %(pokemon)s',
        query_object={
            'schema': 'SHOW_SCHEMA',
            'table': 'MY_TABLE',
            'columns': ['col1', 'col2']
        },
        parameters={
            'foo': 'bar',
            'pokemon': 'pikachu'
        },
    )


data = JsonWrapper.load(open('tests/snowflake/fixture/data.json', ))
df = pd.DataFrame(
    data,
    columns=[
        '1 Column Name',
        '2 Column Name',
        '3 Column Name',
        '4 Column Name',
        '5 Column Name',
        '6 Column Name',
        '7 Column Name',
        '8 Column Name',
        '9 Column Name',
        '10 Column Name',
    ],
)
コード例 #10
0
 def load_file(self) -> dict:
     if not path.exists(self.filename):
         return {}
     with open(self.filename, 'r') as f:
         return JsonWrapper.load(f)
コード例 #11
0
def test_json_load_file_not_found():
    with pytest.raises(FileNotFoundError):
        JsonWrapper.load(open(path_not_found, 'r'))
コード例 #12
0
def test_json_load():
    result = JsonWrapper.load(open(path_loads, 'r'))
    assert {'key1': 'value1', 'key2': 'value2'} == result