1- import unittest
2- import io
31import os
2+ import unittest
3+
44import xml .etree .ElementTree as ET
55
66from tableaudocumentapi import Workbook , Datasource , Connection , ConnectionParser
77
8- # Disable the 120 line limit because of the embedded XML on these lines
9- # TODO: Move the XML into external files and load them when needed
8+ TEST_DIR = os .path .dirname (__file__ )
9+
10+ TABLEAU_93_TWB = os .path .join (TEST_DIR , 'assets' , 'TABLEAU_93_TWB.twb' )
1011
11- TABLEAU_93_WORKBOOK = '''<?xml version='1.0' encoding='utf-8' ?><workbook source-build='9.3.1 (9300.16.0510.0100)' source-platform='mac' version='9.3' xmlns:user='http://www.tableausoftware.com/xml/user'><datasources><datasource caption='xy (TestV1)' inline='true' name='sqlserver.17u3bqc16tjtxn14e2hxh19tyvpo' version='9.3'><connection authentication='sspi' class='sqlserver' dbname='TestV1' odbc-native-protocol='yes' one-time-sql='' server='mssql2012.test.tsi.lan' username=''></connection></datasource></datasources></workbook>''' # noqa
12+ TABLEAU_93_TDS = os . path . join ( TEST_DIR , 'assets' , 'TABLEAU_93_TDS.tds' )
1213
13- TABLEAU_93_TDS = '''<?xml version='1.0' encoding='utf-8' ?><datasource formatted-name='sqlserver.17u3bqc16tjtxn14e2hxh19tyvpo' inline='true' source-platform='mac' version='9.3' xmlns:user='http://www.tableausoftware.com/xml/user'><connection authentication='sspi' class='sqlserver' dbname='TestV1' odbc-native-protocol='yes' one-time-sql='' server='mssql2012.test.tsi.lan' username=''></connection></datasource>''' # noqa
14+ TABLEAU_10_TDS = os . path . join ( TEST_DIR , 'assets' , 'TABLEAU_10_TDS.tds' )
1415
15- TABLEAU_10_TDS = '''<?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>''' # noqa
16+ TABLEAU_10_TWB = os . path . join ( TEST_DIR , 'assets' , 'TABLEAU_10_TWB.twb' )
1617
17- TABLEAU_10_WORKBOOK = '''<?xml version='1.0' encoding='utf-8' ?><workbook source-build='0.0.0 (0000.16.0510.1300)' source-platform='mac' version='10.0' xmlns:user='http://www.tableausoftware.com/ xml/user'><datasources><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></datasources></workbook>''' # noqa
18+ TABLEAU_CONNECTION_XML = ET . parse ( os . path . join ( TEST_DIR , 'assets' , 'CONNECTION. xml' )). getroot ()
1819
19- TABLEAU_CONNECTION_XML = ET .fromstring (
20- '''<connection authentication='sspi' class='sqlserver' dbname='TestV1' odbc-native-protocol='yes' one-time-sql='' server='mssql2012.test.tsi.lan' username=''></connection>''' ) # noqa
20+ TABLEAU_10_TWBX = os .path .join (TEST_DIR , 'assets' , 'TABLEAU_10_TWBX.twbx' )
2121
2222
2323class HelperMethodTests (unittest .TestCase ):
@@ -36,14 +36,14 @@ def test_is_valid_file_with_invalid_inputs(self):
3636class ConnectionParserTests (unittest .TestCase ):
3737
3838 def test_can_extract_legacy_connection (self ):
39- parser = ConnectionParser (ET .fromstring (TABLEAU_93_TDS ), '9.2' )
39+ parser = ConnectionParser (ET .parse (TABLEAU_93_TDS ), '9.2' )
4040 connections = parser .get_connections ()
4141 self .assertIsInstance (connections , list )
4242 self .assertIsInstance (connections [0 ], Connection )
4343 self .assertEqual (connections [0 ].dbname , 'TestV1' )
4444
4545 def test_can_extract_federated_connections (self ):
46- parser = ConnectionParser (ET .fromstring (TABLEAU_10_TDS ), '10.0' )
46+ parser = ConnectionParser (ET .parse (TABLEAU_10_TDS ), '10.0' )
4747 connections = parser .get_connections ()
4848 self .assertIsInstance (connections , list )
4949 self .assertIsInstance (connections [0 ], Connection )
@@ -76,9 +76,9 @@ def test_can_write_attributes_to_connection(self):
7676class DatasourceModelTests (unittest .TestCase ):
7777
7878 def setUp (self ):
79- self . tds_file = io . FileIO ('test.tds' , 'w' )
80- self . tds_file . write (TABLEAU_93_TDS . encode ( 'utf8' ))
81- self .tds_file . seek ( 0 )
79+ with open ( TABLEAU_93_TDS , 'rb' ) as in_file , open ('test.tds' , 'wb' ) as out_file :
80+ out_file . write (in_file . read ( ))
81+ self .tds_file = out_file
8282
8383 def tearDown (self ):
8484 self .tds_file .close ()
@@ -117,9 +117,9 @@ def test_save_has_xml_declaration(self):
117117class DatasourceModelV10Tests (unittest .TestCase ):
118118
119119 def setUp (self ):
120- self . tds_file = io . FileIO ( 'test10.tds ' , 'w' )
121- self . tds_file . write (TABLEAU_10_TDS . encode ( 'utf8' ))
122- self .tds_file . seek ( 0 )
120+ with open ( TABLEAU_10_TDS , 'rb' ) as in_file , open ( 'test.twb ' , 'wb' ) as out_file :
121+ out_file . write (in_file . read ( ))
122+ self .tds_file = out_file
123123
124124 def tearDown (self ):
125125 self .tds_file .close ()
@@ -147,9 +147,9 @@ def test_can_save_tds(self):
147147class WorkbookModelTests (unittest .TestCase ):
148148
149149 def setUp (self ):
150- self . workbook_file = io . FileIO ('test.twb' , 'w' )
151- self . workbook_file . write (TABLEAU_93_WORKBOOK . encode ( 'utf8' ))
152- self .workbook_file . seek ( 0 )
150+ with open ( TABLEAU_93_TWB , 'rb' ) as in_file , open ('test.twb' , 'wb' ) as out_file :
151+ out_file . write (in_file . read ( ))
152+ self .workbook_file = out_file
153153
154154 def tearDown (self ):
155155 self .workbook_file .close ()
@@ -175,9 +175,9 @@ def test_can_update_datasource_connection_and_save(self):
175175class WorkbookModelV10Tests (unittest .TestCase ):
176176
177177 def setUp (self ):
178- self . workbook_file = io . FileIO ( 'testv10 .twb' , 'w' )
179- self . workbook_file . write (TABLEAU_10_WORKBOOK . encode ( 'utf8' ))
180- self .workbook_file . seek ( 0 )
178+ with open ( TABLEAU_10_TWB , 'rb' ) as in_file , open ( 'test .twb' , 'wb' ) as out_file :
179+ out_file . write (in_file . read ( ))
180+ self .workbook_file = out_file
181181
182182 def tearDown (self ):
183183 self .workbook_file .close ()
@@ -213,5 +213,43 @@ def test_save_has_xml_declaration(self):
213213 self .assertEqual (
214214 first_line , "<?xml version='1.0' encoding='utf-8'?>" )
215215
216+
217+ class WorkbookModelV10TWBXTests (unittest .TestCase ):
218+
219+ def setUp (self ):
220+ with open (TABLEAU_10_TWBX , 'rb' ) as in_file , open ('test.twbx' , 'wb' ) as out_file :
221+ out_file .write (in_file .read ())
222+ self .workbook_file = out_file
223+
224+ def tearDown (self ):
225+ self .workbook_file .close ()
226+ os .unlink (self .workbook_file .name )
227+
228+ def test_can_open_twbx (self ):
229+ wb = Workbook (self .workbook_file .name )
230+ self .assertTrue (wb .datasources )
231+ self .assertTrue (wb .datasources [0 ].connections )
232+
233+ def test_can_open_twbx_and_save_changes (self ):
234+ original_wb = Workbook (self .workbook_file .name )
235+ original_wb .datasources [0 ].connections [0 ].server = 'newdb.test.tsi.lan'
236+ original_wb .save ()
237+
238+ new_wb = Workbook (self .workbook_file .name )
239+ self .assertEqual (new_wb .datasources [0 ].connections [
240+ 0 ].server , 'newdb.test.tsi.lan' )
241+
242+ def test_can_open_twbx_and_save_as_changes (self ):
243+ new_twbx_filename = self .workbook_file .name + "_TEST_SAVE_AS"
244+ original_wb = Workbook (self .workbook_file .name )
245+ original_wb .datasources [0 ].connections [0 ].server = 'newdb.test.tsi.lan'
246+ original_wb .save_as (new_twbx_filename )
247+
248+ new_wb = Workbook (new_twbx_filename )
249+ self .assertEqual (new_wb .datasources [0 ].connections [
250+ 0 ].server , 'newdb.test.tsi.lan' )
251+
252+ os .unlink (new_twbx_filename )
253+
216254if __name__ == '__main__' :
217255 unittest .main ()
0 commit comments