def test_pivot(session): query = { 'select': ['date[Day]', 'place', 'cube.count'], 'pivot_on': ['date[Day]'], 'dim_fmt': 'leaf', } res = gasket.dice(query) check_data = res['data'].reset_index() assert all(check_data['Place'].values == ['EU', 'USA']) assert res['headers'] == [('Place', 'Count', 'Count'), ('', 1, 2)] # Use user-friendly name to pivot query['pivot_on'] = ['Date: Day'] check_data = gasket.dice(query)['data'].reset_index() assert all(check_data['Place'].values == ['EU', 'USA']) # Pivot on both dim query['pivot_on'] = ['Date: Day', 'place'] res = gasket.dice(query) assert list(res['data']['Place'].values) == ['EU', 'USA'] * 2 # Pivot by id query['pivot_on'] = 1 res = gasket.dice(query) assert list(res['data']['Count']['EU']) == [2.0, 1.0]
def test_alone(session): # Test only measures ref_data = list(Cube.dice([Cube.total, Cube.count, Cube.average])) query = { 'select': ['cube.total', 'cube.count', 'cube.average'], } check_data = gasket.dice(query)['data'] check_data = [tuple(row) for row in check_data.values] assert ref_data == check_data # Test measure format query['msr_fmt'] = 'auto' check_data = gasket.dice(query)['data'] check_data = [tuple(row) for row in check_data.values] assert check_data == [('30.00', '4.00', '7.50')] # Test dimension format for fmt in [None, 'leaf', 'full']: ref_data = list( Cube.dice([Cube.place['Country'], Cube.count], dim_fmt=fmt)) query = { 'select': ['place[Country]', 'cube.count'], 'dim_fmt': fmt, } check_data = gasket.dice(query)['data'] check_data = [tuple(row) for row in check_data.values] assert ref_data == check_data
def test_limit(session): # Test only measures query = { 'select': ['place[City]', 'cube.total'], 'dim_fmt': 'full', } query['limit'] = 3 check_data = gasket.dice(query)['data'] assert len(check_data) == 3 query['limit'] = 10 check_data = gasket.dice(query)['data'] assert len(check_data) == 4
def dice(coordinates, measures, **options): select = ['%s[%s]' % (d, len(v) - 1) for d, v in coordinates] select = select + measures filters = [] for name, vals in coordinates: filters.append( (name, [tuple(takewhile(lambda x: x is not None, vals))])) query = { 'select': select, 'format': 'leaf', 'filters': filters + options.get('filters', []), 'skip_zero': options.get('skip_zero'), 'msr_fmt': options.get('msr_fmt'), 'limit': options.get('limit'), 'pivot_on': options.get('pivot_on'), 'sort_by': options.get('sort_by'), } res = gasket.dice(query) data = [list(row) for row in res['data'].values] for pos, line in enumerate(data): line = [int(x) if isinstance(x, numpy.int64) else x for x in line] data[pos] = line return { 'data': data, 'headers': res['headers'], 'totals': res['totals'], }
def test_filter(session): # Test only measures query = { 'select': ['place[City]', 'cube.total'], 'dim_fmt': 'leaf', 'filters': [('place', [('EU', 'BE')])], } check_data = gasket.dice(query)['data'] assert all(check_data['Place: City'].values == ['BRU', 'CRL'])
def test_multi(session): AnotherCube.load(DATA) query = { 'select': [ 'place', 'cube.count', 'anothercube.other_count', 'cube.total', 'anothercube.other_total' ], } check_data = gasket.dice(query)['data'] count_check = check_data['Count'] == check_data['Other Count'] assert count_check.all() total_check = check_data['Total'] == check_data['Other Total'] assert total_check.all()