def test_put_remote_raise_error_if_bad_location(): url = 'http://krakoukas.schmilblik/' assert_raises(URLError, lambda: put(tmp_file, url)) with requests_mock.Mocker() as m: m.post(url, text="", status_code=500) assert_raises(URLError, lambda: put(tmp_file, url))
def test_put_remote_write_remote_file(): url = "http://krakoukas.schmilblik/toto.json" def text_callback(request, context): del context assert request.text.strip() == tmp_txt with requests_mock.Mocker() as m: m.post(url, text=text_callback) put(tmp_file, url)
def test_put_local_handle_different_url_format(): pth = "%s/toto1.json" % tmp_dir put(tmp_file, pth) assert os.path.exists(pth) os.remove(pth) pth = "%s/toto2.json" % tmp_dir put(tmp_file, "file:%s" % pth) assert os.path.exists(pth) os.remove(pth)
def test_put_local_write_local_file(): pth = pj(tmp_dir, "tutu.txt") put(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_put_remote_handle_different_url_format(): count = [0] def text_callback(request, context): del request del context count[0] += 1 with requests_mock.Mocker() as m: for url in ('http://test.com/', 'http://test.com/file.txt'): m.post(url, text=text_callback) put(tmp_file, url) assert count[0] == 2
def post_json(url, data): """Encode data in json format and write it on url. Args: url: (url) a string or split url either remote or local data: (any): must be json serializable Returns: None """ pth = "tmp_post_json.json" # TODO better use of random with open(pth, "w") as f: json.dump(data, f) put(pth, url) remove(pth)
def test_put_raise_error_if_local_dst_not_accessible(): assert_raises(URLError, lambda: put(tmp_file, "tugudu/data.json"))
def test_put_raise_error_if_dst_not_accessible(): assert_raises(URLError, lambda: put(tmp_file, bad_url))
def test_put_raise_error_if_src_not_accessible(): assert_raises(IOError, lambda: put(bad_file, tmp_file))