From 006916f90d459b0c932bd89f94f075f25a44bbb3 Mon Sep 17 00:00:00 2001 From: Mohit Date: Sat, 30 Sep 2023 20:12:28 +0530 Subject: [PATCH 1/7] added mocked tests for databases - 4 --- tests/mocked/test_databases.py | 209 +++++++++++++++++++++++++++++++++ 1 file changed, 209 insertions(+) diff --git a/tests/mocked/test_databases.py b/tests/mocked/test_databases.py index 76375166..79f77e46 100644 --- a/tests/mocked/test_databases.py +++ b/tests/mocked/test_databases.py @@ -64,3 +64,212 @@ def test_databases_promote_replica(mock_client: Client, mock_client_url): resp = mock_client.databases.promote_replica(cluster_uuid, replica_name) assert resp is None + + +@responses.activate +def test_databases_patch_config(mock_client: Client, mock_client_url): + """Mocks the databases patch config operation.""" + + cluster_uuid = "9cc10173-e9ea-4176-9dbc-a4cee4c4ff30" + + responses.add( + responses.PATCH, + f"{mock_client_url}/v2/databases/{cluster_uuid}/config", + status = 200, + ) + + resp = mock_client.databases.patch_config(cluster_uuid,{"config": {}}) + + assert resp is None + + +@responses.activate +def test_databases_reset_auth(mock_client: Client, mock_client_url): + """Mocks the databases patch config operation.""" + + expected = { + "user": { + "name": "app-01", + "role": "normal", + "password": "jge5lfxtzhx42iff" + } + } + cluster_uuid = "9cc10173-e9ea-4176-9dbc-a4cee4c4ff30" + user_name = "app-01" + + responses.add( + responses.POST, + f"{mock_client_url}/v2/databases/{cluster_uuid}/users/{user_name}/reset_auth", + json = expected, + status = 200, + ) + + resp = mock_client.databases.reset_auth(cluster_uuid, user_name,{"mysql_settings": {"auth_plugin": "caching_sha2_password"}}) + + assert expected == resp + + user_name = "app-02" + expected = { + "user": { + "name": "app-02", + "role": "normal", + "password": "wv78n3zpz42xezdk", + "mysql_settings": { + "auth_plugin": "mysql_native_password" + } + } + } + + responses.add( + responses.POST, + f"{mock_client_url}/v2/databases/{cluster_uuid}/users/{user_name}/reset_auth", + json = expected, + status = 200, + ) + + resp = mock_client.databases.reset_auth(cluster_uuid,user_name,{"mysql_settings": {"auth_plugin": "caching_sha2_password"}}) + + assert expected == resp + + +@responses.activate +def test_databases_update_cluster_size(mock_client: Client, mock_client_url): + """Mocks the databases patch config operation.""" + + + cluster_uuid = "9cc10173-e9ea-4176-9dbc-a4cee4c4ff30" + + responses.add( + responses.PUT, + f"{mock_client_url}/v2/databases/{cluster_uuid}/resize", + status = 202, + ) + + resp = mock_client.databases.update_cluster_size(cluster_uuid,{"size": "db-s-4vcpu-8gb","num_nodes": 3}) + + assert resp is None + + + +@responses.activate +def test_databases_update_firewall_rules(mock_client: Client, mock_client_url): + """Mocks the databases update firewall rules operation.""" + + cluster_uuid = "9cc10173-e9ea-4176-9dbc-a4cee4c4ff30" + + responses.add( + responses.PUT, + f"{mock_client_url}/v2/databases/{cluster_uuid}/firewall", + status = 204, + ) + + resp = mock_client.databases.update_firewall_rules( + cluster_uuid, { + "rules": [ + { + "type": "ip_addr", + "value": "192.168.1.1" + }, + { + "type": "k8s", + "value": "ff2a6c52-5a44-4b63-b99c-0e98e7a63d61" + }, + { + "type": "droplet", + "value": "163973392" + }, + { + "type": "tag", + "value": "backend" + } + ] + }) + + assert resp is None + + +@responses.activate +def test_databases_update_maintenance_window(mock_client: Client, mock_client_url): + """Mocks the databases update firewall rules operation.""" + + cluster_uuid = "9cc10173-e9ea-4176-9dbc-a4cee4c4ff30" + + responses.add( + responses.PUT, + f"{mock_client_url}/v2/databases/{cluster_uuid}/maintenance", + status = 204, + ) + + resp = mock_client.databases.update_maintenance_window(cluster_uuid, {"day": "tuesday","hour": "14:00"}) + + assert resp is None + +@responses.activate +def test_databases_update_online_migration(mock_client: Client, mock_client_url): + """Mocks the databases update firewall rules operation.""" + + expected = { + "id": "77b28fc8-19ff-11eb-8c9c-c68e24557488", + "status": "running", + "created_at": "2020-10-29T15:57:38Z" + } + + cluster_uuid = "9cc10173-e9ea-4176-9dbc-a4cee4c4ff30" + + responses.add( + responses.PUT, + f"{mock_client_url}/v2/databases/{cluster_uuid}/online-migration", + json = expected, + status = 200, + ) + + resp = mock_client.databases.update_online_migration( + cluster_uuid, + { + "source": { + "host": "source-do-user-6607903-0.b.db.ondigitalocean.com", + "dbname": "defaultdb", + "port": 25060, + "username": "doadmin", + "password": "paakjnfe10rsrsmf" + }, + "disable_ssl": False + } + ) + + assert expected == resp + + +@responses.activate +def test_databases_update_region(mock_client: Client, mock_client_url): + """Mocks the databases update firewall rules operation.""" + + cluster_uuid = "9cc10173-e9ea-4176-9dbc-a4cee4c4ff30" + + responses.add( + responses.PUT, + f"{mock_client_url}/v2/databases/{cluster_uuid}/migrate", + status = 202, + ) + + resp = mock_client.databases.update_region(cluster_uuid, {"region": "lon1"}) + + assert resp is None + + +@responses.activate +def test_databases_update_sql_mode(mock_client: Client, mock_client_url): + """Mocks the databases update firewall rules operation.""" + + cluster_uuid = "9cc10173-e9ea-4176-9dbc-a4cee4c4ff30" + + responses.add( + responses.PUT, + f"{mock_client_url}/v2/databases/{cluster_uuid}/sql_mode", + status = 204, + ) + + resp = mock_client.databases.update_sql_mode(cluster_uuid, {"sql_mode": "ANSI,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE"}) + + assert resp is None + From 96961d20138b00cd8063d48dde4e2fa48a510a40 Mon Sep 17 00:00:00 2001 From: Mohit Date: Mon, 2 Oct 2023 22:37:59 +0530 Subject: [PATCH 2/7] fix linting issues --- tests/mocked/test_databases.py | 169 ++++++++++++++++----------------- 1 file changed, 84 insertions(+), 85 deletions(-) diff --git a/tests/mocked/test_databases.py b/tests/mocked/test_databases.py index 79f77e46..285ea5a3 100644 --- a/tests/mocked/test_databases.py +++ b/tests/mocked/test_databases.py @@ -66,7 +66,7 @@ def test_databases_promote_replica(mock_client: Client, mock_client_url): assert resp is None -@responses.activate +@responses.activate def test_databases_patch_config(mock_client: Client, mock_client_url): """Mocks the databases patch config operation.""" @@ -75,83 +75,85 @@ def test_databases_patch_config(mock_client: Client, mock_client_url): responses.add( responses.PATCH, f"{mock_client_url}/v2/databases/{cluster_uuid}/config", - status = 200, + status=200, ) - resp = mock_client.databases.patch_config(cluster_uuid,{"config": {}}) - + resp = mock_client.databases.patch_config(cluster_uuid, {"config": {}}) + assert resp is None -@responses.activate +@responses.activate def test_databases_reset_auth(mock_client: Client, mock_client_url): """Mocks the databases patch config operation.""" expected = { - "user": { - "name": "app-01", - "role": "normal", - "password": "jge5lfxtzhx42iff" - } - } + "user": {"name": "app-01", "role": "normal", "password": "jge5lfxtzhx42iff"} + } cluster_uuid = "9cc10173-e9ea-4176-9dbc-a4cee4c4ff30" user_name = "app-01" responses.add( responses.POST, f"{mock_client_url}/v2/databases/{cluster_uuid}/users/{user_name}/reset_auth", - json = expected, - status = 200, + json=expected, + status=200, ) - resp = mock_client.databases.reset_auth(cluster_uuid, user_name,{"mysql_settings": {"auth_plugin": "caching_sha2_password"}}) - - assert expected == resp + resp = mock_client.databases.reset_auth( + cluster_uuid, + user_name, + {"mysql_settings": {"auth_plugin": "caching_sha2_password"}}, + ) + + assert expected == resp user_name = "app-02" expected = { - "user": { - "name": "app-02", - "role": "normal", - "password": "wv78n3zpz42xezdk", - "mysql_settings": { - "auth_plugin": "mysql_native_password" - } - } - } + "user": { + "name": "app-02", + "role": "normal", + "password": "wv78n3zpz42xezdk", + "mysql_settings": {"auth_plugin": "mysql_native_password"}, + } + } responses.add( responses.POST, f"{mock_client_url}/v2/databases/{cluster_uuid}/users/{user_name}/reset_auth", - json = expected, - status = 200, + json=expected, + status=200, + ) + + resp = mock_client.databases.reset_auth( + cluster_uuid, + user_name, + {"mysql_settings": {"auth_plugin": "caching_sha2_password"}}, ) - resp = mock_client.databases.reset_auth(cluster_uuid,user_name,{"mysql_settings": {"auth_plugin": "caching_sha2_password"}}) - assert expected == resp -@responses.activate +@responses.activate def test_databases_update_cluster_size(mock_client: Client, mock_client_url): """Mocks the databases patch config operation.""" - cluster_uuid = "9cc10173-e9ea-4176-9dbc-a4cee4c4ff30" responses.add( responses.PUT, f"{mock_client_url}/v2/databases/{cluster_uuid}/resize", - status = 202, + status=202, ) - resp = mock_client.databases.update_cluster_size(cluster_uuid,{"size": "db-s-4vcpu-8gb","num_nodes": 3}) - - assert resp is None + resp = mock_client.databases.update_cluster_size( + cluster_uuid, {"size": "db-s-4vcpu-8gb", "num_nodes": 3} + ) + assert resp is None -@responses.activate +@responses.activate def test_databases_update_firewall_rules(mock_client: Client, mock_client_url): """Mocks the databases update firewall rules operation.""" @@ -160,35 +162,25 @@ def test_databases_update_firewall_rules(mock_client: Client, mock_client_url): responses.add( responses.PUT, f"{mock_client_url}/v2/databases/{cluster_uuid}/firewall", - status = 204, + status=204, ) resp = mock_client.databases.update_firewall_rules( - cluster_uuid, { - "rules": [ - { - "type": "ip_addr", - "value": "192.168.1.1" - }, - { - "type": "k8s", - "value": "ff2a6c52-5a44-4b63-b99c-0e98e7a63d61" - }, - { - "type": "droplet", - "value": "163973392" - }, - { - "type": "tag", - "value": "backend" - } - ] - }) - - assert resp is None + cluster_uuid, + { + "rules": [ + {"type": "ip_addr", "value": "192.168.1.1"}, + {"type": "k8s", "value": "ff2a6c52-5a44-4b63-b99c-0e98e7a63d61"}, + {"type": "droplet", "value": "163973392"}, + {"type": "tag", "value": "backend"}, + ] + }, + ) + + assert resp is None -@responses.activate +@responses.activate def test_databases_update_maintenance_window(mock_client: Client, mock_client_url): """Mocks the databases update firewall rules operation.""" @@ -197,50 +189,53 @@ def test_databases_update_maintenance_window(mock_client: Client, mock_client_ur responses.add( responses.PUT, f"{mock_client_url}/v2/databases/{cluster_uuid}/maintenance", - status = 204, + status=204, ) - resp = mock_client.databases.update_maintenance_window(cluster_uuid, {"day": "tuesday","hour": "14:00"}) - - assert resp is None + resp = mock_client.databases.update_maintenance_window( + cluster_uuid, {"day": "tuesday", "hour": "14:00"} + ) -@responses.activate + assert resp is None + + +@responses.activate def test_databases_update_online_migration(mock_client: Client, mock_client_url): """Mocks the databases update firewall rules operation.""" expected = { - "id": "77b28fc8-19ff-11eb-8c9c-c68e24557488", - "status": "running", - "created_at": "2020-10-29T15:57:38Z" - } - + "id": "77b28fc8-19ff-11eb-8c9c-c68e24557488", + "status": "running", + "created_at": "2020-10-29T15:57:38Z", + } + cluster_uuid = "9cc10173-e9ea-4176-9dbc-a4cee4c4ff30" responses.add( responses.PUT, f"{mock_client_url}/v2/databases/{cluster_uuid}/online-migration", - json = expected, - status = 200, + json=expected, + status=200, ) resp = mock_client.databases.update_online_migration( cluster_uuid, { - "source": { + "source": { "host": "source-do-user-6607903-0.b.db.ondigitalocean.com", "dbname": "defaultdb", "port": 25060, "username": "doadmin", - "password": "paakjnfe10rsrsmf" + "password": "paakjnfe10rsrsmf", }, - "disable_ssl": False - } - ) - + "disable_ssl": False, + }, + ) + assert expected == resp -@responses.activate +@responses.activate def test_databases_update_region(mock_client: Client, mock_client_url): """Mocks the databases update firewall rules operation.""" @@ -249,7 +244,7 @@ def test_databases_update_region(mock_client: Client, mock_client_url): responses.add( responses.PUT, f"{mock_client_url}/v2/databases/{cluster_uuid}/migrate", - status = 202, + status=202, ) resp = mock_client.databases.update_region(cluster_uuid, {"region": "lon1"}) @@ -257,7 +252,7 @@ def test_databases_update_region(mock_client: Client, mock_client_url): assert resp is None -@responses.activate +@responses.activate def test_databases_update_sql_mode(mock_client: Client, mock_client_url): """Mocks the databases update firewall rules operation.""" @@ -266,10 +261,14 @@ def test_databases_update_sql_mode(mock_client: Client, mock_client_url): responses.add( responses.PUT, f"{mock_client_url}/v2/databases/{cluster_uuid}/sql_mode", - status = 204, + status=204, ) - resp = mock_client.databases.update_sql_mode(cluster_uuid, {"sql_mode": "ANSI,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE"}) - - assert resp is None + resp = mock_client.databases.update_sql_mode( + cluster_uuid, + { + "sql_mode": "ANSI,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE" + }, + ) + assert resp is None From a3f8785f5ba84c5d674a8a4572664dc17209edd0 Mon Sep 17 00:00:00 2001 From: Mohit Date: Tue, 3 Oct 2023 03:05:13 +0530 Subject: [PATCH 3/7] Empty-Commit From 9af24ec05234f8a8b278d74a504c14cfb23a7342 Mon Sep 17 00:00:00 2001 From: Mohit Date: Tue, 3 Oct 2023 03:06:17 +0530 Subject: [PATCH 4/7] Empty-Commit From 7301c1a9bf8556428e4fbcae4f8125774fe1c803 Mon Sep 17 00:00:00 2001 From: Mohit Date: Tue, 3 Oct 2023 03:07:00 +0530 Subject: [PATCH 5/7] Empty-Commit From e62230e01cceaf56e1f5aa303ccb741b51e517d3 Mon Sep 17 00:00:00 2001 From: Mohit Date: Tue, 3 Oct 2023 22:02:37 +0530 Subject: [PATCH 6/7] Empty-Commit From d20ff33aa5a5183bd3ec9504f0335c4a41c521da Mon Sep 17 00:00:00 2001 From: Mohit Date: Tue, 3 Oct 2023 22:34:46 +0530 Subject: [PATCH 7/7] added suggestions --- tests/mocked/test_databases.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/mocked/test_databases.py b/tests/mocked/test_databases.py index d1d776e0..79e09ec8 100644 --- a/tests/mocked/test_databases.py +++ b/tests/mocked/test_databases.py @@ -1,4 +1,5 @@ # pylint: disable=line-too-long +# pylint: disable=too-many-lines """Mock tests for the databases API resource.""" import responses