10000 Issue #96: Add port support by t8y8 · Pull Request #97 · tableau/document-api-python · GitHub
[go: up one dir, main page]

Skip to content

Issue #96: Add port support #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you remove the port if it exists and you want to remove it?

Copy link
Contributor Author
@t8y8 t8y8 Oct 17, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. I can invert the logic so that it's:

    @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:
                self._connectionXML.attrib.pop('port')
            except KeyError:
                pass
        else:
            self._connectionXML.set('port', value)

EDIT: This is an implementation that passes tests

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be good, though I prefer this semantic for deleting rather than poping

del self._connectionXML.attrib['port']

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done -- one SO post said del doesn't work, but others said it's fine and it works for me too

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
422A
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