Skip to content

Add a test to verify #80 #83

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 9 commits into from
Sep 19, 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
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
setup(
name='tableaudocumentapi',
version='0.3',
author='Tableau Software',
author='Tableau',
author_email='github@tableau.com',
url='https://github.com/tableau/document-api-python',
packages=['tableaudocumentapi'],
Expand Down
2 changes: 1 addition & 1 deletion tableaudocumentapi/datasource.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
def _get_metadata_xml_for_field(root_xml, field_name):
if "'" in field_name:
field_name = sax.escape(field_name, {"'": "'"})
xpath = ".//metadata-record[@class='column'][local-name='{}']".format(field_name)
xpath = u".//metadata-record[@class='column'][local-name='{}']".format(field_name)
return root_xml.find(xpath)


Expand Down
7 changes: 6 additions & 1 deletion tableaudocumentapi/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,9 @@ def _read_description(xmldata):
if description is None:
return None

return u'{}'.format(ET.tostring(description, encoding='utf-8')) # This is necessary for py3 support
description_string = ET.tostring(description, encoding='utf-8')
# Format expects a unicode string so in Python 2 we have to do the explicit conversion
if isinstance(description_string, bytes):
description_string = description_string.decode('utf-8')

return description_string
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.test.tsi.lan' username=''></connection>
<connection authentication='sspi' class='sqlserver' dbname='TestV1' odbc-native-protocol='yes' one-time-sql='' server='mssql2012' username=''></connection>
2 changes: 1 addition & 1 deletion test/assets/TABLEAU_10_TDS.tds
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?xml version='1.0' encoding='utf-8' ?><datasource caption='xy+ (Multiple Connections)' inline='true' name='federated.1s4nxn20cywkdv13ql0yk0g1mpdx' version='10.0'><connection class='federated'><named-connections><named-connection caption='mysql55.test.tsi.lan' name='mysql.1ewmkrw0mtgsev1dnurma1blii4x'><connection class='mysql' dbname='testv1' odbc-native-protocol='yes' port='3306' server='mysql55.test.tsi.lan' source-charset='' username='test' /></named-connection><named-connection caption='mssql2012.test.tsi.lan' name='sqlserver.1erdwp01uqynlb14ul78p0haai2r'><connection authentication='sqlserver' class='sqlserver' dbname='TestV1' odbc-native-protocol='yes' one-time-sql='' server='mssql2012.test.tsi.lan' username='test' /></named-connection></named-connections></connection></datasource>
<?xml version='1.0' encoding='utf-8' ?><datasource caption='xy+ (Multiple Connections)' inline='true' name='federated.1s4nxn20cywkdv13ql0yk0g1mpdx' version='10.0'><connection class='federated'><named-connections><named-connection caption='mysql55' name='mysql.1ewmkrw0mtgsev1dnurma1blii4x'><connection class='mysql' dbname='TestV1' odbc-native-protocol='yes' port='3306' server='mysql55' source-charset='' username='test' /></named-connection><named-connection caption='mssql2012' name='sqlserver.1erdwp01uqynlb14ul78p0haai2r'><connection authentication='sqlserver' class='sqlserver' dbname='TestV1' odbc-native-protocol='yes' one-time-sql='' server='mssql2012' username='test' /></named-connection></named-connections></connection></datasource>
93 changes: 93 additions & 0 deletions test/assets/unicode.tds
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?xml version='1.0' encoding='utf-8' ?>
<datasource formatted-name='postgres.1of3kl00aoax5d1a1ejma1397430' inline='true' source-platform='mac' version='9.3' xmlns:user='http://www.tableausoftware.com/xml/user'>
<connection authentication='username-password' class='postgres' dbname='TestV1' odbc-native-protocol='yes' port='5432' server='postgres91.test.tsi.lan' username='test'>
<relation name='xy' table='[public].[xy]' type='table' />
<metadata-records>
<metadata-record class='column'>
<remote-name>a</remote-name>
<remote-type>130</remote-type>
<local-name>[a]</local-name>
<parent-name>[xy]</parent-name>
<remote-alias>a</remote-alias>
<ordinal>1</ordinal>
<local-type>string</local-type>
<aggregation>Count</aggregation>
<width>255</width>
<contains-null>true</contains-null>
<attributes>
<attribute datatype='string' name='DebugRemoteType'>&quot;SQL_WVARCHAR&quot;</attribute>
<attribute datatype='string' name='DebugWireType'>&quot;SQL_C_WCHAR&quot;</attribute>
<attribute datatype='string' name='TypeIsVarchar'>&quot;true&quot;</attribute>
</attributes>
</metadata-record>
<metadata-record class='column'>
<remote-name>Today's Date</remote-name>
<remote-type>130</remote-type>
<local-name>[Today&apos;s Date]</local-name>
<parent-name>[xy]</parent-name>
<remote-alias>a</remote-alias>
<ordinal>1</ordinal>
<local-type>string</local-type>
<aggregation>Count</aggregation>
<width>255</width>
<contains-null>true</contains-null>
<attributes>
<attribute datatype='string' name='DebugRemoteType'>&quot;SQL_WVARCHAR&quot;</attribute>
<attribute datatype='string' name='DebugWireType'>&quot;SQL_C_WCHAR&quot;</attribute>
<attribute datatype='string' name='TypeIsVarchar'>&quot;true&quot;</attribute>
</attributes>
</metadata-record>
<metadata-record class='column'>
<remote-name>x</remote-name>
<remote-type>3</remote-type>
<local-name>[x]</local-name>
<parent-name>[xy]</parent-name>
<remote-alias>x</remote-alias>
<ordinal>2</ordinal>
<local-type>integer</local-type>
<aggregation>Sum</aggregation>
<precision>10</precision>
<contains-null>true</contains-null>
<attributes>
<attribute datatype='string' name='DebugRemoteType'>&quot;SQL_INTEGER&quot;</attribute>
<attribute datatype='string' name='DebugWireType'>&quot;SQL_C_SLONG&quot;</attribute>
</attributes>
</metadata-record>
<metadata-record class='column'>
<remote-name>y</remote-name>
<remote-type>3</remote-type>
<local-name>[y]</local-name>
<parent-name>[xy]</parent-name>
<remote-alias>y</remote-alias>
<ordinal>3</ordinal>
<local-type>integer</local-type>
<aggregation>Sum</aggregation>
<precision>10</precision>
<contains-null>true</contains-null>
<attributes>
<attribute datatype='string' name='DebugRemoteType'>&quot;SQL_INTEGER&quot;</attribute>
<attribute datatype='string' name='DebugWireType'>&quot;SQL_C_SLONG&quot;</attribute>
</attributes>
</metadata-record>
</metadata-records>
</connection>
<aliases enabled='yes' />
<column datatype='integer' name='[Number of Records]' role='measure' type='quantitative' user:auto-column='numrec'>
<calculation class='tableau' formula='1' />
</column>
<column caption='A' datatype='string' name='[a]' role='dimension' type='nominal'>
<desc>
<formatted-text>
<run bold='true' fontsize='96'>año</run>
<run fontcolor='#686868'>&#10;Something will go here too, in a muted gray</run>
</formatted-text>
</desc>
</column>
<column caption='Today&apos;s Date' datatype='string' name='[Today&apos;s Date]' role='dimension' type='nominal' />
<column caption='X' datatype='integer' name='[x]' role='measure' type='ordinal' />
<column caption='Y' datatype='integer' name='[y]' role='measure' type='quantitative' />
<layout dim-ordering='alphabetic' dim-percentage='0.5' measure-ordering='alphabetic' measure-percentage='0.5' show-structure='true' />
<semantic-values>
<semantic-value key='[Country].[Name]' value='&quot;United States&quot;' />
</semantic-values>
</datasource>
44 changes: 22 additions & 22 deletions test/bvt.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def test_can_extract_federated_connections(self):
connections = parser.get_connections()
self.assertIsInstance(connections, list)
self.assertIsInstance(connections[0], Connection)
self.assertEqual(connections[0].dbname, 'testv1')
self.assertEqual(connections[0].dbname, 'TestV1')


class ConnectionModelTests(unittest.TestCase):
Expand All @@ -54,18 +54,18 @@ def test_can_read_attributes_from_connection(self):
conn = Connection(self.connection)
self.assertEqual(conn.dbname, 'TestV1')
self.assertEqual(conn.username, '')
self.assertEqual(conn.server, 'mssql2012.test.tsi.lan')
self.assertEqual(conn.server, 'mssql2012')
self.assertEqual(conn.dbclass, 'sqlserver')
self.assertEqual(conn.authentication, 'sspi')

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

def test_bad_dbclass_rasies_attribute_error(self):
conn = Connection(self.connection)
Expand Down Expand Up @@ -117,15 +117,15 @@ def test_can_extract_connection(self):

def test_can_save_tds(self):
original_tds = Datasource.from_file(self.tds_file.name)
original_tds.connections[0].dbname = 'newdb.test.tsi.lan'
original_tds.connections[0].dbname = 'newdb'
original_tds.save()

new_tds = Datasource.from_file(self.tds_file.name)
self.assertEqual(new_tds.connections[0].dbname, 'newdb.test.tsi.lan')
self.assertEqual(new_tds.connections[0].dbname, 'newdb')

def test_save_has_xml_declaration(self):
original_tds = Datasource.from_file(self.tds_file.name)
original_tds.connections[0].dbname = 'newdb.test.tsi.lan'
original_tds.connections[0].dbname = 'newdb'

original_tds.save()

Expand Down Expand Up @@ -158,11 +158,11 @@ def test_can_extract_connection(self):

def test_can_save_tds(self):
original_tds = Datasource.from_file(self.tds_file.name)
original_tds.connections[0].dbname = 'newdb.test.tsi.lan'
original_tds.connections[0].dbname = 'newdb'
original_tds.save()

new_tds = Datasource.from_file(self.tds_file.name)
self.assertEqual(new_tds.connections[0].dbname, 'newdb.test.tsi.lan')
self.assertEqual(new_tds.connections[0].dbname, 'newdb')


class DatasourceModelV10TDSXTests(unittest.TestCase):
Expand All @@ -183,22 +183,22 @@ def test_can_open_tdsx(self):

def test_can_open_tdsx_and_save_changes(self):
original_tdsx = Datasource.from_file(self.tdsx_file.name)
original_tdsx.connections[0].server = 'newdb.test.tsi.lan'
original_tdsx.connections[0].server = 'newdb'
original_tdsx.save()

new_tdsx = Datasource.from_file(self.tdsx_file.name)
self.assertEqual(new_tdsx.connections[
0].server, 'newdb.test.tsi.lan')
0].server, 'newdb')

def test_can_open_tdsx_and_save_as_changes(self):
new_tdsx_filename = 'newtdsx.tdsx'
original_wb = Datasource.from_file(self.tdsx_file.name)
original_wb.connections[0].server = 'newdb.test.tsi.lan'
original_wb.connections[0].server = 'newdb'
original_wb.save_as(new_tdsx_filename)

new_wb = Datasource.from_file(new_tdsx_filename)
self.assertEqual(new_wb.connections[
0].server, 'newdb.test.tsi.lan')
0].server, 'newdb')
os.unlink(new_tdsx_filename)


Expand Down Expand Up @@ -230,12 +230,12 @@ def test_has_filename(self):

def test_can_update_datasource_connection_and_save(self):
original_wb = Workbook(self.workbook_file.name)
original_wb.datasources[0].connections[0].dbname = 'newdb.test.tsi.lan'
original_wb.datasources[0].connections[0].dbname = 'newdb'
original_wb.save()

new_wb = Workbook(self.workbook_file.name)
self.assertEqual(new_wb.datasources[0].connections[
0].dbname, 'newdb.test.tsi.lan')
0].dbname, 'newdb')


class WorkbookModelV10Tests(unittest.TestCase):
Expand All @@ -260,17 +260,17 @@ def test_can_extract_datasourceV10(self):

def test_can_update_datasource_connection_and_saveV10(self):
original_wb = Workbook(self.workbook_file.name)
original_wb.datasources[0].connections[0].dbname = 'newdb.test.tsi.lan'
original_wb.datasources[0].connections[0].dbname = 'newdb'

original_wb.save()

new_wb = Workbook(self.workbook_file.name)
self.assertEqual(new_wb.datasources[0].connections[
0].dbname, 'newdb.test.tsi.lan')
0].dbname, 'newdb')

def test_save_has_xml_declaration(self):
original_wb = Workbook(self.workbook_file.name)
original_wb.datasources[0].connections[0].dbname = 'newdb.test.tsi.lan'
original_wb.datasources[0].connections[0].dbname = 'newdb'

original_wb.save()

Expand Down Expand Up @@ -298,22 +298,22 @@ def test_can_open_twbx(self):

def test_can_open_twbx_and_save_changes(self):
original_wb = Workbook(self.workbook_file.name)
original_wb.datasources[0].connections[0].server = 'newdb.test.tsi.lan'
original_wb.datasources[0].connections[0].server = 'newdb'
original_wb.save()

new_wb = Workbook(self.workbook_file.name)
self.assertEqual(new_wb.datasources[0].connections[
0].server, 'newdb.test.tsi.lan')
0].server, 'newdb')

def test_can_open_twbx_and_save_as_changes(self):
new_twbx_filename = 'newtwbx.twbx'
original_wb = Workbook(self.workbook_file.name)
original_wb.datasources[0].connections[0].server = 'newdb.test.tsi.lan'
original_wb.datasources[0].connections[0].server = 'newdb'
original_wb.save_as(new_twbx_filename)

new_wb = Workbook(new_twbx_filename)
self.assertEqual(new_wb.datasources[0].connections[
0].server, 'newdb.test.tsi.lan')
0].server, 'newdb')

os.unlink(new_twbx_filename)

Expand Down
10 changes: 10 additions & 0 deletions test/test_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
TEST_ASSET_DIR,
'datasource_test.tds'
)
TEST_UNICODE_FILE = os.path.join(
TEST_ASSET_DIR,
'unicode.tds'
)


class FieldsUnitTest(unittest.TestCase):
Expand All @@ -27,3 +31,9 @@ def find(self, *args, **kwargs):

def test_find_metadata_record_returns_none(self):
self.assertIsNone(_find_metadata_record(self.MockXmlWithNoFind(), 'foo'))


class FieldsHandleUnicode(unittest.TestCase):
def test_description_unicode(self):
ds = Datasource.from_file(TEST_UNICODE_FILE)
self.assertIsNotNone(ds.fields['A'].description)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy