'Test historical restrictions for both interday and intraday
restrictions, as well as restrictions defined in/not in order, for both
single- and multi-asset queries'

@parameter_space(date_offset=(pd.Timedelta(0), pd.Timedelta('1    minute'), pd.Timedelta('15    hours    5    minutes')), restriction_order=(list(range(6)), [0, 2, 1, 3, 5, 4], [0, 3, 1, 4, 2, 5], [0, 5, 2, 3, 1, 4]), __fail_fast=True)
def test_historical_restrictions(self, date_offset, restriction_order):

    def rdate(s):
        'Convert    a    date    string    into    a    restriction    for    that    date.'
        return (str_to_ts(s) + date_offset)
    base_restrictions = [Restriction(self.ASSET1, rdate('2011-01-04'), FROZEN), Restriction(self.ASSET1, rdate('2011-01-05'), ALLOWED), Restriction(self.ASSET1, rdate('2011-01-06'), FROZEN), Restriction(self.ASSET2, rdate('2011-01-05'), FROZEN), Restriction(self.ASSET2, rdate('2011-01-06'), ALLOWED), Restriction(self.ASSET2, rdate('2011-01-07'), FROZEN)]
    all_restrictions = [base_restrictions[i] for i in restriction_order]
    restrictions_by_asset = groupby((lambda r: r.asset), all_restrictions)
    rl = HistoricalRestrictions(all_restrictions)
    assert_not_restricted = partial(self.assert_not_restricted, rl)
    assert_is_restricted = partial(self.assert_is_restricted, rl)
    assert_all_restrictions = partial(self.assert_all_restrictions, rl)
    for (asset, r_history) in iteritems(restrictions_by_asset):
        (freeze_dt, unfreeze_dt, re_freeze_dt) = sorted([r.effective_date for r in r_history])
        assert_not_restricted(asset, (freeze_dt - MINUTE))
        assert_is_restricted(asset, freeze_dt)
        assert_is_restricted(asset, (freeze_dt + MINUTE))
        assert_is_restricted(asset, (unfreeze_dt - MINUTE))
        assert_not_restricted(asset, unfreeze_dt)
        assert_not_restricted(asset, (unfreeze_dt + MINUTE))
        assert_not_restricted(asset, (re_freeze_dt - MINUTE))
        assert_is_restricted(asset, re_freeze_dt)
        assert_is_restricted(asset, (re_freeze_dt + MINUTE))
        assert_is_restricted(asset, (re_freeze_dt + (MINUTE * 1000000)))
    d0 = rdate('2011-01-04')
    assert_all_restrictions([False, False, False], (d0 - MINUTE))
    assert_all_restrictions([True, False, False], d0)
    assert_all_restrictions([True, False, False], (d0 + MINUTE))
    d1 = rdate('2011-01-05')
    assert_all_restrictions([True, False, False], (d1 - MINUTE))
    assert_all_restrictions([False, True, False], d1)
    assert_all_restrictions([False, True, False], (d1 + MINUTE))
    d2 = rdate('2011-01-06')
    assert_all_restrictions([False, True, False], (d2 - MINUTE))
    assert_all_restrictions([True, False, False], d2)
    assert_all_restrictions([True, False, False], (d2 + MINUTE))
    d3 = rdate('2011-01-07')
    assert_all_restrictions([True, False, False], (d3 - MINUTE))
    assert_all_restrictions([True, True, False], d3)
    assert_all_restrictions([True, True, False], (d3 + MINUTE))
    assert_all_restrictions([True, True, False], (d3 + (MINUTE * 10000000)))
