1
- import unittest
2
- import io
3
1
import os
2
+ import unittest
3
+
4
4
import xml .etree .ElementTree as ET
5
5
6
6
from tableaudocumentapi import Workbook , Datasource , Connection , ConnectionParser
7
7
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' )
10
11
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' )
12
13
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' )
14
15
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' )
16
17
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 ()
18
19
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' )
21
21
22
22
23
23
class HelperMethodTests (unittest .TestCase ):
@@ -36,14 +36,14 @@ def test_is_valid_file_with_invalid_inputs(self):
36
36
class ConnectionParserTests (unittest .TestCase ):
37
37
38
38
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' )
40
40
connections = parser .get_connections ()
41
41
self .assertIsInstance (connections , list )
42
42
self .assertIsInstance (connections [0 ], Connection )
43
43
self .assertEqual (connections [0 ].dbname , 'TestV1' )
44
44
45
45
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' )
47
47
connections = parser .get_connections ()
48
48
self .assertIsInstance (connections , list )
49
49
self .assertIsInstance (connections [0 ], Connection )
@@ -76,9 +76,9 @@ def test_can_write_attributes_to_connection(self):
76
76
class DatasourceModelTests (unittest .TestCase ):
77
77
78
78
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
82
82
83
83
def tearDown (self ):
84
84
self .tds_file .close ()
@@ -117,9 +117,9 @@ def test_save_has_xml_declaration(self):
117
117
class DatasourceModelV10Tests (unittest .TestCase ):
118
118
119
119
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
123
123
124
124
def tearDown (self ):
125
125
self .tds_file .close ()
@@ -147,9 +147,9 @@ def test_can_save_tds(self):
147
147
class WorkbookModelTests (unittest .TestCase ):
148
148
149
149
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
153
153
154
154
def tearDown (self ):
155
155
self .workbook_file .close ()
@@ -175,9 +175,9 @@ def test_can_update_datasource_connection_and_save(self):
175
175
class WorkbookModelV10Tests (unittest .TestCase ):
176
176
177
177
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
181
181
182
182
def tearDown (self ):
183
183
self .workbook_file .close ()
@@ -213,5 +213,43 @@ def test_save_has_xml_declaration(self):
213
213
self .assertEqual (
214
214
first_line , "<?xml version='1.0' encoding='utf-8'?>" )
215
215
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
+
216
254
if __name__ == '__main__' :
217
255
unittest .main ()
0 commit comments