Repository: quantopian/zipline
File: tests/pipeline/test_pipeline_algo.py (Python)

Code before:
    WithMakeAlgo,
    ZiplineTestCase,
):

    @classmethod
    def init_class_fixtures(cls):
        super(PipelineSequenceTestCase, cls).init_class_fixtures()

    def make_algo_kwargs(self, **overrides):
        return self.merge_with_inherited_algo_kwargs(
            PipelineSequenceTestCase,
            suite_overrides=dict(
                get_pipeline_loader=lambda column: self.seeded_random_loader,
            ),
            method_overrides=overrides,
        )

    def test_pipeline_compute_before_bts(self):

        # for storing and keeping track of calls to BTS and TestFactor.compute
        history = []
        counter = [0]

        class TestFactor(CustomFactor, SingleInputMixin):

            inputs = [TestingDataSet.float_col]

            def compute(self, today, assets, out, inputs):
                history.append(
                    "CustomFactor call"
                )

        def initialize(context):
            pipeline = attach_pipeline(Pipeline(), 'my_pipeline')
            test_factor = TestFactor(window_length=30)
            pipeline.add(test_factor, 'test_factor')

        def handle_data(context, data):
            pass

        def before_trading_start(context, data):
            context.results = pipeline_output('my_pipeline')
            history.append("BTS call %s" % counter[0])
            counter[0] += 1

        self.run_algorithm(
            initialize=initialize,
            handle_data=handle_data,
            before_trading_start=before_trading_start,
        )


Review comment (suggestion):
Since you're not using the inputs here, I would just make inputs an empty tuple. You can also specify `window_length = 1` at class scope here instead of having to pass it to the instance below, though I don't have a super strong preference between either of those styles since there's only one call-site. One thing you might consider doing either way is adding a comment noting that the `window_length` doesn't matter for the behavior under test here.

Code after:
        self.END_DATE = pd.Timestamp('2015-07-15', tz='utc')
        return self.merge_with_inherited_algo_kwargs(
            PipelineSequenceTestCase,
            suite_overrides=dict(
                get_pipeline_loader=lambda column: self.fake_pipeline,
            ),
            method_overrides=overrides,
        )

    def fake_pipeline(self):
        raise AssertionError

    def test_pipeline_compute_before_bts(self):

        # for storing and keeping track of calls to BTS and TestFactor.compute
        trace = []

        class TestFactor(CustomFactor):
            inputs = ()

            # window_length doesn't actually matter for this test case
            window_length = 1

            def compute(self, today, assets, out):
                trace.append("CustomFactor call")

        def initialize(context):
            pipeline = attach_pipeline(Pipeline(), 'my_pipeline')
            test_factor = TestFactor()
            pipeline.add(test_factor, 'test_factor')

        def before_trading_start(context, data):
            trace.append("BTS call")
            pipeline_output('my_pipeline')

        self.run_algorithm(
            initialize=initialize,
            before_trading_start=before_trading_start,
            sim_params=SimulationParameters(
                start_session=pd.Timestamp('2014-12-29', tz='utc'),
                end_session=pd.Timestamp('2014-12-31', tz='utc'),
                data_frequency='daily',
                emission_rate='daily',
                trading_calendar=self.trading_calendar,
            )
        )

        # All pipeline computation calls should occur before any BTS calls,
        # and the algorithm is being run for 3 days, so the first 3 calls
        # should be to the custom factor and the next 3 calls should be to BTS
        expected_result = ["CustomFactor call"] * 3 + ["BTS call"] * 3