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

Code before:
        (102, 100),
        ])
    def test_percentchange(self, seed_value, window_length):

        pct_change = PercentChange(
            inputs=(), 
            window_length=window_length
        )

        today = datetime64(1, 'ns')
        assets = arange(7)

        seed(seed_value)  # Seed so we get deterministic results.
        test_data = randn(window_length, 7)
        test_data[0] = array([1, 2, 1, -1, -1, 0, nan])
        test_data[-1] = array([2, 1, -2, 2, -2, 1, 1])

        # Calculate the expected percent change
        expected = array([1, -0.5, -3, 3, -1, inf, nan])

        out = empty((7,), dtype=float)
        pct_change.compute(today, assets, out, test_data)

        check_allclose(expected, out)

    def gen_ranking_cases():
        seeds = range(int(1e4), int(1e5), int(1e4))
        methods = ('ordinal', 'average')
        use_mask_values = (True, False)
        set_missing_values = (True, False)
        ascending_values = (True, False)
        return product(
            seeds,
            methods,
            use_mask_values,
            set_missing_values,
            ascending_values,
        )

    @parameterized.expand(gen_ranking_cases())
    def test_masked_rankdata_2d(self,
                                seed_value,
                                method,
                                use_mask,
                                set_missing,
                                ascending):
        eyemask = ~eye(5, dtype=bool)
        nomask = ones((5, 5), dtype=bool)

        seed(seed_value)
        asfloat = (randn(5, 5) * seed_value)

Review comment (suggestion):
Another case you could test for is passing a `window_length` of 1 and making sure an exception is raised. This can be done with something like:

```python
with self.assertRaises(ValueError):
    PercentChange(inputs=(), window_length=1)
```

Code after:
        (102, 100),
        ])
    def test_percentchange(self, seed_value, window_length):

        pct_change = PercentChange(
            inputs=[EquityPricing.close, ],
            window_length=window_length,
        )

        today = datetime64(1, 'ns')
        assets = arange(8)

        seed(seed_value)  # Seed so we get deterministic results.
        middle_rows = randn(window_length - 2, 8)
        first_row = array([1, 2, 2, 1, -1, -1, 0, nan])
        end_row = array([2, 1, 2, -2, 2, -2, 1, 1])
        test_data = np.vstack([first_row, middle_rows, end_row])

        # Calculate the expected percent change
        expected = array([1, -0.5, 0, -3, 3, -1, inf, nan])

        out = empty((8,), dtype=float)
        pct_change.compute(today, assets, out, test_data)

        check_allclose(expected, out)

        with self.assertRaises(ValueError):
            PercentChange(inputs=(), window_length=2)

        with self.assertRaises(ValueError):
            PercentChange(inputs=[EquityPricing.close], window_length=1)

    def gen_ranking_cases():
        seeds = range(int(1e4), int(1e5), int(1e4))
        methods = ('ordinal', 'average')
        use_mask_values = (True, False)
        set_missing_values = (True, False)
        ascending_values = (True, False)
        return product(
            seeds,
            methods,
            use_mask_values,
            set_missing_values,
            ascending_values,
        )

    @parameterized.expand(gen_ranking_cases())
    def test_masked_rankdata_2d(self,
                                seed_value,
                                method,
                                use_mask,