'Assert that assets appear at correct times during a backtest, with
correctly-adjusted close price values.'

@parameterized.expand([('default', None), ('day', 1), ('week', 5), ('year', 252), ('all_but_one_day', 'all_but_one_day'), ('custom_iter', 'custom_iter')])
def test_assets_appear_on_correct_days(self, test_name, chunks):

    if (chunks == 'all_but_one_day'):
        chunks = ((self.dates.get_loc(self.last_asset_end) - self.dates.get_loc(self.first_asset_start)) - 1)
    elif (chunks == 'custom_iter'):
        chunks = []
        st = np.random.RandomState(12345)
        remaining = (self.dates.get_loc(self.last_asset_end) - self.dates.get_loc(self.first_asset_start))
        while (remaining > 0):
            chunk = st.randint(3)
            chunks.append(chunk)
            remaining -= chunk
    def initialize(context):
        p = attach_pipeline(Pipeline(), 'test', chunks=chunks)
        p.add(USEquityPricing.close.latest, 'close')
    def handle_data(context, data):
        results = pipeline_output('test')
        date = get_datetime().normalize()
        for asset in self.assets:
            exists_today = self.exists(date, asset)
            existed_yesterday = self.exists((date - self.trading_day), asset)
            if (exists_today and existed_yesterday):
                latest = results.loc[(asset, 'close')]
                self.assertEqual(latest, self.expected_close(date, asset))
            else:
                self.assertNotIn(asset, results.index)
    before_trading_start = handle_data
    algo = TradingAlgorithm(initialize=initialize, handle_data=handle_data, before_trading_start=before_trading_start, data_frequency='daily', get_pipeline_loader=(lambda column: self.pipeline_loader), start=self.first_asset_start, end=self.last_asset_end, env=self.env)
    algo.run(self.data_portal)
