|
| 1 | +import unittest |
| 2 | +import io |
| 3 | +import os |
| 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( |
| 25 | + '''<connection authentication='sspi' class='sqlserver' dbname='TestV1' odbc-native-protocol='yes' one-time-sql='' server='mssql2012.test.tsi.lan' username=''></connection>''') |
| 26 | + |
| 27 | + |
| 28 | +class HelperMethodTests(unittest.TestCase): |
| 29 | + |
| 30 | + def test_is_valid_file_with_valid_inputs(self): |
| 31 | + self.assertTrue(Workbook._is_valid_file('file1.tds')) |
| 32 | + self.assertTrue(Workbook._is_valid_file('file2.twb')) |
| 33 | + self.assertTrue(Workbook._is_valid_file('tds.twb')) |
| 34 | + |
| 35 | + def test_is_valid_file_with_invalid_inputs(self): |
| 36 | + self.assertFalse(Workbook._is_valid_file('')) |
| 37 | + self.assertFalse(Workbook._is_valid_file('file1.tds2')) |
| 38 | + self.assertFalse(Workbook._is_valid_file('file2.twb3')) |
| 39 | + |
| 40 | + |
| 41 | +class ConnectionModelTests(unittest.TestCase): |
| 42 | + |
| 43 | + def setUp(self): |
| 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') |
| 59 | + |
| 60 | + |
| 61 | +class DatasourceModelTests(unittest.TestCase): |
| 62 | + |
| 63 | + def setUp(self): |
| 64 | + self.tds_file = io.FileIO('test.tds', 'w') |
| 65 | + self.tds_file.write(TABLEAU_93_TDS.encode('utf8')) |
| 66 | + self.tds_file.seek(0) |
| 67 | + |
| 68 | + def tearDown(self): |
| 69 | + self.tds_file.close() |
| 70 | + os.unlink(self.tds_file.name) |
| 71 | + |
| 72 | + def test_can_extract_datasource_from_file(self): |
| 73 | + ds = Datasource.from_file(self.tds_file.name) |
| 74 | + self.assertEqual(ds.name, 'sqlserver.17u3bqc16tjtxn14e2hxh19tyvpo') |
| 75 | + self.assertEqual(ds.version, '9.3') |
| 76 | + |
| 77 | + def test_can_extract_connection(self): |
| 78 | + ds = Datasource.from_file(self.tds_file.name) |
| 79 | + self.assertIsInstance(ds.connection, Connection) |
| 80 | + |
| 81 | + |
| 82 | +class WorkbookModelTests(unittest.TestCase): |
| 83 | + |
| 84 | + def setUp(self): |
| 85 | + self.workbook_file = io.FileIO('test.twb', 'w') |
| 86 | + self.workbook_file.write(TABLEAU_93_WORKBOOK.encode('utf8')) |
| 87 | + self.workbook_file.seek(0) |
| 88 | + |
| 89 | + def tearDown(self): |
| 90 | + self.workbook_file.close() |
| 91 | + os.unlink(self.workbook_file.name) |
| 92 | + |
| 93 | + def test_can_extract_datasource(self): |
| 94 | + wb = Workbook(self.workbook_file.name) |
| 95 | + self.assertEqual(len(wb.datasources), 1) |
| 96 | + self.assertIsInstance(wb.datasources[0], Datasource) |
| 97 | + self.assertEqual(wb.datasources[0].name, |
| 98 | + 'sqlserver.17u3bqc16tjtxn14e2hxh19tyvpo') |
| 99 | + |
| 100 | + def test_can_update_datasource_connection_and_save(self): |
| 101 | + original_wb = Workbook(self.workbook_file.name) |
| 102 | + original_wb.datasources[0].connection.dbname = 'newdb.test.tsi.lan' |
| 103 | + original_wb.save() |
| 104 | + |
| 105 | + new_wb = Workbook(self.workbook_file.name) |
| 106 | + self.assertEqual(new_wb.datasources[0].connection.dbname, 'newdb.test.tsi.lan') |
| 107 | + |
| 108 | + |
| 109 | +if __name__ == '__main__': |
| 110 | + unittest.main() |
0 commit comments