def test_feed_root_nonempty(data_dir, scheme): # we know this returns the right thing based on all of the tests above good_url = str(data_dir.join('full.rss')) good_result = default_parser('')(good_url) test_url = scheme + 'full.rss' test_result = default_parser(data_dir)(test_url) # sanity check assert good_result.feed.url == good_url good_entries = list(good_result.entries) assert {e.feed_url for e in good_entries} == { good_url, } assert test_result.feed.url == test_url test_entries = list(test_result.entries) assert {e.feed_url for e in test_entries} == { test_url, } assert test_result.feed == good_result.feed._replace(url=test_url) assert test_entries == [ e._replace(feed_url=test_url) for e in good_entries ]
def test_feed_root_relative_root_error(monkeypatch, os_name, root): import ntpath, posixpath monkeypatch.setattr('os.name', os_name) monkeypatch.setattr('os.path', {'nt': ntpath, 'posix': posixpath}[os_name]) with pytest.raises(ValueError) as excinfo: default_parser(root) monkeypatch.undo() assert 'root must be absolute' in str(excinfo.value)
def test_parse_requests_exception(monkeypatch, exc_cls): exc = exc_cls('exc') class BadWrapper(SessionWrapper): def get(self, *args, **kwargs): raise exc monkeypatch.setattr('reader._parser.Parser.make_session', BadWrapper) with pytest.raises(ParseError) as excinfo: default_parser('')('http://example.com') assert excinfo.value.__cause__ is exc assert excinfo.value.url == 'http://example.com' assert 'while getting feed' in excinfo.value.message
def test_feed_root_none(data_dir, scheme): parse = default_parser(None) url = scheme + str(data_dir.join('full.atom')) with pytest.raises(ParseError) as excinfo: parse(url) assert excinfo.value.url == url assert 'no retriever' in excinfo.value.message
def test_parse_response_plugins(monkeypatch, tmpdir, make_http_url, data_dir): feed_url = make_http_url(data_dir.join('empty.atom')) make_http_url(data_dir.join('full.atom')) import requests def req_plugin(session, request, **kwargs): req_plugin.called = True assert request.url == feed_url def do_nothing_plugin(session, response, request, **kwargs): do_nothing_plugin.called = True assert isinstance(session, requests.Session) assert isinstance(response, requests.Response) assert isinstance(request, requests.Request) assert request.url == feed_url def rewrite_to_empty_plugin(session, response, request, **kwargs): rewrite_to_empty_plugin.called = True request.url = request.url.replace('empty', 'full') return request parse = default_parser() parse.session_hooks.request.append(req_plugin) parse.session_hooks.response.append(do_nothing_plugin) parse.session_hooks.response.append(rewrite_to_empty_plugin) feed, _, _, _ = parse(feed_url) assert req_plugin.called assert do_nothing_plugin.called assert rewrite_to_empty_plugin.called assert feed.link is not None
def test_feed_root_empty(data_dir, scheme, relative): # TODO: this test looks a lot like test_feed_root_nonempty if relative and scheme.startswith('file://'): pytest.skip("can't have relative URIs with 'file://...'") parse = default_parser('') # we know this returns the right thing based on all of the tests above good_path = data_dir.join('full.rss') good_url = str(good_path) good_result = parse(good_url) test_path = data_dir.join('full.rss') if relative: test_path = test_path.relto(type(data_dir)()) test_url = scheme + str(test_path) test_result = parse(test_url) # sanity check assert good_result.feed.url == good_url good_entries = list(good_result.entries) assert {e.feed_url for e in good_entries} == { good_url, } assert test_result.feed.url == test_url test_entries = list(test_result.entries) assert {e.feed_url for e in test_entries} == { test_url, } assert test_result.feed == good_result.feed._replace(url=test_url) assert test_entries == [e._replace(feed_url=test_url) for e in good_entries]
def test_feed_root_nonenmpty_bad_paths(data_dir, url, reason): with pytest.raises(ParseError) as excinfo: default_parser(data_dir)(url) assert excinfo.value.url == url assert reason in excinfo.value.message
def parse(): parse = default_parser('') yield parse