def test_server_compute_use_pycode_in_data(): result_url = pj(tmp_dir, "result.json") oas = OAServer("oas") oas.registered() oas.compute("pycode:def main(a): return a", "a = 1", result_url) assert get_json(result_url) == dict(id='oas', result=1) oas.compute("pycode:def main(a): return a", "code:a = 2", result_url) assert get_json(result_url) == dict(id='oas', result=2)
def test_server_register_correctly_locally(): reg_file = pj(tmp_dir, "registration.json") oas = OAServerFile("doofus", wdir) oas.register(reg_file) assert exists(reg_file) reg = get_json(reg_file) for key in ('id', 'url', 'urlping', 'urldelete'): assert key in reg['args']
def test_get_json_read_local_file(): url = pj(tmp_dir, "toto.json") data = dict(toto=1) with open(url, 'w') as f: json.dump(data, f) new_data = get_json(url) assert id(data) != id(new_data) assert data == new_data
def test_server_compute_use_remote_in_data(): result_url = pj(tmp_dir, "result.json") data_url = "http://myserver.myns/data.json" oas = OAServer("oas") oas.registered() with requests_mock.Mocker() as m: m.get(data_url, text=json.dumps({'a': 4})) oas.compute("pycode:def main(a): return a", data_url, result_url) assert get_json(result_url) == dict(id='oas', result=4)
def test_server_compute_use_local_in_data(): result_url = pj(tmp_dir, "result.json") data_url = pj(tmp_dir, "data.json") oas = OAServer("oas") oas.registered() with open(data_url, 'w') as f: json.dump({'a': 3}, f) oas.compute("pycode:def main(a): return a", "file:%s" % data_url, result_url) assert get_json(result_url) == dict(id='oas', result=3)
def test_server_register_correctly_when_started_already(): reg_file = pj(tmp_dir, "registration.json") oas = OAServerFile("doofus", wdir) oas.start() oas.register(reg_file) res = [exists(reg_file)] reg = get_json(reg_file) for key in ('id', 'url', 'urlping', 'urldelete'): res.append(key in reg['args']) oas.stop() assert all(res)
def test_server_ping(): answer_file = pj(tmp_dir, "answer.json") oas = OAServerFile("doofus", wdir) oas.start() oas.register(answer_file) ping_pth = pj(wdir, get_json(answer_file)['args']['urlping']) remove(answer_file) post_json(ping_pth, dict(url=answer_file)) ans = wait_for_content(answer_file, NB) res = [ans['state'] == 'waiting', ans['id'] == "doofus"] oas.stop() oas.join() assert all(res) assert not exists(ping_pth)
def test_server_do_not_react_to_unknown_files(): answer_file = pj(tmp_dir, "answer.json") oas = OAServerFile("doofus", wdir) oas.start() oas.register(answer_file) post_json(pj(wdir, "takapouet.cmd"), dict(a=1)) ping_pth = pj(wdir, get_json(answer_file)['args']['urlping']) remove(answer_file) post_json(ping_pth, dict(url=answer_file)) ans = wait_for_content(answer_file, NB) res = [ans['state'] == 'waiting', ans['id'] == "doofus"] oas.stop() oas.join() assert all(res)
def test_server_compute(): answer_file = pj(tmp_dir, "answer.json") oas = OAServerFile("doofus", wdir) oas.start() oas.register(answer_file) cpt_pth = pj(wdir, get_json(answer_file)['args']['url']) remove(answer_file) post_json(cpt_pth, dict(workflow="pycode:def main(a): return a", urldata="a = 1", urlreturn=answer_file)) ans = wait_for_content(answer_file, NB) res = [ans['result'] == 1, ans['id'] == "doofus"] oas.stop() oas.join() assert all(res) assert not exists(cpt_pth)
def test_get_json_raise_error_if_bad_json(): url = pj(tmp_dir, "toto.json") with open(url, 'w') as f: f.write("bad data") assert_raises(ValueError, lambda: get_json(url))
def test_get_json_raise_error_if_no_file(): assert_raises(URLError, lambda: get_json(pj(tmp_dir, "toto.tutu")))