diff --git a/test/new_tests/test_admin_change_password.py b/test/new_tests/test_admin_change_password.py index 82e3cee21..2e895cbcf 100644 --- a/test/new_tests/test_admin_change_password.py +++ b/test/new_tests/test_admin_change_password.py @@ -77,12 +77,10 @@ def test_change_password_with_invalid_timeout_policy_value(self): user = "testchangepassworduser" password = "newpassword" - try: + with pytest.raises(aerospike.exception.ParamError) as excinfo: self.client.admin_change_password(user, password, policy) - - except aerospike.exception.ParamError as exception: - assert exception.code == -2 - assert exception.msg == "timeout is invalid" + assert excinfo.value.code == -2 + assert excinfo.value.msg == "timeout is invalid" # NOTE: This will fail if auth_mode is PKI_AUTH (3). @pytest.mark.xfail(reason="Might fail depending on auth_mode.") @@ -116,24 +114,20 @@ def test_change_password_with_none_username(self): user = None password = "newpassword" - try: + with pytest.raises(aerospike.exception.ParamError) as excinfo: self.client.admin_change_password(user, password) - - except aerospike.exception.ParamError as exception: - assert exception.code == -2 - assert exception.msg == "Username should be a string" + assert excinfo.value.code == -2 + assert excinfo.value.msg == "Username should be a string" def test_change_password_with_none_password(self): user = "testchangepassworduser" password = None - try: + with pytest.raises(aerospike.exception.ParamError) as excinfo: self.client.admin_change_password(user, password) - - except aerospike.exception.ParamError as exception: - assert exception.code == -2 - assert exception.msg == "Password should be a string" + assert excinfo.value.code == -2 + assert excinfo.value.msg == "Password should be a string" def test_change_password_with_non_existent_user(self): diff --git a/test/new_tests/test_admin_create_role.py b/test/new_tests/test_admin_create_role.py index 4ce516438..dbfab5c90 100644 --- a/test/new_tests/test_admin_create_role.py +++ b/test/new_tests/test_admin_create_role.py @@ -250,11 +250,10 @@ def test_create_role_incorrect_role_type(self): """ role name not string """ - try: + with pytest.raises(e.ParamError) as excinfo: self.client.admin_create_role(1, [{"code": aerospike.PRIV_USER_ADMIN}]) - except e.ParamError as exception: - assert exception.code == -2 - assert "Role name should be a string" in exception.msg + assert excinfo.value.code == -2 + assert "Role name should be a string" in excinfo.value.msg def test_create_role_unknown_privilege_type(self): """ @@ -277,12 +276,10 @@ def test_create_role_incorrect_privilege_type(self): """ privilege type incorrect """ - try: + with pytest.raises(e.ParamError) as excinfo: self.client.admin_create_role("usr-sys-admin-test", None) - - except e.ParamError as exception: - assert exception.code == -2 - assert exception.msg == "Privileges should be a list" + assert excinfo.value.code == -2 + assert excinfo.value.msg == "Privileges should be a list" def test_create_role_existing_role(self): """ @@ -303,7 +300,6 @@ def test_create_role_existing_role(self): self.client.admin_create_role( "usr-sys-admin-test", [{"code": aerospike.PRIV_USER_ADMIN}, {"code": aerospike.PRIV_SYS_ADMIN}] ) - except e.RoleExistsError as exception: assert exception.code == 71 assert exception.msg == "AEROSPIKE_ROLE_ALREADY_EXISTS" diff --git a/test/new_tests/test_admin_create_user.py b/test/new_tests/test_admin_create_user.py index 5bb481215..1a9c1913b 100644 --- a/test/new_tests/test_admin_create_user.py +++ b/test/new_tests/test_admin_create_user.py @@ -108,12 +108,10 @@ def test_create_user_with_invalid_timeout_policy_value(self): except Exception: pass - try: + with pytest.raises(e.ParamError) as excinfo: self.client.admin_create_user(user, password, roles, policy) - - except e.ParamError as exception: - assert exception.code == -2 - assert exception.msg == "timeout is invalid" + assert excinfo.value.code == -2 + assert excinfo.value.msg == "timeout is invalid" def test_create_user_with_proper_timeout_policy_value(self): @@ -146,12 +144,10 @@ def test_create_user_with_none_username(self): password = "user3-test" roles = ["sys-admin"] - try: + with pytest.raises(e.ParamError) as excinfo: self.client.admin_create_user(user, password, roles) - - except e.ParamError as exception: - assert exception.code == -2 - assert exception.msg == "Username should be a string" + assert excinfo.value.code == -2 + assert excinfo.value.msg == "Username should be a string" def test_create_user_with_empty_username(self): @@ -190,12 +186,10 @@ def test_create_user_with_none_password(self): password = None roles = ["sys-admin"] - try: + with pytest.raises(e.ParamError) as excinfo: self.client.admin_create_user(user, password, roles) - - except e.ParamError as exception: - assert exception.code == -2 - assert exception.msg == "Password should be a string" + assert excinfo.value.code == -2 + assert excinfo.value.msg == "Password should be a string" def test_create_user_with_empty_string_as_password(self): diff --git a/test/new_tests/test_admin_drop_role.py b/test/new_tests/test_admin_drop_role.py index b32bb0d28..ab1a1d619 100644 --- a/test/new_tests/test_admin_drop_role.py +++ b/test/new_tests/test_admin_drop_role.py @@ -136,12 +136,10 @@ def test_drop_role_rolename_None(self): """ Drop role with role name None """ - try: + with pytest.raises(e.ParamError) as excinfo: self.client.admin_drop_role(None) - - except e.ParamError as exception: - assert exception.code == -2 - assert exception.msg == "Role name should be a string" + assert excinfo.value.code == -2 + assert excinfo.value.msg == "Role name should be a string" def test_drop_role_with_incorrect_policy(self): """ @@ -151,12 +149,12 @@ def test_drop_role_with_incorrect_policy(self): assert status == 0 time.sleep(3) - try: + + with pytest.raises(e.ParamError) as excinfo: self.client.admin_drop_role("usr-sys-admin-test", {"timeout": 0.2}) + assert excinfo.value.code == -2 + assert excinfo.value.msg == "timeout is invalid" - except e.ParamError as exception: - assert exception.code == -2 - assert exception.msg == "timeout is invalid" try: self.client.admin_drop_role("usr-sys-admin-test") except Exception: diff --git a/test/new_tests/test_admin_drop_user.py b/test/new_tests/test_admin_drop_user.py index 1765a90a4..3798fe3c7 100644 --- a/test/new_tests/test_admin_drop_user.py +++ b/test/new_tests/test_admin_drop_user.py @@ -76,12 +76,10 @@ def test_drop_user_with_user_none(self): """ Invoke drop_user() with policy none """ - try: + with pytest.raises(e.ParamError) as excinfo: self.client.admin_drop_user(None) - - except e.ParamError as exception: - assert exception.code == -2 - assert exception.msg == "Username should be a string" + assert excinfo.value.code == -2 + assert excinfo.value.msg == "Username should be a string" def test_drop_user_positive(self): """ @@ -175,12 +173,10 @@ def test_drop_user_policy_incorrect(self): assert user_details["roles"] == ["read", "read-write", "sys-admin"] policy = {"timeout": 0.2} - try: + with pytest.raises(e.ParamError) as excinfo: status = self.client.admin_drop_user(user, policy) - - except e.ParamError as exception: - assert exception.code == -2 - assert exception.msg == "timeout is invalid" + assert excinfo.value.code == -2 + assert excinfo.value.msg == "timeout is invalid" status = self.client.admin_drop_user(user) diff --git a/test/new_tests/test_admin_get_role.py b/test/new_tests/test_admin_get_role.py index 4379ad576..d20240e13 100644 --- a/test/new_tests/test_admin_get_role.py +++ b/test/new_tests/test_admin_get_role.py @@ -92,9 +92,7 @@ def test_admin_get_role_incorrect_role_type(self): """ Incorrect role type """ - try: + with pytest.raises(e.ParamError) as excinfo: self.client.admin_get_role(None) - - except e.ParamError as exception: - assert exception.code == -2 - assert exception.msg == "Role name should be a string" + assert excinfo.value.code == -2 + assert excinfo.value.msg == "Role name should be a string" diff --git a/test/new_tests/test_admin_get_roles.py b/test/new_tests/test_admin_get_roles.py index 06a455e31..7a93b01ce 100644 --- a/test/new_tests/test_admin_get_roles.py +++ b/test/new_tests/test_admin_get_roles.py @@ -75,9 +75,7 @@ def test_admin_get_roles_incorrect_policy(self): """ Get roles incorrect policy """ - try: + with pytest.raises(e.ParamError) as excinfo: self.client.admin_get_roles({"timeout": 0.2}) - - except e.ParamError as exception: - assert exception.code == -2 - assert exception.msg == "timeout is invalid" + assert excinfo.value.code == -2 + assert excinfo.value.msg == "timeout is invalid" diff --git a/test/new_tests/test_admin_grant_privileges.py b/test/new_tests/test_admin_grant_privileges.py index b4d599043..e2bd94457 100644 --- a/test/new_tests/test_admin_grant_privileges.py +++ b/test/new_tests/test_admin_grant_privileges.py @@ -167,12 +167,10 @@ def test_grant_privileges_incorrect_role_type(self): """ role name not string """ - try: + with pytest.raises(e.ParamError) as excinfo: self.client.admin_grant_privileges(1, [{"code": aerospike.PRIV_USER_ADMIN}]) - - except e.ParamError as exception: - assert exception.code == -2 - assert exception.msg == "Role name should be a string" + assert excinfo.value.code == -2 + assert excinfo.value.msg == "Role name should be a string" def test_grant_privileges_unknown_privilege_type(self): """ @@ -187,12 +185,10 @@ def test_grant_privileges_incorrect_privilege_type(self): """ privilege type incorrect """ - try: + with pytest.raises(e.ParamError) as excinfo: self.client.admin_grant_privileges("usr-sys-admin-test", None) - - except e.ParamError as exception: - assert exception.code == -2 - assert exception.msg == "Privileges should be a list" + assert excinfo.value.code == -2 + assert excinfo.value.msg == "Privileges should be a list" def test_grant_privileges_empty_list_privileges(self): """ diff --git a/test/new_tests/test_admin_grant_roles.py b/test/new_tests/test_admin_grant_roles.py index c009904f9..75114d953 100644 --- a/test/new_tests/test_admin_grant_roles.py +++ b/test/new_tests/test_admin_grant_roles.py @@ -88,12 +88,10 @@ def test_grant_roles_with_invalid_timeout_policy_value(self): user = "example-test" roles = ["sys-admin"] - try: + with pytest.raises(e.ParamError) as excinfo: self.client.admin_grant_roles(user, roles, policy) - - except e.ParamError as exception: - assert exception.code == -2 - assert exception.msg == "timeout is invalid" + assert excinfo.value.code == -2 + assert excinfo.value.msg == "timeout is invalid" def test_grant_roles_with_proper_timeout_policy_value(self): @@ -115,12 +113,10 @@ def test_grant_roles_with_none_username(self): user = None roles = ["sys-admin"] - try: + with pytest.raises(e.ParamError) as excinfo: self.client.admin_grant_roles(user, roles) - - except e.ParamError as exception: - assert exception.code == -2 - assert exception.msg == "Username should be a string" + assert excinfo.value.code == -2 + assert excinfo.value.msg == "Username should be a string" def test_grant_roles_with_empty_username(self): diff --git a/test/new_tests/test_admin_query_role.py b/test/new_tests/test_admin_query_role.py index 0510ae367..a4524716a 100644 --- a/test/new_tests/test_admin_query_role.py +++ b/test/new_tests/test_admin_query_role.py @@ -82,9 +82,7 @@ def test_admin_query_role_incorrect_role_type(self): """ Incorrect role type """ - try: + with pytest.raises(e.ParamError) as excinfo: self.client.admin_query_role(None) - - except e.ParamError as exception: - assert exception.code == -2 - assert exception.msg == "Role name should be a string" + assert excinfo.value.code == -2 + assert excinfo.value.msg == "Role name should be a string" diff --git a/test/new_tests/test_admin_query_roles.py b/test/new_tests/test_admin_query_roles.py index 803d34e9e..71af76ff9 100644 --- a/test/new_tests/test_admin_query_roles.py +++ b/test/new_tests/test_admin_query_roles.py @@ -65,9 +65,7 @@ def test_admin_query_roles_incorrect_policy(self): """ Query roles incorrect policy """ - try: + with pytest.raises(e.ParamError) as excinfo: self.client.admin_query_roles({"timeout": 0.2}) - - except e.ParamError as exception: - assert exception.code == -2 - assert exception.msg == "timeout is invalid" + assert excinfo.value.code == -2 + assert excinfo.value.msg == "timeout is invalid" diff --git a/test/new_tests/test_admin_query_user_info.py b/test/new_tests/test_admin_query_user_info.py index b56a3e70f..86fc1ecb1 100644 --- a/test/new_tests/test_admin_query_user_info.py +++ b/test/new_tests/test_admin_query_user_info.py @@ -93,34 +93,28 @@ def test_query_user_info_with_none_username(self): user = None - try: + with pytest.raises(e.ParamError) as excinfo: self.client.admin_query_user_info(user) - - except e.ParamError as exception: - assert exception.code == -2 - assert exception.msg == "Username should be a string" + assert excinfo.value.code == -2 + assert excinfo.value.msg == "Username should be a string" def test_query_user_info_with_empty_username(self): user = "" - try: + with pytest.raises(e.InvalidUser) as excinfo: self.client.admin_query_user_info(user) - - except e.InvalidUser as exception: - assert exception.code == 60 - assert exception.msg == "AEROSPIKE_INVALID_USER" + assert excinfo.value.code == 60 + assert excinfo.value.msg == "AEROSPIKE_INVALID_USER" def test_query_user_info_with_nonexistent_username(self): user = "non-existent" - try: + with pytest.raises(e.InvalidUser) as excinfo: self.client.admin_query_user_info(user) - - except e.InvalidUser as exception: - assert exception.code == 60 - assert exception.msg == "AEROSPIKE_INVALID_USER" + assert excinfo.value.code == 60 + assert excinfo.value.msg == "AEROSPIKE_INVALID_USER" def test_query_user_info_with_no_roles(self): @@ -148,9 +142,7 @@ def test_query_user_info_with_policy_as_string(self): Invoke query_user() with policy as string """ policy = "" - try: + with pytest.raises(e.AerospikeError) as excinfo: self.client.admin_query_user_info("foo", policy) - - except e.AerospikeError as exception: - assert exception.code == -2 - assert exception.msg == "policy must be a dict" + assert excinfo.value.code == -2 + assert excinfo.value.msg == "policy must be a dict" diff --git a/test/new_tests/test_admin_query_users.py b/test/new_tests/test_admin_query_users.py new file mode 100644 index 000000000..ae895a9cb --- /dev/null +++ b/test/new_tests/test_admin_query_users.py @@ -0,0 +1,110 @@ +# -*- coding: utf-8 -*- + +import pytest +import time +from .test_base_class import TestBaseClass +from aerospike import exception as e + +import aerospike + + +@pytest.mark.skip("client.admin_query_users() is deprecated") +class TestQueryUsers(TestBaseClass): + + pytestmark = pytest.mark.skipif( + not TestBaseClass.auth_in_use(), reason="No user specified, may be not secured cluster." + ) + + def setup_method(self, method): + """ + Setup method + """ + config = TestBaseClass.get_connection_config() + TestQueryUsers.Me = self + self.client = aerospike.client(config).connect(config["user"], config["password"]) + + try: + self.client.admin_drop_user("example-test") + time.sleep(2) + except e.InvalidUser: + pass + user = "example-test" + password = "foo2" + roles = ["read-write", "sys-admin", "read"] + + try: + self.client.admin_create_user(user, password, roles) + time.sleep(2) + except e.UserExistsError: + pass + self.delete_users = [] + + def teardown_method(self, method): + """ + Teardown method + """ + + try: + self.client.admin_drop_user("example-test") + except Exception: + pass + self.client.close() + + def test_query_users_with_proper_parameters(self): + + time.sleep(2) + user_details = self.client.admin_query_users() + + assert user_details["example-test"] == ["read", "read-write", "sys-admin"] + + def test_query_users_with_invalid_timeout_policy_value(self): + + policy = {"timeout": 0.1} + + with pytest.raises(e.ParamError) as excinfo: + self.client.admin_query_users(policy) + assert excinfo.value.code == -2 + assert excinfo.value.msg == "timeout is invalid" + + def test_query_users_with_proper_timeout_policy_value(self): + + policy = {"timeout": 180000} + + time.sleep(2) + user_details = self.client.admin_query_users(policy) + + time.sleep(2) + assert user_details["example-test"] == ["read", "read-write", "sys-admin"] + + def test_query_users_with_no_roles(self): + + user = "example-test" + roles = ["sys-admin", "read", "read-write"] + + status = self.client.admin_revoke_roles(user, roles) + assert status == 0 + time.sleep(2) + + user_details = self.client.admin_query_users() + + time.sleep(2) + assert user_details["example-test"] == [] + + def test_query_users_with_extra_argument(self): + """ + Invoke query_users() with extra argument. + """ + with pytest.raises(TypeError) as typeError: + self.client.admin_query_users(None, "") + + assert "admin_query_users() takes at most 1 argument (2 given)" in str(typeError.value) + + def test_query_users_with_policy_as_string(self): + """ + Invoke query_users() with policy as string + """ + policy = "" + with pytest.raises(e.ParamError) as excinfo: + self.client.admin_query_users(policy) + assert excinfo.value.code == -2 + assert excinfo.value.msg == "policy must be a dict" diff --git a/test/new_tests/test_admin_query_users_info.py b/test/new_tests/test_admin_query_users_info.py index 11db38ef3..7cb61d97e 100644 --- a/test/new_tests/test_admin_query_users_info.py +++ b/test/new_tests/test_admin_query_users_info.py @@ -65,12 +65,10 @@ def test_query_users_info_with_invalid_timeout_policy_value(self): policy = {"timeout": 0.1} - try: + with pytest.raises(e.ParamError) as excinfo: self.client.admin_query_users_info(policy) - - except e.ParamError as exception: - assert exception.code == -2 - assert exception.msg == "timeout is invalid" + assert excinfo.value.code == -2 + assert excinfo.value.msg == "timeout is invalid" def test_query_users_info_with_proper_timeout_policy_value(self): @@ -107,9 +105,7 @@ def test_query_users_info_with_policy_as_string(self): Invoke query_users() with policy as string """ policy = "" - try: + with pytest.raises(e.ParamError) as excinfo: self.client.admin_query_users_info(policy) - - except e.ParamError as exception: - assert exception.code == -2 - assert exception.msg == "policy must be a dict" + assert excinfo.value.code == -2 + assert excinfo.value.msg == "policy must be a dict" diff --git a/test/new_tests/test_admin_revoke_privileges.py b/test/new_tests/test_admin_revoke_privileges.py index e5bfef9c2..69a7cb524 100644 --- a/test/new_tests/test_admin_revoke_privileges.py +++ b/test/new_tests/test_admin_revoke_privileges.py @@ -207,40 +207,33 @@ def test_revoke_privileges_incorrect_role_type(self): """ role name not string """ - try: + with pytest.raises(e.ParamError) as excinfo: self.client.admin_revoke_privileges(1, [{"code": aerospike.PRIV_USER_ADMIN}]) - - except e.ParamError as exception: - assert exception.code == -2 - assert exception.msg == "Role name should be a string" + assert excinfo.value.code == -2 + assert excinfo.value.msg == "Role name should be a string" def test_revoke_privileges_unknown_privilege_type(self): """ privilege type unknown """ - try: + with pytest.raises(e.InvalidPrivilege) as excinfo: self.client.admin_revoke_privileges("usr-sys-admin-test", [{"code": 64}]) - except e.InvalidPrivilege as exception: - assert exception.code == 72 + assert excinfo.value.code == 72 def test_revoke_privileges_incorrect_privilege_type(self): """ privilege type incorrect """ - try: + with pytest.raises(e.ParamError) as excinfo: self.client.admin_revoke_privileges("usr-sys-admin-test", None) - - except e.ParamError as exception: - assert exception.code == -2 - assert exception.msg == "Privileges should be a list" + assert excinfo.value.code == -2 + assert excinfo.value.msg == "Privileges should be a list" def test_revoke_privileges_empty_list_privileges(self): """ privilege type is an empty list """ - try: + with pytest.raises(e.InvalidPrivilege) as excinfo: self.client.admin_revoke_privileges("usr-sys-admin-test", []) - - except e.InvalidPrivilege as exception: - assert exception.code == 72 - assert exception.msg == "AEROSPIKE_INVALID_PRIVILEGE" + assert excinfo.value.code == 72 + assert excinfo.value.msg == "AEROSPIKE_INVALID_PRIVILEGE" diff --git a/test/new_tests/test_admin_revoke_roles.py b/test/new_tests/test_admin_revoke_roles.py index 10616159b..10c1976df 100644 --- a/test/new_tests/test_admin_revoke_roles.py +++ b/test/new_tests/test_admin_revoke_roles.py @@ -88,12 +88,10 @@ def test_revoke_roles_with_invalid_timeout_policy_value(self): user = "example-test" roles = ["sys-admin"] - try: + with pytest.raises(e.ParamError) as excinfo: self.client.admin_revoke_roles(user, roles, policy) - - except e.ParamError as exception: - assert exception.code == -2 - assert exception.msg == "timeout is invalid" + assert excinfo.value.code == -2 + assert excinfo.value.msg == "timeout is invalid" def test_revoke_roles_with_proper_timeout_policy_value(self): @@ -116,48 +114,40 @@ def test_revoke_roles_with_none_username(self): user = None roles = ["sys-admin"] - try: + with pytest.raises(e.ParamError) as excinfo: self.client.admin_revoke_roles(user, roles) - - except e.ParamError as exception: - assert exception.code == -2 - assert exception.msg == "Username should be a string" + assert excinfo.value.code == -2 + assert excinfo.value.msg == "Username should be a string" def test_revoke_roles_with_empty_username(self): user = "" roles = ["read-write"] - try: + with pytest.raises(e.InvalidUser) as excinfo: self.client.admin_revoke_roles(user, roles) - - except e.InvalidUser as exception: - assert exception.code == 60 - assert exception.msg == "AEROSPIKE_INVALID_USER" + assert excinfo.value.code == 60 + assert excinfo.value.msg == "AEROSPIKE_INVALID_USER" def test_revoke_roles_with_empty_roles_list(self): user = "example-test" roles = [] - try: + with pytest.raises(e.InvalidRole) as excinfo: self.client.admin_revoke_roles(user, roles) - - except e.InvalidRole as exception: - assert exception.code == 70 - assert exception.msg == "AEROSPIKE_INVALID_ROLE" + assert excinfo.value.code == 70 + assert excinfo.value.msg == "AEROSPIKE_INVALID_ROLE" def test_revoke_roles_with_nonexistent_username(self): user = "non-existent" roles = ["read-write"] - try: + with pytest.raises(e.InvalidUser) as excinfo: self.client.admin_revoke_roles(user, roles) - - except e.InvalidUser as exception: - assert exception.code == 60 - assert exception.msg == "AEROSPIKE_INVALID_USER" + assert excinfo.value.code == 60 + assert excinfo.value.msg == "AEROSPIKE_INVALID_USER" def test_revoke_roles_with_special_characters_in_username(self): diff --git a/test/new_tests/test_admin_set_password.py b/test/new_tests/test_admin_set_password.py index 0e0d5d400..ffe718910 100644 --- a/test/new_tests/test_admin_set_password.py +++ b/test/new_tests/test_admin_set_password.py @@ -69,12 +69,10 @@ def test_set_password_with_invalid_timeout_policy_value(self): user = "testsetpassworduser" password = "newpassword" - try: + with pytest.raises(e.ParamError) as excinfo: self.client.admin_set_password(user, password, policy) - - except e.ParamError as exception: - assert exception.code == -2 - assert exception.msg == "timeout is invalid" + assert excinfo.value.code == -2 + assert excinfo.value.msg == "timeout is invalid" def test_set_password_with_proper_timeout_policy_value(self): @@ -91,36 +89,30 @@ def test_set_password_with_none_username(self): user = None password = "newpassword" - try: + with pytest.raises(e.ParamError) as excinfo: self.client.admin_set_password(user, password) - - except e.ParamError as exception: - assert exception.code == -2 - assert exception.msg == "Username should be a string" + assert excinfo.value.code == -2 + assert excinfo.value.msg == "Username should be a string" def test_set_password_with_none_password(self): user = "testsetpassworduser" password = None - try: + with pytest.raises(e.ParamError) as excinfo: self.client.admin_set_password(user, password) - - except e.ParamError as exception: - assert exception.code == -2 - assert exception.msg == "Password should be a string" + assert excinfo.value.code == -2 + assert excinfo.value.msg == "Password should be a string" def test_set_password_with_non_existent_user(self): user = "new_user" password = "newpassword" - try: + with pytest.raises(e.InvalidUser) as excinfo: self.client.admin_set_password(user, password) - - except e.InvalidUser as exception: - assert exception.code == 60 - assert exception.msg == "AEROSPIKE_INVALID_USER" + assert excinfo.value.code == 60 + assert excinfo.value.msg == "AEROSPIKE_INVALID_USER" def test_set_password_with_too_long_password(self): diff --git a/test/new_tests/test_admin_set_quotas.py b/test/new_tests/test_admin_set_quotas.py index 4f7e21316..2767c869c 100644 --- a/test/new_tests/test_admin_set_quotas.py +++ b/test/new_tests/test_admin_set_quotas.py @@ -125,24 +125,21 @@ def test_admin_set_quota_incorrect_role_name(self): """ Incorrect role name """ - try: + with pytest.raises(e.InvalidRole) as excinfo: self.client.admin_set_quotas( role="bad-role-name", read_quota=250, write_quota=300 ) - - except e.InvalidRole as exception: - assert exception.code == 70 - assert exception.msg == "AEROSPIKE_INVALID_ROLE" + assert excinfo.value.code == 70 + assert excinfo.value.msg == "AEROSPIKE_INVALID_ROLE" def test_admin_set_quota_incorrect_role_type(self): """ Incorrect role type """ - try: + with pytest.raises(e.ParamError) as excinfo: self.client.admin_set_quotas(role=None, read_quota=250, write_quota=300) - except e.ParamError as exception: - assert exception.code == -2 - assert exception.msg == "Role name should be a string." + assert excinfo.value.code == -2 + assert excinfo.value.msg == "Role name should be a string." def test_admin_set_quota_incorrect_quota(self): """ @@ -161,11 +158,9 @@ def test_admin_set_quota_incorrect_quota_type(self): """ Incorrect role type """ - try: + with pytest.raises(e.ParamError) as excinfo: self.client.admin_set_quotas( role="usr-sys-admin-test", read_quota=None, write_quota=300 ) - - except e.ParamError as exception: - assert exception.code == -2 - assert exception.msg == "py_read_quota must be an integer." + assert excinfo.value.code == -2 + assert excinfo.value.msg == "py_read_quota must be an integer." diff --git a/test/new_tests/test_admin_set_whitelist.py b/test/new_tests/test_admin_set_whitelist.py index 1577129a3..15bbff212 100644 --- a/test/new_tests/test_admin_set_whitelist.py +++ b/test/new_tests/test_admin_set_whitelist.py @@ -127,44 +127,37 @@ def test_admin_set_whitelist_incorrect_role_name(self): """ Incorrect role name """ - try: + with pytest.raises(e.InvalidRole) as excinfo: self.client.admin_set_whitelist(role="bad-role-name", whitelist=["10.0.2.0/24"]) - - except e.InvalidRole as exception: - assert exception.code == 70 - assert exception.msg == "AEROSPIKE_INVALID_ROLE" + assert excinfo.value.code == 70 + assert excinfo.value.msg == "AEROSPIKE_INVALID_ROLE" def test_admin_set_whitelist_incorrect_role_type(self): """ Incorrect role type """ - try: + with pytest.raises(e.ParamError) as excinfo: self.client.admin_set_whitelist(role=None, whitelist=["10.0.2.0/24"]) - - except e.ParamError as exception: - assert exception.code == -2 - assert exception.msg == "Role name should be a string." + assert excinfo.value.code == -2 + assert excinfo.value.msg == "Role name should be a string." def test_admin_set_whitelist_incorrect_whitelist(self): """ Incorrect role name """ - try: + with pytest.raises(e.InvalidWhitelist) as excinfo: self.client.admin_set_whitelist(role="usr-sys-admin-test", whitelist=["bad_IP"]) - except e.InvalidWhitelist as exception: - assert exception.code == 73 - assert exception.msg == "AEROSPIKE_INVALID_WHITELIST" + assert excinfo.value.code == 73 + assert excinfo.value.msg == "AEROSPIKE_INVALID_WHITELIST" def test_admin_set_whitelist_incorrect_whitelist_type(self): """ Incorrect role type """ - try: - self.client.admin_set_whitelist(role="usr-sys-admin-test", whitelist=None) - - except e.ParamError as exception: - assert exception.code == -2 - assert exception.msg == "Whitelist must be a list of IP strings." + with pytest.raises(e.ParamError) as excinfo: + self.client.admin_set_whitelist(role="usr-sys-admin-test", whitelist=1) + assert excinfo.value.code == -2 + assert excinfo.value.msg == "Whitelist must be a list of IP strings, or None." def test_admin_set_whitelist_forbiden_host(self): """ diff --git a/test/new_tests/test_aggregate.py b/test/new_tests/test_aggregate.py index e7255afc9..3882d1ea4 100644 --- a/test/new_tests/test_aggregate.py +++ b/test/new_tests/test_aggregate.py @@ -270,7 +270,7 @@ def test_neg_aggregate_with_correct_parameters_without_connection(self): client1 = aerospike.client(config) client1.close() - try: + with pytest.raises(e.ClusterError) as excinfo: query = client1.query("test", "demo") query.select("name", "test_age") query.where(p.between("test_age", 1, 5)) @@ -283,8 +283,7 @@ def user_callback(value): query.foreach(user_callback) - except e.ClusterError as exception: - assert exception.code == 11 + assert excinfo.value.code == 11 def test_neg_aggregate_with_extra_parameter(self): """ @@ -309,19 +308,17 @@ def test_neg_aggregate_with_no_parameters(self): """ Invoke aggregate() without any mandatory parameters. """ - try: + with pytest.raises(e.ParamError) as excinfo: query = self.as_connection.query() query.select() query.where() - - except e.ParamError as exception: - assert exception.code == -2 + assert excinfo.value.code == -2 def test_neg_aggregate_no_sec_index(self): """ Invoke aggregate() with no secondary index """ - try: + with pytest.raises(e.IndexNotFound) as excinfo: query = self.as_connection.query("test", "demo") query.select("name", "no") query.where(p.between("no", 1, 5)) @@ -331,14 +328,13 @@ def user_callback(value): _ = value query.foreach(user_callback) - except e.IndexNotFound as exception: - assert exception.code == 201 + assert excinfo.value.code == 201 def test_neg_aggregate_with_incorrect_ns_set(self): """ Invoke aggregate() with incorrect ns and set """ - try: + with pytest.raises((e.InvalidRequest, e.NamespaceNotFound)) as excinfo: query = self.as_connection.query("test1", "demo1") query.select("name", "test_age") query.where(p.equals("test_age", 1)) @@ -349,10 +345,10 @@ def user_callback(value): query.foreach(user_callback) - except e.InvalidRequest as exception: - assert exception.code == 4 - except e.NamespaceNotFound as exception: - assert exception.code == 20 + if excinfo.type == e.InvalidRequest: + assert excinfo.value.code == 4 + elif excinfo.type == e.NamespaceNotFound: + assert excinfo.value.code == 20 def test_neg_aggregate_with_where_incorrect(self): """ @@ -376,7 +372,7 @@ def test_neg_aggregate_with_where_none_value(self): """ query = self.as_connection.query("test", "demo") query.select("name", "test_age") - try: + with pytest.raises(e.ParamError) as excinfo: query.where(p.equals("test_age", None)) query.apply("stream_example", "count") @@ -384,9 +380,7 @@ def user_callback(value): _ = value query.foreach(user_callback) - - except e.ParamError as exception: - assert exception.code == -2 + assert excinfo.value.code == -2 # We can't use the inspect library to check the keyword args of a method defined using the C-API # It doesn't work, so just check that passing in an invalid arg fails diff --git a/test/new_tests/test_append.py b/test/new_tests/test_append.py index bf0a4cbbe..c2c90c8fa 100644 --- a/test/new_tests/test_append.py +++ b/test/new_tests/test_append.py @@ -300,11 +300,10 @@ def test_neg_append_with_policy_key_gen_GT_lesser(self): gen = meta["gen"] meta = {"gen": gen, "ttl": 1200} - try: + with pytest.raises(e.RecordGenerationError) as excinfo: self.as_connection.append(key, "name", "str", meta, policy) - except e.RecordGenerationError as exception: - assert exception.code == 3 - assert exception.bin == "name" + assert excinfo.value.code == 3 + assert excinfo.value.bin == "name" (key, meta, bins) = self.as_connection.get(key) assert bins == {"age": 1, "name": "name1"} assert key == ( @@ -326,12 +325,10 @@ def test_neg_append_with_policy_key_gen_EQ_not_equal(self): gen = meta["gen"] meta = {"gen": gen + 5, "ttl": 1200} - try: + with pytest.raises(e.RecordGenerationError) as excinfo: self.as_connection.append(key, "name", "str", meta, policy) - - except e.RecordGenerationError as exception: - assert exception.code == 3 - assert exception.bin == "name" + assert excinfo.value.code == 3 + assert excinfo.value.bin == "name" (key, meta, bins) = self.as_connection.get(key) @@ -407,11 +404,9 @@ def test_neg_append_with_correct_parameters_without_connection(self): client1.close() key = ("test", "demo", 1) - try: + with pytest.raises(e.ClusterError) as excinfo: client1.append(key, "name", "str") - - except e.ClusterError as exception: - assert exception.code == 11 + assert excinfo.value.code == 11 def test_neg_append_with_low_timeout(self): """ @@ -426,8 +421,8 @@ def test_neg_append_with_low_timeout(self): } try: self.as_connection.append(key, "name", "str", {}, policy) - except e.TimeoutError as exception: - assert exception.code == 9 + except e.TimeoutError as ex: + assert ex.code == 9 def test_neg_append_with_non_existent_ns(self): """ diff --git a/test/new_tests/test_cdt_index.py b/test/new_tests/test_cdt_index.py index b894b3d64..257b89885 100644 --- a/test/new_tests/test_cdt_index.py +++ b/test/new_tests/test_cdt_index.py @@ -549,7 +549,7 @@ def test_neg_cdtindex_with_namespace_is_none(self): Invoke createindex() with namespace is None """ policy = {} - try: + with pytest.raises(e.ParamError) as excinfo: self.as_connection.index_cdt_create( None, "demo", @@ -561,16 +561,17 @@ def test_neg_cdtindex_with_namespace_is_none(self): policy, ) - except e.ParamError as exception: - assert exception.code == -2 - assert exception.msg == "Namespace should be a string" + assert excinfo.value.code == -2 + assert excinfo.value.msg == "Namespace should be a string" def test_neg_cdtindex_with_set_is_int(self): """ Invoke createindex() with set is int """ policy = {} - try: + # This can raise a subclass of ParamError + # But pytest.raises() will not raise an exception if it is a subclass + with pytest.raises(e.ParamError) as excinfo: self.as_connection.index_cdt_create( "test", 1, @@ -582,32 +583,25 @@ def test_neg_cdtindex_with_set_is_int(self): policy, ) assert False - except e.ParamError as exception: - assert exception.code == -2 - assert exception.msg == "Set should be string, unicode or None" - except Exception as exception: - assert isinstance(exception, e.ParamError) + assert excinfo.value.code == -2 + assert excinfo.value.msg == "Set should be string, unicode or None" - def test_neg_cdtindex_with_set_is_none(self): + def test_cdtindex_with_set_is_none(self): """ Invoke createindex() with set is None """ policy = {} - try: - self.as_connection.index_cdt_create( - "test", - None, - "string_list", - aerospike.INDEX_TYPE_LIST, - aerospike.INDEX_STRING, - "test_string_list_cdt_index", - ctx_list_index, - policy, - ) + self.as_connection.index_cdt_create( + "test", + None, + "string_list", + aerospike.INDEX_TYPE_LIST, + aerospike.INDEX_STRING, + "test_string_list_cdt_index", + {"ctx": ctx_list_index}, + policy, + ) - except e.ParamError as exception: - assert exception.code == -2 - assert exception.msg == "Set should be a string" self.as_connection.index_remove("test", "test_string_list_cdt_index", policy) ensure_dropped_index(self.as_connection, "test", "test_string_list_cdt_index") @@ -616,7 +610,7 @@ def test_neg_cdtindex_with_bin_is_none(self): Invoke createindex() with bin is None """ policy = {} - try: + with pytest.raises(e.ParamError) as excinfo: self.as_connection.index_cdt_create( "test", "demo", @@ -627,17 +621,15 @@ def test_neg_cdtindex_with_bin_is_none(self): ctx_list_index, policy, ) - - except e.ParamError as exception: - assert exception.code == -2 - assert exception.msg == "Bin should be a string" + assert excinfo.value.code == -2 + assert excinfo.value.msg == "Bin should be a string" def test_neg_cdtindex_with_index_is_none(self): """ Invoke createindex() with index is None """ policy = {} - try: + with pytest.raises(e.ParamError) as excinfo: self.as_connection.index_cdt_create( "test", "demo", @@ -648,10 +640,8 @@ def test_neg_cdtindex_with_index_is_none(self): ctx_list_index, policy, ) - - except e.ParamError as exception: - assert exception.code == -2 - assert exception.msg == "Index name should be string or unicode" + assert excinfo.value.code == -2 + assert excinfo.value.msg == "Index name should be string or unicode" def test_neg_cdtindex_with_incorrect_namespace(self): """ @@ -659,7 +649,7 @@ def test_neg_cdtindex_with_incorrect_namespace(self): """ policy = {} - try: + with pytest.raises(e.NamespaceNotFound) as excinfo: self.as_connection.index_cdt_create( "test1", "demo", @@ -671,8 +661,7 @@ def test_neg_cdtindex_with_incorrect_namespace(self): policy, ) - except e.NamespaceNotFound as exception: - assert exception.code == 20 + assert excinfo.value.code == 20 def test_neg_cdtindex_with_incorrect_set(self): """ @@ -703,7 +692,7 @@ def test_neg_cdtindex_with_correct_parameters_no_connection(self): client1 = aerospike.client(config) client1.close() - try: + with pytest.raises(e.ClusterError) as excinfo: client1.index_cdt_create( "test", "demo", @@ -715,8 +704,7 @@ def test_neg_cdtindex_with_correct_parameters_no_connection(self): policy, ) - except e.ClusterError as exception: - assert exception.code == 11 + assert excinfo.value.code == 11 def test_neg_cdtindex_with_no_paramters(self): """ diff --git a/test/new_tests/test_close.py b/test/new_tests/test_close.py index d2598fd8d..1535b4b0b 100644 --- a/test/new_tests/test_close.py +++ b/test/new_tests/test_close.py @@ -32,9 +32,8 @@ def test_pos_close_without_connection(self): try: self.closeobject = self.client.close() - - except e.ClusterError as exception: - assert exception.code == 11 + except e.ClusterError as ex: + assert ex.code == 11 def test_neg_close(self): """ diff --git a/test/new_tests/test_exists.py b/test/new_tests/test_exists.py index c27038c53..a5f6b6317 100644 --- a/test/new_tests/test_exists.py +++ b/test/new_tests/test_exists.py @@ -92,16 +92,17 @@ def test_neg_exists_with_non_existent_data(self, key, ex, ex_code): """ Invoke exists() for non-existent data. """ - try: + if ex == e.NamespaceNotFound: + with pytest.raises(ex): + key, meta = self.as_connection.exists(key) + elif ex == e.RecordNotFound: key, meta = self.as_connection.exists(key) assert meta is None - """ - We are making the api backward compatible. In case of RecordNotFound an - exception will not be raised. Instead Ok response is returned withe the - meta as None. This might change with further releases. - """ - except ex as exception: - assert exception.code == ex_code + """ + We are making the api backward compatible. In case of RecordNotFound an + exception will not be raised. Instead Ok response is returned withe the + meta as None. This might change with further releases. + """ def test_neg_exists_with_only_key_without_connection(self): """ @@ -112,11 +113,9 @@ def test_neg_exists_with_only_key_without_connection(self): client1 = aerospike.client(config) client1.close() - try: + with pytest.raises(e.ClusterError) as excinfo: key, _ = client1.exists(key) - - except e.ClusterError as exception: - assert exception.code == 11 + assert excinfo.value.code == 11 @pytest.mark.parametrize( "key, record, meta, policy", diff --git a/test/new_tests/test_exists_many.py b/test/new_tests/test_exists_many.py new file mode 100644 index 000000000..90f916bcf --- /dev/null +++ b/test/new_tests/test_exists_many.py @@ -0,0 +1,272 @@ +# -*- coding: utf-8 -*- + +import pytest +import time + +try: + from collections import Counter +except ImportError: + from counter26 import Counter + +from .test_base_class import TestBaseClass +import aerospike +from aerospike import exception as e + + +@pytest.mark.usefixtures("as_connection") +class TestExistsMany: + def test_pos_exists_many_without_policy(self, put_data): + self.keys = [] + rec_length = 5 + for i in range(rec_length): + key = ("test", "demo", i) + record = {"name": "name%s" % (str(i)), "age": i} + put_data(self.as_connection, key, record) + self.keys.append(key) + records = self.as_connection.exists_many(self.keys) + assert isinstance(records, list) + assert len(records) == rec_length + + def test_pos_exists_many_with_proper_parameters(self, put_data): + self.keys = [] + rec_length = 5 + for i in range(rec_length): + key = ("test", "demo", i) + record = {"name": "name%s" % (str(i)), "age": i} + put_data(self.as_connection, key, record) + self.keys.append(key) + records = self.as_connection.exists_many(self.keys, {"max_retries": 1}) + + assert isinstance(records, list) + assert len(records) == rec_length + assert Counter([x[0][2] for x in records]) == Counter([0, 1, 2, 3, 4]) + + def test_pos_exists_many_with_none_policy(self, put_data): + self.keys = [] + rec_length = 5 + for i in range(rec_length): + key = ("test", "demo", i) + record = {"name": "name%s" % (str(i)), "age": i} + put_data(self.as_connection, key, record) + self.keys.append(key) + + records = self.as_connection.exists_many(self.keys, None) + + assert isinstance(records, list) + assert len(records) == rec_length + assert Counter([x[0][2] for x in records]) == Counter([0, 1, 2, 3, 4]) + + def test_pos_exists_many_with_non_existent_keys(self, put_data): + self.keys = [] + rec_length = 5 + for i in range(rec_length): + key = ("test", "demo", i) + record = {"name": "name%s" % (str(i)), "age": i} + put_data(self.as_connection, key, record) + self.keys.append(key) + + self.keys.append(("test", "demo", "some_key")) + + records = self.as_connection.exists_many(self.keys) + + assert isinstance(records, list) + assert len(records) == rec_length + 1 + assert Counter([x[0][2] for x in records]) == Counter([0, 1, 2, 3, 4, "some_key"]) + for x in records: + if x[0][2] == "some_key": + assert x[1] is None + + def test_pos_exists_many_with_all_non_existent_keys(self): + + keys = [("test", "demo", "NonExistingKey")] + + records = self.as_connection.exists_many(keys) + + assert len(records) == 1 + for x in records: + if x[0][2] == "key": + assert x[1] is None + + def test_pos_exists_many_with_initkey_as_digest(self, put_data): + + keys = [] + key = ("test", "demo", None, bytearray("asd;as[d'as;djk;uyfl", "utf-8")) + rec = {"name": "name1", "age": 1} + put_data(self.as_connection, key, rec) + keys.append(key) + + key = ("test", "demo", None, bytearray("ase;as[d'as;djk;uyfl", "utf-8")) + rec = {"name": "name2", "age": 2} + put_data(self.as_connection, key, rec) + keys.append(key) + + records = self.as_connection.exists_many(keys) + + assert isinstance(records, list) + assert len(records) == 2 + i = 0 + for x in records: + if i: + assert x[0][3] == bytearray(b"ase;as[d'as;djk;uyfl") + else: + assert x[0][3] == bytearray(b"asd;as[d'as;djk;uyfl") + i += 1 + + def test_pos_exists_many_with_non_existent_keys_in_middle(self, put_data): + self.keys = [] + rec_length = 5 + for i in range(rec_length): + key = ("test", "demo", i) + record = {"name": "name%s" % (str(i)), "age": i} + put_data(self.as_connection, key, record) + self.keys.append(key) + + self.keys.append(("test", "demo", "some_key")) + + for i in range(15, 20): + key = ("test", "demo", i) + rec = {"name": "name%s" % (str(i)), "age": i} + put_data(self.as_connection, key, rec) + self.keys.append(key) + + records = self.as_connection.exists_many(self.keys) + + assert isinstance(records, list) + assert len(records) == 11 + assert Counter([x[0][2] for x in records]) == Counter([0, 1, 2, 3, 4, "some_key", 15, 16, 17, 18, 19]) + for x in records: + if x[0][2] == "some_key": + assert x[1] is None + + def test_pos_exists_many_with_record_expiry(self, put_data): + keys = [] + key = ("test", "demo", 20) + rec = {"name": "John"} + meta = {"gen": 3, "ttl": 1} + put_data(self.as_connection, key, rec, meta) + keys.append(key) + time.sleep(2) + records = self.as_connection.exists_many(keys) + assert isinstance(records, list) + assert len(records) == 1 + for record in records: + assert record[1] is None + + def test_exists_many_with_bytearray_key(self, put_data): + self.keys = [("test", "demo", bytearray([1, 2, 3]))] + for key in self.keys: + put_data(self.as_connection, key, {"byte": "array"}) + + records = self.as_connection.exists_many(self.keys) + + bytearray_key = records[0][0] + assert len(bytearray_key) == 4 + + bytearray_pk = bytearray_key[2] + assert bytearray_pk == bytearray([1, 2, 3]) + + # Negative Tests + + def test_neg_exists_many_with_none_keys(self): + + with pytest.raises(e.ParamError): + self.as_connection.exists_many(None, {}) + + def test_neg_exists_many_with_an_invalid_key_in_list(self): + + with pytest.raises(e.ParamError): + self.as_connection.exists_many([("test", "demo", 1), ("test", "demo", 2), 5]) + + def test_neg_exists_many_with_invalid_key(self): + + with pytest.raises(e.ParamError): + self.as_connection.exists_many("key") + + def test_neg_exists_many_with_invalid_timeout(self, put_data): + self.keys = [] + rec_length = 5 + for i in range(rec_length): + key = ("test", "demo", i) + record = {"name": "name%s" % (str(i)), "age": i} + put_data(self.as_connection, key, record) + self.keys.append(key) + policies = {"total_timeout": 0.2} + with pytest.raises(e.ParamError): + self.as_connection.exists_many(self.keys, policies) + + def test_neg_exists_many_with_proper_parameters_without_connection(self, put_data): + self.keys = [] + rec_length = 5 + for i in range(rec_length): + key = ("test", "demo", i) + record = {"name": "name%s" % (str(i)), "age": i} + put_data(self.as_connection, key, record) + self.keys.append(key) + + config = TestBaseClass.get_connection_config() + client1 = aerospike.client(config) + client1.close() + + with pytest.raises(e.ClusterError) as excinfo: + client1.exists_many(self.keys, {"total_timeout": 20}) + assert excinfo.value.code == 11 + + def test_neg_exists_many_with_extra_parameter_in_key(self, put_data): + keys = [] + key = ("test", "demo", None, bytearray("asd;as[d'as;djk;uyfl", "utf-8")) + rec = {"name": "name1", "age": 1} + put_data(self.as_connection, key, rec) + keys.append(key) + + key = ("test", "demo", None, bytearray("ase;as[d'as;djk;uyfl", "utf-8")) + rec = {"name": "name2", "age": 2} + put_data(self.as_connection, key, rec) + keys.append(key) + + keys_get = [] + key = ("test", "demo", None, bytearray("asd;as[d'as;djk;uyfl", "utf-8"), None) + keys_get.append(key) + + key = ("test", "demo", None, bytearray("ase;as[d'as;djk;uyfl", "utf-8"), None) + keys_get.append(key) + + with pytest.raises(e.ParamError) as excinfo: + self.as_connection.exists_many(keys_get) + assert excinfo.value.code == -2 + assert excinfo.value.msg == "key tuple must be (Namespace, Set, Key) or (Namespace, Set, None, Digest)" + + for key in keys: + self.as_connection.remove(key) + + def test_neg_exists_many_with_low_timeout(self, put_data): + keys = [] + key = ("test", "demo", 20) + rec = {"name": "John"} + meta = {"gen": 3, "ttl": 1} + policy = {"total_timeout": 2} + + try: + put_data(self.as_connection, key, rec, meta, policy) + except e.TimeoutError as exception: + assert exception.code == 9 + + keys.append(key) + records = self.as_connection.exists_many(keys) + assert isinstance(records, list) + assert len(records) == 1 + + def test_neg_exists_many_with_invalid_ns(self): + # ToDo: not sure about put operation + keys = [] + key = ("test2", "demo", 20) + # self.as_connection.put(key, rec, meta, policy) + keys.append(key) + with pytest.raises(e.ClientError): + self.as_connection.exists_many(keys) + + def test_neg_exists_many_without_any_parameter(self): + + with pytest.raises(TypeError) as typeError: + self.as_connection.exists_many() + + assert "argument 'keys' (pos 1)" in str(typeError.value) diff --git a/test/new_tests/test_get_cdtctx_base64.py b/test/new_tests/test_get_cdtctx_base64.py index 3617f9831..1eb2e44fb 100644 --- a/test/new_tests/test_get_cdtctx_base64.py +++ b/test/new_tests/test_get_cdtctx_base64.py @@ -120,16 +120,12 @@ def test_get_cdtctxb64_with_invalid_parameters(self): """ Invoke get_cdtctx_base64() with invalid arguments """ - try: + with pytest.raises(e.ParamError): self.as_connection.get_cdtctx_base64({}) - except e.ParamError: - pass def test_get_cdtctxb64_with_invalid_ctx(self): """ Invoke get_cdtctx_base64() with invalid arguments """ - try: + with pytest.raises(e.ParamError): self.as_connection.get_cdtctx_base64(ctx_empty) - except e.ParamError: - pass diff --git a/test/new_tests/test_get_put.py b/test/new_tests/test_get_put.py index 628078a5b..dabbc60ab 100644 --- a/test/new_tests/test_get_put.py +++ b/test/new_tests/test_get_put.py @@ -183,14 +183,14 @@ def test_neg_get_with_key_digest(self): Invoke get() with a key digest. """ key = ("test", "demo", 1) - try: + with pytest.raises((e.ParamError, e.RecordNotFound)) as excinfo: key, _ = self.as_connection.exists(key) key, _, _ = self.as_connection.get((key[0], key[1], None, key[2])) - except e.ParamError as exception: - assert exception.code == -2 - assert exception.msg == "digest is invalid. expected a bytearray" - except e.RecordNotFound as exception: - assert exception.code == 2 + if excinfo.value == e.ParamError: + assert excinfo.value.code == -2 + assert excinfo.value.msg == "digest is invalid. expected a bytearray" + elif excinfo.value == e.RecordNotFound: + assert excinfo.value.code == 2 @pytest.mark.parametrize("key, ex_code, ex_msg", test_data.key_neg) def test_neg_get_with_none(self, key, ex_code, ex_msg): @@ -229,11 +229,9 @@ def test_neg_get_remove_key_and_check_get(self, _input, _expected, put_data): """ put_data(self.as_connection, _input, _expected) self.as_connection.remove(_input) - try: + with pytest.raises(e.RecordNotFound) as excinfo: _, _, bins = self.as_connection.get(_input) - assert bins is None - except e.RecordNotFound as exception: - assert exception.code == 2 + assert excinfo.value.code == 2 def test_neg_get_with_only_key_no_connection(self): """ @@ -688,12 +686,10 @@ def test_neg_put_with_policy_exists_update_negative(self): "max_retries": 1, "key": aerospike.POLICY_KEY_SEND, } - try: + with pytest.raises(e.RecordNotFound) as excinfo: assert 0 == self.as_connection.put(key, rec, meta, policy) - - except e.RecordNotFound as exception: - assert exception.code == 2 - assert exception.msg == "AEROSPIKE_ERR_RECORD_NOT_FOUND" + assert excinfo.value.code == 2 + assert excinfo.value.msg == "AEROSPIKE_ERR_RECORD_NOT_FOUND" def test_neg_put_with_policy_gen_GT_lesser(self): """ @@ -713,12 +709,10 @@ def test_neg_put_with_policy_gen_GT_lesser(self): policy = {"gen": aerospike.POLICY_GEN_GT} meta = {"gen": gen} - try: + with pytest.raises(e.RecordGenerationError) as excinfo: self.as_connection.put(key, rec, meta, policy) - - except e.RecordGenerationError as exception: - assert exception.code == 3 - assert exception.msg == "AEROSPIKE_ERR_RECORD_GENERATION" + assert excinfo.value.code == 3 + assert excinfo.value.msg == "AEROSPIKE_ERR_RECORD_GENERATION" (key, meta, bins) = self.as_connection.get(key) assert {"name": "John"} == bins @@ -825,14 +819,11 @@ def test_edge_put_with_integer_greater_than_maxisze(self): bins = {"no": 111111111111111111111111111111111111111111111} - try: + with pytest.raises((e.ParamError, SystemError)) as excinfo: assert 0 == self.as_connection.put(key, bins) - - except e.ParamError as exception: - assert exception.code == -2 - assert exception.msg == "integer value exceeds sys.maxsize" - except SystemError: - pass + if excinfo.type == e.ParamError: + assert excinfo.value.code == -2 + assert excinfo.value.msg == "integer value exceeds sys.maxsize" def test_edge_put_with_key_as_an_integer_greater_than_maxsize(self): """ diff --git a/test/new_tests/test_increment.py b/test/new_tests/test_increment.py index 1f6e71aca..0f579457c 100644 --- a/test/new_tests/test_increment.py +++ b/test/new_tests/test_increment.py @@ -355,13 +355,11 @@ def test_increment_with_integer_greaterthan_maxsize(self): key = ("test", "demo", 1) bins = {"age": 10} self.as_connection.put(key, bins) - try: + with pytest.raises((SystemError, Exception)) as excinfo: self.as_connection.increment(key, "age", 68786586756785785745) - # except SystemError: - # pass - except Exception as exception: - assert exception.code == AerospikeStatus.AEROSPIKE_ERR_PARAM - assert exception.msg == "integer value exceeds sys.maxsize" + if excinfo.type == Exception: + assert excinfo.value.code == AerospikeStatus.AEROSPIKE_ERR_PARAM + assert excinfo.value.msg == "integer value exceeds sys.maxsize" def test_increment_with_string_value(self): """ diff --git a/test/new_tests/test_list_index.py b/test/new_tests/test_list_index.py index 91a3ac64c..792fd8c89 100644 --- a/test/new_tests/test_list_index.py +++ b/test/new_tests/test_list_index.py @@ -79,15 +79,14 @@ def test_pos_listindex_with_correct_parameters_set_length_extra(self): for _ in range(100): set_name = set_name + "a" policy = {} - try: + with pytest.raises((Exception, e.InvalidRequest)) as excinfo: self.as_connection.index_list_create( "test", set_name, "string_list", aerospike.INDEX_STRING, "test_string_list_index", policy ) - assert False - except e.InvalidRequest as exception: - assert exception.code == 4 - except Exception as exception: - assert isinstance(exception, e.InvalidRequest) + if excinfo.type == e.InvalidRequest: + assert excinfo.value.code == 4 + elif excinfo.type == Exception: + assert isinstance(excinfo.value, e.InvalidRequest) def test_pos_listindex_with_incorrect_bin(self): """ @@ -271,44 +270,33 @@ def test_neg_listindex_with_namespace_is_none(self): Invoke createindex() with namespace is None """ policy = {} - try: + with pytest.raises(e.ParamError) as excinfo: self.as_connection.index_list_create( None, "demo", "string_list", aerospike.INDEX_STRING, "test_string_list_index", policy ) - - except e.ParamError as exception: - assert exception.code == -2 - assert exception.msg == "Namespace should be a string" + assert excinfo.value.code == -2 + assert excinfo.value.msg == "Namespace should be a string" def test_neg_listindex_with_set_is_int(self): """ Invoke createindex() with set is int """ policy = {} - try: + with pytest.raises(e.ParamError) as excinfo: self.as_connection.index_list_create( "test", 1, "string_list", aerospike.INDEX_STRING, "test_string_list_index", policy ) - assert False - except e.ParamError as exception: - assert exception.code == -2 - assert exception.msg == "Set should be string, unicode or None" - except Exception as exception: - assert isinstance(exception, e.ParamError) + assert excinfo.value.code == -2 + assert excinfo.value.msg == "Set should be string, unicode or None" - def test_neg_listindex_with_set_is_none(self): + def test_listindex_with_set_is_none(self): """ Invoke createindex() with set is None """ policy = {} - try: - self.as_connection.index_list_create( - "test", None, "string_list", aerospike.INDEX_STRING, "test_string_list_index", policy - ) - - except e.ParamError as exception: - assert exception.code == -2 - assert exception.msg == "Set should be a string" + self.as_connection.index_list_create( + "test", None, "string_list", aerospike.INDEX_STRING, "test_string_list_index", policy + ) self.as_connection.index_remove("test", "test_string_list_index", policy) ensure_dropped_index(self.as_connection, "test", "test_string_list_index") @@ -317,26 +305,22 @@ def test_neg_listindex_with_bin_is_none(self): Invoke createindex() with bin is None """ policy = {} - try: + with pytest.raises(e.ParamError) as excinfo: self.as_connection.index_list_create( "test", "demo", None, aerospike.INDEX_NUMERIC, "test_numeric_list_index", policy ) - - except e.ParamError as exception: - assert exception.code == -2 - assert exception.msg == "Bin should be a string" + assert excinfo.value.code == -2 + assert excinfo.value.msg == "Bin should be a string" def test_neg_listindex_with_index_is_none(self): """ Invoke createindex() with index is None """ policy = {} - try: + with pytest.raises(e.ParamError) as excinfo: self.as_connection.index_list_create("test", "demo", "string_list", aerospike.INDEX_STRING, None, policy) - - except e.ParamError as exception: - assert exception.code == -2 - assert exception.msg == "Index name should be string or unicode" + assert excinfo.value.code == -2 + assert excinfo.value.msg == "Index name should be string or unicode" def test_neg_listindex_with_incorrect_namespace(self): """ @@ -344,13 +328,11 @@ def test_neg_listindex_with_incorrect_namespace(self): """ policy = {} - try: + with pytest.raises(e.NamespaceNotFound) as excinfo: self.as_connection.index_list_create( "test1", "demo", "numeric_list", aerospike.INDEX_NUMERIC, "test_numeric_list_index", policy ) - - except e.NamespaceNotFound as exception: - assert exception.code == 20 + assert excinfo.code == 20 def test_neg_listindex_with_incorrect_set(self): """ @@ -374,13 +356,12 @@ def test_neg_listindex_with_correct_parameters_no_connection(self): client1 = aerospike.client(config) client1.close() - try: + with pytest.raises(e.ClusterError) as excinfo: client1.index_list_create( "test", "demo", "string_list", aerospike.INDEX_STRING, "test_string_list_index", policy ) - except e.ClusterError as exception: - assert exception.code == 11 + assert excinfo.value.code == 11 def test_neg_listindex_with_no_paramters(self): """ diff --git a/test/new_tests/test_max_error_rate.py b/test/new_tests/test_max_error_rate.py index a5d0194d3..b7594ec10 100644 --- a/test/new_tests/test_max_error_rate.py +++ b/test/new_tests/test_max_error_rate.py @@ -36,13 +36,13 @@ def callback(input_tuple): raise Exception for i in range(2 * max_error_rate): - try: + with pytest.raises(e.ClientError) as excinfo: query.foreach(callback) - except e.ClientError as ex: - if i < max_error_rate: - assert ex.code == AerospikeStatus.AEROSPIKE_ERR_CLIENT - else: - assert ex.code == AerospikeStatus.AEROSPIKE_MAX_ERROR_RATE + + if i < max_error_rate: + assert excinfo.value.code == AerospikeStatus.AEROSPIKE_ERR_CLIENT + else: + assert excinfo.value.code == AerospikeStatus.AEROSPIKE_MAX_ERROR_RATE def test_max_error_rate_is_reseted_by_healthcheck_thread(self): """ @@ -61,10 +61,9 @@ def callback(input_tuple): raise Exception for i in range(2 * MAX_ERROR_RATE): - try: + with pytest.raises(e.ClientError) as excinfo: query.foreach(callback) - except e.ClientError as ex: - assert ex.code == AerospikeStatus.AEROSPIKE_ERR_CLIENT + assert excinfo.value.code == AerospikeStatus.AEROSPIKE_ERR_CLIENT def test_max_error_rate_is_zero(self): """ @@ -83,7 +82,6 @@ def callback(_): # MaxRetriesExceeded should never be thrown for _ in range(6): - try: + with pytest.raises(e.ClientError) as excinfo: query.foreach(callback) - except e.ClientError as ex: - assert ex.code == AerospikeStatus.AEROSPIKE_ERR_CLIENT + assert excinfo.value.code == AerospikeStatus.AEROSPIKE_ERR_CLIENT diff --git a/test/new_tests/test_operate.py b/test/new_tests/test_operate.py index 3c5a9550b..6bdafe64a 100644 --- a/test/new_tests/test_operate.py +++ b/test/new_tests/test_operate.py @@ -1040,11 +1040,9 @@ def test_neg_operate_list_operation_bin_notlist(self): key = ("test", "demo", 1) list = [{"op": aerospike.OP_LIST_INSERT, "bin": "age", "index": 2, "val": 9}] - try: + with pytest.raises(e.BinIncompatibleType) as excinfo: (key, _, _) = self.as_connection.operate(key, list) - - except e.BinIncompatibleType as exception: - assert exception.code == 12 + assert excinfo.value.code == 12 def test_neg_operate_append_items_not_a_list(self): """ @@ -1056,10 +1054,9 @@ def test_neg_operate_append_items_not_a_list(self): {"op": aerospike.OP_LIST_APPEND_ITEMS, "bin": "int_bin", "val": 7}, ] - try: + with pytest.raises(e.ParamError) as excinfo: key, _, bins = self.as_connection.operate(key, list) - except e.ParamError as exception: - assert exception.code == -2 + assert excinfo.value.code == -2 @pytest.mark.parametrize( "list", @@ -1084,10 +1081,9 @@ def test_neg_operate_list_invalid_requests(self, list): Invoke operate() with list addition operations negative """ key = ("test", "demo", "list_key") - try: + with pytest.raises(e.OpNotApplicable) as excinfo: key, _, _ = self.as_connection.operate(key, list) - except e.OpNotApplicable as exception: - assert exception.code == 26 + assert excinfo.value.code == 26 def test_neg_operate_with_command_invalid(self): """ @@ -1101,11 +1097,9 @@ def test_neg_operate_with_command_invalid(self): {"op": aerospike.OPERATOR_READ, "bin": "name"}, ] - try: + with pytest.raises(e.ParamError) as excinfo: key, _, _ = self.as_connection.operate(key, llist) - - except e.ParamError as exception: - assert exception.code == -2 + assert excinfo.value.code == -2 def test_neg_operate_with_bin_length_extra(self): """ @@ -1123,23 +1117,19 @@ def test_neg_operate_with_bin_length_extra(self): {"op": aerospike.OPERATOR_READ, "bin": "name"}, ] - try: + with pytest.raises(e.BinNameError) as excinfo: key, _, _ = self.as_connection.operate(key, llist) - - except e.BinNameError as exception: - assert exception.code == 21 - assert exception.msg == "A bin name should not exceed 15 characters limit" + assert excinfo.value.code == 21 + assert excinfo.value.msg == "A bin name should not exceed 15 characters limit" def test_neg_operate_empty_string_key(self): """ Invoke operate() with empty string key """ llist = [{"op": aerospike.OPERATOR_PREPEND, "bin": "name", "val": "ram"}] - try: + with pytest.raises(e.ParamError) as excinfo: self.as_connection.operate("", llist) - - except e.ParamError as exception: - assert exception.code == -2 + assert excinfo.value.code == -2 def test_neg_operate_with_extra_parameter(self): """ @@ -1159,22 +1149,18 @@ def test_neg_operate_policy_is_string(self): """ key = ("test", "demo", 1) llist = [{"op": aerospike.OPERATOR_PREPEND, "bin": "name", "val": "ram"}] - try: + with pytest.raises(e.ParamError) as excinfo: self.as_connection.operate(key, llist, {}, "") - - except e.ParamError as exception: - assert exception.code == -2 + assert excinfo.value.code == -2 def test_neg_operate_key_is_none(self): """ Invoke operate() with key is none """ llist = [{"op": aerospike.OPERATOR_PREPEND, "bin": "name", "val": "ram"}] - try: + with pytest.raises(e.ParamError) as excinfo: self.as_connection.operate(None, llist) - - except e.ParamError as exception: - assert exception.code == -2 + assert excinfo.value.code == -2 @pytest.mark.parametrize( "key, policy, llist, ex_code", @@ -1232,10 +1218,9 @@ def test_neg_operate_append_value_integer(self): {"op": aerospike.OPERATOR_READ, "bin": "name"}, ] - try: + with pytest.raises(e.ParamError) as excinfo: self.as_connection.operate(key, llist) - except e.ParamError as exception: - assert exception.code == -2 + assert excinfo.value.code == -2 def test_neg_opearte_with_incorrect_polic(self): """ @@ -1249,11 +1234,9 @@ def test_neg_opearte_with_incorrect_polic(self): {"op": aerospike.OPERATOR_READ, "bin": "name"}, ] - try: + with pytest.raises(e.ParamError) as excinfo: self.as_connection.operate(key, llist, {}, policy) - - except e.ParamError as exception: - assert exception.code == -2 + assert excinfo.value.code == -2 def test_neg_opearte_on_same_bin(self): """ @@ -1270,6 +1253,5 @@ def test_neg_opearte_on_same_bin(self): try: self.as_connection.operate(key, llist, {}, policy) - except e.InvalidRequest as exception: assert exception.code == 4 diff --git a/test/new_tests/test_operate_helpers.py b/test/new_tests/test_operate_helpers.py index d61a2e2a2..9dea49d3d 100644 --- a/test/new_tests/test_operate_helpers.py +++ b/test/new_tests/test_operate_helpers.py @@ -1019,22 +1019,18 @@ def test_neg_operate_policy_is_string(self): """ key = ("test", "demo", 1) llist = [operations.prepend("name", "ram")] - try: + with pytest.raises(e.ParamError) as excinfo: self.as_connection.operate(key, llist, {}, "") - - except e.ParamError as exception: - assert exception.code == -2 + assert excinfo.value.code == -2 def test_neg_operate_key_is_none(self): """ Invoke operate() with key is none """ llist = [operations.prepend("name", "ram")] - try: + with pytest.raises(e.ParamError) as excinfo: self.as_connection.operate(None, llist) - - except e.ParamError as exception: - assert exception.code == -2 + assert excinfo.value.code == -2 def test_neg_operate_append_value_integer(self): """ @@ -1043,10 +1039,9 @@ def test_neg_operate_append_value_integer(self): key = ("test", "demo", 1) llist = [operations.append("name", 12)] - try: + with pytest.raises(e.ParamError) as excinfo: self.as_connection.operate(key, llist) - except e.ParamError as exception: - assert exception.code == -2 + assert excinfo.value.code == -2 def test_neg_operate_with_incorrect_polic(self): """ diff --git a/test/new_tests/test_operate_ordered.py b/test/new_tests/test_operate_ordered.py index 67a10299b..fa39b4042 100644 --- a/test/new_tests/test_operate_ordered.py +++ b/test/new_tests/test_operate_ordered.py @@ -604,11 +604,9 @@ def test_neg_operate_ordered_with_policy_gen_EQ_not_equal(self): {"op": aerospike.OPERATOR_INCR, "bin": "age", "val": 3}, {"op": aerospike.OPERATOR_READ, "bin": "name"}, ] - try: + with pytest.raises(e.RecordGenerationError) as excinfo: key, meta, _ = self.as_connection.operate_ordered(key, llist, meta, policy) - - except e.RecordGenerationError as exception: - assert exception.code == 3 + assert excinfo.value.code == 3 (key, meta, bins) = self.as_connection.get(key) assert bins == {"age": 1, "name": "name1"} @@ -635,11 +633,9 @@ def test_neg_operate_ordered_with_policy_gen_GT_lesser(self): {"op": aerospike.OPERATOR_READ, "bin": "name"}, ] - try: + with pytest.raises(e.RecordGenerationError) as excinfo: (key, meta, _) = self.as_connection.operate_ordered(key, llist, meta, policy) - - except e.RecordGenerationError as exception: - assert exception.code == 3 + assert excinfo.value.code == 3 (key, meta, bins) = self.as_connection.get(key) assert bins == {"age": 1, "name": "name1"} @@ -664,11 +660,9 @@ def test_neg_operate_ordered_without_connection(self): {"op": aerospike.OPERATOR_READ, "bin": "name"}, ] - try: + with pytest.raises(e.ClusterError) as excinfo: key, _, _ = client1.operate_ordered(key, llist) - - except e.ClusterError as exception: - assert exception.code == 11 + assert excinfo.value.code == 11 def test_neg_operate_ordered_prepend_set_to_aerospike_null(self): """ @@ -685,11 +679,9 @@ def test_neg_operate_ordered_prepend_set_to_aerospike_null(self): {"op": aerospike.OPERATOR_READ, "bin": "no"}, ] - try: + with pytest.raises(e.InvalidRequest) as excinfo: (key, _, bins) = self.as_connection.operate_ordered(key, llist) - - except e.InvalidRequest as exception: - assert exception.code == 4 + assert excinfo.value.code == 4 self.as_connection.remove(key) def test_neg_operate_ordered_with_command_invalid(self): @@ -700,11 +692,9 @@ def test_neg_operate_ordered_with_command_invalid(self): llist = [{"op": 999, "bin": "age", "val": 3}, {"op": aerospike.OPERATOR_READ, "bin": "name"}] - try: + with pytest.raises(e.ParamError) as excinfo: key, _, _ = self.as_connection.operate_ordered(key, llist) - - except e.ParamError as exception: - assert exception.code == -2 + assert excinfo.value.code == -2 def test_neg_operate_ordered_with_bin_length_extra(self): """ @@ -721,23 +711,19 @@ def test_neg_operate_ordered_with_bin_length_extra(self): {"op": aerospike.OPERATOR_READ, "bin": "name"}, ] - try: + with pytest.raises(e.BinNameError) as excinfo: key, _, _ = self.as_connection.operate_ordered(key, llist) - - except e.BinNameError as exception: - assert exception.code == 21 - assert exception.msg == "A bin name should not exceed 15 characters limit" + assert excinfo.value.code == 21 + assert excinfo.value.msg == "A bin name should not exceed 15 characters limit" def test_neg_operate_ordered_empty_string_key(self): """ Invoke operate_ordered() with empty string key """ llist = [{"op": aerospike.OPERATOR_PREPEND, "bin": "name", "val": "ram"}] - try: + with pytest.raises(e.ParamError) as excinfo: self.as_connection.operate_ordered("", llist) - - except e.ParamError as exception: - assert exception.code == -2 + assert excinfo.value.code == -2 def test_neg_operate_ordered_with_extra_parameter(self): """ @@ -757,22 +743,18 @@ def test_neg_operate_ordered_policy_is_string(self): """ key = ("test", "demo", 1) llist = [{"op": aerospike.OPERATOR_PREPEND, "bin": "name", "val": "ram"}] - try: + with pytest.raises(e.ParamError) as excinfo: self.as_connection.operate_ordered(key, llist, {}, "") - - except e.ParamError as exception: - assert exception.code == -2 + assert excinfo.value.code == -2 def test_neg_operate_ordered_key_is_none(self): """ Invoke operate_ordered() with key is none """ llist = [{"op": aerospike.OPERATOR_PREPEND, "bin": "name", "val": "ram"}] - try: + with pytest.raises(e.ParamError) as excinfo: self.as_connection.operate_ordered(None, llist) - - except e.ParamError as exception: - assert exception.code == -2 + assert excinfo.value.code == -2 @pytest.mark.parametrize( "key, policy, list, ex_code", @@ -813,11 +795,9 @@ def test_neg_operate_ordered_append_without_value_parameter(self, key, policy, l Invoke operate_ordered() with append op and append val is not given """ - try: + with pytest.raises(e.ParamError) as excinfo: self.as_connection.operate_ordered(key, list, {}, policy) - - except e.ParamError as exception: - assert exception.code == ex_code + assert excinfo.value.code == ex_code def test_neg_operate_ordered_append_value_integer(self): """ @@ -830,10 +810,9 @@ def test_neg_operate_ordered_append_value_integer(self): {"op": aerospike.OPERATOR_READ, "bin": "name"}, ] - try: + with pytest.raises(e.ParamError) as excinfo: self.as_connection.operate_ordered(key, llist) - except e.ParamError as exc: - assert exc.code == -2 + assert excinfo.value.code == -2 def test_neg_operate_ordered_with_incorrect_policy(self): """ @@ -847,11 +826,9 @@ def test_neg_operate_ordered_with_incorrect_policy(self): {"op": aerospike.OPERATOR_READ, "bin": "name"}, ] - try: + with pytest.raises(e.ParamError) as excinfo: self.as_connection.operate_ordered(key, llist, {}, policy) - - except e.ParamError as exception: - assert exception.code == -2 + assert excinfo.value.code == -2 def test_neg_operate_ordered_list_operation_bin_notlist(self): """ @@ -860,11 +837,9 @@ def test_neg_operate_ordered_list_operation_bin_notlist(self): key = ("test", "demo", 1) list = [{"op": aerospike.OP_LIST_INSERT, "bin": "age", "index": 2, "val": 9}] - try: + with pytest.raises(e.BinIncompatibleType) as excinfo: (key, _, _) = self.as_connection.operate_ordered(key, list) - - except e.BinIncompatibleType as exception: - assert exception.code == 12 + assert excinfo.value.code == 12 def test_neg_operate_ordered_append_items_not_a_list(self): """ @@ -876,11 +851,9 @@ def test_neg_operate_ordered_append_items_not_a_list(self): {"op": aerospike.OP_LIST_APPEND_ITEMS, "bin": "int_bin", "val": 7}, ] - try: + with pytest.raises(e.ParamError) as excinfo: key, _, bins = self.as_connection.operate_ordered(key, list) - - except e.ParamError as exception: - assert exception.code == -2 + assert excinfo.value.code == -2 @pytest.mark.parametrize( "key, llist", @@ -933,10 +906,8 @@ def test_neg_operate_ordered_notypecheck_existing_record(self, key, llist): """ Invoke operate() with no typecheck on existing record """ - try: + with pytest.raises(e.BinIncompatibleType) as excinfo: (key, _, _) = TestOperateOrdered.client_no_typechecks.operate_ordered(key, llist) - - except e.BinIncompatibleType as exception: - assert exception.code == 12 + assert excinfo.value.code == 12 TestOperateOrdered.client_no_typechecks.remove(key) diff --git a/test/new_tests/test_prepend.py b/test/new_tests/test_prepend.py index e6bfb08e9..82d3fca4a 100644 --- a/test/new_tests/test_prepend.py +++ b/test/new_tests/test_prepend.py @@ -302,12 +302,11 @@ def test_neg_prepend_with_policy_key_gen_EQ_not_equal(self): gen = meta["gen"] meta = {"gen": gen + 5, "ttl": 1200} - try: + with pytest.raises(e.RecordGenerationError) as excinfo: self.as_connection.prepend(key, "name", "str", meta, policy) - except e.RecordGenerationError as exception: - assert exception.code == 3 - assert exception.bin == "name" + assert excinfo.value.code == 3 + assert excinfo.value.bin == "name" (key, meta, bins) = self.as_connection.get(key) @@ -333,12 +332,11 @@ def test_neg_prepend_with_policy_key_gen_GT_lesser(self): gen = meta["gen"] meta = {"gen": gen, "ttl": 1200} - try: + with pytest.raises(e.RecordGenerationError) as excinfo: self.as_connection.prepend(key, "name", "str", meta, policy) - except e.RecordGenerationError as exception: - assert exception.code == 3 - assert exception.bin == "name" + assert excinfo.value.code == 3 + assert excinfo.value.bin == "name" (key, meta, bins) = self.as_connection.get(key) @@ -356,12 +354,11 @@ def test_neg_prepend_with_incorrect_policy(self): """ key = ("test", "demo", 1) policy = {"total_timeout": 0.5} - try: + with pytest.raises(e.ParamError) as excinfo: self.as_connection.prepend(key, "name", "str", {}, policy) - except e.ParamError as exception: - assert exception.code == -2 - assert exception.msg == "total_timeout is invalid" + assert excinfo.value.code == -2 + assert excinfo.value.msg == "total_timeout is invalid" @pytest.mark.parametrize( "key, bin, value, meta, policy, ex_code, ex_msg", @@ -375,12 +372,11 @@ def test_neg_prepend_parameters_incorrect_datatypes(self, key, bin, value, meta, """ Invoke prepend() with parameters of incorrect datatypes """ - try: + with pytest.raises(e.ParamError) as excinfo: self.as_connection.prepend(key, bin, value, meta, policy) - except e.ParamError as exception: - assert exception.code == ex_code - assert exception.msg == ex_msg + assert excinfo.value.code == ex_code + assert excinfo.value.msg == ex_msg def test_neg_prepend_with_extra_parameter(self): """ @@ -404,12 +400,11 @@ def test_neg_prepend_parameters_as_none(self, key, bin, value, ex_code, ex_msg): """ Invoke prepend() with parameters as None """ - try: + with pytest.raises(e.ParamError) as excinfo: self.as_connection.prepend(key, bin, value) - except e.ParamError as exception: - assert exception.code == ex_code - assert exception.msg == ex_msg + assert excinfo.value.code == ex_code + assert excinfo.value.msg == ex_msg def test_neg_prepend_invalid_key_invalid_ns(self): """ @@ -456,13 +451,12 @@ def test_neg_prepend_invalid_key_combinations(self, key, bin, value, meta, polic """ Invoke prepend() with invalid key combinations """ - try: + with pytest.raises(e.ParamError) as excinfo: self.as_connection.prepend(key, bin, value, meta, policy) key, meta, _ = self.as_connection.get(key) - except e.ParamError as exception: - assert exception.code == ex_code - assert exception.msg == ex_msg + assert excinfo.value.code == ex_code + assert excinfo.value.msg == ex_msg def test_neg_prepend_without_bin_name(self): """ @@ -470,12 +464,11 @@ def test_neg_prepend_without_bin_name(self): """ key = ("test", "demo", 1) policy = {} - try: + with pytest.raises(e.ParamError) as excinfo: self.as_connection.prepend(key, "ABC", {}, policy) key, _, _ = self.as_connection.get(key) - except e.ParamError as exception: - assert exception.code == -2 - assert exception.msg == "Cannot concatenate 'str' and 'non-str' objects" + assert excinfo.value.code == -2 + assert excinfo.value.msg == "Cannot concatenate 'str' and 'non-str' objects" def test_neg_prepend_with_correct_parameters_without_connection(self): """ @@ -486,8 +479,7 @@ def test_neg_prepend_with_correct_parameters_without_connection(self): client1.close() key = ("test", "demo", 1) - try: + with pytest.raises(e.ClusterError) as excinfo: client1.prepend(key, "name", "str") - except e.ClusterError as exception: - assert exception.code == 11 + assert excinfo.value.code == 11 diff --git a/test/new_tests/test_query.py b/test/new_tests/test_query.py index c94f16190..c0c6d3828 100644 --- a/test/new_tests/test_query.py +++ b/test/new_tests/test_query.py @@ -403,12 +403,10 @@ def test_query_with_where_none_value(self): """ query = self.as_connection.query("test", "demo") query.select("name", "test_age") - try: + with pytest.raises(e.ParamError) as excinfo: query.where(p.equals("test_age", None)) - - except e.ParamError as exception: - assert exception.code == -2 - assert exception.msg == "predicate is invalid." + assert excinfo.value.code == -2 + assert excinfo.value.msg == "predicate is invalid." def test_query_where_called_multiple_times(self): query = self.as_connection.query("test", "demo") diff --git a/test/new_tests/test_remove_bin.py b/test/new_tests/test_remove_bin.py index 6714f053f..20032ef97 100644 --- a/test/new_tests/test_remove_bin.py +++ b/test/new_tests/test_remove_bin.py @@ -125,11 +125,9 @@ def test_pos_remove_bin_with_single_bin_in_a_record(self): policy = {} self.as_connection.remove_bin(key, ["name"], {}, policy) - try: + with pytest.raises(e.RecordNotFound) as exceptionInfo: _, _, bins = self.as_connection.get(key) - assert bins is None - except e.RecordNotFound as exception: - assert exception.code == 2 + assert exceptionInfo.value.code == 2 def test_pos_remove_bin_no_bin(self, put_data): """ @@ -138,12 +136,9 @@ def test_pos_remove_bin_no_bin(self, put_data): key = ("test", "demo", 1) record = {"name": "jeff", "age": 45} put_data(self.as_connection, key, record) - try: + with pytest.raises(e.InvalidRequest) as exceptionInfo: self.as_connection.remove_bin(key, []) - (key, _, bins) = self.as_connection.get(key) - assert bins == record - except e.InvalidRequest: - pass + assert exceptionInfo.value.code == 4 @pytest.mark.parametrize( "key, record, bins_for_removal", @@ -160,11 +155,9 @@ def test_pos_remove_bin_with_unicode_all(self, key, record, bins_for_removal, pu put_data(self.as_connection, key, record) self.as_connection.remove_bin(key, bins_for_removal) - try: + with pytest.raises(e.RecordNotFound) as exceptionInfo: (key, _, _) = self.as_connection.get(key) - - except e.RecordNotFound as exception: - assert exception.code == 2 + assert exceptionInfo.value.code == 2 @pytest.mark.parametrize( "key, record, policy, bin_for_removal", @@ -235,12 +228,10 @@ def test_neg_remove_bin_with_none(self, key, bin_for_removal, ex_code, ex_msg): """ Invoke remove_bin() with none """ - try: + with pytest.raises(e.ParamError) as exceptionInfo: self.as_connection.remove_bin(None, bin_for_removal) - - except e.ParamError as exception: - assert exception.code == ex_code - assert exception.msg == ex_msg + assert exceptionInfo.value.code == ex_code + assert exceptionInfo.value.msg == ex_msg def test_neg_remove_bin_with_correct_parameters_without_connection(self): """ @@ -251,12 +242,9 @@ def test_neg_remove_bin_with_correct_parameters_without_connection(self): client1.close() key = ("test", "demo", 1) - - try: + with pytest.raises(e.ClusterError) as exceptionInfo: client1.remove_bin(key, ["age"]) - - except e.ClusterError as exception: - assert exception.code == 11 + assert exceptionInfo.value.code == 11 def test_neg_remove_bin_with_incorrect_meta(self): """ @@ -276,14 +264,10 @@ def test_neg_remove_bin_with_incorrect_policy(self): Invoke remove_bin() with incorrect policy """ key = ("test", "demo", 1) - policy = {"time": 1001} - try: + with pytest.raises((e.ClientError, e.RecordNotFound)): self.as_connection.remove_bin(key, ["age"], {}, policy) - except (e.ClientError, e.RecordNotFound): - pass - def test_neg_remove_bin_with_no_parameters(self): """ Invoke remove_bin() without any mandatory parameters. @@ -309,11 +293,10 @@ def test_neg_remove_bin_with_policy_send_gen_eq_not_equal(self, put_data): gen = meta["gen"] meta = {"gen": gen + 5, "ttl": 1000} - try: + with pytest.raises(e.RecordGenerationError) as exceptionInfo: self.as_connection.remove_bin(key, ["age"], meta, policy) - except e.RecordGenerationError as exception: - assert exception.code == 3 + assert exceptionInfo.value.code == 3 (key, meta, bins) = self.as_connection.get(key) @@ -341,12 +324,9 @@ def test_neg_remove_bin_with_policy_send_gen_GT_lesser(self, put_data): (key, meta) = self.as_connection.exists(key) gen = meta["gen"] meta = {"gen": gen, "ttl": 1000} - - try: + with pytest.raises(e.RecordGenerationError) as exceptionInfo: self.as_connection.remove_bin(key, ["age"], meta, policy) - - except e.RecordGenerationError as exception: - assert exception.code == 3 + assert exceptionInfo.value.code == 3 (key, meta, bins) = self.as_connection.get(key) @@ -365,11 +345,9 @@ def test_neg_remove_bin_with_incorrect_policy_value(self): key = ("test", "demo", 1) policy = {"total_timeout": 0.5} - try: + with pytest.raises(e.ClientError) as exceptionInfo: self.as_connection.remove_bin(key, ["age"], {}, policy) - - except e.ClientError as exception: - assert exception.code == -1 + assert exceptionInfo.value.code == -1 @pytest.mark.parametrize( "key, bin_for_removal, ex_code", @@ -382,12 +360,9 @@ def test_neg_remove_bin_with_nonexistent_data(self, key, bin_for_removal, ex_cod """ Invoke remove_bin() with non-existent data """ - try: + with pytest.raises(e.RecordNotFound): self.as_connection.remove_bin(key, bin_for_removal) - except e.RecordNotFound: - pass - def test_neg_remove_bin_with_extra_parameter(self): """ Invoke remove_bin() with extra parameter. diff --git a/test/new_tests/test_user_serializer.py b/test/new_tests/test_user_serializer.py index 2a9952b7e..afcde75f2 100644 --- a/test/new_tests/test_user_serializer.py +++ b/test/new_tests/test_user_serializer.py @@ -165,12 +165,10 @@ def test_put_with_float_data_user_serializer_none(self): Invoke put() for float data record with user serializer. """ - try: + with pytest.raises(e.ParamError) as excinfo: aerospike.set_serializer(None) - - except e.ParamError as exception: - assert exception.code == -2 - assert exception.msg == "Parameter must be a callable" + assert excinfo.value.code == -2 + assert excinfo.value.msg == "Parameter must be a callable" def test_put_with_float_data_user_deserializer_none(self): """ @@ -192,12 +190,10 @@ def test_put_with_float_data_user_deserializer_none(self): self.delete_keys.append(key) - try: + with pytest.raises(e.ParamError) as excinfo: aerospike.set_deserializer(None) - - except e.ParamError as exception: - assert exception.code == -2 - assert exception.msg == "Parameter must be a callable" + assert excinfo.value.code == -2 + assert excinfo.value.msg == "Parameter must be a callable" def test_put_with_mixed_data_user_serializer(self): pytest.xfail(reason="Need Python 2/3 compatible bytearray for strings")