33import requests_mock
44import tableauserverclient as TSC
55from tableauserverclient .datetime_helpers import format_datetime
6+ from ._utils import read_xml_asset , read_xml_assets , asset
67
7- TEST_ASSET_DIR = os .path .join (os .path .dirname (__file__ ), 'assets' )
8-
9- ADD_TAGS_XML = os .path .join (TEST_ASSET_DIR , 'datasource_add_tags.xml' )
10- GET_XML = os .path .join (TEST_ASSET_DIR , 'datasource_get.xml' )
11- GET_EMPTY_XML = os .path .join (TEST_ASSET_DIR , 'datasource_get_empty.xml' )
12- GET_BY_ID_XML = os .path .join (TEST_ASSET_DIR , 'datasource_get_by_id.xml' )
13- POPULATE_CONNECTIONS_XML = os .path .join (TEST_ASSET_DIR , 'datasource_populate_connections.xml' )
14- PUBLISH_XML = os .path .join (TEST_ASSET_DIR , 'datasource_publish.xml' )
15- UPDATE_XML = os .path .join (TEST_ASSET_DIR , 'datasource_update.xml' )
8+ ADD_TAGS_XML = 'datasource_add_tags.xml'
9+ GET_XML = 'datasource_get.xml'
10+ GET_EMPTY_XML = 'datasource_get_empty.xml'
11+ GET_BY_ID_XML = 'datasource_get_by_id.xml'
12+ POPULATE_CONNECTIONS_XML = 'datasource_populate_connections.xml'
13+ PUBLISH_XML = 'datasource_publish.xml'
14+ UPDATE_XML = 'datasource_update.xml'
15+ UPDATE_CONNECTION_XML = 'datasource_connection_update.xml'
1616
1717
1818class DatasourceTests (unittest .TestCase ):
@@ -26,8 +26,7 @@ def setUp(self):
2626 self .baseurl = self .server .datasources .baseurl
2727
2828 def test_get (self ):
29- with open (GET_XML , 'rb' ) as f :
30- response_xml = f .read ().decode ('utf-8' )
29+ response_xml = read_xml_asset (GET_XML )
3130 with requests_mock .mock () as m :
3231 m .get (self .baseurl , text = response_xml )
3332 all_datasources , pagination_item = self .server .datasources .get ()
@@ -59,8 +58,7 @@ def test_get_before_signin(self):
5958 self .assertRaises (TSC .NotSignedInError , self .server .datasources .get )
6059
6160 def test_get_empty (self ):
62- with open (GET_EMPTY_XML , 'rb' ) as f :
63- response_xml = f .read ().decode ('utf-8' )
61+ response_xml = read_xml_asset (GET_EMPTY_XML )
6462 with requests_mock .mock () as m :
6563 m .get (self .baseurl , text = response_xml )
6664 all_datasources , pagination_item = self .server .datasources .get ()
@@ -69,8 +67,7 @@ def test_get_empty(self):
6967 self .assertEqual ([], all_datasources )
7068
7169 def test_get_by_id (self ):
72- with open (GET_BY_ID_XML , 'rb' ) as f :
73- response_xml = f .read ().decode ('utf-8' )
70+ response_xml = read_xml_asset (GET_BY_ID_XML )
7471 with requests_mock .mock () as m :
7572 m .get (self .baseurl + '/9dbd2263-16b5-46e1-9c43-a76bb8ab65fb' , text = response_xml )
7673 single_datasource = self .server .datasources .get_by_id ('9dbd2263-16b5-46e1-9c43-a76bb8ab65fb' )
@@ -87,8 +84,7 @@ def test_get_by_id(self):
8784 self .assertEqual (set (['world' , 'indicators' , 'sample' ]), single_datasource .tags )
8885
8986 def test_update (self ):
90- with open (UPDATE_XML , 'rb' ) as f :
91- response_xml = f .read ().decode ('utf-8' )
87+ response_xml = read_xml_asset (UPDATE_XML )
9288 with requests_mock .mock () as m :
9389 m .put (self .baseurl + '/9dbd2263-16b5-46e1-9c43-a76bb8ab65fb' , text = response_xml )
9490 single_datasource = TSC .DatasourceItem ('test' , '1d0304cd-3796-429f-b815-7258370b9b74' )
@@ -105,7 +101,7 @@ def test_update(self):
105101 self .assertEqual ("Warning, here be dragons." , single_datasource .certification_note )
106102
107103 def test_update_copy_fields (self ):
108- with open (UPDATE_XML , 'rb' ) as f :
104+ with open (asset ( UPDATE_XML ) , 'rb' ) as f :
109105 response_xml = f .read ().decode ('utf-8' )
110106 with requests_mock .mock () as m :
111107 m .put (self .baseurl + '/9dbd2263-16b5-46e1-9c43-a76bb8ab65fb' , text = response_xml )
@@ -118,10 +114,7 @@ def test_update_copy_fields(self):
118114 self .assertEqual (single_datasource ._project_name , updated_datasource ._project_name )
119115
120116 def test_update_tags (self ):
121- with open (ADD_TAGS_XML , 'rb' ) as f :
122- add_tags_xml = f .read ().decode ('utf-8' )
123- with open (UPDATE_XML , 'rb' ) as f :
124- update_xml = f .read ().decode ('utf-8' )
117+ add_tags_xml , update_xml = read_xml_assets (ADD_TAGS_XML , UPDATE_XML )
125118 with requests_mock .mock () as m :
126119 m .put (self .baseurl + '/9dbd2263-16b5-46e1-9c43-a76bb8ab65fb/tags' , text = add_tags_xml )
127120 m .delete (self .baseurl + '/9dbd2263-16b5-46e1-9c43-a76bb8ab65fb/tags/b' , status_code = 204 )
@@ -137,8 +130,7 @@ def test_update_tags(self):
137130 self .assertEqual (single_datasource ._initial_tags , updated_datasource ._initial_tags )
138131
139132 def test_populate_connections (self ):
140- with open (POPULATE_CONNECTIONS_XML , 'rb' ) as f :
141- response_xml = f .read ().decode ('utf-8' )
133+ response_xml = read_xml_asset (POPULATE_CONNECTIONS_XML )
142134 with requests_mock .mock () as m :
143135 m .get (self .baseurl + '/9dbd2263-16b5-46e1-9c43-a76bb8ab65fb/connections' , text = response_xml )
144136 single_datasource = TSC .DatasourceItem ('test' , '1d0304cd-3796-429f-b815-7258370b9b74' )
@@ -155,14 +147,33 @@ def test_populate_connections(self):
155147 self .assertEqual (ds2 .id , '970e24bc-e200-4841-a3e9-66e7d122d77e' )
156148 self .assertEqual (ds3 .id , '7d85b889-283b-42df-b23e-3c811e402f1f' )
157149
150+ def test_update_connection (self ):
151+ populate_xml , response_xml = read_xml_assets (POPULATE_CONNECTIONS_XML , UPDATE_CONNECTION_XML )
152+
153+ with requests_mock .mock () as m :
154+ m .get (self .baseurl + '/9dbd2263-16b5-46e1-9c43-a76bb8ab65fb/connections' , text = populate_xml )
155+ m .put (self .baseurl +
156+ '/9dbd2263-16b5-46e1-9c43-a76bb8ab65fb/connections/be786ae0-d2bf-4a4b-9b34-e2de8d2d4488' ,
157+ text = response_xml )
158+ single_datasource = TSC .DatasourceItem ('test' , '1d0304cd-3796-429f-b815-7258370b9b74' )
159+ single_datasource .owner_id = 'dd2239f6-ddf1-4107-981a-4cf94e415794'
160+ single_datasource ._id = '9dbd2263-16b5-46e1-9c43-a76bb8ab65fb'
161+ self .server .datasources .populate_connections (single_datasource )
162+
163+ connection = single_datasource .connections [0 ]
164+ connection .username = 'foo'
165+ new_connection = self .server .datasources .update_connection (single_datasource , connection )
166+ self .assertEqual (connection .id , new_connection .id )
167+ self .assertEqual (connection .connection_type , new_connection .connection_type )
168+ self .assertEqual ('foo' , new_connection .username )
169+
158170 def test_publish (self ):
159- with open (PUBLISH_XML , 'rb' ) as f :
160- response_xml = f .read ().decode ('utf-8' )
171+ response_xml = read_xml_asset (PUBLISH_XML )
161172 with requests_mock .mock () as m :
162173 m .post (self .baseurl , text = response_xml )
163174 new_datasource = TSC .DatasourceItem ('SampleDS' , 'ee8c6e70-43b6-11e6-af4f-f7b0d8e20760' )
164175 new_datasource = self .server .datasources .publish (new_datasource ,
165- os . path . join ( TEST_ASSET_DIR , 'SampleDS.tds' ),
176+ asset ( 'SampleDS.tds' ),
166177 mode = self .server .PublishMode .CreateNew )
167178
168179 self .assertEqual ('e76a1461-3b1d-4588-bf1b-17551a879ad9' , new_datasource .id )
@@ -224,9 +235,9 @@ def test_publish_missing_path(self):
224235 def test_publish_missing_mode (self ):
225236 new_datasource = TSC .DatasourceItem ('test' , 'ee8c6e70-43b6-11e6-af4f-f7b0d8e20760' )
226237 self .assertRaises (ValueError , self .server .datasources .publish , new_datasource ,
227- os . path . join ( TEST_ASSET_DIR , 'SampleDS.tds' ), None )
238+ asset ( 'SampleDS.tds' ), None )
228239
229240 def test_publish_invalid_file_type (self ):
230241 new_datasource = TSC .DatasourceItem ('test' , 'ee8c6e70-43b6-11e6-af4f-f7b0d8e20760' )
231242 self .assertRaises (ValueError , self .server .datasources .publish , new_datasource ,
232- os . path . join ( TEST_ASSET_DIR , 'SampleWB.twbx' ), self .server .PublishMode .Append )
243+ asset ( 'SampleWB.twbx' ), self .server .PublishMode .Append )
0 commit comments