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

Code before:
        num_positions = results.num_positions
        amounts = results.amounts
        self.assertTrue(all(num_positions == 0))
        self.assertTrue(all(amounts == 0))

    def test_zero_trading_days(self):
        """
        Test that when we run a simulation with no trading days (e.g. beginning
        and ending the same weekend), we don't crash on calculating the
        benchmark
        """
        sim_params = copy(self.sim_params)
        sim_params.period_start = pd.Timestamp('2006-01-14', tz='UTC')
        sim_params.period_end = pd.Timestamp('2006-01-15', tz='UTC')
        sim_params.first_open = pd.Timestamp('2006-01-14 14:31:00', tz='UTC')
        sim_params.last_close = pd.Timestamp('2006-01-15 21:00:00', tz='UTC')

        script = """
from zipline.api import symbol, set_benchmark

def initialize(context):
    set_benchmark(symbol('TEST'))
    context.sid = symbol('TEST')

def handle_data(context, data):
    pass
        """
        algo = TradingAlgorithm(
            script=script,
            sim_params=sim_params,
            env=self.env
        )
        algo.run(self.data_portal)

        script = """
from zipline.api import symbol, set_benchmark

def initialize(context):
    context.sid = symbol('TEST')

def handle_data(context, data):
    pass
        """
        algo = TradingAlgorithm(
            script=script,
            sim_params=sim_params,
            env=self.env
        )
        algo.run(self.data_portal)



Review comment (suggestion):
Every test method is called with a new data portal b/c the fixture makes it in [`init_instance_fixtures`](https://github.com/quantopian/zipline/blob/0191d9d90354d8a83366a2374f7615299c4d1025/zipline/testing/fixtures.py#L1041-L1041) (as opposed to `init_class_fixtures`, which is class level).  But here, we're using the portal twice in a single test.


Code after:
        num_positions = results.num_positions
        amounts = results.amounts
        self.assertTrue(all(num_positions == 0))
        self.assertTrue(all(amounts == 0))

    @parameterized.expand([
        ('noop_algo', noop_algo),
        ('with_benchmark_set', set_benchmark_algo)]
    )
    def test_zero_trading_days(self, name, algocode):
        """
        Test that when we run a simulation with no trading days (e.g. beginning
        and ending the same weekend), we don't crash on calculating the
        benchmark
        """
        sim_params = factory.create_simulation_parameters(
            start=pd.Timestamp('2006-01-14', tz='UTC'),
            end=pd.Timestamp('2006-01-15', tz='UTC')
        )

        algo = TradingAlgorithm(
            script=algocode,
            sim_params=sim_params,
            env=self.env
        )
        algo.run(self.data_portal)


class TestGetDatetime(WithLogger,
                      WithSimParams,
                      WithDataPortal,
                      ZiplineTestCase):
    SIM_PARAMS_DATA_FREQUENCY = 'minute'
    START_DATE = to_utc('2014-01-02 9:31')
    END_DATE = to_utc('2014-01-03 9:31')

    ASSET_FINDER_EQUITY_SIDS = 0, 1

    @parameterized.expand(
        [
            ('default', None,),
            ('utc', 'UTC',),
            ('us_east', 'US/Eastern',),
        ]
    )
    def test_get_datetime(self, name, tz):
        algo = dedent(
            """
            import pandas as pd
            from zipline.api import get_datetime