import pytest, os, datetime
from pprint import pformat
from tradinghours import Market
from tradinghours.exceptions import NoAccess
import tradinghours.store as st


@pytest.mark.xfail(
 st.db.access_level == st.AccessLevel.only_holidays,
 reason="No access", strict=True, raises=NoAccess
)
@pytest.mark.parametrize("fin_id, expected", [
    ("US.NYSE", [{'schedule_group': 'Partial', 'in_force_start_date': None, 'season_start': None,
                  'start': datetime.time(6, 30), 'end': datetime.time(9, 30), 'fin_id': 'US.NYSE',
                  'schedule_group_memo': None, 'timezone': 'America/New_York', 'phase_type': 'Pre-Trading Session',
                  'phase_name': 'Pre-Opening Session', 'phase_memo': None, 'days': 'Mon-Fri',
                  'offset_days': 0, 'duration': 10800, 'min_start': None, 'max_start': None, 'min_end': None,
                  'max_end': None, 'in_force_end_date': None, 'season_end': None, 'end_with_offset': '09:30:00   ',
                  'has_season': False},
                 {'schedule_group': 'Partial', 'in_force_start_date': None, 'season_start': None,
                  'start': datetime.time(9, 30), 'end': datetime.time(13, 0),
                  'fin_id': 'US.NYSE', 'schedule_group_memo': None, 'timezone': 'America/New_York',
                  'phase_type': 'Primary Trading Session', 'phase_name': 'Core Trading Session',
                  'phase_memo': None, 'days': 'Mon-Fri', 'offset_days': 0, 'duration': 12600,
                  'min_start': None, 'max_start': None, 'min_end': None, 'max_end': None,
                  'in_force_end_date': None, 'season_end': None, 'end_with_offset': '13:00:00   ',
                  'has_season': False},
                 {'schedule_group': 'Partial', 'in_force_start_date': None, 'season_start': None,
                  'start': datetime.time(13, 0), 'end': datetime.time(13, 30), 'fin_id': 'US.NYSE',
                  'schedule_group_memo': None, 'timezone': 'America/New_York', 'phase_type': 'Post-Trading Session',
                  'phase_name': 'Crossing Session', 'phase_memo': None, 'days': 'Mon-Fri',
                  'offset_days': 0, 'duration': 1800, 'min_start': None, 'max_start': None, 'min_end': None,
                  'max_end': None, 'in_force_end_date': None, 'season_end': None, 'end_with_offset': '13:30:00   ',
                  'has_season': False},
                 {'schedule_group': 'Regular', 'in_force_start_date': None, 'season_start': None,
                  'start': datetime.time(4, 0), 'end': datetime.time(9, 30), 'fin_id': 'US.NYSE',
                  'schedule_group_memo': None, 'timezone': 'America/New_York',
                  'phase_type': 'Pre-Trading Session', 'phase_name': 'Pre-Trading Session', 'phase_memo': None,
                  'days': 'Mon-Fri', 'offset_days': 0, 'duration': 19800, 'min_start': None,
                  'max_start': None, 'min_end': None, 'max_end': None, 'in_force_end_date': None,
                  'season_end': None, 'end_with_offset': '09:30:00   ', 'has_season': False},
                 {'schedule_group': 'Regular', 'in_force_start_date': None, 'season_start': None,
                  'start': datetime.time(6, 30), 'end': datetime.time(9, 30), 'fin_id': 'US.NYSE',
                  'schedule_group_memo': None, 'timezone': 'America/New_York', 'phase_type': 'Pre-Open',
                  'phase_name': 'Pre-Opening Session', 'phase_memo': None, 'days': 'Mon-Fri',
                  'offset_days': 0, 'duration': 10800, 'min_start': None, 'max_start': None, 'min_end': None,
                  'max_end': None, 'in_force_end_date': None, 'season_end': None, 'end_with_offset': '09:30:00   ',
                  'has_season': False},
                 {'schedule_group': 'Regular', 'in_force_start_date': None, 'season_start': None,
                  'start': datetime.time(9, 30), 'end': datetime.time(9, 30), 'fin_id': 'US.NYSE',
                  'schedule_group_memo': None, 'timezone': 'America/New_York', 'phase_type': 'Call Auction', 'phase_name': 'Core Open Auction', 'phase_memo': None,
                  'days': 'Mon-Fri', 'offset_days': 0, 'duration': 0, 'min_start': None,
                  'max_start': None, 'min_end': None, 'max_end': None, 'in_force_end_date': None, 'season_end': None,
                  'end_with_offset': '09:30:00   ', 'has_season': False},
                 {'schedule_group': 'Regular', 'in_force_start_date': None, 'season_start': None,
                  'start': datetime.time(9, 30), 'end': datetime.time(16, 0), 'fin_id': 'US.NYSE', 'schedule_group_memo': None, 'timezone': 'America/New_York',
                  'phase_type': 'Primary Trading Session', 'phase_name': 'Core Trading Session', 'phase_memo': None,
                  'days': 'Mon-Fri', 'offset_days': 0, 'duration': 23400, 'min_start': None, 'max_start': None,
                  'min_end': None, 'max_end': None, 'in_force_end_date': None, 'season_end': None,
                  'end_with_offset': '16:00:00   ', 'has_season': False},
                 {'schedule_group': 'Regular', 'in_force_start_date': None, 'season_start': None,
                  'start': datetime.time(15, 50), 'end': datetime.time(16, 0), 'fin_id': 'US.NYSE',
                  'schedule_group_memo': None, 'timezone': 'America/New_York', 'phase_type': 'Pre-Close',
                  'phase_name': 'Closing Imbalance Period', 'phase_memo': None, 'days': 'Mon-Fri', 'offset_days': 0,
                  'duration': 600, 'min_start': None, 'max_start': None, 'min_end': None, 'max_end': None,
                  'in_force_end_date': None, 'season_end': None, 'end_with_offset': '16:00:00   ', 'has_season': False},
                 {'schedule_group': 'Regular', 'in_force_start_date': None, 'season_start': None,
                  'start': datetime.time(16, 0), 'end': datetime.time(20, 0), 'fin_id': 'US.NYSE',
                  'schedule_group_memo': None, 'timezone': 'America/New_York', 'phase_type': 'Post-Trading Session',
                  'phase_name': 'Extended Hours', 'phase_memo': None, 'days': 'Mon-Fri', 'offset_days': 0,
                  'duration': 14400, 'min_start': None, 'max_start': None, 'min_end': None, 'max_end': None,
                  'in_force_end_date': None, 'season_end': None, 'end_with_offset': '20:00:00   ',
                  'has_season': False}
                 ]
     ),
    ("BR.BOVESPA", [
        {'schedule_group': 'Partial', 'in_force_start_date': None, 'season_start': None,
         'start': datetime.time(12, 30), 'end': datetime.time(12, 45), 'fin_id': 'BR.BOVESPA',
         'schedule_group_memo': None, 'timezone': 'America/Sao_Paulo', 'phase_type': 'Pre-Open',
         'phase_name': 'Order Cancelation', 'phase_memo': None, 'days': 'Mon-Fri', 'offset_days': 0,
         'duration': 900, 'min_start': None, 'max_start': None, 'min_end': None,
         'max_end': None, 'in_force_end_date': datetime.date(2022, 12, 18), 'season_end': None, 'end_with_offset': '12:45:00   ',
         'has_season': False},
        {'schedule_group': 'Partial', 'in_force_start_date': None, 'season_start': None,
         'start': datetime.time(12, 45), 'end': datetime.time(13, 0), 'fin_id': 'BR.BOVESPA',
         'schedule_group_memo': None, 'timezone': 'America/Sao_Paulo', 'phase_type': 'Pre-Open',
         'phase_name': 'Opening Session', 'phase_memo': None, 'days': 'Mon-Fri', 'offset_days': 0,
         'duration': 900, 'min_start': None, 'max_start': None, 'min_end': None, 'max_end': None,
         'in_force_end_date': datetime.date(2022, 12, 18), 'season_end': None, 'end_with_offset': '13:00:00   ',
         'has_season': False},
        {'schedule_group': 'Partial', 'in_force_start_date': None, 'season_start': None,
         'start': datetime.time(13, 0), 'end': datetime.time(16, 55), 'fin_id': 'BR.BOVESPA',
         'schedule_group_memo': None, 'timezone': 'America/Sao_Paulo', 'phase_type': 'Primary Trading Session',
         'phase_name': 'Trading Session', 'phase_memo': None, 'days': 'Mon-Fri', 'offset_days': 0,
         'duration': 14100, 'min_start': None, 'max_start': None, 'min_end': None, 'max_end': None,
         'in_force_end_date': datetime.date(2022, 12, 18), 'season_end': None, 'end_with_offset': '16:55:00   ',
         'has_season': False},
        {'schedule_group': 'Partial', 'in_force_start_date': None, 'season_start': None,
         'start': datetime.time(16, 55), 'end': datetime.time(17, 0), 'fin_id': 'BR.BOVESPA',
         'schedule_group_memo': None, 'timezone': 'America/Sao_Paulo', 'phase_type': 'Call Auction',
         'phase_name': 'Closing Call', 'phase_memo': None, 'days': 'Mon-Fri', 'offset_days': 0,
         'duration': 300, 'min_start': None, 'max_start': None, 'min_end': None, 'max_end': None,
         'in_force_end_date': datetime.date(2022, 12, 18), 'season_end': None, 'end_with_offset': '17:00:00   ',
         'has_season': False},
        {'schedule_group': 'Partial', 'in_force_start_date': None, 'season_start': None,
         'start': datetime.time(17, 25), 'end': datetime.time(17, 30), 'fin_id': 'BR.BOVESPA',
         'schedule_group_memo': None, 'timezone': 'America/Sao_Paulo', 'phase_type': 'Other',
         'phase_name': 'After Market - Order Cancelation', 'phase_memo': None, 'days': 'Mon-Fri',
         'offset_days': 0, 'duration': 300, 'min_start': None, 'max_start': None,
         'min_end': None, 'max_end': None, 'in_force_end_date': datetime.date(2022, 12, 18),
         'season_end': None, 'end_with_offset': '17:30:00   ',
         'has_season': False},
        {'schedule_group': 'Partial', 'in_force_start_date': None, 'season_start': None,
         'start': datetime.time(17, 30), 'end': datetime.time(18, 0), 'fin_id': 'BR.BOVESPA',
         'schedule_group_memo': None, 'timezone': 'America/Sao_Paulo', 'phase_type': 'Post-Trading Session',
         'phase_name': 'After Market - Trading Session', 'phase_memo': None, 'days': 'Mon-Fri',
         'offset_days': 0, 'duration': 1800, 'min_start': None, 'max_start': None,
         'min_end': None, 'max_end': None, 'in_force_end_date': datetime.date(2022, 12, 18),
         'season_end': None, 'end_with_offset': '18:00:00   ', 'has_season': False},
        {'schedule_group': 'Partial', 'in_force_start_date': None, 'season_start': None,
         'start': datetime.time(18, 30), 'end': datetime.time(18, 45), 'fin_id': 'BR.BOVESPA',
         'schedule_group_memo': None, 'timezone': 'America/Sao_Paulo', 'phase_type': 'Other',
         'phase_name': 'After Market - Order Cancelation', 'phase_memo': None, 'days': 'Mon-Fri',
         'offset_days': 0, 'duration': 900, 'min_start': None, 'max_start': None, 'min_end': None,
         'max_end': None, 'in_force_end_date': datetime.date(2022, 12, 18), 'season_end': None,
         'end_with_offset': '18:45:00   ', 'has_season': False},
        {'schedule_group': 'Partial', 'in_force_start_date': datetime.date(2022, 12, 19), 'season_start': None,
         'start': datetime.time(12, 30), 'end': datetime.time(12, 45), 'fin_id': 'BR.BOVESPA',
         'schedule_group_memo': None, 'timezone': 'America/Sao_Paulo', 'phase_type': 'Pre-Open',
         'phase_name': 'Order Cancelation', 'phase_memo': None, 'days': 'Mon-Fri', 'offset_days': 0,
         'duration': 900, 'min_start': None, 'max_start': None, 'min_end': None, 'max_end': None,
         'in_force_end_date': None, 'season_end': None, 'end_with_offset': '12:45:00   ',
         'has_season': False},
        {'schedule_group': 'Partial', 'in_force_start_date': datetime.date(2022, 12, 19), 'season_start': None,
         'start': datetime.time(12, 45), 'end': datetime.time(13, 0), 'fin_id': 'BR.BOVESPA',
         'schedule_group_memo': None, 'timezone': 'America/Sao_Paulo', 'phase_type': 'Pre-Open',
         'phase_name': 'Opening Session', 'phase_memo': None, 'days': 'Mon-Fri', 'offset_days': 0,
         'duration': 900, 'min_start': None, 'max_start': None, 'min_end': None, 'max_end': None,
         'in_force_end_date': None, 'season_end': None, 'end_with_offset': '13:00:00   ',
         'has_season': False},
        {'schedule_group': 'Partial', 'in_force_start_date': datetime.date(2022, 12, 19), 'season_start': None,
         'start': datetime.time(13, 0), 'end': datetime.time(16, 55), 'fin_id': 'BR.BOVESPA',
         'schedule_group_memo': None, 'timezone': 'America/Sao_Paulo', 'phase_type': 'Primary Trading Session',
         'phase_name': 'Trading Session', 'phase_memo': None, 'days': 'Mon-Fri', 'offset_days': 0,
         'duration': 14100, 'min_start': None, 'max_start': None, 'min_end': None, 'max_end': None,
         'in_force_end_date': None, 'season_end': None, 'end_with_offset': '16:55:00   ', 'has_season': False},
        {'schedule_group': 'Partial', 'in_force_start_date': datetime.date(2022, 12, 19), 'season_start': None,
         'start': datetime.time(16, 55), 'end': datetime.time(17, 0), 'fin_id': 'BR.BOVESPA',
         'schedule_group_memo': None, 'timezone': 'America/Sao_Paulo', 'phase_type': 'Call Auction',
         'phase_name': 'Closing Call', 'phase_memo': None, 'days': 'Mon-Fri', 'offset_days': 0, 'duration': 300,
         'min_start': None, 'max_start': None, 'min_end': None, 'max_end': None, 'in_force_end_date': None,
         'season_end': None, 'end_with_offset': '17:00:00   ', 'has_season': False},
        {'schedule_group': 'Partial', 'in_force_start_date': datetime.date(2022, 12, 19), 'season_start': None,
         'start': datetime.time(17, 25), 'end': datetime.time(17, 30), 'fin_id': 'BR.BOVESPA',
         'schedule_group_memo': None, 'timezone': 'America/Sao_Paulo', 'phase_type': 'Other',
         'phase_name': 'After Market - Order Cancelation', 'phase_memo': None, 'days': 'Mon-Fri', 'offset_days': 0,
         'duration': 300, 'min_start': None, 'max_start': None, 'min_end': None, 'max_end': None,
         'in_force_end_date': None, 'season_end': None, 'end_with_offset': '17:30:00   ', 'has_season': False},
        {'schedule_group': 'Partial', 'in_force_start_date': datetime.date(2022, 12, 19), 'season_start': None,
         'start': datetime.time(17, 30), 'end': datetime.time(18, 0), 'fin_id': 'BR.BOVESPA',
         'schedule_group_memo': None, 'timezone': 'America/Sao_Paulo', 'phase_type': 'Post-Trading Session',
         'phase_name': 'After Market - Trading Session', 'phase_memo': None, 'days': 'Mon-Fri', 'offset_days': 0,
         'duration': 1800, 'min_start': None, 'max_start': None, 'min_end': None, 'max_end': None,
         'in_force_end_date': None, 'season_end': None, 'end_with_offset': '18:00:00   ', 'has_season': False},
        {'schedule_group': 'Partial', 'in_force_start_date': datetime.date(2022, 12, 19), 'season_start': None,
         'start': datetime.time(18, 25), 'end': datetime.time(18, 45), 'fin_id': 'BR.BOVESPA',
         'schedule_group_memo': None, 'timezone': 'America/Sao_Paulo', 'phase_type': 'Other',
         'phase_name': 'After Market - Order Cancelation', 'phase_memo': None, 'days': 'Mon-Fri',
         'offset_days': 0, 'duration': 1200, 'min_start': None, 'max_start': None, 'min_end': None,
         'max_end': None, 'in_force_end_date': None, 'season_end': None, 'end_with_offset': '18:45:00   ',
         'has_season': False},
        {'schedule_group': 'Regular', 'in_force_start_date': None, 'season_start': 'First Sunday of November',
         'start': datetime.time(9, 30), 'end': datetime.time(9, 45), 'fin_id': 'BR.BOVESPA',
         'schedule_group_memo': None, 'timezone': 'America/Sao_Paulo', 'phase_type': 'Other',
         'phase_name': 'Order Cancelation', 'phase_memo': None, 'days': 'Mon-Fri', 'offset_days': 0,
         'duration': 900, 'min_start': None, 'max_start': None, 'min_end': None, 'max_end': None,
         'in_force_end_date': datetime.date(2022, 12, 18), 'season_end': 'Previous day Second Sunday of march',
         'end_with_offset': '09:45:00   ', 'has_season': True},
        {'schedule_group': 'Regular', 'in_force_start_date': None, 'season_start': 'First Sunday of November',
         'start': datetime.time(9, 45), 'end': datetime.time(10, 0), 'fin_id': 'BR.BOVESPA',
         'schedule_group_memo': None, 'timezone': 'America/Sao_Paulo', 'phase_type': 'Pre-Open',
         'phase_name': 'Opening Session', 'phase_memo': None, 'days': 'Mon-Fri', 'offset_days': 0,
         'duration': 900, 'min_start': None, 'max_start': None, 'min_end': None, 'max_end': None,
         'in_force_end_date': datetime.date(2022, 12, 18), 'season_end': 'Previous day Second Sunday of march',
         'end_with_offset': '10:00:00   ', 'has_season': True},
        {'schedule_group': 'Regular', 'in_force_start_date': None, 'season_start': 'First Sunday of November',
         'start': datetime.time(10, 0), 'end': datetime.time(17, 55), 'fin_id': 'BR.BOVESPA',
         'schedule_group_memo': None, 'timezone': 'America/Sao_Paulo', 'phase_type': 'Primary Trading Session',
         'phase_name': 'Trading Session', 'phase_memo': None, 'days': 'Mon-Fri', 'offset_days': 0,
         'duration': 28500, 'min_start': None, 'max_start': None, 'min_end': None, 'max_end': None,
         'in_force_end_date': datetime.date(2022, 12, 18), 'season_end': 'Previous day Second Sunday of march',
         'end_with_offset': '17:55:00   ', 'has_season': True},
        {'schedule_group': 'Regular', 'in_force_start_date': None, 'season_start': 'First Sunday of November',
         'start': datetime.time(17, 55), 'end': datetime.time(18, 0), 'fin_id': 'BR.BOVESPA',
         'schedule_group_memo': None, 'timezone': 'America/Sao_Paulo', 'phase_type': 'Call Auction',
         'phase_name': 'Closing Call', 'phase_memo': None, 'days': 'Mon-Fri', 'offset_days': 0,
         'duration': 300, 'min_start': None, 'max_start': None, 'min_end': None, 'max_end': None,
         'in_force_end_date': datetime.date(2022, 12, 18), 'season_end': 'Previous day Second Sunday of march',
         'end_with_offset': '18:00:00   ', 'has_season': True},
        {'schedule_group': 'Regular', 'in_force_start_date': None, 'season_start': 'Second Sunday of March',
         'start': datetime.time(9, 30), 'end': datetime.time(9, 45), 'fin_id': 'BR.BOVESPA',
         'schedule_group_memo': None, 'timezone': 'America/Sao_Paulo', 'phase_type': 'Other',
         'phase_name': 'Order Cancelation', 'phase_memo': None, 'days': 'Mon-Fri', 'offset_days': 0,
         'duration': 900, 'min_start': None, 'max_start': None, 'min_end': None, 'max_end': None,
         'in_force_end_date': datetime.date(2022, 12, 18), 'season_end': 'Previous day first Sunday of November',
         'end_with_offset': '09:45:00   ', 'has_season': True},
        {'schedule_group': 'Regular', 'in_force_start_date': None, 'season_start': 'Second Sunday of March',
         'start': datetime.time(9, 45), 'end': datetime.time(10, 0), 'fin_id': 'BR.BOVESPA',
         'schedule_group_memo': None, 'timezone': 'America/Sao_Paulo', 'phase_type': 'Pre-Open',
         'phase_name': 'Opening Session', 'phase_memo': None, 'days': 'Mon-Fri', 'offset_days': 0,
         'duration': 900, 'min_start': None, 'max_start': None, 'min_end': None, 'max_end': None,
         'in_force_end_date': datetime.date(2022, 12, 18), 'season_end': 'Previous day first Sunday of November',
         'end_with_offset': '10:00:00   ', 'has_season': True},
        {'schedule_group': 'Regular', 'in_force_start_date': None, 'season_start': 'Second Sunday of March',
         'start': datetime.time(10, 0), 'end': datetime.time(16, 55), 'fin_id': 'BR.BOVESPA',
         'schedule_group_memo': None, 'timezone': 'America/Sao_Paulo', 'phase_type': 'Primary Trading Session',
         'phase_name': 'Trading Session', 'phase_memo': None, 'days': 'Mon-Fri', 'offset_days': 0,
         'duration': 24900, 'min_start': None, 'max_start': None, 'min_end': None, 'max_end': None,
         'in_force_end_date': datetime.date(2022, 12, 18), 'season_end': 'Previous day first Sunday of November',
         'end_with_offset': '16:55:00   ', 'has_season': True},
        {'schedule_group': 'Regular', 'in_force_start_date': None, 'season_start': 'Second Sunday of March',
         'start': datetime.time(16, 55), 'end': datetime.time(17, 0), 'fin_id': 'BR.BOVESPA',
         'schedule_group_memo': None, 'timezone': 'America/Sao_Paulo', 'phase_type': 'Call Auction',
         'phase_name': 'Closing Call', 'phase_memo': None, 'days': 'Mon-Fri', 'offset_days': 0,
         'duration': 300, 'min_start': None, 'max_start': None, 'min_end': None, 'max_end': None,
         'in_force_end_date': datetime.date(2022, 12, 18), 'season_end': 'Previous day first Sunday of November',
         'end_with_offset': '17:00:00   ', 'has_season': True},
        {'schedule_group': 'Regular', 'in_force_start_date': None, 'season_start': 'Second Sunday of March',
         'start': datetime.time(17, 25), 'end': datetime.time(17, 30), 'fin_id': 'BR.BOVESPA',
         'schedule_group_memo': None, 'timezone': 'America/Sao_Paulo', 'phase_type': 'Other',
         'phase_name': 'After Market - Order Cancelation', 'phase_memo': None, 'days': 'Mon-Fri',
         'offset_days': 0, 'duration': 300, 'min_start': None, 'max_start': None, 'min_end': None, 'max_end': None,
         'in_force_end_date': datetime.date(2022, 12, 18), 'season_end': 'Previous day first Sunday of November',
         'end_with_offset': '17:30:00   ', 'has_season': True},
        {'schedule_group': 'Regular', 'in_force_start_date': None, 'season_start': 'Second Sunday of March',
         'start': datetime.time(17, 30), 'end': datetime.time(18, 0), 'fin_id': 'BR.BOVESPA',
         'schedule_group_memo': None, 'timezone': 'America/Sao_Paulo', 'phase_type': 'Post-Trading Session',
         'phase_name': 'After Market - Trading Session', 'phase_memo': None, 'days': 'Mon-Fri',
         'offset_days': 0, 'duration': 1800, 'min_start': None, 'max_start': None, 'min_end': None,
         'max_end': None, 'in_force_end_date': datetime.date(2022, 12, 18),
         'season_end': 'Previous day first Sunday of November', 'end_with_offset': '18:00:00   ',
         'has_season': True},
        {'schedule_group': 'Regular', 'in_force_start_date': None, 'season_start': 'Second Sunday of March',
         'start': datetime.time(18, 30), 'end': datetime.time(18, 45), 'fin_id': 'BR.BOVESPA',
         'schedule_group_memo': None, 'timezone': 'America/Sao_Paulo', 'phase_type': 'Other',
         'phase_name': 'After Market - Order Cancelation', 'phase_memo': None, 'days': 'Mon-Fri',
         'offset_days': 0, 'duration': 900, 'min_start': None, 'max_start': None, 'min_end': None, 'max_end': None,
         'in_force_end_date': datetime.date(2022, 12, 18), 'season_end': 'Previous day first Sunday of November',
         'end_with_offset': '18:45:00   ', 'has_season': True},
        {'schedule_group': 'Regular', 'in_force_start_date': datetime.date(2022, 12, 19), 'season_start': None,
         'start': datetime.time(18, 25), 'end': datetime.time(18, 45), 'fin_id': 'BR.BOVESPA',
         'schedule_group_memo': None, 'timezone': 'America/Sao_Paulo', 'phase_type': 'Other',
         'phase_name': 'After Market - Order Cancelation', 'phase_memo': None, 'days': 'Mon-Fri',
         'offset_days': 0, 'duration': 1200, 'min_start': None, 'max_start': None,
         'min_end': None, 'max_end': None, 'in_force_end_date': None, 'season_end': None,
         'end_with_offset': '18:45:00   ', 'has_season': False},
        {'schedule_group': 'Regular', 'in_force_start_date': datetime.date(2022, 12, 19),
         'season_start': 'First Sunday of November', 'start': datetime.time(9, 30), 'end': datetime.time(9, 45),
         'fin_id': 'BR.BOVESPA', 'schedule_group_memo': None, 'timezone': 'America/Sao_Paulo',
         'phase_type': 'Other', 'phase_name': 'Order Cancelation', 'phase_memo': None,
         'days': 'Mon-Fri', 'offset_days': 0, 'duration': 900, 'min_start': None, 'max_start': None,
         'min_end': None, 'max_end': None, 'in_force_end_date': None,
         'season_end': 'Previous day Second Sunday of march', 'end_with_offset': '09:45:00   ',
         'has_season': True},
        {'schedule_group': 'Regular', 'in_force_start_date': datetime.date(2022, 12, 19),
         'season_start': 'First Sunday of November', 'start': datetime.time(9, 45), 'end': datetime.time(10, 0),
         'fin_id': 'BR.BOVESPA', 'schedule_group_memo': None, 'timezone': 'America/Sao_Paulo',
         'phase_type': 'Pre-Open', 'phase_name': 'Opening Session', 'phase_memo': None, 'days': 'Mon-Fri',
         'offset_days': 0, 'duration': 900, 'min_start': None, 'max_start': None, 'min_end': None,
         'max_end': None, 'in_force_end_date': None, 'season_end': 'Previous day Second Sunday of march',
         'end_with_offset': '10:00:00   ', 'has_season': True},
        {'schedule_group': 'Regular', 'in_force_start_date': datetime.date(2022, 12, 19),
         'season_start': 'First Sunday of November', 'start': datetime.time(10, 0),
         'end': datetime.time(17, 55), 'fin_id': 'BR.BOVESPA', 'schedule_group_memo': None,
         'timezone': 'America/Sao_Paulo', 'phase_type': 'Primary Trading Session', 'phase_name': 'Trading Session',
         'phase_memo': None, 'days': 'Mon-Fri', 'offset_days': 0, 'duration': 28500, 'min_start': None,
         'max_start': None, 'min_end': None, 'max_end': None, 'in_force_end_date': None,
         'season_end': 'Previous day Second Sunday of march', 'end_with_offset': '17:55:00   ',
         'has_season': True},
     {'schedule_group': 'Regular', 'in_force_start_date': datetime.date(2022, 12, 19),
      'season_start': 'First Sunday of November', 'start': datetime.time(17, 55), 'end': datetime.time(18, 0),
      'fin_id': 'BR.BOVESPA', 'schedule_group_memo': None, 'timezone': 'America/Sao_Paulo', 'phase_type': 'Call Auction',
      'phase_name': 'Closing Call', 'phase_memo': None, 'days': 'Mon-Fri', 'offset_days': 0, 'duration': 300,
      'min_start': None, 'max_start': None, 'min_end': None, 'max_end': None, 'in_force_end_date': None,
      'season_end': 'Previous day Second Sunday of march', 'end_with_offset': '18:00:00   ', 'has_season': True},
     {'schedule_group': 'Regular', 'in_force_start_date': datetime.date(2022, 12, 19), 'season_start': 'Second Sunday of March',
      'start': datetime.time(9, 30), 'end': datetime.time(9, 45), 'fin_id': 'BR.BOVESPA', 'schedule_group_memo': None,
      'timezone': 'America/Sao_Paulo', 'phase_type': 'Other', 'phase_name': 'Order Cancelation', 'phase_memo': None,
      'days': 'Mon-Fri', 'offset_days': 0, 'duration': 900, 'min_start': None, 'max_start': None,
      'min_end': None, 'max_end': None, 'in_force_end_date': None, 'season_end': 'Previous day first Sunday of November',
      'end_with_offset': '09:45:00   ', 'has_season': True},
     {'schedule_group': 'Regular', 'in_force_start_date': datetime.date(2022, 12, 19),
      'season_start': 'Second Sunday of March', 'start': datetime.time(9, 45), 'end': datetime.time(10, 0),
      'fin_id': 'BR.BOVESPA', 'schedule_group_memo': None, 'timezone': 'America/Sao_Paulo', 'phase_type': 'Pre-Open',
      'phase_name': 'Opening Session', 'phase_memo': None, 'days': 'Mon-Fri', 'offset_days': 0, 'duration': 900,
      'min_start': None, 'max_start': None, 'min_end': None, 'max_end': None, 'in_force_end_date': None,
      'season_end': 'Previous day first Sunday of November', 'end_with_offset': '10:00:00   ', 'has_season': True},
     {'schedule_group': 'Regular', 'in_force_start_date': datetime.date(2022, 12, 19),
      'season_start': 'Second Sunday of March', 'start': datetime.time(10, 0), 'end': datetime.time(16, 55),
      'fin_id': 'BR.BOVESPA', 'schedule_group_memo': None, 'timezone': 'America/Sao_Paulo',
      'phase_type': 'Primary Trading Session', 'phase_name': 'Trading Session', 'phase_memo': None,
      'days': 'Mon-Fri', 'offset_days': 0, 'duration': 24900, 'min_start': None, 'max_start': None,
      'min_end': None, 'max_end': None, 'in_force_end_date': None, 'season_end': 'Previous day first Sunday of November',
      'end_with_offset': '16:55:00   ', 'has_season': True},
     {'schedule_group': 'Regular', 'in_force_start_date': datetime.date(2022, 12, 19), 'season_start': 'Second Sunday of March',
      'start': datetime.time(16, 55), 'end': datetime.time(17, 0), 'fin_id': 'BR.BOVESPA', 'schedule_group_memo': None,
      'timezone': 'America/Sao_Paulo', 'phase_type': 'Call Auction', 'phase_name': 'Closing Call',
      'phase_memo': None, 'days': 'Mon-Fri', 'offset_days': 0, 'duration': 300, 'min_start': None,
      'max_start': None, 'min_end': None, 'max_end': None, 'in_force_end_date': None,
      'season_end': 'Previous day first Sunday of November', 'end_with_offset': '17:00:00   ', 'has_season': True},
     {'schedule_group': 'Regular', 'in_force_start_date': datetime.date(2022, 12, 19), 'season_start': 'Second Sunday of March',
      'start': datetime.time(17, 25), 'end': datetime.time(17, 30), 'fin_id': 'BR.BOVESPA', 'schedule_group_memo': None,
      'timezone': 'America/Sao_Paulo', 'phase_type': 'Other', 'phase_name': 'After Market - Order Cancelation',
      'phase_memo': None, 'days': 'Mon-Fri', 'offset_days': 0, 'duration': 300, 'min_start': None, 'max_start': None,
      'min_end': None, 'max_end': None, 'in_force_end_date': None, 'season_end': 'Previous day first Sunday of November',
      'end_with_offset': '17:30:00   ', 'has_season': True},
     {'schedule_group': 'Regular', 'in_force_start_date': datetime.date(2022, 12, 19),
      'season_start': 'Second Sunday of March', 'start': datetime.time(17, 30), 'end': datetime.time(18, 0),
      'fin_id': 'BR.BOVESPA', 'schedule_group_memo': None, 'timezone': 'America/Sao_Paulo',
      'phase_type': 'Post-Trading Session', 'phase_name': 'After Market - Trading Session', 'phase_memo': None,
      'days': 'Mon-Fri', 'offset_days': 0, 'duration': 1800, 'min_start': None, 'max_start': None, 'min_end': None,
      'max_end': None, 'in_force_end_date': None, 'season_end': 'Previous day first Sunday of November', 'end_with_offset': '18:00:00   ',
      'has_season': True}]
)
])
def test_list_schedules(fin_id, expected):
    scheds = Market.get(fin_id).list_schedules()
    scheds = [s.to_dict() for s in scheds]

    assert len(scheds) == len(expected)

    matched = []
    for sched in scheds:
        equals = 0
        for i, expect in enumerate(expected):
            if i in matched: continue
            if (sorted(sched.keys()) == sorted(expect.keys()) and
                    all(sched[k] == expect[k] for k in sched)):
                    matched.append(i)
                    equals += 1
        assert equals == 1


        # # apply custom sorting of dictionaries
        # # for easier check if the sorting in .list_schedules is correct
        # assert sorted(sched.keys()) == sorted(expect.keys())
        # for k in sched:
        #     assert sched[k] == expect[k], f"{k} failed, expected:\n{pformat(expect)}"
        #
