Skip to content

Commit e39dbcb

Browse files
authored
fix: virtual connection ConnectionItem attributes (#1566)
Closes #1558 Connection XML element for VirtualConnections has different attribute keys compared to connection XML elements when returned by Datasources, Workbooks, and Flows. This PR adds in flexibility to ConnectionItem's reading of XML to account for both sets of attributes that may be present elements. Co-authored-by: Jordan Woods <[email protected]>
1 parent f17e8e7 commit e39dbcb

File tree

3 files changed

+32
-20
lines changed

3 files changed

+32
-20
lines changed

tableauserverclient/models/connection_item.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,11 @@ def from_response(cls, resp, ns) -> list["ConnectionItem"]:
103103
all_connection_xml = parsed_response.findall(".//t:connection", namespaces=ns)
104104
for connection_xml in all_connection_xml:
105105
connection_item = cls()
106-
connection_item._id = connection_xml.get("id", None)
106+
connection_item._id = connection_xml.get("id", connection_xml.get("connectionId", None))
107107
connection_item._connection_type = connection_xml.get("type", connection_xml.get("dbClass", None))
108108
connection_item.embed_password = string_to_bool(connection_xml.get("embedPassword", ""))
109-
connection_item.server_address = connection_xml.get("serverAddress", None)
110-
connection_item.server_port = connection_xml.get("serverPort", None)
109+
connection_item.server_address = connection_xml.get("serverAddress", connection_xml.get("server", None))
110+
connection_item.server_port = connection_xml.get("serverPort", connection_xml.get("port", None))
111111
connection_item.username = connection_xml.get("userName", None)
112112
connection_item._query_tagging = (
113113
string_to_bool(s) if (s := connection_xml.get("queryTagging", None)) else None
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<tsResponse xmlns="http://tableau.com/api" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://tableau.com/api http://tableau.com/api/ts-api-2.3.xsd">
3+
<virtualConnectionConnections>
4+
<connection connectionId="37ca6ced-58d7-4dcf-99dc-f0a85223cbef" dbClass="postgres" server="localhost" userName="pgadmin" port="5432"/>
5+
</virtualConnectionConnections>
6+
</tsResponse>

test/test_virtual_connection.py

+23-17
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from pathlib import Path
33
import unittest
44

5+
import pytest
56
import requests_mock
67

78
import tableauserverclient as TSC
@@ -12,6 +13,7 @@
1213

1314
VIRTUAL_CONNECTION_GET_XML = ASSET_DIR / "virtual_connections_get.xml"
1415
VIRTUAL_CONNECTION_POPULATE_CONNECTIONS = ASSET_DIR / "virtual_connection_populate_connections.xml"
16+
VIRTUAL_CONNECTION_POPULATE_CONNECTIONS2 = ASSET_DIR / "virtual_connection_populate_connections2.xml"
1517
VC_DB_CONN_UPDATE = ASSET_DIR / "virtual_connection_database_connection_update.xml"
1618
VIRTUAL_CONNECTION_DOWNLOAD = ASSET_DIR / "virtual_connections_download.xml"
1719
VIRTUAL_CONNECTION_UPDATE = ASSET_DIR / "virtual_connections_update.xml"
@@ -54,23 +56,27 @@ def test_virtual_connection_get(self):
5456
assert items[0].name == "vconn"
5557

5658
def test_virtual_connection_populate_connections(self):
57-
vconn = VirtualConnectionItem("vconn")
58-
vconn._id = "8fd7cc02-bb55-4d15-b8b1-9650239efe79"
59-
with requests_mock.mock() as m:
60-
m.get(f"{self.baseurl}/{vconn.id}/connections", text=VIRTUAL_CONNECTION_POPULATE_CONNECTIONS.read_text())
61-
vc_out = self.server.virtual_connections.populate_connections(vconn)
62-
connection_list = list(vconn.connections)
63-
64-
assert vc_out is vconn
65-
assert vc_out._connections is not None
66-
67-
assert len(connection_list) == 1
68-
connection = connection_list[0]
69-
assert connection.id == "37ca6ced-58d7-4dcf-99dc-f0a85223cbef"
70-
assert connection.connection_type == "postgres"
71-
assert connection.server_address == "localhost"
72-
assert connection.server_port == "5432"
73-
assert connection.username == "pgadmin"
59+
for i, populate_connections_xml in enumerate(
60+
(VIRTUAL_CONNECTION_POPULATE_CONNECTIONS, VIRTUAL_CONNECTION_POPULATE_CONNECTIONS2)
61+
):
62+
with self.subTest(i):
63+
vconn = VirtualConnectionItem("vconn")
64+
vconn._id = "8fd7cc02-bb55-4d15-b8b1-9650239efe79"
65+
with requests_mock.mock() as m:
66+
m.get(f"{self.baseurl}/{vconn.id}/connections", text=populate_connections_xml.read_text())
67+
vc_out = self.server.virtual_connections.populate_connections(vconn)
68+
connection_list = list(vconn.connections)
69+
70+
assert vc_out is vconn
71+
assert vc_out._connections is not None
72+
73+
assert len(connection_list) == 1
74+
connection = connection_list[0]
75+
assert connection.id == "37ca6ced-58d7-4dcf-99dc-f0a85223cbef"
76+
assert connection.connection_type == "postgres"
77+
assert connection.server_address == "localhost"
78+
assert connection.server_port == "5432"
79+
assert connection.username == "pgadmin"
7480

7581
def test_virtual_connection_update_connection_db_connection(self):
7682
vconn = VirtualConnectionItem("vconn")

0 commit comments

Comments
 (0)