Skip to content

Commit

Permalink
Issue tableau#96: Add port support (tableau#97)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
t8y8 committed Oct 17, 2016
1 parent 12aab0e commit 1aa4fc0
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
23 changes: 22 additions & 1 deletion tableaudocumentapi/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,20 @@ def __init__(self, connxml):
self._username = connxml.get('username')
self._authentication = connxml.get('authentication')
self._class = connxml.get('class')
self._port = connxml.get('port', None)

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

@classmethod
def from_attributes(cls, server, dbname, username, dbclass, authentication=''):
def from_attributes(cls, server, dbname, username, dbclass, port=None, authentication=''):
root = ET.Element('connection', authentication=authentication)
xml = cls(root)
xml.server = server
xml.dbname = dbname
xml.username = username
xml.dbclass = dbclass
xml.port = port

return xml

Expand Down Expand Up @@ -133,3 +135,22 @@ def dbclass(self, value):

self._class = value
self._connectionXML.set('class', value)

###########
# port
###########
@property
def port(self):
return self._port

@port.setter
def port(self, value):
self._port = value
# If port is None we remove the element and don't write it to XML
if value is None:
try:
del self._connectionXML.attrib['port']
except KeyError:
pass
else:
self._connectionXML.set('port', value)
2 changes: 1 addition & 1 deletion test/assets/CONNECTION.xml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<connection authentication='sspi' class='sqlserver' dbname='TestV1' odbc-native-protocol='yes' one-time-sql='' server='mssql2012' username=''></connection>
<connection authentication='sspi' class='sqlserver' dbname='TestV1' odbc-native-protocol='yes' one-time-sql='' server='mssql2012' username='' port='1433'></connection>
18 changes: 14 additions & 4 deletions test/bvt.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@

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

TABLEAU_CONNECTION_XML = ET.parse(os.path.join(
TEST_DIR, 'assets', 'CONNECTION.xml')).getroot()
TABLEAU_CONNECTION_XML = os.path.join(TEST_DIR, 'assets', 'CONNECTION.xml')

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

Expand Down Expand Up @@ -51,7 +50,7 @@ def test_can_extract_federated_connections(self):
class ConnectionModelTests(unittest.TestCase):

def setUp(self):
self.connection = TABLEAU_CONNECTION_XML
self.connection = ET.parse(TABLEAU_CONNECTION_XML).getroot()

def test_can_read_attributes_from_connection(self):
conn = Connection(self.connection)
Expand All @@ -60,15 +59,24 @@ def test_can_read_attributes_from_connection(self):
self.assertEqual(conn.server, 'mssql2012')
self.assertEqual(conn.dbclass, 'sqlserver')
self.assertEqual(conn.authentication, 'sspi')
self.assertEqual(conn.port, '1433')

def test_can_write_attributes_to_connection(self):
conn = Connection(self.connection)
conn.dbname = 'BubblesInMyDrink'
conn.server = 'mssql2014'
conn.username = 'bob'
conn.port = '1337'
self.assertEqual(conn.dbname, 'BubblesInMyDrink')
self.assertEqual(conn.username, 'bob')
self.assertEqual(conn.server, 'mssql2014')
self.assertEqual(conn.port, '1337')

def test_can_delete_port_from_connection(self):
conn = Connection(self.connection)
conn.port = None
self.assertEqual(conn.port, None)
self.assertIsNone(conn._connectionXML.get('port'))

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

self.assertEqual(ds.connections[0].server, 'a')
self.assertEqual(ds.connections[0].port, None)
self.assertEqual(ds.connections[1].server, '1')
self.assertEqual(ds.connections[1].port, '1337')


class ConnectionParserInComplicatedWorkbooks(unittest.TestCase):
Expand Down

0 comments on commit 1aa4fc0

Please sign in to comment.