def test_query_string(self): with requests_mock.mock() as mock: mock.get( raw.get_full_url("data/projects?name=Test"), text=json.dumps([{"name": "Project"}]), ) self.assertEqual( raw.fetch_first("projects", {"name": "Test"}), {"name": "Project"}, ) mock.get(raw.get_full_url("data/persons"), text=json.dumps([])) self.assertIsNone(raw.fetch_first("persons"))
def test_fetch_first(self): with requests_mock.mock() as mock: mock.get(client.get_full_url("data/persons"), text=json.dumps([{ "first_name": "John" }, { "first_name": "Jane" }])) self.assertEquals(client.fetch_first("persons"), {"first_name": "John"}) mock.get(client.get_full_url("data/persons"), text=json.dumps([])) self.assertIsNone(client.fetch_first("persons"))
def test_get(self): with requests_mock.mock() as mock: mock.get( raw.get_full_url("data/persons"), text='{"first_name": "John"}', ) self.assertEqual(raw.get("data/persons"), {"first_name": "John"})
def test_upload_multiple_files(self): with open("./tests/fixtures/v1.png", "rb") as test_file: with requests_mock.Mocker() as mock: mock.post( raw.get_full_url("data/new-file"), text='{"id": "person-01", "first_name": "John"}', ) def verify_file_callback(request): body_file = io.BytesIO(request.body) _, pdict = cgi.parse_header( request.headers['Content-Type']) if sys.version_info[0] == 3: pdict["boundary"] = bytes(pdict["boundary"], "UTF-8") else: pdict["boundary"] = bytes(pdict["boundary"]) parsed = cgi.parse_multipart(fp=body_file, pdict=pdict) assert "file" in parsed assert "test" in parsed and parsed["test"] file_data = test_file.read() assert file_data == parsed["file"][0] assert file_data == parsed["file-2"][0] return None mock.post("data/new-file", json={}) mock.add_matcher(verify_file_callback) raw.upload("data/new-file", "./tests/fixtures/v1.png", data={ "test": True, }, extra_files=["./tests/fixtures/v1.png"])
def test_get_file_data_from_url(self): with requests_mock.mock() as mock: mock.get( raw.get_full_url("test_url"), text="test", ) self.assertEqual(raw.get_file_data_from_url("test_url"), b"test")
def test_fetch_all(self): with requests_mock.mock() as mock: mock.get(client.get_full_url("data/persons"), text='[{"first_name": "John"}]') self.assertEquals(client.fetch_all("persons"), [{ "first_name": "John" }])
def test_put(self): with requests_mock.mock() as mock: mock.put(client.get_full_url('data/persons'), text='{"id": "person-1", "first_name": "John"}') self.assertEquals(client.put('data/persons', "person-1"), { "id": "person-1", "first_name": "John" })
def test_get_current_user(self): with requests_mock.mock() as mock: mock.get(client.get_full_url("auth/authenticated"), text=json.dumps({"user": { "id": "123" }})) current_user = client.get_current_user() self.assertEquals(current_user["id"], "123")
def test_init_log_in_fail(self): with requests_mock.mock() as mock: mock.post( client.get_full_url("auth/login"), text=json.dumps({"login": False}), ) self.assertRaises(AuthFailedException, gazu.log_in, "frank", "test")
def test_init_log_in_fail(self): with requests_mock.mock() as mock: mock.post( raw.get_full_url("auth/login"), text=json.dumps({"login": False}), ) self.assertRaises(AuthFailedException, gazu.log_in, "frank", "test") self.assertRaises(AuthFailedException, gazu.log_in, "", "") with requests_mock.mock() as mock: mock.head(raw.get_host()) mock.post( raw.get_full_url("auth/login"), text=json.dumps({}), status_code=400, ) with self.assertRaises(AuthFailedException): gazu.log_in("", "")
def test_delete(self): with requests_mock.mock() as mock: mock.delete( client.get_full_url("data/persons/person-1"), text='' ) self.assertEquals( client.delete("data/persons/person-1"), "" )
def test_fetch_all(self): with requests_mock.mock() as mock: mock.get( raw.get_full_url("data/persons"), text=json.dumps([{"first_name": "John"}]), ) self.assertEqual( raw.fetch_all("persons"), [{"first_name": "John"}] )
def test_upload(self): with open("./tests/fixtures/v1.png", "rb") as test_file: with requests_mock.Mocker() as mock: mock.post( raw.get_full_url("data/new-file"), text=json.dumps({ "id": "person-01", "first_name": "John" }), ) def verify_file_callback(request): body_file = io.BytesIO(request.body) _, pdict = cgi.parse_header( request.headers["Content-Type"]) if sys.version_info[0] == 3: pdict["boundary"] = bytes(pdict["boundary"], "UTF-8") else: pdict["boundary"] = bytes(pdict["boundary"]) parsed = cgi.parse_multipart(fp=body_file, pdict=pdict) assert "file" in parsed assert "test" in parsed and parsed["test"] assert test_file.read() == parsed["file"][0] return None mock.post("data/new-file", json={}) mock.add_matcher(verify_file_callback) raw.upload( "data/new-file", "./tests/fixtures/v1.png", data={"test": True}, ) with requests_mock.Mocker() as mock: mock.post( raw.get_full_url("data/new-file"), text=json.dumps({"message": "Error"}), ) mock.post("data/new-file", json={}) with self.assertRaises(gazu.client.UploadFailedException): raw.upload( "data/new-file", "./tests/fixtures/v1.png", data={"test": True}, )
def test_init_log_out(self): with requests_mock.mock() as mock: mock.head(raw.get_host()) mock.get( raw.get_full_url("auth/logout"), text=json.dumps({}), status_code=400, ) gazu.log_out() self.assertEqual(raw.default_client.tokens, {})
def test_post_with_a_date_field(self): now = datetime.datetime.now() with requests_mock.mock() as mock: mock.post(client.get_full_url('data/persons'), text='{"id": "person-1", "first_name": "John"}') self.assertEquals(client.post("data/persons", {"birth_date": now}), { "id": "person-1", "first_name": "John" })
def test_host_is_valid(self): self.assertFalse(raw.host_is_valid()) with requests_mock.mock() as mock: mock.head(raw.get_host()) mock.post( raw.get_full_url("auth/login"), text=json.dumps({}), status_code=400, ) self.assertTrue(raw.host_is_valid())
def test_init_log_in(self): with requests_mock.mock() as mock: mock.post( client.get_full_url("auth/login"), text=json.dumps( {"login": True, "tokens": {"access_token": "tokentest"}} ) ) gazu.log_in("frank", "test") self.assertEquals(client.tokens["tokens"]["access_token"], "tokentest")
def test_create(self): with requests_mock.mock() as mock: mock.post( raw.get_full_url("data/persons"), text=json.dumps({"id": "person-01", "first_name": "John"}), ) self.assertEqual( raw.create("persons", {"first_name": "John"}), {"id": "person-01", "first_name": "John"}, )
def test_post_with_a_date_field(self): now = datetime.datetime.now() with requests_mock.mock() as mock: mock.post( raw.get_full_url("data/persons"), text=json.dumps({"id": "person-01", "first_name": "John"}), ) self.assertEqual( raw.post("data/persons", {"birth_date": now}), {"id": "person-01", "first_name": "John"}, )
def test_get_two_clients(self): with requests_mock.mock() as mock: second_client = gazu.raw.create_client("http://second.host/api") mock.get( raw.get_full_url("data/persons"), text='{"first_name": "John"}', ) self.assertEqual(raw.get("data/persons"), {"first_name": "John"}) self.assertRaises(requests_mock.exceptions.NoMockAddress, raw.get, "data/persons", client=second_client) mock.get( raw.get_full_url("data/persons", client=second_client), text='{"first_name": "John2"}', ) self.assertEqual( raw.get("data/persons", client=second_client), {"first_name": "John2"}, )
def test_fetch_one(self): with requests_mock.mock() as mock: mock.get( raw.get_full_url("data/persons/person-01"), text='{"id": "person-01", "first_name": "John"}', ) self.assertEqual( raw.fetch_one("persons", "person-01"), { "id": "person-01", "first_name": "John" }, )
def test_update(self): with requests_mock.mock() as mock: mock.put( raw.get_full_url("data/persons/person-1"), text=json.dumps( { "id": "person-1", "first_name": "John", "last_name": "Doe", } ), ) data = {"last_name": "Doe"} self.assertEqual( raw.update("persons", "person-1", data), {"id": "person-1", "first_name": "John", "last_name": "Doe"}, )
def test_get_full_url(self): test_route = "test_route" expected_url = client.url_path_join(client.get_host(), test_route) self.assertEqual(client.get_full_url(test_route), expected_url)
def test_delete(self): with requests_mock.mock() as mock: mock.delete(raw.get_full_url("data/persons/person-01"), text="") self.assertEqual(raw.delete("data/persons/person-01"), "")