Skip to content

Commit e72c643

Browse files
committed
address codeQL warnings
1 parent 4d3c836 commit e72c643

File tree

3 files changed

+193
-9
lines changed

3 files changed

+193
-9
lines changed

test/unit/groups/database_test.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import logging
12
from test.unit.base import ClientBaseCase
23

34
from linode_api4.objects import MySQLDatabase
45

6+
logger = logging.getLogger(__name__)
7+
58

69
class DatabaseTest(ClientBaseCase):
710
"""
@@ -116,8 +119,10 @@ def test_create(self):
116119
"g6-standard-1",
117120
cluster_size=3,
118121
)
119-
except Exception:
120-
pass
122+
except Exception as e:
123+
logger.warning(
124+
"An error occurred while validating the request: %s", e
125+
)
121126

122127
self.assertEqual(m.method, "post")
123128
self.assertEqual(m.call_url, "/databases/mysql/instances")
@@ -173,8 +178,10 @@ def test_create(self):
173178
"g6-standard-1",
174179
cluster_size=3,
175180
)
176-
except Exception:
177-
pass
181+
except Exception as e:
182+
logger.warning(
183+
"An error occurred while validating the request: %s", e
184+
)
178185

179186
self.assertEqual(m.method, "post")
180187
self.assertEqual(m.call_url, "/databases/postgresql/instances")

test/unit/groups/vpc_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def test_list_ips(self):
7171

7272
ip = result[0]
7373
assert ip.address == "10.0.0.2"
74-
assert ip.address_range == None
74+
assert ip.address_range is None
7575
assert ip.vpc_id == 123
7676
assert ip.subnet_id == 456
7777
assert ip.region == "us-mia"

test/unit/objects/database_test.py

Lines changed: 181 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,136 @@
1+
import logging
12
from test.unit.base import ClientBaseCase
23

34
from linode_api4 import PostgreSQLDatabase
45
from linode_api4.objects import MySQLDatabase
56

7+
logger = logging.getLogger(__name__)
8+
9+
10+
class DatabaseTest(ClientBaseCase):
11+
"""
12+
Tests methods of the DatabaseGroup class
13+
"""
14+
15+
def test_get_types(self):
16+
"""
17+
Test that database types are properly handled
18+
"""
19+
types = self.client.database.types()
20+
21+
self.assertEqual(len(types), 1)
22+
self.assertEqual(types[0].type_class, "nanode")
23+
self.assertEqual(types[0].id, "g6-nanode-1")
24+
self.assertEqual(types[0].engines.mysql[0].price.monthly, 20)
25+
26+
def test_get_engines(self):
27+
"""
28+
Test that database engines are properly handled
29+
"""
30+
engines = self.client.database.engines()
31+
32+
self.assertEqual(len(engines), 2)
33+
34+
self.assertEqual(engines[0].engine, "mysql")
35+
self.assertEqual(engines[0].id, "mysql/8.0.26")
36+
self.assertEqual(engines[0].version, "8.0.26")
37+
38+
self.assertEqual(engines[1].engine, "postgresql")
39+
self.assertEqual(engines[1].id, "postgresql/10.14")
40+
self.assertEqual(engines[1].version, "10.14")
41+
42+
def test_get_databases(self):
43+
"""
44+
Test that databases are properly handled
45+
"""
46+
dbs = self.client.database.instances()
47+
48+
self.assertEqual(len(dbs), 1)
49+
self.assertEqual(dbs[0].allow_list[1], "192.0.1.0/24")
50+
self.assertEqual(dbs[0].cluster_size, 3)
51+
self.assertEqual(dbs[0].encrypted, False)
52+
self.assertEqual(dbs[0].engine, "mysql")
53+
self.assertEqual(
54+
dbs[0].hosts.primary,
55+
"lin-123-456-mysql-mysql-primary.servers.linodedb.net",
56+
)
57+
self.assertEqual(
58+
dbs[0].hosts.secondary,
59+
"lin-123-456-mysql-primary-private.servers.linodedb.net",
60+
)
61+
self.assertEqual(dbs[0].id, 123)
62+
self.assertEqual(dbs[0].region, "us-east")
63+
self.assertEqual(dbs[0].updates.duration, 3)
64+
self.assertEqual(dbs[0].version, "8.0.26")
65+
66+
def test_database_instance(self):
67+
"""
68+
Ensures that the .instance attribute properly translates database types
69+
"""
70+
71+
dbs = self.client.database.instances()
72+
db_translated = dbs[0].instance
73+
74+
self.assertTrue(isinstance(db_translated, MySQLDatabase))
75+
self.assertEqual(db_translated.ssl_connection, True)
76+
677

778
class MySQLDatabaseTest(ClientBaseCase):
879
"""
980
Tests methods of the MySQLDatabase class
1081
"""
1182

83+
def test_get_instances(self):
84+
"""
85+
Test that database types are properly handled
86+
"""
87+
dbs = self.client.database.mysql_instances()
88+
89+
self.assertEqual(len(dbs), 1)
90+
self.assertEqual(dbs[0].allow_list[1], "192.0.1.0/24")
91+
self.assertEqual(dbs[0].cluster_size, 3)
92+
self.assertEqual(dbs[0].encrypted, False)
93+
self.assertEqual(dbs[0].engine, "mysql")
94+
self.assertEqual(
95+
dbs[0].hosts.primary,
96+
"lin-123-456-mysql-mysql-primary.servers.linodedb.net",
97+
)
98+
self.assertEqual(
99+
dbs[0].hosts.secondary,
100+
"lin-123-456-mysql-primary-private.servers.linodedb.net",
101+
)
102+
self.assertEqual(dbs[0].id, 123)
103+
self.assertEqual(dbs[0].region, "us-east")
104+
self.assertEqual(dbs[0].updates.duration, 3)
105+
self.assertEqual(dbs[0].version, "8.0.26")
106+
107+
def test_create(self):
108+
"""
109+
Test that MySQL databases can be created
110+
"""
111+
112+
with self.mock_post("/databases/mysql/instances") as m:
113+
# We don't care about errors here; we just want to
114+
# validate the request.
115+
try:
116+
self.client.database.mysql_create(
117+
"cool",
118+
"us-southeast",
119+
"mysql/8.0.26",
120+
"g6-standard-1",
121+
cluster_size=3,
122+
)
123+
except Exception:
124+
pass
125+
126+
self.assertEqual(m.method, "post")
127+
self.assertEqual(m.call_url, "/databases/mysql/instances")
128+
self.assertEqual(m.call_data["label"], "cool")
129+
self.assertEqual(m.call_data["region"], "us-southeast")
130+
self.assertEqual(m.call_data["engine"], "mysql/8.0.26")
131+
self.assertEqual(m.call_data["type"], "g6-standard-1")
132+
self.assertEqual(m.call_data["cluster_size"], 3)
133+
12134
def test_update(self):
13135
"""
14136
Test that the MySQL database can be updated
@@ -59,8 +181,10 @@ def test_create_backup(self):
59181
# validate the request.
60182
try:
61183
db.backup_create("mybackup", target="secondary")
62-
except Exception:
63-
pass
184+
except Exception as e:
185+
logger.warning(
186+
"An error occurred while validating the request: %s", e
187+
)
64188

65189
self.assertEqual(m.method, "post")
66190
self.assertEqual(
@@ -141,6 +265,57 @@ class PostgreSQLDatabaseTest(ClientBaseCase):
141265
Tests methods of the PostgreSQLDatabase class
142266
"""
143267

268+
def test_get_instances(self):
269+
"""
270+
Test that database types are properly handled
271+
"""
272+
dbs = self.client.database.postgresql_instances()
273+
274+
self.assertEqual(len(dbs), 1)
275+
self.assertEqual(dbs[0].allow_list[1], "192.0.1.0/24")
276+
self.assertEqual(dbs[0].cluster_size, 3)
277+
self.assertEqual(dbs[0].encrypted, False)
278+
self.assertEqual(dbs[0].engine, "postgresql")
279+
self.assertEqual(
280+
dbs[0].hosts.primary,
281+
"lin-0000-000-pgsql-primary.servers.linodedb.net",
282+
)
283+
self.assertEqual(
284+
dbs[0].hosts.secondary,
285+
"lin-0000-000-pgsql-primary-private.servers.linodedb.net",
286+
)
287+
self.assertEqual(dbs[0].id, 123)
288+
self.assertEqual(dbs[0].region, "us-east")
289+
self.assertEqual(dbs[0].updates.duration, 3)
290+
self.assertEqual(dbs[0].version, "13.2")
291+
292+
def test_create(self):
293+
"""
294+
Test that PostgreSQL databases can be created
295+
"""
296+
297+
with self.mock_post("/databases/postgresql/instances") as m:
298+
# We don't care about errors here; we just want to
299+
# validate the request.
300+
try:
301+
self.client.database.postgresql_create(
302+
"cool",
303+
"us-southeast",
304+
"postgresql/13.2",
305+
"g6-standard-1",
306+
cluster_size=3,
307+
)
308+
except Exception:
309+
pass
310+
311+
self.assertEqual(m.method, "post")
312+
self.assertEqual(m.call_url, "/databases/postgresql/instances")
313+
self.assertEqual(m.call_data["label"], "cool")
314+
self.assertEqual(m.call_data["region"], "us-southeast")
315+
self.assertEqual(m.call_data["engine"], "postgresql/13.2")
316+
self.assertEqual(m.call_data["type"], "g6-standard-1")
317+
self.assertEqual(m.call_data["cluster_size"], 3)
318+
144319
def test_update(self):
145320
"""
146321
Test that the PostgreSQL database can be updated
@@ -191,8 +366,10 @@ def test_create_backup(self):
191366
# validate the request.
192367
try:
193368
db.backup_create("mybackup", target="secondary")
194-
except Exception:
195-
pass
369+
except Exception as e:
370+
logger.warning(
371+
"An error occurred while validating the request: %s", e
372+
)
196373

197374
self.assertEqual(m.method, "post")
198375
self.assertEqual(

0 commit comments

Comments
 (0)