2121 </connection>
2222</datasource>'''
2323
24- TABLEAU_CONNECTION_XML = ET .fromstring ('''<connection authentication='sspi' class='sqlserver' dbname='TestV1' odbc-native-protocol='yes' one-time-sql='' server='mssql2012.test.tsi.lan' username=''></connection>''' )
24+ TABLEAU_CONNECTION_XML = ET .fromstring (
25+ '''<connection authentication='sspi' class='sqlserver' dbname='TestV1' odbc-native-protocol='yes' one-time-sql='' server='mssql2012.test.tsi.lan' username=''></connection>''' )
26+
2527
2628class HelperMethodTests (unittest .TestCase ):
2729
@@ -35,18 +37,26 @@ def test_is_valid_file_with_invalid_inputs(self):
3537 self .assertFalse (Workbook ._is_valid_file ('file1.tds2' ))
3638 self .assertFalse (Workbook ._is_valid_file ('file2.twb3' ))
3739
38- class WorkbookModelTests (unittest .TestCase ):
40+
41+ class ConnectionModelTests (unittest .TestCase ):
3942
4043 def setUp (self ):
41- self .workbook_file = tempfile .NamedTemporaryFile (suffix = '.twb' )
42- self .workbook_file .write (TABLEAU_93_WORKBOOK .encode ('utf8' ))
43- self .workbook_file .seek (0 )
44+ self .connection = TABLEAU_CONNECTION_XML
45+
46+ def test_can_read_attributes_from_connection (self ):
47+ conn = Connection (self .connection )
48+ self .assertEqual (conn .dbname , 'TestV1' )
49+ self .assertEqual (conn .username , '' )
50+ self .assertEqual (conn .server , 'mssql2012.test.tsi.lan' )
51+
52+ def test_can_write_attributes_to_connection (self ):
53+ conn = Connection (self .connection )
54+ conn .dbname = 'BubblesInMyDrink'
55+ conn .server = 'mssql2014.test.tsi.lan'
56+ self .assertEqual (conn .dbname , 'BubblesInMyDrink' )
57+ self .assertEqual (conn .username , '' )
58+ self .assertEqual (conn .server , 'mssql2014.test.tsi.lan' )
4459
45- def test_can_extract_datasource (self ):
46- wb = Workbook (self .workbook_file .name )
47- self .assertEqual (len (wb .datasources ), 1 )
48- self .assertIsInstance (wb .datasources [0 ], Datasource )
49- self .assertEqual (wb .datasources [0 ].name , 'sqlserver.17u3bqc16tjtxn14e2hxh19tyvpo' )
5060
5161class DatasourceModelTests (unittest .TestCase ):
5262
@@ -63,28 +73,30 @@ def test_can_extract_datasource_from_file(self):
6373 def test_can_extract_connection (self ):
6474 ds = Datasource .from_file (self .tds_file .name )
6575 self .assertIsInstance (ds .connection , Connection )
66- self .assertEqual (ds .connection .dbname , 'TestV1' )
67- self .assertEqual (ds .connection .server , 'mssql2012.test.tsi.lan' )
68- self .assertEqual (ds .connection .username , '' )
6976
70- class ConnectionModelTests (unittest .TestCase ):
77+
78+ class WorkbookModelTests (unittest .TestCase ):
7179
7280 def setUp (self ):
73- self .connection = TABLEAU_CONNECTION_XML
81+ self .workbook_file = tempfile .NamedTemporaryFile (suffix = '.twb' )
82+ self .workbook_file .write (TABLEAU_93_WORKBOOK .encode ('utf8' ))
83+ self .workbook_file .seek (0 )
7484
75- def test_can_read_attributes_from_connection (self ):
76- conn = Connection (self .connection )
77- self .assertEqual (conn .dbname , 'TestV1' )
78- self .assertEqual (conn .username , '' )
79- self .assertEqual (conn .server , 'mssql2012.test.tsi.lan' )
85+ def test_can_extract_datasource (self ):
86+ wb = Workbook (self .workbook_file .name )
87+ self .assertEqual (len (wb .datasources ), 1 )
88+ self .assertIsInstance (wb .datasources [0 ], Datasource )
89+ self .assertEqual (wb .datasources [0 ].name ,
90+ 'sqlserver.17u3bqc16tjtxn14e2hxh19tyvpo' )
91+
92+ def test_can_update_datasource_connection_and_save (self ):
93+ original_wb = Workbook (self .workbook_file .name )
94+ original_wb .datasources [0 ].connection .dbname = 'newdb.test.tsi.lan'
95+ original_wb .save ()
96+
97+ new_wb = Workbook (self .workbook_file .name )
98+ self .assertEqual (new_wb .datasources [0 ].connection .dbname , 'newdb.test.tsi.lan' )
8099
81- def test_can_write_attributes_to_connection (self ):
82- conn = Connection (self .connection )
83- conn .dbname = 'BubblesInMyDrink'
84- conn .server = 'mssql2014.test.tsi.lan'
85- self .assertEqual (conn .dbname , 'BubblesInMyDrink' )
86- self .assertEqual (conn .username , '' )
87- self .assertEqual (conn .server , 'mssql2014.test.tsi.lan' )
88100
89101if __name__ == '__main__' :
90102 unittest .main ()
0 commit comments