8000 Issue #96: Add port support (#97) · onware/document-api-python@1aa4fc0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1aa4fc0

Browse files
authored
Issue tableau#96: Add port support (tableau#97)
Issue tableau#96: Add port support We now support reading and setting the port attribute on connections. If you set the port attribute to None it will remove it entirely. Small change to tests so that they don't have side effects between test cases.
1 parent 12aab0e commit 1aa4fc0

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

tableaudocumentapi/connection.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,20 @@ def __init__(self, connxml):
3030
self._username = connxml.get('username')
3131
self._authentication = connxml.get('authentication')
3232
self._class = connxml.get('class')
33+
self._port = connxml.get('port', None)
3334

3435
def __repr__(self):
3536
return "'<Connection server='{}' dbname='{}' @ {}>'".format(self._server, self._dbname, hex(id(self)))
3637

3738
@classmethod
38-
def from_attributes(cls, server, dbname, username, dbclass, authentication=''):
39+
def from_attributes(cls, server, dbname, username, dbclass, port=None, authentication=''):
3940
root = ET.Element('connection', authentication=authentication)
4041
xml = cls(root)
4142
xml.server = server
4243
xml.dbname = dbname
4344
xml.username = username
4445
xml.dbclass = dbclass
46+
xml.port = port
4547

4648
return xml
4749

@@ -133,3 +135,22 @@ def dbclass(self, value):
133135

134136
self._class = value
135137
self._connectionXML.set('class', value)
138+
139+
###########
140+
# port
141+
###########
142+
@property
143+
def port(self):
144+
return self._port
145+
146+
@port.setter
147+
def port(self, value):
148+
self._port = value
149+
# If port is None we remove the element and don't write it to XML
150+
if value is None:
151+
try:
152+
del self._connectionXML.attrib['port']
153+
except KeyError:
154+
pass
155+
else:
156+
self._connectionXML.set('port', value)

test/assets/CONNECTION.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<connection authentication='sspi' class='sqlserver' dbname='TestV1' odbc-native-protocol='yes' one-time-sql='' server='mssql2012' username=''></connection>
1+
<connection authentication='sspi' class='sqlserver' dbname='TestV1' odbc-native-protocol='yes' one-time-sql='' server='mssql2012' username='' port='1433'></connection>

test/bvt.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818

1919
TABLEAU_10_TWB = os.path.join(TEST_DIR, 'assets', 'TABLEAU_10_TWB.twb')
2020

21-
TABLEAU_CONNECTION_XML = ET.parse(os.path.join(
22-
TEST_DIR, 'assets', 'CONNECTION.xml')).getroot()
21+
TABLEAU_CONNECTION_XML = os.path.join(TEST_DIR, 'assets', 'CONNECTION.xml')
2322

2423
TABLEAU_10_TWBX = os.path.join(TEST_DIR, 'assets', 'TABLEAU_10_TWBX.twbx')
2524

@@ -51,7 +50,7 @@ def test_can_extract_federated_connections(self):
5150
class ConnectionModelTests(unittest.TestCase):
5251

5352
def setUp(self):
54-
self.connection = TABLEAU_CONNECTION_XML
53+
self.connection = ET.parse(TABLEAU_CONNECTION_XML).getroot()
5554

5655
def test_can_read_attributes_from_connection(self):
5756
conn = Connection(self.connection)
@@ -60,15 +59,24 @@ def test_can_read_attributes_from_connection(self):
6059
self.assertEqual(conn.server, 'mssql2012')
6160
self.assertEqual(conn.dbclass, 'sqlserver')
6261
self.assertEqual(conn.authentication, 'sspi')
62+
self.assertEqual(conn.port, '1433')
6363

6464
def test_can_write_attributes_to_connection(self):
6565
conn = Connection(self.connection)
6666
conn.dbname = 'BubblesInMyDrink'
6767
conn.server = 'mssql2014'
6868
conn.username = 'bob'
69+
conn.port = '1337'
6970
self.assertEqual(conn.dbname, 'BubblesInMyDrink')
7071
self.assertEqual(conn.username, 'bob')
7172
self.assertEqual(conn.server, 'mssql2014')
73+
self.assertEqual(conn.port, '1337')
74+
75+
def test_can_delete_port_from_connection(self):
76+
conn = Connection(self.connection)
77+
conn.port = None
78+
self.assertEqual(conn.port, None)
79+
self.assertIsNone(conn._connectionXML.get('port'))
7280

7381
def test_bad_dbclass_rasies_attribute_error(self):
7482
conn = Connection(self.connection)
@@ -90,11 +98,13 @@ def test_can_create_datasource_from_connections(self):
9098
conn1 = Connection.from_attributes(
9199
server='a', dbname='b', username='c', dbclass='mysql', authentication='d')
92100
conn2 = Connection.from_attributes(
93-
server='1', dbname='2', username='3', dbclass='mysql', authentication='7')
101+
server='1', dbname='2', username='3', dbclass='mysql', port='1337', authentication='7')
94102
ds = Datasource.from_connections('test', connections=[conn1, conn2])
95103

96104
self.assertEqual(ds.connections[0].server, 'a')
105+
self.assertEqual(ds.connections[0].port, None)
97106
self.assertEqual(ds.connections[1].server, '1')
107+
self.assertEqual(ds.connections[1].port, '1337')
98108

99109

100110
class ConnectionParserInComplicatedWorkbooks(unittest.TestCase):

0 commit comments

Comments
 (0)
0