From a4e206e044afdf99a6da9171265ac3ac8600e3a2 Mon Sep 17 00:00:00 2001 From: juliannguyen4 <109386615+juliannguyen4@users.noreply.github.com> Date: Fri, 21 Apr 2023 14:35:41 -0700 Subject: [PATCH 01/20] Fix test_append neg tests --- test/new_tests/test_prepend.py | 55 ++++++++++++++-------------------- 1 file changed, 23 insertions(+), 32 deletions(-) diff --git a/test/new_tests/test_prepend.py b/test/new_tests/test_prepend.py index e26e555ed0..6b49bb62c5 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) @@ -355,13 +353,11 @@ def test_neg_prepend_with_incorrect_policy(self): Invoke prepend() with incorrect policy """ 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 == "timeout is invalid" @pytest.mark.parametrize( "key, bin, value, meta, policy, ex_code, ex_msg", @@ -375,12 +371,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 +399,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 +450,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 +463,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 +478,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 From e0ee1df950e46f9c16f8aa7e55ec7eefde7009d5 Mon Sep 17 00:00:00 2001 From: Dominic Pelini <111786059+DomPeliniAerospike@users.noreply.github.com> Date: Mon, 20 Nov 2023 15:39:54 -0700 Subject: [PATCH 02/20] Changed tests in test_remove_bin.py --- test/new_tests/test_remove_bin.py | 69 +++++++++++-------------------- 1 file changed, 24 insertions(+), 45 deletions(-) diff --git a/test/new_tests/test_remove_bin.py b/test/new_tests/test_remove_bin.py index 0d40d215a1..8bb708b878 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): """ @@ -268,24 +256,21 @@ def test_neg_remove_bin_with_incorrect_meta(self): "key": aerospike.POLICY_KEY_SEND, "gen": aerospike.POLICY_GEN_IGNORE, } - try: - self.as_connection.remove_bin(key, ["age"], policy) - except (e.ClusterError, e.RecordNotFound): - pass + with pytest.raises(e.RecordNotFound) as exceptionInfo: + self.as_connection.remove_bin(key, ["age"], policy) + assert exceptionInfo.value.code == 2 - def test_neg_remove_bin_with_incorrect_policy(self): + def test_neg_remove_bin_with_incorrect_policy(self, put_data): """ Invoke remove_bin() with incorrect policy """ key = ("test", "demo", 1) - + put_data(self.as_connection, key, {"age": 15}) policy = {"time": 1001} - try: - self.as_connection.remove_bin(key, ["age"], {}, policy) + self.as_connection.remove_bin(key, ["age"], {}, policy) + - except (e.ClientError, e.RecordNotFound): - pass def test_neg_remove_bin_with_no_parameters(self): """ @@ -312,11 +297,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) @@ -344,12 +328,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) @@ -368,11 +349,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", From c931bc0bd14560ffc15233b3f03f7cca541e3d9b Mon Sep 17 00:00:00 2001 From: Julian Nguyen <109386615+juliannguyen4@users.noreply.github.com> Date: Mon, 20 Nov 2023 15:02:17 -0800 Subject: [PATCH 03/20] Revert test_remove_bin to original behavior --- test/new_tests/test_remove_bin.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/test/new_tests/test_remove_bin.py b/test/new_tests/test_remove_bin.py index 8bb708b878..f98f9392f7 100644 --- a/test/new_tests/test_remove_bin.py +++ b/test/new_tests/test_remove_bin.py @@ -257,20 +257,18 @@ def test_neg_remove_bin_with_incorrect_meta(self): "gen": aerospike.POLICY_GEN_IGNORE, } - with pytest.raises(e.RecordNotFound) as exceptionInfo: + with pytest.raises((e.ClusterError, e.RecordNotFound)) as exceptionInfo: self.as_connection.remove_bin(key, ["age"], policy) assert exceptionInfo.value.code == 2 - def test_neg_remove_bin_with_incorrect_policy(self, put_data): + def test_neg_remove_bin_with_incorrect_policy(self): """ Invoke remove_bin() with incorrect policy """ key = ("test", "demo", 1) - put_data(self.as_connection, key, {"age": 15}) policy = {"time": 1001} - self.as_connection.remove_bin(key, ["age"], {}, policy) - - + with pytest.raises((e.ClientError, e.RecordNotFound)): + self.as_connection.remove_bin(key, ["age"], {}, policy) def test_neg_remove_bin_with_no_parameters(self): """ From 6a3ac8cdd142a5f05d7cf755b1c094734f8a3c9b Mon Sep 17 00:00:00 2001 From: Julian Nguyen <109386615+juliannguyen4@users.noreply.github.com> Date: Mon, 20 Nov 2023 15:07:08 -0800 Subject: [PATCH 04/20] Fix prepend test --- test/new_tests/test_prepend.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/new_tests/test_prepend.py b/test/new_tests/test_prepend.py index 6b49bb62c5..912fa40e37 100644 --- a/test/new_tests/test_prepend.py +++ b/test/new_tests/test_prepend.py @@ -353,11 +353,12 @@ def test_neg_prepend_with_incorrect_policy(self): Invoke prepend() with incorrect policy """ key = ("test", "demo", 1) + policy = {"total_timeout": 0.5} with pytest.raises(e.ParamError) as excinfo: self.as_connection.prepend(key, "name", "str", {}, policy) assert excinfo.value.code == -2 - assert excinfo.value.msg == "timeout is invalid" + assert excinfo.value.msg == "total_timeout is invalid" @pytest.mark.parametrize( "key, bin, value, meta, policy, ex_code, ex_msg", From b319732dbb7e2dda2734d41d7d1d1cf79bf2b518 Mon Sep 17 00:00:00 2001 From: Julian Nguyen <109386615+juliannguyen4@users.noreply.github.com> Date: Mon, 20 Nov 2023 15:37:34 -0800 Subject: [PATCH 05/20] Fix more admin tests --- test/new_tests/test_admin_change_password.py | 32 +++++------ test/new_tests/test_admin_create_role.py | 36 +++++-------- test/new_tests/test_admin_create_user.py | 57 ++++++++------------ test/new_tests/test_admin_drop_role.py | 24 ++++----- test/new_tests/test_admin_drop_user.py | 32 +++++------ test/new_tests/test_admin_get_role.py | 16 +++--- test/new_tests/test_admin_get_roles.py | 8 ++- 7 files changed, 78 insertions(+), 127 deletions(-) diff --git a/test/new_tests/test_admin_change_password.py b/test/new_tests/test_admin_change_password.py index 82e3cee210..85636dbbfc 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,36 +114,30 @@ 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): user = "readwriteuser" password = "newpassword" - try: + with pytest.raises(aerospike.exception.InvalidUser) as excinfo: self.client.admin_change_password(user, password) - - except aerospike.exception.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_change_password_with_too_long_password(self): diff --git a/test/new_tests/test_admin_create_role.py b/test/new_tests/test_admin_create_role.py index 04b1fe62cc..16ebed3106 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): """ @@ -268,21 +267,18 @@ def test_create_role_unknown_privilege_type(self): except e.InvalidRole: pass # we are good, no such role exists - try: + with pytest.raises(e.InvalidPrivilege) as excinfo: self.client.admin_create_role("usr-sys-admin-test", [{"code": 64}]) - except e.InvalidPrivilege as exception: - assert exception.code == 72 + assert excinfo.value.code == 72 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): """ @@ -299,14 +295,12 @@ 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}] ) - try: + with pytest.raises(e.RoleExistsError) as excinfo: 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" + assert excinfo.value.code == 71 + assert excinfo.value.msg == "AEROSPIKE_ROLE_ALREADY_EXISTS" time.sleep(1) status = self.client.admin_drop_role("usr-sys-admin-test") @@ -364,11 +358,9 @@ def test_create_role_positive_with_too_long_role_name(self): """ role_name = "role$" * 1000 - try: + with pytest.raises(e.InvalidRole) as excinfo: self.client.admin_create_role( role_name, [{"code": aerospike.PRIV_READ, "ns": "test", "set": "demo"}] ) - - 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" diff --git a/test/new_tests/test_admin_create_user.py b/test/new_tests/test_admin_create_user.py index 431804d7df..47dca88667 100644 --- a/test/new_tests/test_admin_create_user.py +++ b/test/new_tests/test_admin_create_user.py @@ -107,12 +107,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): @@ -145,12 +143,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): @@ -158,12 +154,10 @@ def test_create_user_with_empty_username(self): password = "user3-test" roles = ["read-write"] - try: + with pytest.raises(e.InvalidUser) as excinfo: self.client.admin_create_user(user, password, 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_create_user_with_special_characters_in_username(self): @@ -189,12 +183,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): @@ -244,15 +236,12 @@ def test_create_user_with_too_long_username(self): except Exception: pass - try: + with pytest.raises((e.InvalidUser, e.ClientError)) as excinfo: self.client.admin_create_user(user, password, roles) - except e.InvalidUser as exception: - assert exception.code == 60 - assert exception.msg == "AEROSPIKE_INVALID_USER" - - except e.ClientError: - pass + if excinfo.type == e.InvalidUser: + assert excinfo.value.code == 60 + assert excinfo.value.msg == "AEROSPIKE_INVALID_USER" def test_create_user_with_too_long_password(self): @@ -281,12 +270,10 @@ def test_create_user_with_empty_roles_list(self): except Exception: pass - try: + with pytest.raises(e.InvalidRole) as excinfo: self.client.admin_create_user(user, password, 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_create_user_with_non_user_admin_user(self): @@ -309,7 +296,7 @@ def test_create_user_with_non_user_admin_user(self): non_admin_client = None - try: + with pytest.raises(e.RoleViolation) as excinfo: # Close and reconnect with non_admin_test user non_admin_client = aerospike.client(config) non_admin_client.close() @@ -318,9 +305,7 @@ def test_create_user_with_non_user_admin_user(self): if non_admin_client: non_admin_client.close() - - except e.RoleViolation as exception: - assert exception.code == 81 + assert excinfo.value.code == 81 self.delete_users.append("non_admin_test") diff --git a/test/new_tests/test_admin_drop_role.py b/test/new_tests/test_admin_drop_role.py index b32bb0d286..fbfaeef394 100644 --- a/test/new_tests/test_admin_drop_role.py +++ b/test/new_tests/test_admin_drop_role.py @@ -125,23 +125,19 @@ def test_drop_non_existent_role(self): """ Drop non-existent role """ - try: + with pytest.raises(e.InvalidRole) as excinfo: self.client.admin_drop_role("usr-sys-admin-test") - - 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_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 +147,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 cb0071efba..250be812a3 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): """ @@ -151,12 +149,10 @@ def test_drop_user_negative(self): assert exception.code == 60 assert exception.msg == "AEROSPIKE_INVALID_USER" - try: + with pytest.raises(e.InvalidUser) as excinfo: self.client.admin_drop_user(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_drop_user_policy_incorrect(self): """ @@ -175,12 +171,10 @@ def test_drop_user_policy_incorrect(self): assert user_details == ["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) @@ -207,12 +201,10 @@ def test_drop_user_with_too_long_username(self): assert exception.code == 60 assert exception.msg == "AEROSPIKE_INVALID_USER" - try: + with pytest.raises(e.InvalidUser) as excinfo: self.client.admin_drop_user(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_drop_user_with_special_characters_in_username(self): diff --git a/test/new_tests/test_admin_get_role.py b/test/new_tests/test_admin_get_role.py index 4379ad5768..949b0c58ba 100644 --- a/test/new_tests/test_admin_get_role.py +++ b/test/new_tests/test_admin_get_role.py @@ -81,20 +81,16 @@ def test_admin_get_role_incorrect_role_name(self): """ Incorrect role name """ - try: + with pytest.raises(e.InvalidRole) as excinfo: self.client.admin_get_role("usr-sys-admin-test-non-existent") - - 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_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 06a455e31d..7a93b01cee 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" From 8e168133496e83a9adc6d4ff4debf95ffebba23c Mon Sep 17 00:00:00 2001 From: Julian Nguyen <109386615+juliannguyen4@users.noreply.github.com> Date: Mon, 20 Nov 2023 15:59:31 -0800 Subject: [PATCH 06/20] Fix more admin tests --- test/new_tests/test_admin_grant_privileges.py | 29 +++++--------- test/new_tests/test_admin_grant_roles.py | 32 ++++++--------- test/new_tests/test_admin_query_role.py | 16 +++----- test/new_tests/test_admin_query_roles.py | 8 ++-- test/new_tests/test_admin_query_user.py | 40 +++++++------------ test/new_tests/test_admin_query_user_info.py | 40 +++++++------------ test/new_tests/test_admin_query_users.py | 16 +++----- test/new_tests/test_admin_query_users_info.py | 16 +++----- .../new_tests/test_admin_revoke_privileges.py | 29 +++++--------- test/new_tests/test_admin_revoke_roles.py | 40 +++++++------------ 10 files changed, 100 insertions(+), 166 deletions(-) diff --git a/test/new_tests/test_admin_grant_privileges.py b/test/new_tests/test_admin_grant_privileges.py index b4d5990430..70b88e2158 100644 --- a/test/new_tests/test_admin_grant_privileges.py +++ b/test/new_tests/test_admin_grant_privileges.py @@ -167,40 +167,33 @@ 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): """ privilege type unknown """ - try: + with pytest.raises(e.InvalidPrivilege) as excinfo: self.client.admin_grant_privileges("usr-sys-admin-test", [{"code": 64}]) - except e.InvalidPrivilege as exception: - assert exception.code == 72 + assert excinfo.value.code == 72 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): """ privilege type is an empty list """ - try: + with pytest.raises(e.InvalidPrivilege) as excinfo: self.client.admin_grant_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_grant_roles.py b/test/new_tests/test_admin_grant_roles.py index 88472a7466..900f8fb5f1 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,24 +113,20 @@ 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): user = "" roles = ["read-write"] - try: + with pytest.raises(e.InvalidUser) as excinfo: self.client.admin_grant_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_grant_roles_with_special_characters_in_username(self): @@ -164,12 +158,10 @@ def test_grant_roles_with_empty_roles_list(self): user = "example-test" roles = [] - try: + with pytest.raises(e.InvalidUser) as excinfo: self.client.admin_grant_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" # TODO: incorrect test def test_grant_roles_with_role_name_exceeding_max_length(self): diff --git a/test/new_tests/test_admin_query_role.py b/test/new_tests/test_admin_query_role.py index 0510ae3677..3dc72fefb1 100644 --- a/test/new_tests/test_admin_query_role.py +++ b/test/new_tests/test_admin_query_role.py @@ -71,20 +71,16 @@ def test_admin_query_role_incorrect_role_name(self): """ Incorrect role name """ - try: + with pytest.raises(e.InvalidRole) as excinfo: self.client.admin_query_role("usr-sys-admin-test-non-existent") - - 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_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 803d34e9e9..71af76ff93 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.py b/test/new_tests/test_admin_query_user.py index 3997e6ab02..d191e90d9f 100644 --- a/test/new_tests/test_admin_query_user.py +++ b/test/new_tests/test_admin_query_user.py @@ -69,12 +69,10 @@ def test_query_user_with_invalid_timeout_policy_value(self): policy = {"timeout": 0.1} user = "example-test" - try: + with pytest.raises(e.ParamError) as excinfo: self.client.admin_query_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" def test_query_user_with_proper_timeout_policy_value(self): @@ -90,34 +88,28 @@ def test_query_user_with_none_username(self): user = None - try: + with pytest.raises(e.ParamError) as excinfo: self.client.admin_query_user(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_with_empty_username(self): user = "" - try: + with pytest.raises(e.ParamError) as excinfo: self.client.admin_query_user(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_with_nonexistent_username(self): user = "non-existent" - try: + with pytest.raises(e.InvalidUser) as excinfo: self.client.admin_query_user(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_with_no_roles(self): @@ -146,9 +138,7 @@ def test_query_user_with_policy_as_string(self): Invoke query_user() with policy as string """ policy = "" - try: + with pytest.raises(e.AerospikeError) as typeError: self.client.admin_query_user("foo", policy) - - except e.AerospikeError as exception: - assert exception.code == -2 - assert exception.msg == "policy must be a dict" + assert typeError.value.code == -2 + assert typeError.value.msg == "policy must be a dict" diff --git a/test/new_tests/test_admin_query_user_info.py b/test/new_tests/test_admin_query_user_info.py index 85ce194079..0ba603544c 100644 --- a/test/new_tests/test_admin_query_user_info.py +++ b/test/new_tests/test_admin_query_user_info.py @@ -79,12 +79,10 @@ def test_query_user_info_with_invalid_timeout_policy_value(self): policy = {"timeout": 0.1} user = "example-test" - try: + with pytest.raises(e.ParamError) as excinfo: self.client.admin_query_user_info(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" def test_query_user_info_with_proper_timeout_policy_value(self): @@ -100,34 +98,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): @@ -156,9 +148,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 index d8456ad671..ae895a9cb2 100644 --- a/test/new_tests/test_admin_query_users.py +++ b/test/new_tests/test_admin_query_users.py @@ -61,12 +61,10 @@ def test_query_users_with_invalid_timeout_policy_value(self): policy = {"timeout": 0.1} - try: + with pytest.raises(e.ParamError) as excinfo: self.client.admin_query_users(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_with_proper_timeout_policy_value(self): @@ -106,9 +104,7 @@ def test_query_users_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(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_query_users_info.py b/test/new_tests/test_admin_query_users_info.py index 11db38ef38..7cb61d97e5 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 e5bfef9c24..69a7cb524d 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 f02163b0d0..1cb87089ca 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): From fd60e4a97909b24469959576011206323bfb07a9 Mon Sep 17 00:00:00 2001 From: Julian Nguyen <109386615+juliannguyen4@users.noreply.github.com> Date: Mon, 20 Nov 2023 16:31:12 -0800 Subject: [PATCH 07/20] Finish fixing admin tests and more API tests --- test/new_tests/test_admin_set_password.py | 32 +++++--------- test/new_tests/test_admin_set_quotas.py | 30 +++++-------- test/new_tests/test_admin_set_whitelist.py | 42 ++++++++---------- test/new_tests/test_aggregate.py | 31 ++++++------- test/new_tests/test_append.py | 26 +++++------ test/new_tests/test_cdt_index.py | 51 +++++++++------------- test/new_tests/test_close.py | 5 +-- test/new_tests/test_exists.py | 11 ++--- test/new_tests/test_exists_many.py | 14 +++--- test/new_tests/test_get_cdtctx_base64.py | 8 +--- 10 files changed, 98 insertions(+), 152 deletions(-) diff --git a/test/new_tests/test_admin_set_password.py b/test/new_tests/test_admin_set_password.py index 0e0d5d400e..ffe7189108 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 4f7e21316d..5ff3770e61 100644 --- a/test/new_tests/test_admin_set_quotas.py +++ b/test/new_tests/test_admin_set_quotas.py @@ -125,47 +125,41 @@ 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): """ Incorrect role name """ - try: + with pytest.raises(e.InvalidRole) as excinfo: self.client.admin_set_quotas( role="usr-sys-admin-test", read_quota=-20, 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_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 1577129a34..36ea9ae667 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: + with pytest.raises(e.ParamError) as excinfo: 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." + assert excinfo.value.code == -2 + assert excinfo.value.msg == "Whitelist must be a list of IP strings." def test_admin_set_whitelist_forbiden_host(self): """ @@ -176,10 +169,9 @@ def test_admin_set_whitelist_forbiden_host(self): config = TestBaseClass.get_connection_config() new_client = aerospike.client(config).connect(config["user"], config["password"]) - try: + with pytest.raises(e.NotWhitelisted) as excinfo: new_client.connect("test_whitelist_user", "123") - except e.NotWhitelisted as exception: - assert exception.code == 82 - assert exception.msg == "Failed to connect" - finally: - self.client.admin_drop_user("test_whitelist_user") + assert excinfo.value.code == 82 + assert excinfo.value.msg == "Failed to connect" + + self.client.admin_drop_user("test_whitelist_user") diff --git a/test/new_tests/test_aggregate.py b/test/new_tests/test_aggregate.py index 7529e089a6..bd2d75bf0d 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") @@ -385,5 +381,4 @@ def user_callback(value): query.foreach(user_callback) - except e.ParamError as exception: - assert exception.code == -2 + assert excinfo.value.code == -2 diff --git a/test/new_tests/test_append.py b/test/new_tests/test_append.py index 3b6aef19c3..8ce5c1ad19 100644 --- a/test/new_tests/test_append.py +++ b/test/new_tests/test_append.py @@ -298,11 +298,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 == ( @@ -324,12 +323,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) @@ -405,11 +402,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): """ @@ -422,10 +417,9 @@ def test_neg_append_with_low_timeout(self): # 'retry': aerospike.POLICY_RETRY_ONCE, "commit_level": aerospike.POLICY_COMMIT_LEVEL_MASTER, } - try: + with pytest.raises(e.TimeoutError) as excinfo: self.as_connection.append(key, "name", "str", {}, policy) - except e.TimeoutError as exception: - assert exception.code == 9 + assert excinfo.value.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 e5a3d9c54e..ee6db2bdba 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,18 +583,15 @@ 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): """ Invoke createindex() with set is None """ policy = {} - try: + with pytest.raises(e.ParamError) as excinfo: self.as_connection.index_cdt_create( "test", None, @@ -605,9 +603,8 @@ def test_neg_cdtindex_with_set_is_none(self): policy, ) - except e.ParamError as exception: - assert exception.code == -2 - assert exception.msg == "Set should be a string" + assert excinfo.value.code == -2 + assert excinfo.value.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 +613,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 +624,15 @@ def test_neg_cdtindex_with_bin_is_none(self): {"ctx": 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 +643,8 @@ def test_neg_cdtindex_with_index_is_none(self): {"ctx": 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 +652,7 @@ def test_neg_cdtindex_with_incorrect_namespace(self): """ policy = {} - try: + with pytest.raises(e.InvalidRequest) as excinfo: self.as_connection.index_cdt_create( "test1", "demo", @@ -671,8 +664,7 @@ def test_neg_cdtindex_with_incorrect_namespace(self): policy, ) - except e.InvalidRequest as exception: - assert exception.code == 4 + assert excinfo.value.code == 4 def test_neg_cdtindex_with_incorrect_set(self): """ @@ -703,7 +695,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 +707,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 d2598fd8d4..80f64782a7 100644 --- a/test/new_tests/test_close.py +++ b/test/new_tests/test_close.py @@ -30,11 +30,10 @@ def test_pos_close_without_connection(self): self.client = aerospike.client(config) self.client.close() - try: + with pytest.raises(e.ClusterError) as excinfo: self.closeobject = self.client.close() - except e.ClusterError as exception: - assert exception.code == 11 + assert excinfo.value.code == 11 def test_neg_close(self): """ diff --git a/test/new_tests/test_exists.py b/test/new_tests/test_exists.py index 48d1f0fc93..44b64549ef 100644 --- a/test/new_tests/test_exists.py +++ b/test/new_tests/test_exists.py @@ -92,7 +92,7 @@ def test_neg_exists_with_non_existent_data(self, key, ex, ex_code): """ Invoke exists() for non-existent data. """ - try: + with pytest.raises(ex) as excinfo: key, meta = self.as_connection.exists(key) assert meta is None """ @@ -100,8 +100,7 @@ def test_neg_exists_with_non_existent_data(self, key, ex, ex_code): 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 + assert excinfo.value.code == ex_code def test_neg_exists_with_only_key_without_connection(self): """ @@ -112,11 +111,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 index 364a578185..90f916bcf7 100644 --- a/test/new_tests/test_exists_many.py +++ b/test/new_tests/test_exists_many.py @@ -207,11 +207,9 @@ def test_neg_exists_many_with_proper_parameters_without_connection(self, put_dat client1 = aerospike.client(config) client1.close() - try: + with pytest.raises(e.ClusterError) as excinfo: client1.exists_many(self.keys, {"total_timeout": 20}) - - except e.ClusterError as exception: - assert exception.code == 11 + assert excinfo.value.code == 11 def test_neg_exists_many_with_extra_parameter_in_key(self, put_data): keys = [] @@ -232,12 +230,10 @@ def test_neg_exists_many_with_extra_parameter_in_key(self, put_data): key = ("test", "demo", None, bytearray("ase;as[d'as;djk;uyfl", "utf-8"), None) keys_get.append(key) - try: + with pytest.raises(e.ParamError) as excinfo: self.as_connection.exists_many(keys_get) - - except e.ParamError as exception: - assert exception.code == -2 - assert exception.msg == "key tuple must be (Namespace, Set, Key) or (Namespace, Set, None, Digest)" + 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) diff --git a/test/new_tests/test_get_cdtctx_base64.py b/test/new_tests/test_get_cdtctx_base64.py index 3617f9831f..1eb2e44fb1 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 From 0bf7902ed1de4dc6671fdaabee11f10462eb4958 Mon Sep 17 00:00:00 2001 From: Julian Nguyen <109386615+juliannguyen4@users.noreply.github.com> Date: Mon, 20 Nov 2023 16:43:58 -0800 Subject: [PATCH 08/20] Fix a few get put tests --- test/new_tests/test_get_put.py | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/test/new_tests/test_get_put.py b/test/new_tests/test_get_put.py index 2f83d6ca02..ae95d0697b 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): "retry": aerospike.POLICY_RETRY_ONCE, "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): """ From ac3b1cfd74620de0870d92a037a9181661003d9c Mon Sep 17 00:00:00 2001 From: Julian Nguyen <109386615+juliannguyen4@users.noreply.github.com> Date: Mon, 27 Nov 2023 14:55:07 -0800 Subject: [PATCH 09/20] Fix more --- test/new_tests/test_get_put.py | 19 +++----- test/new_tests/test_increment.py | 10 ++-- test/new_tests/test_list_index.py | 64 ++++++++++---------------- test/new_tests/test_max_error_rate.py | 23 ++++----- test/new_tests/test_operate_helpers.py | 17 +++---- test/new_tests/test_operate_ordered.py | 12 ++--- 6 files changed, 55 insertions(+), 90 deletions(-) diff --git a/test/new_tests/test_get_put.py b/test/new_tests/test_get_put.py index ae95d0697b..4fc617b366 100644 --- a/test/new_tests/test_get_put.py +++ b/test/new_tests/test_get_put.py @@ -709,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 @@ -821,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 b566cdb683..ce0d464831 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 7a785a5561..3be8fd0d70 100644 --- a/test/new_tests/test_list_index.py +++ b/test/new_tests/test_list_index.py @@ -75,15 +75,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): """ @@ -249,44 +248,36 @@ 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): """ Invoke createindex() with set is None """ policy = {} - try: + with pytest.raises(e.ParamError) as excinfo: 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" + assert excinfo.value.code == -2 + assert excinfo.value.msg == "Set should be a string" self.as_connection.index_remove("test", "test_string_list_index", policy) ensure_dropped_index(self.as_connection, "test", "test_string_list_index") @@ -295,26 +286,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): """ @@ -322,13 +309,11 @@ def test_neg_listindex_with_incorrect_namespace(self): """ policy = {} - try: + with pytest.raises(e.InvalidRequest) as excinfo: self.as_connection.index_list_create( "test1", "demo", "numeric_list", aerospike.INDEX_NUMERIC, "test_numeric_list_index", policy ) - - except e.InvalidRequest as exception: - assert exception.code == 4 + assert excinfo.value.code == 4 def test_neg_listindex_with_incorrect_set(self): """ @@ -352,13 +337,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 a5d0194d37..c7c5233af7 100644 --- a/test/new_tests/test_max_error_rate.py +++ b/test/new_tests/test_max_error_rate.py @@ -36,13 +36,12 @@ def callback(input_tuple): raise Exception for i in range(2 * max_error_rate): - try: + with pytest.raises((e.ClientError, e.MaxErrorRateExceeded)) 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): """ @@ -60,11 +59,10 @@ def callback(input_tuple): time.sleep(2) # wait for mote than tend_interval raise Exception - for i in range(2 * MAX_ERROR_RATE): - try: + for _ in range(2 * MAX_ERROR_RATE): + 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 +81,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_helpers.py b/test/new_tests/test_operate_helpers.py index 41e3dcc827..04b1d6cd6d 100644 --- a/test/new_tests/test_operate_helpers.py +++ b/test/new_tests/test_operate_helpers.py @@ -902,22 +902,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): """ @@ -926,10 +922,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 4812c937ec..9ec0e41f72 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"} From 25a795a4f07894cc07787eceb84cac7d79f65830 Mon Sep 17 00:00:00 2001 From: Julian Nguyen <109386615+juliannguyen4@users.noreply.github.com> Date: Thu, 30 Nov 2023 12:47:18 -0800 Subject: [PATCH 10/20] finish cleaning --- test/new_tests/test_operate.py | 65 ++++++++------------- test/new_tests/test_operate_ordered.py | 79 +++++++++----------------- test/new_tests/test_query.py | 8 +-- test/new_tests/test_remove_bin.py | 5 +- test/new_tests/test_user_serializer.py | 16 ++---- 5 files changed, 60 insertions(+), 113 deletions(-) diff --git a/test/new_tests/test_operate.py b/test/new_tests/test_operate.py index 02c04b6216..3ec0b4f837 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): """ @@ -1268,8 +1251,6 @@ def test_neg_opearte_on_same_bin(self): {"op": aerospike.OPERATOR_READ, "bin": "name"}, ] - try: + with pytest.raises(e.InvalidRequest) as excinfo: self.as_connection.operate(key, llist, {}, policy) - - except e.InvalidRequest as exception: - assert exception.code == 4 + assert excinfo.value.code == 4 diff --git a/test/new_tests/test_operate_ordered.py b/test/new_tests/test_operate_ordered.py index 9ec0e41f72..2969d31aaa 100644 --- a/test/new_tests/test_operate_ordered.py +++ b/test/new_tests/test_operate_ordered.py @@ -660,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): """ @@ -681,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): @@ -696,11 +692,9 @@ def test_neg_operate_ordered_with_command_invalid(self): llist = [{"op": 3, "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): """ @@ -717,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): """ @@ -753,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", @@ -809,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): """ @@ -826,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): """ @@ -843,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): """ @@ -856,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): """ @@ -872,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", @@ -929,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_query.py b/test/new_tests/test_query.py index b6edee579b..fa4dd0a249 100644 --- a/test/new_tests/test_query.py +++ b/test/new_tests/test_query.py @@ -397,12 +397,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_with_policy(self): """ diff --git a/test/new_tests/test_remove_bin.py b/test/new_tests/test_remove_bin.py index f98f9392f7..bbf9a21356 100644 --- a/test/new_tests/test_remove_bin.py +++ b/test/new_tests/test_remove_bin.py @@ -362,12 +362,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 98bd44277b..209700a1ae 100644 --- a/test/new_tests/test_user_serializer.py +++ b/test/new_tests/test_user_serializer.py @@ -173,12 +173,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): """ @@ -200,12 +198,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") From 66f9a038771424f8ae3b98925f8934bee3db1623 Mon Sep 17 00:00:00 2001 From: Julian Nguyen <109386615+juliannguyen4@users.noreply.github.com> Date: Thu, 30 Nov 2023 12:53:37 -0800 Subject: [PATCH 11/20] Exists shouldnt throw an error if record not found --- test/new_tests/test_exists.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/test/new_tests/test_exists.py b/test/new_tests/test_exists.py index 44b64549ef..8204c735de 100644 --- a/test/new_tests/test_exists.py +++ b/test/new_tests/test_exists.py @@ -92,15 +92,13 @@ def test_neg_exists_with_non_existent_data(self, key, ex, ex_code): """ Invoke exists() for non-existent data. """ - with pytest.raises(ex) as excinfo: - 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. - """ - assert excinfo.value.code == ex_code + 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. + """ def test_neg_exists_with_only_key_without_connection(self): """ From 030c475d189739e5438eaf763666a1f4715117c7 Mon Sep 17 00:00:00 2001 From: Julian Nguyen <109386615+juliannguyen4@users.noreply.github.com> Date: Thu, 30 Nov 2023 13:37:52 -0800 Subject: [PATCH 12/20] revert max_error_rate test --- test/new_tests/test_max_error_rate.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/test/new_tests/test_max_error_rate.py b/test/new_tests/test_max_error_rate.py index c7c5233af7..a5d0194d37 100644 --- a/test/new_tests/test_max_error_rate.py +++ b/test/new_tests/test_max_error_rate.py @@ -36,12 +36,13 @@ def callback(input_tuple): raise Exception for i in range(2 * max_error_rate): - with pytest.raises((e.ClientError, e.MaxErrorRateExceeded)) as excinfo: + try: query.foreach(callback) - if i < max_error_rate: - assert excinfo.value.code == AerospikeStatus.AEROSPIKE_ERR_CLIENT - else: - assert excinfo.value.code == AerospikeStatus.AEROSPIKE_MAX_ERROR_RATE + 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 def test_max_error_rate_is_reseted_by_healthcheck_thread(self): """ @@ -59,10 +60,11 @@ def callback(input_tuple): time.sleep(2) # wait for mote than tend_interval raise Exception - for _ in range(2 * MAX_ERROR_RATE): - with pytest.raises(e.ClientError) as excinfo: + for i in range(2 * MAX_ERROR_RATE): + try: query.foreach(callback) - assert excinfo.value.code == AerospikeStatus.AEROSPIKE_ERR_CLIENT + except e.ClientError as ex: + assert ex.code == AerospikeStatus.AEROSPIKE_ERR_CLIENT def test_max_error_rate_is_zero(self): """ @@ -81,6 +83,7 @@ def callback(_): # MaxRetriesExceeded should never be thrown for _ in range(6): - with pytest.raises(e.ClientError) as excinfo: + try: query.foreach(callback) - assert excinfo.value.code == AerospikeStatus.AEROSPIKE_ERR_CLIENT + except e.ClientError as ex: + assert ex.code == AerospikeStatus.AEROSPIKE_ERR_CLIENT From e0592df069f79046e91c29400fb0c712a463a8cb Mon Sep 17 00:00:00 2001 From: Julian Nguyen <109386615+juliannguyen4@users.noreply.github.com> Date: Thu, 30 Nov 2023 14:09:39 -0800 Subject: [PATCH 13/20] Fix some tests --- test/new_tests/test_append.py | 5 +++-- test/new_tests/test_close.py | 6 +++--- test/new_tests/test_exists.py | 8 ++++++-- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/test/new_tests/test_append.py b/test/new_tests/test_append.py index 8ce5c1ad19..eccc337b9c 100644 --- a/test/new_tests/test_append.py +++ b/test/new_tests/test_append.py @@ -417,9 +417,10 @@ def test_neg_append_with_low_timeout(self): # 'retry': aerospike.POLICY_RETRY_ONCE, "commit_level": aerospike.POLICY_COMMIT_LEVEL_MASTER, } - with pytest.raises(e.TimeoutError) as excinfo: + try: self.as_connection.append(key, "name", "str", {}, policy) - assert excinfo.value.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_close.py b/test/new_tests/test_close.py index 80f64782a7..1535b4b0bc 100644 --- a/test/new_tests/test_close.py +++ b/test/new_tests/test_close.py @@ -30,10 +30,10 @@ def test_pos_close_without_connection(self): self.client = aerospike.client(config) self.client.close() - with pytest.raises(e.ClusterError) as excinfo: + try: self.closeobject = self.client.close() - - assert excinfo.value.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 8204c735de..e0611978fd 100644 --- a/test/new_tests/test_exists.py +++ b/test/new_tests/test_exists.py @@ -92,8 +92,12 @@ def test_neg_exists_with_non_existent_data(self, key, ex, ex_code): """ Invoke exists() for non-existent data. """ - key, meta = self.as_connection.exists(key) - assert meta is None + 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 From b59817b30b2936b3065a68a19a27e79b50727126 Mon Sep 17 00:00:00 2001 From: Julian Nguyen <109386615+juliannguyen4@users.noreply.github.com> Date: Thu, 30 Nov 2023 14:12:30 -0800 Subject: [PATCH 14/20] Fix bug --- src/main/client/sec_index.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/client/sec_index.c b/src/main/client/sec_index.c index 0276148b6d..7c13e139d4 100644 --- a/src/main/client/sec_index.c +++ b/src/main/client/sec_index.c @@ -225,8 +225,6 @@ PyObject *AerospikeClient_Index_Cdt_Create(AerospikeClient *self, as_cdt_ctx_destroy(&ctx); - return py_obj; - CLEANUP: if (py_obj == NULL) { PyObject *py_err = NULL; From 632a051291fab65198927d8237ddba62cdd85797 Mon Sep 17 00:00:00 2001 From: Julian Nguyen <109386615+juliannguyen4@users.noreply.github.com> Date: Thu, 30 Nov 2023 14:20:33 -0800 Subject: [PATCH 15/20] Fix test --- test/new_tests/test_admin_set_whitelist.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/new_tests/test_admin_set_whitelist.py b/test/new_tests/test_admin_set_whitelist.py index 36ea9ae667..362955bb3a 100644 --- a/test/new_tests/test_admin_set_whitelist.py +++ b/test/new_tests/test_admin_set_whitelist.py @@ -155,9 +155,9 @@ def test_admin_set_whitelist_incorrect_whitelist_type(self): Incorrect role type """ with pytest.raises(e.ParamError) as excinfo: - self.client.admin_set_whitelist(role="usr-sys-admin-test", whitelist=None) + 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." + assert excinfo.value.msg == "Whitelist must be a list of IP strings, or None." def test_admin_set_whitelist_forbiden_host(self): """ From 4bca4ac8d45bf7499abc5e8edc9cfc1a00cf2584 Mon Sep 17 00:00:00 2001 From: Julian Nguyen <109386615+juliannguyen4@users.noreply.github.com> Date: Fri, 1 Dec 2023 16:10:49 -0800 Subject: [PATCH 16/20] fix --- test/new_tests/test_admin_create_role.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/new_tests/test_admin_create_role.py b/test/new_tests/test_admin_create_role.py index 16ebed3106..914a09787c 100644 --- a/test/new_tests/test_admin_create_role.py +++ b/test/new_tests/test_admin_create_role.py @@ -295,6 +295,7 @@ 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}] ) + time.sleep(1) with pytest.raises(e.RoleExistsError) as excinfo: self.client.admin_create_role( "usr-sys-admin-test", [{"code": aerospike.PRIV_USER_ADMIN}, {"code": aerospike.PRIV_SYS_ADMIN}] @@ -302,7 +303,6 @@ def test_create_role_existing_role(self): assert excinfo.value.code == 71 assert excinfo.value.msg == "AEROSPIKE_ROLE_ALREADY_EXISTS" - time.sleep(1) status = self.client.admin_drop_role("usr-sys-admin-test") assert status == 0 From 3438330557eb66944bb7f85cdb61674c51a57ab3 Mon Sep 17 00:00:00 2001 From: Julian Nguyen <109386615+juliannguyen4@users.noreply.github.com> Date: Fri, 8 Dec 2023 13:24:57 -0800 Subject: [PATCH 17/20] Revert tests that were failing but could not fix --- test/new_tests/test_admin_create_user.py | 8 ++++--- test/new_tests/test_admin_grant_roles.py | 8 ++++--- test/new_tests/test_admin_set_quotas.py | 7 +++--- test/new_tests/test_admin_set_whitelist.py | 11 +++++----- test/new_tests/test_cdt_index.py | 25 ++++++++++------------ test/new_tests/test_list_index.py | 11 ++++------ test/new_tests/test_operate.py | 5 +++-- 7 files changed, 38 insertions(+), 37 deletions(-) diff --git a/test/new_tests/test_admin_create_user.py b/test/new_tests/test_admin_create_user.py index 47dca88667..c7fa0818ce 100644 --- a/test/new_tests/test_admin_create_user.py +++ b/test/new_tests/test_admin_create_user.py @@ -270,10 +270,12 @@ def test_create_user_with_empty_roles_list(self): except Exception: pass - with pytest.raises(e.InvalidRole) as excinfo: + try: self.client.admin_create_user(user, password, roles) - assert excinfo.value.code == 70 - assert excinfo.value.msg == "AEROSPIKE_INVALID_ROLE" + + except e.InvalidRole as exception: + assert exception.code == 70 + assert exception.msg == "AEROSPIKE_INVALID_ROLE" def test_create_user_with_non_user_admin_user(self): diff --git a/test/new_tests/test_admin_grant_roles.py b/test/new_tests/test_admin_grant_roles.py index 900f8fb5f1..9cf25327db 100644 --- a/test/new_tests/test_admin_grant_roles.py +++ b/test/new_tests/test_admin_grant_roles.py @@ -158,10 +158,12 @@ def test_grant_roles_with_empty_roles_list(self): user = "example-test" roles = [] - with pytest.raises(e.InvalidUser) as excinfo: + try: self.client.admin_grant_roles(user, roles) - assert excinfo.value.code == 70 - assert excinfo.value.msg == "AEROSPIKE_INVALID_ROLE" + + except e.InvalidRole as exception: + assert exception.code == 70 + assert exception.msg == "AEROSPIKE_INVALID_ROLE" # TODO: incorrect test def test_grant_roles_with_role_name_exceeding_max_length(self): diff --git a/test/new_tests/test_admin_set_quotas.py b/test/new_tests/test_admin_set_quotas.py index 5ff3770e61..2767c869c2 100644 --- a/test/new_tests/test_admin_set_quotas.py +++ b/test/new_tests/test_admin_set_quotas.py @@ -145,13 +145,14 @@ def test_admin_set_quota_incorrect_quota(self): """ Incorrect role name """ - with pytest.raises(e.InvalidRole) as excinfo: + try: self.client.admin_set_quotas( role="usr-sys-admin-test", read_quota=-20, write_quota=300 ) - assert excinfo.value.code == 70 - assert excinfo.value.msg == "AEROSPIKE_INVALID_ROLE" + except e.InvalidRole as exception: + assert exception.code == 70 + assert exception.msg == "AEROSPIKE_INVALID_ROLE" def test_admin_set_quota_incorrect_quota_type(self): """ diff --git a/test/new_tests/test_admin_set_whitelist.py b/test/new_tests/test_admin_set_whitelist.py index 362955bb3a..15bbff2122 100644 --- a/test/new_tests/test_admin_set_whitelist.py +++ b/test/new_tests/test_admin_set_whitelist.py @@ -169,9 +169,10 @@ def test_admin_set_whitelist_forbiden_host(self): config = TestBaseClass.get_connection_config() new_client = aerospike.client(config).connect(config["user"], config["password"]) - with pytest.raises(e.NotWhitelisted) as excinfo: + try: new_client.connect("test_whitelist_user", "123") - assert excinfo.value.code == 82 - assert excinfo.value.msg == "Failed to connect" - - self.client.admin_drop_user("test_whitelist_user") + except e.NotWhitelisted as exception: + assert exception.code == 82 + assert exception.msg == "Failed to connect" + finally: + self.client.admin_drop_user("test_whitelist_user") diff --git a/test/new_tests/test_cdt_index.py b/test/new_tests/test_cdt_index.py index ee6db2bdba..9bc746d592 100644 --- a/test/new_tests/test_cdt_index.py +++ b/test/new_tests/test_cdt_index.py @@ -586,25 +586,22 @@ def test_neg_cdtindex_with_set_is_int(self): 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 = {} - with pytest.raises(e.ParamError) as excinfo: - 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, - ) + 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, + ) - assert excinfo.value.code == -2 - assert excinfo.value.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") diff --git a/test/new_tests/test_list_index.py b/test/new_tests/test_list_index.py index 3be8fd0d70..6f36157f79 100644 --- a/test/new_tests/test_list_index.py +++ b/test/new_tests/test_list_index.py @@ -267,17 +267,14 @@ def test_neg_listindex_with_set_is_int(self): 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 = {} - with pytest.raises(e.ParamError) as excinfo: - self.as_connection.index_list_create( - "test", None, "string_list", aerospike.INDEX_STRING, "test_string_list_index", policy - ) - assert excinfo.value.code == -2 - assert excinfo.value.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") diff --git a/test/new_tests/test_operate.py b/test/new_tests/test_operate.py index 3ec0b4f837..4b37917eaa 100644 --- a/test/new_tests/test_operate.py +++ b/test/new_tests/test_operate.py @@ -1251,6 +1251,7 @@ def test_neg_opearte_on_same_bin(self): {"op": aerospike.OPERATOR_READ, "bin": "name"}, ] - with pytest.raises(e.InvalidRequest) as excinfo: + try: self.as_connection.operate(key, llist, {}, policy) - assert excinfo.value.code == 4 + except e.InvalidRequest as exception: + assert exception.code == 4 From baf921895a1e44c64f5ed1d304c10b1b836ba78e Mon Sep 17 00:00:00 2001 From: Julian Nguyen <109386615+juliannguyen4@users.noreply.github.com> Date: Fri, 8 Dec 2023 13:36:56 -0800 Subject: [PATCH 18/20] Revert "Fix bug" This reverts commit 7d041deda85e04ad9855bb2ed16da4bc1ee8f8df. --- src/main/client/sec_index.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/client/sec_index.c b/src/main/client/sec_index.c index 7c13e139d4..0276148b6d 100644 --- a/src/main/client/sec_index.c +++ b/src/main/client/sec_index.c @@ -225,6 +225,8 @@ PyObject *AerospikeClient_Index_Cdt_Create(AerospikeClient *self, as_cdt_ctx_destroy(&ctx); + return py_obj; + CLEANUP: if (py_obj == NULL) { PyObject *py_err = NULL; From 6224dd512675fd08abe4cbfe51aa04248b8eff89 Mon Sep 17 00:00:00 2001 From: Julian Nguyen <109386615+juliannguyen4@users.noreply.github.com> Date: Fri, 19 Sep 2025 13:32:22 -0700 Subject: [PATCH 19/20] fix max_error_rate tests. check if tests are correct or not --- test/new_tests/test_max_error_rate.py | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/test/new_tests/test_max_error_rate.py b/test/new_tests/test_max_error_rate.py index a5d0194d37..b7594ec106 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 From 48c71cd415f47b52c8f57abae8833376a689eaed Mon Sep 17 00:00:00 2001 From: Julian Nguyen <109386615+juliannguyen4@users.noreply.github.com> Date: Mon, 9 Feb 2026 08:45:47 -0800 Subject: [PATCH 20/20] WIP TODO --- test/new_tests/test_admin_change_password.py | 8 +- test/new_tests/test_admin_create_role.py | 22 +-- test/new_tests/test_admin_create_user.py | 25 +-- test/new_tests/test_admin_drop_role.py | 8 +- test/new_tests/test_admin_drop_user.py | 16 +- test/new_tests/test_admin_get_role.py | 8 +- test/new_tests/test_admin_grant_privileges.py | 13 +- test/new_tests/test_admin_grant_roles.py | 8 +- test/new_tests/test_admin_query_role.py | 8 +- test/new_tests/test_admin_query_user.py | 144 ------------------ 10 files changed, 72 insertions(+), 188 deletions(-) delete mode 100644 test/new_tests/test_admin_query_user.py diff --git a/test/new_tests/test_admin_change_password.py b/test/new_tests/test_admin_change_password.py index 85636dbbfc..2e895cbcfe 100644 --- a/test/new_tests/test_admin_change_password.py +++ b/test/new_tests/test_admin_change_password.py @@ -134,10 +134,12 @@ def test_change_password_with_non_existent_user(self): user = "readwriteuser" password = "newpassword" - with pytest.raises(aerospike.exception.InvalidUser) as excinfo: + try: self.client.admin_change_password(user, password) - assert excinfo.value.code == 60 - assert excinfo.value.msg == "AEROSPIKE_INVALID_USER" + + except aerospike.exception.InvalidUser as exception: + assert exception.code == 60 + assert exception.msg == "AEROSPIKE_INVALID_USER" def test_change_password_with_too_long_password(self): diff --git a/test/new_tests/test_admin_create_role.py b/test/new_tests/test_admin_create_role.py index 41af7b1c33..dbfab5c907 100644 --- a/test/new_tests/test_admin_create_role.py +++ b/test/new_tests/test_admin_create_role.py @@ -267,9 +267,10 @@ def test_create_role_unknown_privilege_type(self): except e.InvalidRole: pass # we are good, no such role exists - with pytest.raises(e.InvalidPrivilege) as excinfo: + try: self.client.admin_create_role("usr-sys-admin-test", [{"code": 64}]) - assert excinfo.value.code == 72 + except e.InvalidPrivilege as exception: + assert exception.code == 72 def test_create_role_incorrect_privilege_type(self): """ @@ -295,14 +296,15 @@ 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}] ) - time.sleep(1) - with pytest.raises(e.RoleExistsError) as excinfo: + try: self.client.admin_create_role( "usr-sys-admin-test", [{"code": aerospike.PRIV_USER_ADMIN}, {"code": aerospike.PRIV_SYS_ADMIN}] ) - assert excinfo.value.code == 71 - assert excinfo.value.msg == "AEROSPIKE_ROLE_ALREADY_EXISTS" + except e.RoleExistsError as exception: + assert exception.code == 71 + assert exception.msg == "AEROSPIKE_ROLE_ALREADY_EXISTS" + time.sleep(1) status = self.client.admin_drop_role("usr-sys-admin-test") assert status == 0 @@ -358,9 +360,11 @@ def test_create_role_positive_with_too_long_role_name(self): """ role_name = "role$" * 1000 - with pytest.raises(e.InvalidRole) as excinfo: + try: self.client.admin_create_role( role_name, [{"code": aerospike.PRIV_READ, "ns": "test", "set": "demo"}] ) - assert excinfo.value.code == 70 - assert excinfo.value.msg == "AEROSPIKE_INVALID_ROLE" + + except e.InvalidRole as exception: + assert exception.code == 70 + assert exception.msg == "AEROSPIKE_INVALID_ROLE" diff --git a/test/new_tests/test_admin_create_user.py b/test/new_tests/test_admin_create_user.py index 56a577cac4..1a9c1913b6 100644 --- a/test/new_tests/test_admin_create_user.py +++ b/test/new_tests/test_admin_create_user.py @@ -155,10 +155,12 @@ def test_create_user_with_empty_username(self): password = "user3-test" roles = ["read-write"] - with pytest.raises(e.InvalidUser) as excinfo: + try: self.client.admin_create_user(user, password, roles) - assert excinfo.value.code == 60 - assert excinfo.value.msg == "AEROSPIKE_INVALID_USER" + + except e.InvalidUser as exception: + assert exception.code == 60 + assert exception.msg == "AEROSPIKE_INVALID_USER" def test_create_user_with_special_characters_in_username(self): @@ -237,12 +239,15 @@ def test_create_user_with_too_long_username(self): except Exception: pass - with pytest.raises((e.InvalidUser, e.ClientError)) as excinfo: + try: self.client.admin_create_user(user, password, roles) - if excinfo.type == e.InvalidUser: - assert excinfo.value.code == 60 - assert excinfo.value.msg == "AEROSPIKE_INVALID_USER" + except e.InvalidUser as exception: + assert exception.code == 60 + assert exception.msg == "AEROSPIKE_INVALID_USER" + + except e.ClientError: + pass def test_create_user_with_too_long_password(self): @@ -299,7 +304,7 @@ def test_create_user_with_non_user_admin_user(self): non_admin_client = None - with pytest.raises(e.RoleViolation) as excinfo: + try: # Close and reconnect with non_admin_test user non_admin_client = aerospike.client(config) non_admin_client.close() @@ -308,7 +313,9 @@ def test_create_user_with_non_user_admin_user(self): if non_admin_client: non_admin_client.close() - assert excinfo.value.code == 81 + + except e.RoleViolation as exception: + assert exception.code == 81 self.delete_users.append("non_admin_test") diff --git a/test/new_tests/test_admin_drop_role.py b/test/new_tests/test_admin_drop_role.py index fbfaeef394..ab1a1d6197 100644 --- a/test/new_tests/test_admin_drop_role.py +++ b/test/new_tests/test_admin_drop_role.py @@ -125,10 +125,12 @@ def test_drop_non_existent_role(self): """ Drop non-existent role """ - with pytest.raises(e.InvalidRole) as excinfo: + try: self.client.admin_drop_role("usr-sys-admin-test") - assert excinfo.value.code == 70 - assert excinfo.value.msg == "AEROSPIKE_INVALID_ROLE" + + except e.InvalidRole as exception: + assert exception.code == 70 + assert exception.msg == "AEROSPIKE_INVALID_ROLE" def test_drop_role_rolename_None(self): """ diff --git a/test/new_tests/test_admin_drop_user.py b/test/new_tests/test_admin_drop_user.py index 6c3e4a47fc..3798fe3c71 100644 --- a/test/new_tests/test_admin_drop_user.py +++ b/test/new_tests/test_admin_drop_user.py @@ -149,10 +149,12 @@ def test_drop_user_negative(self): assert exception.code == 60 assert exception.msg == "AEROSPIKE_INVALID_USER" - with pytest.raises(e.InvalidUser) as excinfo: + try: self.client.admin_drop_user(user) - assert excinfo.value.code == 60 - assert excinfo.value.msg == "AEROSPIKE_INVALID_USER" + + except e.InvalidUser as exception: + assert exception.code == 60 + assert exception.msg == "AEROSPIKE_INVALID_USER" def test_drop_user_policy_incorrect(self): """ @@ -201,10 +203,12 @@ def test_drop_user_with_too_long_username(self): assert exception.code == 60 assert exception.msg == "AEROSPIKE_INVALID_USER" - with pytest.raises(e.InvalidUser) as excinfo: + try: self.client.admin_drop_user(user) - assert excinfo.value.code == 60 - assert excinfo.value.msg == "AEROSPIKE_INVALID_USER" + + except e.InvalidUser as exception: + assert exception.code == 60 + assert exception.msg == "AEROSPIKE_INVALID_USER" def test_drop_user_with_special_characters_in_username(self): diff --git a/test/new_tests/test_admin_get_role.py b/test/new_tests/test_admin_get_role.py index 949b0c58ba..d20240e13b 100644 --- a/test/new_tests/test_admin_get_role.py +++ b/test/new_tests/test_admin_get_role.py @@ -81,10 +81,12 @@ def test_admin_get_role_incorrect_role_name(self): """ Incorrect role name """ - with pytest.raises(e.InvalidRole) as excinfo: + try: self.client.admin_get_role("usr-sys-admin-test-non-existent") - assert excinfo.value.code == 70 - assert excinfo.value.msg == "AEROSPIKE_INVALID_ROLE" + + except e.InvalidRole as exception: + assert exception.code == 70 + assert exception.msg == "AEROSPIKE_INVALID_ROLE" def test_admin_get_role_incorrect_role_type(self): """ diff --git a/test/new_tests/test_admin_grant_privileges.py b/test/new_tests/test_admin_grant_privileges.py index 70b88e2158..e2bd944577 100644 --- a/test/new_tests/test_admin_grant_privileges.py +++ b/test/new_tests/test_admin_grant_privileges.py @@ -176,9 +176,10 @@ def test_grant_privileges_unknown_privilege_type(self): """ privilege type unknown """ - with pytest.raises(e.InvalidPrivilege) as excinfo: + try: self.client.admin_grant_privileges("usr-sys-admin-test", [{"code": 64}]) - assert excinfo.value.code == 72 + except e.InvalidPrivilege as exception: + assert exception.code == 72 def test_grant_privileges_incorrect_privilege_type(self): """ @@ -193,7 +194,9 @@ def test_grant_privileges_empty_list_privileges(self): """ privilege type is an empty list """ - with pytest.raises(e.InvalidPrivilege) as excinfo: + try: self.client.admin_grant_privileges("usr-sys-admin-test", []) - assert excinfo.value.code == 72 - assert excinfo.value.msg == "AEROSPIKE_INVALID_PRIVILEGE" + + except e.InvalidPrivilege as exception: + assert exception.code == 72 + assert exception.msg == "AEROSPIKE_INVALID_PRIVILEGE" diff --git a/test/new_tests/test_admin_grant_roles.py b/test/new_tests/test_admin_grant_roles.py index 3a4653075b..75114d953c 100644 --- a/test/new_tests/test_admin_grant_roles.py +++ b/test/new_tests/test_admin_grant_roles.py @@ -123,10 +123,12 @@ def test_grant_roles_with_empty_username(self): user = "" roles = ["read-write"] - with pytest.raises(e.InvalidUser) as excinfo: + try: self.client.admin_grant_roles(user, roles) - assert excinfo.value.code == 60 - assert excinfo.value.msg == "AEROSPIKE_INVALID_USER" + + except e.InvalidUser as exception: + assert exception.code == 60 + assert exception.msg == "AEROSPIKE_INVALID_USER" def test_grant_roles_with_special_characters_in_username(self): diff --git a/test/new_tests/test_admin_query_role.py b/test/new_tests/test_admin_query_role.py index 3dc72fefb1..a4524716a3 100644 --- a/test/new_tests/test_admin_query_role.py +++ b/test/new_tests/test_admin_query_role.py @@ -71,10 +71,12 @@ def test_admin_query_role_incorrect_role_name(self): """ Incorrect role name """ - with pytest.raises(e.InvalidRole) as excinfo: + try: self.client.admin_query_role("usr-sys-admin-test-non-existent") - assert excinfo.value.code == 70 - assert excinfo.value.msg == "AEROSPIKE_INVALID_ROLE" + + except e.InvalidRole as exception: + assert exception.code == 70 + assert exception.msg == "AEROSPIKE_INVALID_ROLE" def test_admin_query_role_incorrect_role_type(self): """ diff --git a/test/new_tests/test_admin_query_user.py b/test/new_tests/test_admin_query_user.py deleted file mode 100644 index d191e90d9f..0000000000 --- a/test/new_tests/test_admin_query_user.py +++ /dev/null @@ -1,144 +0,0 @@ -# -*- 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_user() is deprecated") -class TestQueryUser(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() - TestQueryUser.Me = self - self.client = aerospike.client(config).connect(config["user"], config["password"]) - try: - self.client.admin_drop_user("example-test") - time.sleep(1) - 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(1) - except e.UserExistsError: - pass - self.delete_users = [] - - def teardown_method(self, method): - """ - Teardown method - """ - - try: - self.client.admin_drop_user("example-test") - time.sleep(1) - except e.InvalidUser: - pass - - self.client.close() - - def test_query_user_without_any_parameters(self): - - with pytest.raises(TypeError): - self.client.admin_query_user() - - def test_query_user_with_proper_parameters(self): - - user = "example-test" - - time.sleep(2) - user_details = self.client.admin_query_user(user) - assert user_details == ["read", "read-write", "sys-admin"] - - def test_query_user_with_invalid_timeout_policy_value(self): - - policy = {"timeout": 0.1} - user = "example-test" - - with pytest.raises(e.ParamError) as excinfo: - self.client.admin_query_user(user, policy) - assert excinfo.value.code == -2 - assert excinfo.value.msg == "timeout is invalid" - - def test_query_user_with_proper_timeout_policy_value(self): - - policy = {"timeout": 180000} - user = "example-test" - - time.sleep(2) - user_details = self.client.admin_query_user(user, policy) - - assert user_details == ["read", "read-write", "sys-admin"] - - def test_query_user_with_none_username(self): - - user = None - - with pytest.raises(e.ParamError) as excinfo: - self.client.admin_query_user(user) - assert excinfo.value.code == -2 - assert excinfo.value.msg == "Username should be a string" - - def test_query_user_with_empty_username(self): - - user = "" - - with pytest.raises(e.ParamError) as excinfo: - self.client.admin_query_user(user) - assert excinfo.value.code == 60 - assert excinfo.value.msg == "AEROSPIKE_INVALID_USER" - - def test_query_user_with_nonexistent_username(self): - - user = "non-existent" - - with pytest.raises(e.InvalidUser) as excinfo: - self.client.admin_query_user(user) - assert excinfo.value.code == 60 - assert excinfo.value.msg == "AEROSPIKE_INVALID_USER" - - def test_query_user_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_user(user) - - assert user_details == [] - - def test_query_user_with_extra_argument(self): - """ - Invoke query_user() with extra argument. - """ - with pytest.raises(TypeError) as typeError: - self.client.admin_query_user("foo", None, "") - - assert "admin_query_user() takes at most 2 arguments (3 given)" in str(typeError.value) - - def test_query_user_with_policy_as_string(self): - """ - Invoke query_user() with policy as string - """ - policy = "" - with pytest.raises(e.AerospikeError) as typeError: - self.client.admin_query_user("foo", policy) - assert typeError.value.code == -2 - assert typeError.value.msg == "policy must be a dict"