def test_get_report_meta(token):
    client = Client(api_key=token, host="localhost:8030/api2", ssl=False)
    reports = client.list_reports()
    pk = reports['results'][0]['pk']
    result = client.get_report_meta(pk)
    print(result)
    assert 'pk' in result
def test_get_report(token):
    client = Client(api_key=token, host="localhost:8030/api2", ssl=False)
    reports = client.list_reports()
    pk = reports['results'][0]['pk']
    report = client.get_report(pk)
    print(report.title)
    print(report.elements)
    assert isinstance(report, Report)
def test_get_meta_and_data(token):
    client = Client(api_key=token, host="localhost:8030/api2", ssl=False)
    datasources = client.list_datasources()
    primary_key = datasources['results'][0]['pk']
    datasource = client.get_datasource(primary_key)
    resp = datasource.get_meta_and_data()
    print(resp.keys())
    assert 'meta' in resp
    assert 'data' in resp
def test_create_report_and_add_chart(token):
    client = Client(api_key=token, host="localhost:8030/api2", ssl=False)
    datasource_pk = client.list_datasources()['results'][0]['pk']
    report = client.create_report('my test')
    original_length = len(report.elements)
    resp = report.add_chart(datasource_pk, ['numitems', 'org'], y='agecat')
    report2 = client.get_report(report._pk)
    report.delete()
    assert len(report2.elements) == original_length + 1
    assert 'Type' in resp
def test_get_combined_tables(token):
    client = Client(api_key=token, host="localhost:8030/api2", ssl=False)
    datasources = client.list_datasources()
    primary_key = datasources['results'][0]['pk']
    datasource = client.get_datasource(primary_key)
    tables = datasource.get_tables(['price', 'quality'], ['gender', 'agecat'],
                                   ['counts', 'c%', 'cbase', 'stddev'],
                                   combine=True,
                                   language='en-GB')
    assert tables.shape == (24, 7)
def test_get_table_set(token):
    client = Client(api_key=token, host="localhost:8030/api2", ssl=False)
    datasources = client.list_datasources()
    primary_key = datasources['results'][0]['pk']
    datasource = client.get_datasource(primary_key)
    stubs = [['distance', 'store', 'contact'], ['reason1', 'reason2', 'dept'],
             ['price', 'numitems', 'org', 'service', 'quality', 'overall']]
    banners = [['gender', 'agecat'], ['regular', 'purchase']]
    table_set = datasource.get_table_set(stubs, banners,
                                         ['base', 'counts', 'c%', 'stddev'])
    assert len(table_set) == len(stubs) * len(banners)
def test_dataset_to_excel(token):
    client = Client(api_key=token, host="localhost:8030/api2", ssl=False)
    datasources = client.list_datasources()
    primary_key = datasources['results'][0]['pk']
    datasource = client.get_datasource(primary_key)
    stubs = [['distance', 'store', 'contact'], ['reason1', 'reason2', 'dept'],
             ['price', 'numitems', 'org', 'service', 'quality', 'overall']]
    banners = [['gender', 'agecat'], ['regular', 'purchase']]
    table_set = datasource.get_table_set(stubs, banners,
                                         ['base', 'counts', 'c%', 'stddev'])
    datasource.table_set_to_excel(table_set, 'myexcel.xlsx')
    assert os.path.isfile('myexcel.xlsx')
    os.remove("myexcel.xlsx")
def test_update_datasource(token, dataset_meta, dataset_data):
    client = Client(api_key=token, host="localhost:8030/api2", ssl=False)
    datasources = client.list_datasources()
    primary_key = datasources['results'][0]['pk']
    datasource = client.get_datasource(primary_key)
    meta = dataset_meta
    meta['info']['from_source']['pandas_reader'] = 'changed'
    data = dataset_data[:90].to_csv()
    resp = datasource.update_meta_and_data(meta=meta, data=data)
    datasource2 = client.get_datasource(primary_key)
    meta_and_data = datasource2.get_meta_and_data()
    new_meta = meta_and_data['meta']
    new_data = meta_and_data['data']
    new_data_df = pd.read_csv(StringIO(new_data))

    assert new_meta['info']['from_source']['pandas_reader'] == 'changed'
    assert resp.status_code == 200
def test_create_report(token):
    client = Client(api_key=token, host="localhost:8030/api2", ssl=False)
    number_of_reports = client.list_reports()['count']
    report = client.create_report(title="api created report")
    assert client.list_reports()['count'] == number_of_reports + 1
    report.delete()
    assert client.list_reports()['count'] == number_of_reports
def test_add_chart(token):
    client = Client(api_key=token, host="localhost:8030/api2", ssl=False)
    reports = client.list_reports()
    datasources = client.list_datasources()
    datasource_id = datasources['results'][0]['pk']
    report = client.get_report(reports['results'][0]['pk'])
    original_length = len(report.elements)
    report.add_chart(datasource_primary_key=datasource_id,
                     x=['service', 'quality'])
    report = client.get_report(reports['results'][0]['pk'])
    assert len(report.elements) == original_length + 1
def test_update_meta_element(token):
    client = Client(api_key=token, host="localhost:8030/api2", ssl=False)
    reports = client.list_reports()
    datasources = client.list_datasources()
    datasource_id = datasources['results'][0]['pk']
    report = client.get_report(reports['results'][0]['pk'])
    meta = report.meta
    resp = report.update_meta_element('title', 'new title')
    assert resp.status_code == 200
    report = client.get_report(reports['results'][0]['pk'])
    assert report.meta['title'] == 'new title'
def test_add_multiple_charts(token):
    client = Client(api_key=token, host="localhost:8030/api2", ssl=False)
    reports = client.list_reports()
    datasources = client.list_datasources()
    datasource_id = datasources['results'][0]['pk']
    report = client.get_report(reports['results'][0]['pk'])
    original_length = len(report.elements)
    report.add_charts(datasource_primary_key=datasource_id,
                      x_y_pairs=[('price', 'gender'), ('quality', '@'),
                                 ('service', '@'), ('distance', '@'),
                                 ('quality', '@'), ('quality', '@'),
                                 ('quality', '@'), ('quality', '@')],
                      user_filters=['gender', 'agecat'],
                      filter=['gender == 1'],
                      comparison_variables=['agecat'],
                      charts_per_row=3)
    report = client.get_report(report._pk)
    assert len(report.elements) == original_length + 8
def test_list_datasources(token):
    client = Client(api_key=token, host="localhost:8030/api2", ssl=False)
    resp = client.list_datasources()
    assert 'results' in resp
def test_get_content(token):
    client = Client(api_key=token, host="localhost:8030/api2", ssl=False)
    reports = client.list_reports()
    report = client.get_report_elements(reports['results'][0]['pk'])
    print(report)
    assert 'elements' in report
def test_get_datasource(token):
    client = Client(api_key=token, host="localhost:8030/api2", ssl=False)
    datasources = client.list_datasources()
    primary_key = datasources['results'][0]['pk']
    resp = client.get_datasource(primary_key)
    assert isinstance(resp, Datasource)
def test_create_datasource(token, dataset_data, dataset_meta):
    client = Client(api_key=token, host="localhost:8030/api2", ssl=False)
    datasource = client.create_datasource("My datasource")
    data = dataset_data.to_csv()
    datasource.update_meta_and_data(meta=dataset_meta, data=data)
    assert isinstance(datasource, Datasource)
def test_incorrect_token():
    client = Client(api_key='incorrect')
    resp = client.get_request('datasource')
    assert resp['detail'] == 'Invalid token.'
def test_list_reports(token):
    client = Client(api_key=token, host="localhost:8030/api2", ssl=False)
    reports = client.list_reports()
    assert 'results' in reports
def test_get_url(token):
    client = Client(api_key=token, host="localhost:8030/api2", ssl=False)
    reports = client.list_reports()
    report = client.get_report(reports['results'][0]['pk'])
    assert len(report.get_url()) > 0
def test_new_client(token):
    client = Client(api_key=token, host="localhost:8030/api2", ssl=False)
    resp = client.get_request('datasource')
    assert 'results' in resp