class TestProphetSplit:
    """make sure behavior for splits is maintained"""
    demosplit_obj = split_utils.SplitInfo(DEMO_SPLIT)
    revrsplit_obj = split_utils.SplitInfo(DEMO_UNSPLIT)

    def test_validate_forward_split(self):
        """run split as intended, new item with split in the past"""

        if platform.system() == 'Darwin':
            pytest.xfail('Unable to run fbprophet on mac')

        test_clear_caches()
        assert TEST_API_KEY != ''

        # vv FIXME vv: depends on split_utils package
        api_utils.SPLIT_INFO = split_utils.read_split_info()
        api_utils.SPLIT_INFO[self.demosplit_obj.type_id] = self.demosplit_obj
        # ^^ FIXME ^^ #

        req = self.client.get(
            url_for('prophetendpoint', return_type='csv') +
            '?typeID={type_id}&regionID={region_id}&api={api_key}&range={range}'
            .format(type_id=self.demosplit_obj.type_id,
                    region_id=CONFIG.get('TEST', 'region_id'),
                    api_key=TEST_API_KEY,
                    range=CONFIG.get('TEST', 'forecast_range')))

        data = None
        with io.StringIO(req.data.decode()) as buff:
            data = pd.read_csv(buff)

        assert req._status_code == 200

    def test_validate_reverse_split(self):
        """run split as intended, old item with new data"""

        if platform.system() == 'Darwin':
            pytest.xfail('Unable to run fbprophet on mac')

        test_clear_caches()
        assert TEST_API_KEY != ''

        # vv FIXME vv: depends on split_utils package
        api_utils.SPLIT_INFO = split_utils.read_split_info()
        api_utils.SPLIT_INFO[self.demosplit_obj.type_id] = self.demosplit_obj
        # ^^ FIXME ^^ #

        req = self.client.get(
            url_for('prophetendpoint', return_type='csv') +
            '?typeID={type_id}&regionID={region_id}&api={api_key}&range={range}'
            .format(type_id=self.demosplit_obj.original_id,
                    region_id=CONFIG.get('TEST', 'region_id'),
                    api_key=TEST_API_KEY,
                    range=CONFIG.get('TEST', 'forecast_range')))

        data = None
        with io.StringIO(req.data.decode()) as buff:
            data = pd.read_csv(buff)

        assert req._status_code == 200
def test_load_data():
    """push data into global scope for testing"""
    api_utils.SPLIT_INFO = split_utils.read_split_info()
    demosplit_obj = split_utils.SplitInfo(DEMO_SPLIT)
    revrsplit_obj = split_utils.SplitInfo(DEMO_UNSPLIT)

    api_utils.SPLIT_INFO[demosplit_obj.type_id] = demosplit_obj
    api_utils.SPLIT_INFO[revrsplit_obj.type_id] = revrsplit_obj
def test_splitinfo_happypath():
    """test SplitInfo behavior"""
    split_obj = split_utils.SplitInfo(DEMO_SPLIT)

    ## Validate data inside obj ##
    assert split_obj.type_id == DEMO_SPLIT['type_id']
    assert split_obj.type_name == DEMO_SPLIT['type_name']
    assert split_obj.original_id == DEMO_SPLIT['original_id']
    assert split_obj.new_id == DEMO_SPLIT['new_id']
    assert split_obj.split_date == datetime.strptime(DEMO_SPLIT['split_date'],
                                                     '%Y-%m-%d')
    assert split_obj.date_str == DEMO_SPLIT['split_date']
    assert split_obj.bool_mult_div == False

    assert split_obj.split_rate == DEMO_SPLIT['split_rate']

    assert split_obj.current_typeid() == DEMO_SPLIT['new_id']

    ## Validate magicmethod behavior ##
    assert int(split_obj) == DEMO_SPLIT['new_id']
    assert bool(split_obj)  #should be True
    assert str(split_obj) == DEMO_SPLIT['type_name']

    test_price = 3.5
    test_volume = 1e6

    expected_price = test_price / DEMO_SPLIT['split_rate']
    expected_volume = test_volume * DEMO_SPLIT['split_rate']

    assert test_price * split_obj == expected_price
    assert split_obj * test_price == expected_price
    assert test_volume / split_obj == expected_volume
def test_execute_split_backwards():
    """check if execute_split works as expected"""
    split_obj = split_utils.SplitInfo(DEMO_UNSPLIT)
    cache_data = split_utils.fetch_split_cache_data(
        TEST_CONFIG.get('TEST', 'region_id'),
        TEST_CONFIG.get('TEST', 'type_id'),
        #split_cache_file=SPLIT_CACHE_FILE
    )

    split_data = split_utils.execute_split(
        cache_data.copy(),  #copy b/c otherwise passed by reference
        split_obj)

    price_mod = split_obj.split_rate
    if not split_obj.bool_mult_div:
        price_mod = 1 / price_mod
    for col_name in split_utils.PRICE_KEYS:
        price_diff = abs(split_data[col_name] -
                         (cache_data[col_name] * price_mod))
        assert price_diff.max() < float(TEST_CONFIG.get('TEST', 'float_limit'))

    vol_mod = 1 / price_mod
    for col_name in split_utils.VOLUME_KEYS:
        vol_diff = abs(split_data[col_name] - (cache_data[col_name] * vol_mod))
        assert vol_diff.max() < float(TEST_CONFIG.get('TEST', 'float_limit'))
class TestODBCsplit:
    """make sure behavior for splits is maintained"""
    demosplit_obj = split_utils.SplitInfo(DEMO_SPLIT)
    revrsplit_obj = split_utils.SplitInfo(DEMO_UNSPLIT)

    def test_validate_forward_split(self):
        """run split as intended, new item with split in the past"""
        # vv FIXME vv: depends on split_utils package
        api_utils.SPLIT_INFO = split_utils.read_split_info()
        api_utils.SPLIT_INFO[self.demosplit_obj.type_id] = self.demosplit_obj
        # ^^ FIXME ^^ #
        req = self.client.get(
            url_for('ohlc_endpoint', return_type='csv') +
            '?typeID={type_id}&regionID={region_id}'.format(
                type_id=self.demosplit_obj.type_id,
                #type_id=CONFIG.get('TEST', 'type_id'),
                region_id=CONFIG.get('TEST', 'region_id')))

        data = None
        with io.StringIO(req.data.decode()) as buff:
            data = pd.read_csv(buff)

        assert req._status_code == 200
        #TODO: validate return

    def test_validate_reverse_split(self):
        """run split normally, old item with forward data"""
        # vv FIXME vv: depends on split_utils package
        api_utils.SPLIT_INFO = split_utils.read_split_info()
        api_utils.SPLIT_INFO[self.demosplit_obj.type_id] = self.demosplit_obj
        # ^^ FIXME ^^ #
        req = self.client.get(
            url_for('ohlc_endpoint', return_type='csv') +
            '?typeID={type_id}&regionID={region_id}'.format(
                type_id=self.demosplit_obj.original_id,
                #type_id=CONFIG.get('TEST', 'type_id'),
                region_id=CONFIG.get('TEST', 'region_id')))

        data = None
        with io.StringIO(req.data.decode()) as buff:
            data = pd.read_csv(buff)

        assert req._status_code == 200
def test_splitinfo_throws():
    """make sure bad behavior is caught"""
    short_profile = dict(DEMO_SPLIT)
    short_profile.pop('split_rate', None)
    with pytest.raises(exceptions.InvalidSplitConfig):
        split_obj = split_utils.SplitInfo(short_profile)

    bad_split = dict(DEMO_SPLIT)
    bad_split['split_rate'] = 'bacon'
    with pytest.raises(exceptions.InvalidSplitConfig):
        split_obj = split_utils.SplitInfo(bad_split)

    bad_date = dict(DEMO_SPLIT)
    bad_date['split_date'] = 'Tomorrow'
    with pytest.raises(exceptions.InvalidSplitConfig):
        split_obj = split_utils.SplitInfo(bad_date)

    bad_bool = dict(DEMO_SPLIT)
    bad_bool['bool_mult_div'] = 'bacon'
    with pytest.raises(exceptions.InvalidSplitConfig):
        split_obj = split_utils.SplitInfo(bad_bool)
def test_splitinfo_reverse():
    """validate SplitInfo with "True" bool_mult_div"""
    split_obj = split_utils.SplitInfo(DEMO_UNSPLIT)

    ## Validate data inside obj ##
    assert split_obj.bool_mult_div == True
    assert split_obj.current_typeid() == DEMO_UNSPLIT['original_id']
    test_price = 3.5
    test_volume = 1e6

    expected_price = test_price * DEMO_SPLIT['split_rate']
    expected_volume = test_volume / DEMO_SPLIT['split_rate']

    assert test_price * split_obj == expected_price
    assert split_obj * test_price == expected_price
    assert test_volume / split_obj == expected_volume
    def test_forward_happypath_emd(self):
        """test a forward-split: crest"""
        split_obj = split_utils.SplitInfo(DEMO_SPLIT)
        raw_emd_data = forecast_utils.fetch_market_history_emd(
            TEST_CONFIG.get('TEST', 'region_id'),
            self.test_type_id,
            data_range=TEST_CONFIG.get('TEST', 'history_count'),
            config=ROOT_CONFIG)
        raw_emd_data1 = forecast_utils.parse_emd_data(raw_emd_data['result'])
        raw_emd_data = forecast_utils.fetch_market_history_emd(
            TEST_CONFIG.get('TEST', 'region_id'),
            self.test_original_id,
            data_range=TEST_CONFIG.get('TEST', 'history_count'),
            config=ROOT_CONFIG)
        raw_emd_data2 = forecast_utils.parse_emd_data(raw_emd_data['result'])

        split_data = split_utils.fetch_split_history(
            TEST_CONFIG.get('TEST', 'region_id'),
            DEMO_SPLIT['type_id'],
            api_utils.SwitchCCPSource.EMD,
            config=ROOT_CONFIG)
        #split_data.to_csv('split_data_emd.csv', index=False)

        ## Doctor data for testing ##
        min_split_date = split_data.date.min()
        raw_emd_data1 = prep_raw_data(raw_emd_data1.copy(), min_split_date)
        raw_emd_data2 = prep_raw_data(raw_emd_data2.copy(), min_split_date)

        pre_split_data = split_data[
            split_data.date <= split_obj.date_str].reset_index()
        pre_raw_data = raw_emd_data2[
            raw_emd_data2.date <= split_obj.date_str].reset_index()
        post_split_data = split_data[
            split_data.date > split_obj.date_str].reset_index()
        post_raw_data = raw_emd_data1[
            raw_emd_data1.date > split_obj.date_str].reset_index()

        ## Validate pre/post Split values ##
        validate_plain_data(post_raw_data, post_split_data)

        validate_split_data(pre_raw_data, pre_split_data, split_obj)
    def test_forward_happypath_crest(self):
        """test a forward-split: crest"""
        split_obj = split_utils.SplitInfo(DEMO_SPLIT)
        raw_crest_data1 = crest_utils.fetch_market_history(
            TEST_CONFIG.get('TEST', 'region_id'),
            self.test_type_id,
            mode=api_utils.SwitchCCPSource.CREST,
            config=ROOT_CONFIG)
        raw_crest_data2 = crest_utils.fetch_market_history(
            TEST_CONFIG.get('TEST', 'region_id'),
            self.test_original_id,
            mode=api_utils.SwitchCCPSource.CREST,
            config=ROOT_CONFIG)
        split_data = split_utils.fetch_split_history(
            TEST_CONFIG.get('TEST', 'region_id'),
            DEMO_SPLIT['type_id'],
            api_utils.SwitchCCPSource.CREST,
            config=ROOT_CONFIG)
        #split_data.to_csv('split_data_crest.csv', index=False)

        ## Doctor data for testing ##
        min_split_date = split_data.date.min()
        raw_crest_data1 = prep_raw_data(raw_crest_data1.copy(), min_split_date)
        raw_crest_data2 = prep_raw_data(raw_crest_data2.copy(), min_split_date)

        split_date_str = datetime.strftime(split_obj.split_date,
                                           '%Y-%m-%dT%H:%M:%S')
        pre_split_data = split_data[
            split_data.date <= split_date_str].reset_index()
        pre_raw_data = raw_crest_data2[
            raw_crest_data2.date <= split_date_str].reset_index()
        post_split_data = split_data[
            split_data.date > split_date_str].reset_index()
        post_raw_data = raw_crest_data1[
            raw_crest_data1.date > split_date_str].reset_index()

        ## Validate pre/post Split values ##
        validate_plain_data(post_raw_data, post_split_data)

        validate_split_data(pre_raw_data, pre_split_data, split_obj)