This repository was archived by the owner on Nov 2, 2020. It is now read-only.
forked from tableau/server-client-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_datasource.py
More file actions
178 lines (150 loc) · 9.87 KB
/
test_datasource.py
File metadata and controls
178 lines (150 loc) · 9.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import unittest
import os
import requests_mock
import tableauserverclient as TSC
from tableauserverclient.datetime_helpers import format_datetime
TEST_ASSET_DIR = os.path.join(os.path.dirname(__file__), 'assets')
GET_XML = os.path.join(TEST_ASSET_DIR, 'datasource_get.xml')
GET_EMPTY_XML = os.path.join(TEST_ASSET_DIR, 'datasource_get_empty.xml')
GET_BY_ID_XML = os.path.join(TEST_ASSET_DIR, 'datasource_get_by_id.xml')
UPDATE_XML = os.path.join(TEST_ASSET_DIR, 'datasource_update.xml')
PUBLISH_XML = os.path.join(TEST_ASSET_DIR, 'datasource_publish.xml')
class DatasourceTests(unittest.TestCase):
def setUp(self):
self.server = TSC.Server('http://test')
# Fake signin
self.server._site_id = 'dad65087-b08b-4603-af4e-2887b8aafc67'
self.server._auth_token = 'j80k54ll2lfMZ0tv97mlPvvSCRyD0DOM'
self.baseurl = self.server.datasources.baseurl
def test_get(self):
with open(GET_XML, 'rb') as f:
response_xml = f.read().decode('utf-8')
with requests_mock.mock() as m:
m.get(self.baseurl, text=response_xml)
all_datasources, pagination_item = self.server.datasources.get()
self.assertEqual(2, pagination_item.total_available)
self.assertEqual('e76a1461-3b1d-4588-bf1b-17551a879ad9', all_datasources[0].id)
self.assertEqual('dataengine', all_datasources[0].datasource_type)
self.assertEqual('SampleDS', all_datasources[0].content_url)
self.assertEqual('2016-08-11T21:22:40Z', format_datetime(all_datasources[0].created_at))
self.assertEqual('2016-08-11T21:34:17Z', format_datetime(all_datasources[0].updated_at))
self.assertEqual('default', all_datasources[0].project_name)
self.assertEqual('SampleDS', all_datasources[0].name)
self.assertEqual('ee8c6e70-43b6-11e6-af4f-f7b0d8e20760', all_datasources[0].project_id)
self.assertEqual('5de011f8-5aa9-4d5b-b991-f462c8dd6bb7', all_datasources[0].owner_id)
self.assertEqual('9dbd2263-16b5-46e1-9c43-a76bb8ab65fb', all_datasources[1].id)
self.assertEqual('dataengine', all_datasources[1].datasource_type)
self.assertEqual('Sampledatasource', all_datasources[1].content_url)
self.assertEqual('2016-08-04T21:31:55Z', format_datetime(all_datasources[1].created_at))
self.assertEqual('2016-08-04T21:31:55Z', format_datetime(all_datasources[1].updated_at))
self.assertEqual('default', all_datasources[1].project_name)
self.assertEqual('Sample datasource', all_datasources[1].name)
self.assertEqual('ee8c6e70-43b6-11e6-af4f-f7b0d8e20760', all_datasources[1].project_id)
self.assertEqual('5de011f8-5aa9-4d5b-b991-f462c8dd6bb7', all_datasources[1].owner_id)
self.assertEqual(set(['world', 'indicators', 'sample']), all_datasources[1].tags)
def test_get_before_signin(self):
self.server._auth_token = None
self.assertRaises(TSC.NotSignedInError, self.server.datasources.get)
def test_get_empty(self):
with open(GET_EMPTY_XML, 'rb') as f:
response_xml = f.read().decode('utf-8')
with requests_mock.mock() as m:
m.get(self.baseurl, text=response_xml)
all_datasources, pagination_item = self.server.datasources.get()
self.assertEqual(0, pagination_item.total_available)
self.assertEqual([], all_datasources)
def test_get_by_id(self):
with open(GET_BY_ID_XML, 'rb') as f:
response_xml = f.read().decode('utf-8')
with requests_mock.mock() as m:
m.get(self.baseurl + '/9dbd2263-16b5-46e1-9c43-a76bb8ab65fb', text=response_xml)
single_datasource = self.server.datasources.get_by_id('9dbd2263-16b5-46e1-9c43-a76bb8ab65fb')
self.assertEqual('9dbd2263-16b5-46e1-9c43-a76bb8ab65fb', single_datasource.id)
self.assertEqual('dataengine', single_datasource.datasource_type)
self.assertEqual('Sampledatasource', single_datasource.content_url)
self.assertEqual('2016-08-04T21:31:55Z', format_datetime(single_datasource.created_at))
self.assertEqual('2016-08-04T21:31:55Z', format_datetime(single_datasource.updated_at))
self.assertEqual('default', single_datasource.project_name)
self.assertEqual('Sample datasource', single_datasource.name)
self.assertEqual('ee8c6e70-43b6-11e6-af4f-f7b0d8e20760', single_datasource.project_id)
self.assertEqual('5de011f8-5aa9-4d5b-b991-f462c8dd6bb7', single_datasource.owner_id)
self.assertEqual(set(['world', 'indicators', 'sample']), single_datasource.tags)
def test_update(self):
with open(UPDATE_XML, 'rb') as f:
response_xml = f.read().decode('utf-8')
with requests_mock.mock() as m:
m.put(self.baseurl + '/9dbd2263-16b5-46e1-9c43-a76bb8ab65fb', text=response_xml)
single_datasource = TSC.DatasourceItem('test', '1d0304cd-3796-429f-b815-7258370b9b74')
single_datasource.owner_id = 'dd2239f6-ddf1-4107-981a-4cf94e415794'
single_datasource._id = '9dbd2263-16b5-46e1-9c43-a76bb8ab65fb'
single_datasource = self.server.datasources.update(single_datasource)
self.assertEqual('9dbd2263-16b5-46e1-9c43-a76bb8ab65fb', single_datasource.id)
self.assertEqual('1d0304cd-3796-429f-b815-7258370b9b74', single_datasource.project_id)
self.assertEqual('dd2239f6-ddf1-4107-981a-4cf94e415794', single_datasource.owner_id)
def test_update_copy_fields(self):
with open(UPDATE_XML, 'rb') as f:
response_xml = f.read().decode('utf-8')
with requests_mock.mock() as m:
m.put(self.baseurl + '/9dbd2263-16b5-46e1-9c43-a76bb8ab65fb', text=response_xml)
single_datasource = TSC.DatasourceItem('test', '1d0304cd-3796-429f-b815-7258370b9b74')
single_datasource._id = '9dbd2263-16b5-46e1-9c43-a76bb8ab65fb'
single_datasource._tags = ['a', 'b', 'c']
single_datasource._project_name = 'Tester'
updated_datasource = self.server.datasources.update(single_datasource)
self.assertEqual(single_datasource.tags, updated_datasource.tags)
self.assertEqual(single_datasource._project_name, updated_datasource._project_name)
def test_publish(self):
with open(PUBLISH_XML, 'rb') as f:
response_xml = f.read().decode('utf-8')
with requests_mock.mock() as m:
m.post(self.baseurl, text=response_xml)
new_datasource = TSC.DatasourceItem('SampleDS', 'ee8c6e70-43b6-11e6-af4f-f7b0d8e20760')
new_datasource = self.server.datasources.publish(new_datasource,
os.path.join(TEST_ASSET_DIR, 'SampleDS.tds'),
mode=self.server.PublishMode.CreateNew)
self.assertEqual('e76a1461-3b1d-4588-bf1b-17551a879ad9', new_datasource.id)
self.assertEqual('SampleDS', new_datasource.name)
self.assertEqual('SampleDS', new_datasource.content_url)
self.assertEqual('dataengine', new_datasource.datasource_type)
self.assertEqual('2016-08-11T21:22:40Z', format_datetime(new_datasource.created_at))
self.assertEqual('2016-08-17T23:37:08Z', format_datetime(new_datasource.updated_at))
self.assertEqual('ee8c6e70-43b6-11e6-af4f-f7b0d8e20760', new_datasource.project_id)
self.assertEqual('default', new_datasource.project_name)
self.assertEqual('5de011f8-5aa9-4d5b-b991-f462c8dd6bb7', new_datasource.owner_id)
def test_delete(self):
with requests_mock.mock() as m:
m.delete(self.baseurl + '/9dbd2263-16b5-46e1-9c43-a76bb8ab65fb', status_code=204)
self.server.datasources.delete('9dbd2263-16b5-46e1-9c43-a76bb8ab65fb')
def test_download(self):
with requests_mock.mock() as m:
m.get(self.baseurl + '/9dbd2263-16b5-46e1-9c43-a76bb8ab65fb/content',
headers={'Content-Disposition': 'name="tableau_datasource"; filename="Sample datasource.tds"'})
file_path = self.server.datasources.download('9dbd2263-16b5-46e1-9c43-a76bb8ab65fb')
self.assertTrue(os.path.exists(file_path))
os.remove(file_path)
def test_download_extract_only(self):
# Pretend we're 2.5 for 'extract_only'
self.server.version = "2.5"
self.baseurl = self.server.datasources.baseurl
with requests_mock.mock() as m:
m.get(self.baseurl + '/9dbd2263-16b5-46e1-9c43-a76bb8ab65fb/content?includeExtract=False',
headers={'Content-Disposition': 'name="tableau_datasource"; filename="Sample datasource.tds"'},
complete_qs=True)
file_path = self.server.datasources.download('9dbd2263-16b5-46e1-9c43-a76bb8ab65fb', no_extract=True)
self.assertTrue(os.path.exists(file_path))
os.remove(file_path)
def test_update_missing_id(self):
single_datasource = TSC.DatasourceItem('test', 'ee8c6e70-43b6-11e6-af4f-f7b0d8e20760')
self.assertRaises(TSC.MissingRequiredFieldError, self.server.datasources.update, single_datasource)
def test_publish_missing_path(self):
new_datasource = TSC.DatasourceItem('test', 'ee8c6e70-43b6-11e6-af4f-f7b0d8e20760')
self.assertRaises(IOError, self.server.datasources.publish, new_datasource,
'', self.server.PublishMode.CreateNew)
def test_publish_missing_mode(self):
new_datasource = TSC.DatasourceItem('test', 'ee8c6e70-43b6-11e6-af4f-f7b0d8e20760')
self.assertRaises(ValueError, self.server.datasources.publish, new_datasource,
os.path.join(TEST_ASSET_DIR, 'SampleDS.tds'), None)
def test_publish_invalid_file_type(self):
new_datasource = TSC.DatasourceItem('test', 'ee8c6e70-43b6-11e6-af4f-f7b0d8e20760')
self.assertRaises(ValueError, self.server.datasources.publish, new_datasource,
os.path.join(TEST_ASSET_DIR, 'SampleWB.twbx'), self.server.PublishMode.Append)