1
1
import unittest
2
+ import tempfile
2
3
3
- from tableaudocumentapi import Workbook
4
+ import xml .etree .ElementTree as ET
5
+
6
+ from tableaudocumentapi import Workbook , Datasource , Connection
7
+
8
+ TABLEAU_93_WORKBOOK = '''<?xml version='1.0' encoding='utf-8' ?>
9
+ <workbook source-build='9.3.1 (9300.16.0510.0100)' source-platform='mac' version='9.3' xmlns:user='http://www.tableausoftware.com/xml/user'>
10
+ <datasources>
11
+ <datasource caption='xy (TestV1)' inline='true' name='sqlserver.17u3bqc16tjtxn14e2hxh19tyvpo' version='9.3'>
12
+ <connection authentication='sspi' class='sqlserver' dbname='TestV1' odbc-native-protocol='yes' one-time-sql='' server='mssql2012.test.tsi.lan' username=''>
13
+ </connection>
14
+ </datasource>
15
+ </datasources>
16
+ </workbook>'''
17
+
18
+ TABLEAU_93_TDS = '''<?xml version='1.0' encoding='utf-8' ?>
19
+ <datasource formatted-name='sqlserver.17u3bqc16tjtxn14e2hxh19tyvpo' inline='true' source-platform='mac' version='9.3' xmlns:user='http://www.tableausoftware.com/xml/user'>
20
+ <connection authentication='sspi' class='sqlserver' dbname='TestV1' odbc-native-protocol='yes' one-time-sql='' server='mssql2012.test.tsi.lan' username=''>
21
+ </connection>
22
+ </datasource>'''
23
+
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>''' )
4
25
5
26
class HelperMethodTests (unittest .TestCase ):
6
27
@@ -14,5 +35,56 @@ def test_is_valid_file_with_invalid_inputs(self):
14
35
self .assertFalse (Workbook ._is_valid_file ('file1.tds2' ))
15
36
self .assertFalse (Workbook ._is_valid_file ('file2.twb3' ))
16
37
38
+ class WorkbookModelTests (unittest .TestCase ):
39
+
40
+ 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
+
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' )
50
+
51
+ class DatasourceModelTests (unittest .TestCase ):
52
+
53
+ def setUp (self ):
54
+ self .tds_file = tempfile .NamedTemporaryFile (suffix = '.tds' )
55
+ self .tds_file .write (TABLEAU_93_TDS .encode ('utf8' ))
56
+ self .tds_file .seek (0 )
57
+
58
+ def test_can_extract_datasource_from_file (self ):
59
+ ds = Datasource .from_file (self .tds_file .name )
60
+ self .assertEqual (ds .name , 'sqlserver.17u3bqc16tjtxn14e2hxh19tyvpo' )
61
+ self .assertEqual (ds .version , '9.3' )
62
+
63
+ def test_can_extract_connection (self ):
64
+ ds = Datasource .from_file (self .tds_file .name )
65
+ 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 , '' )
69
+
70
+ class ConnectionModelTests (unittest .TestCase ):
71
+
72
+ def setUp (self ):
73
+ self .connection = TABLEAU_CONNECTION_XML
74
+
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' )
80
+
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' )
88
+
17
89
if __name__ == '__main__' :
18
90
unittest .main ()
0 commit comments