def test_get_read_local_file(): pth = pj(tmp_dir, "guiliguili.txt") get(tmp_file, pth) assert os.path.exists(pth) with open(pth, 'r') as f: assert f.read().strip() == tmp_txt os.remove(pth)
def test_get_github_url(): url = "https://raw.githubusercontent.com/revesansparole/oaserver/master/README.rst" pth = pj(tmp_dir, "tmp_test_github.txt") get(url, pth) assert os.path.exists(pth) with open(pth, 'r') as f: assert "oaserver" in f.read() os.remove(pth)
def test_get_handle_different_url_format(): txt = "some data in any format" with requests_mock.Mocker() as m: for url in ('http://test.com/', 'http://test.com/file.txt'): m.get(url, text=txt) pth = pj(tmp_dir, "tmp_test_file.txt") get(url, pth) assert os.path.exists(pth) with open(pth, 'r') as f: assert f.read().strip() == txt os.remove(pth)
def test_get_read_remote_file(): url = 'http://test.com/' txt = "some data in any format" with requests_mock.Mocker() as m: m.get(url, text=txt) pth = pj(tmp_dir, "tmp_test_file.txt") get(url, pth) assert os.path.exists(pth) with open(pth, 'r') as f: assert f.read().strip() == txt os.remove(pth)
def get_json(url): """Load json coded data from a given url. Args: url: (url) either string or url Returns: (any) data encode in json file """ pth = "tmp_get_json.json" # TODO better use of random get(url, pth) with open(pth, "r") as f: data = json.load(f) remove(pth) return data
def test_get_raise_error_if_no_file(): url = 'http://test.com/' with requests_mock.Mocker() as m: m.get(url, text="", status_code=500) assert_raises(URLError, lambda: get(url, bad_file))
def test_get_raise_error_if_dst_not_accessible(): assert_raises(IOError, lambda: get(tmp_file, "tugudu/data.json"))
def test_get_raise_error_if_local_src_not_accessible(): assert_raises(URLError, lambda: get(bad_file, tmp_file))