def test_market_provider_epics_list(config, make_broker):
    """
    Test the MarketProvider configured to fetch markets from an epics list
    """
    # Configure TradingBot to use an epic list
    config["general"]["market_source"]["value"] = "list"
    config["general"]["epic_ids_filepath"] = "test/test_data/epics_list.txt"

    # load test data for market info response, so it can be used to mock the info
    # for each epic in the epic_list
    mock_info = None
    try:
        with open("test/test_data/ig/mock_market_info.json", "r") as file:
            mock_info = json.load(file)
    except IOError:
        exit()

    # Create the class to test
    mp = MarketProvider(config, make_broker)

    # Run the test several times resetting the market provider
    for _ in range(4):
        # Read the test epic list and create a local list of the expected epics
        expected_list = []
        with open("test/test_data/epics_list.txt", "r") as epics_list:
            for cnt, line in enumerate(epics_list):
                epic = line.rstrip()
                expected_list += [epic]

        # Keep caling the test function building a list of returned epics
        actual_list = []
        try:
            while True:
                actual_list.append(mp.next().epic)
        except StopIteration:
            # Verify we read all epics in the list
            assert len(expected_list) == len(actual_list)
            # Verify reading the next raise another exception
            with pytest.raises(StopIteration) as e:
                mp.next()
            mp.reset()
            continue
        # If we get here it means that next did not raise an exception at the end of the list
        assert False
def test_market_provider_watchlist(config, make_broker):
    """
    Test the MarketProvider configured to fetch markets from an IG watchlist
    """
    # Define configuration for this test
    config["general"]["market_source"]["value"] = "watchlist"
    # Watchlist name depending on test data json
    config["general"]["watchlist_name"] = "My Watchlist"

    # Create class to test
    mp = MarketProvider(config, make_broker)

    # The test data for market_info return always the same epic id, but the test
    # data for the watchlist contains 3 markets
    # Run the test several times resetting the market provider
    for _ in range(4):
        assert mp.next().epic == "KA.D.GSK.DAILY.IP"
        assert mp.next().epic == "KA.D.GSK.DAILY.IP"
        assert mp.next().epic == "KA.D.GSK.DAILY.IP"

        with pytest.raises(StopIteration) as e:
            mp.next()
        mp.reset()