"""Tests for market_share_apple.""" import pandas as pd from datalytics.bin import market_share_apple as ms def test_set_column_value(): """Should add rows to data frame and aggregate Revenue and sub days.""" input_data = [ {'col_name': 'TestCol', 'col_values': [1.0, '-']}, {'col_name': 'Revenue', 'col_values': [1.0, 3.0]}, {'col_name': 'Revenue', 'col_values': [2.0, 1.0]}, {'col_name': 'Total Subscriber Days', 'col_values': [5.0, 4.0]}, {'col_name': 'Total Subscriber Days', 'col_values': [2.0, 1.0]} ] df = pd.DataFrame(['US', 'UA'], columns=['Country']) for line in input_data: ms.set_column_value(line['col_name'], line['col_values'], df) assert df['TestCol'].tolist() == [1.0, '-'] assert df['Revenue'].tolist() == [3.0, 4.0] assert df['Total Subscriber Days'].tolist() == [7.0, 5.0] def test_populate_column(): """Test populate_column.""" test_data = [ ['TestCol1', '1.0'], ['TestCol2', '2.0'], ['', '2.0'], ['01/01/2018-02/01/2018', '2.0'], ['Total Subscription Days', '2.0'] ] expected_result = [ ('TestCol1', ['1.0']), ('TestCol2', ['2.0']), ('Total Subscriber Days', ['2.0']) ] df = pd.DataFrame() for line in test_data: ms.populate_column(df, line) for col_name, val in expected_result: assert col_name in df assert df[col_name].tolist() == val assert '01/01/2018-02/01/2018' not in df assert '' not in df def test_parse_product(): """Test parse_product.""" file_contents = {'Countries': ['UA']} test_products = [ {'input': ['LINEAR RADIO SERVICE', 'TestCol\t1.0'], 'expected': { 'product': 'LINEAR RADIO', 'col': 'TestCol', 'val': ['1.0']}}, {'input': ['TEST SERVICE', 'TestCol\t2.0'], 'expected': { 'product': 'TEST SERVICE', 'col': 'TestCol', 'val': ['2.0']}}, {'input': ['TEST2 SERVICE', 'TestCol\t3.0'], 'expected': { 'product': 'TEST2 SERVICE', 'col': 'TestCol', 'val': ['3.0']}} ] for t in test_products: df = ms.parse_product(file_contents, t['input']) assert df['Product'][0] == t['expected']['product'] assert t['expected']['col'] in df assert df[t['expected']['col']].tolist() == t['expected']['val']