Skip to content

Commit 5efa1b6

Browse files
jorwoodsjacalata
authored andcommitted
chore: embrace pytest in test_datasource (#1648)
Co-authored-by: Jordan Woods <13803242+jorwoods@users.noreply.github.com>
1 parent 0205f17 commit 5efa1b6

File tree

1 file changed

+30
-153
lines changed

1 file changed

+30
-153
lines changed

test/test_datasource.py

Lines changed: 30 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,23 @@
1717
from tableauserverclient.server.endpoint.fileuploads_endpoint import Fileuploads
1818
from tableauserverclient.server.request_factory import RequestFactory
1919

20-
ADD_TAGS_XML = "datasource_add_tags.xml"
21-
GET_XML = "datasource_get.xml"
22-
GET_EMPTY_XML = "datasource_get_empty.xml"
23-
GET_BY_ID_XML = "datasource_get_by_id.xml"
24-
GET_XML_ALL_FIELDS = "datasource_get_all_fields.xml"
25-
POPULATE_CONNECTIONS_XML = "datasource_populate_connections.xml"
26-
POPULATE_PERMISSIONS_XML = "datasource_populate_permissions.xml"
27-
PUBLISH_XML = "datasource_publish.xml"
28-
PUBLISH_XML_ASYNC = "datasource_publish_async.xml"
29-
REFRESH_XML = "datasource_refresh.xml"
30-
REVISION_XML = "datasource_revision.xml"
31-
UPDATE_XML = "datasource_update.xml"
32-
UPDATE_HYPER_DATA_XML = "datasource_data_update.xml"
33-
UPDATE_CONNECTION_XML = "datasource_connection_update.xml"
34-
UPDATE_CONNECTIONS_XML = "datasource_connections_update.xml"
20+
TEST_ASSET_DIR = Path(__file__).parent / "assets"
21+
22+
ADD_TAGS_XML = TEST_ASSET_DIR / "datasource_add_tags.xml"
23+
GET_XML = TEST_ASSET_DIR / "datasource_get.xml"
24+
GET_EMPTY_XML = TEST_ASSET_DIR / "datasource_get_empty.xml"
25+
GET_BY_ID_XML = TEST_ASSET_DIR / "datasource_get_by_id.xml"
26+
GET_XML_ALL_FIELDS = TEST_ASSET_DIR / "datasource_get_all_fields.xml"
27+
POPULATE_CONNECTIONS_XML = TEST_ASSET_DIR / "datasource_populate_connections.xml"
28+
POPULATE_PERMISSIONS_XML = TEST_ASSET_DIR / "datasource_populate_permissions.xml"
29+
PUBLISH_XML = TEST_ASSET_DIR / "datasource_publish.xml"
30+
PUBLISH_XML_ASYNC = TEST_ASSET_DIR / "datasource_publish_async.xml"
31+
REFRESH_XML = TEST_ASSET_DIR / "datasource_refresh.xml"
32+
REVISION_XML = TEST_ASSET_DIR / "datasource_revision.xml"
33+
UPDATE_XML = TEST_ASSET_DIR / "datasource_update.xml"
34+
UPDATE_HYPER_DATA_XML = TEST_ASSET_DIR / "datasource_data_update.xml"
35+
UPDATE_CONNECTION_XML = TEST_ASSET_DIR / "datasource_connection_update.xml"
36+
UPDATE_CONNECTIONS_XML = TEST_ASSET_DIR / "datasource_connections_update.xml"
3537

3638

3739
@pytest.fixture(scope="function")
@@ -157,90 +159,19 @@ def test_update_copy_fields(server) -> None:
157159
assert single_datasource._project_name == updated_datasource._project_name
158160

159161

160-
def test_update_connections(self) -> None:
161-
populate_xml, response_xml = read_xml_assets(POPULATE_CONNECTIONS_XML, UPDATE_CONNECTIONS_XML)
162-
163-
with requests_mock.Mocker() as m:
164-
165-
datasource_id = "9dbd2263-16b5-46e1-9c43-a76bb8ab65fb"
166-
connection_luids = ["be786ae0-d2bf-4a4b-9b34-e2de8d2d4488", "a1b2c3d4-e5f6-7a8b-9c0d-123456789abc"]
167-
168-
datasource = TSC.DatasourceItem(datasource_id)
169-
datasource._id = "9dbd2263-16b5-46e1-9c43-a76bb8ab65fb"
170-
datasource.owner_id = "dd2239f6-ddf1-4107-981a-4cf94e415794"
171-
self.server.version = "3.26"
172-
173-
url = f"{self.server.baseurl}/{datasource.id}/connections"
174-
m.get(
175-
"http://test/api/3.26/sites/dad65087-b08b-4603-af4e-2887b8aafc67/datasources/9dbd2263-16b5-46e1-9c43-a76bb8ab65fb/connections",
176-
text=populate_xml,
177-
)
178-
m.put(
179-
"http://test/api/3.26/sites/dad65087-b08b-4603-af4e-2887b8aafc67/datasources/9dbd2263-16b5-46e1-9c43-a76bb8ab65fb/connections",
180-
text=response_xml,
181-
)
182-
183-
print("BASEURL:", self.server.baseurl)
184-
print("Calling PUT on:", f"{self.server.baseurl}/{datasource.id}/connections")
185-
186-
connection_items = self.server.datasources.update_connections(
187-
datasource_item=datasource,
188-
connection_luids=connection_luids,
189-
authentication_type="auth-keypair",
190-
username="testuser",
191-
password="testpass",
192-
embed_password=True,
193-
)
194-
updated_ids = [conn.id for conn in connection_items]
195-
196-
self.assertEqual(updated_ids, connection_luids)
197-
198-
def test_update_connections(self) -> None:
199-
populate_xml, response_xml = read_xml_assets(POPULATE_CONNECTIONS_XML, UPDATE_CONNECTIONS_XML)
200-
201-
with requests_mock.Mocker() as m:
202-
203-
datasource_id = "9dbd2263-16b5-46e1-9c43-a76bb8ab65fb"
204-
connection_luids = ["be786ae0-d2bf-4a4b-9b34-e2de8d2d4488", "a1b2c3d4-e5f6-7a8b-9c0d-123456789abc"]
205-
206-
datasource = TSC.DatasourceItem(datasource_id)
207-
datasource._id = "9dbd2263-16b5-46e1-9c43-a76bb8ab65fb"
208-
datasource.owner_id = "dd2239f6-ddf1-4107-981a-4cf94e415794"
209-
self.server.version = "3.26"
210-
211-
url = f"{self.server.baseurl}/{datasource.id}/connections"
212-
m.get(
213-
"http://test/api/3.26/sites/dad65087-b08b-4603-af4e-2887b8aafc67/datasources/9dbd2263-16b5-46e1-9c43-a76bb8ab65fb/connections",
214-
text=populate_xml,
215-
)
216-
m.put(
217-
"http://test/api/3.26/sites/dad65087-b08b-4603-af4e-2887b8aafc67/datasources/9dbd2263-16b5-46e1-9c43-a76bb8ab65fb/connections",
218-
text=response_xml,
219-
)
220-
221-
print("BASEURL:", self.server.baseurl)
222-
print("Calling PUT on:", f"{self.server.baseurl}/{datasource.id}/connections")
223-
224-
connection_items = self.server.datasources.update_connections(
225-
datasource_item=datasource,
226-
connection_luids=connection_luids,
227-
authentication_type="auth-keypair",
228-
username="testuser",
229-
password="testpass",
230-
embed_password=True,
231-
)
232-
updated_ids = [conn.id for conn in connection_items]
233-
234-
self.assertEqual(updated_ids, connection_luids)
235-
self.assertEqual("auth-keypair", connection_items[0].auth_type)
236-
237-
def test_populate_permissions(self) -> None:
238-
with open(asset(POPULATE_PERMISSIONS_XML), "rb") as f:
239-
response_xml = f.read().decode("utf-8")
240-
with requests_mock.mock() as m:
241-
m.get(self.baseurl + "/0448d2ed-590d-4fa0-b272-a2a8a24555b5/permissions", text=response_xml)
242-
single_datasource = TSC.DatasourceItem("1d0304cd-3796-429f-b815-7258370b9b74", "test")
243-
single_datasource._id = "0448d2ed-590d-4fa0-b272-a2a8a24555b5"
162+
def test_update_tags(server) -> None:
163+
add_tags_xml = ADD_TAGS_XML.read_text()
164+
update_xml = UPDATE_XML.read_text()
165+
with requests_mock.mock() as m:
166+
m.delete(server.datasources.baseurl + "/9dbd2263-16b5-46e1-9c43-a76bb8ab65fb/tags/b", status_code=204)
167+
m.delete(server.datasources.baseurl + "/9dbd2263-16b5-46e1-9c43-a76bb8ab65fb/tags/d", status_code=204)
168+
m.put(server.datasources.baseurl + "/9dbd2263-16b5-46e1-9c43-a76bb8ab65fb/tags", text=add_tags_xml)
169+
m.put(server.datasources.baseurl + "/9dbd2263-16b5-46e1-9c43-a76bb8ab65fb", text=update_xml)
170+
single_datasource = TSC.DatasourceItem("1d0304cd-3796-429f-b815-7258370b9b74")
171+
single_datasource._id = "9dbd2263-16b5-46e1-9c43-a76bb8ab65fb"
172+
single_datasource._initial_tags.update(["a", "b", "c", "d"])
173+
single_datasource.tags.update(["a", "c", "e"])
174+
updated_datasource = server.datasources.update(single_datasource)
244175

245176
assert single_datasource.tags == updated_datasource.tags
246177
assert single_datasource._initial_tags == updated_datasource._initial_tags
@@ -919,57 +850,3 @@ def test_get_datasource_all_fields(server) -> None:
919850
assert datasources[0].owner.last_login == parse_datetime("2025-02-04T06:39:20Z")
920851
assert datasources[0].owner.name == "bob@example.com"
921852
assert datasources[0].owner.site_role == "SiteAdministratorCreator"
922-
923-
924-
def test_update_description(server: TSC.Server) -> None:
925-
response_xml = UPDATE_XML.read_text()
926-
with requests_mock.mock() as m:
927-
m.put(server.datasources.baseurl + "/9dbd2263-16b5-46e1-9c43-a76bb8ab65fb", text=response_xml)
928-
single_datasource = TSC.DatasourceItem("1d0304cd-3796-429f-b815-7258370b9b74", "Sample datasource")
929-
single_datasource.owner_id = "dd2239f6-ddf1-4107-981a-4cf94e415794"
930-
single_datasource._content_url = "Sampledatasource"
931-
single_datasource._id = "9dbd2263-16b5-46e1-9c43-a76bb8ab65fb"
932-
single_datasource.certified = True
933-
single_datasource.certification_note = "Warning, here be dragons."
934-
single_datasource.description = "Sample description"
935-
_ = server.datasources.update(single_datasource)
936-
937-
history = m.request_history[0]
938-
body = fromstring(history.body)
939-
ds_elem = body.find(".//datasource")
940-
assert ds_elem is not None
941-
assert ds_elem.attrib["description"] == "Sample description"
942-
943-
944-
def test_publish_description(server: TSC.Server) -> None:
945-
response_xml = PUBLISH_XML.read_text()
946-
with requests_mock.mock() as m:
947-
m.post(server.datasources.baseurl, text=response_xml)
948-
single_datasource = TSC.DatasourceItem("1d0304cd-3796-429f-b815-7258370b9b74", "Sample datasource")
949-
single_datasource.owner_id = "dd2239f6-ddf1-4107-981a-4cf94e415794"
950-
single_datasource._content_url = "Sampledatasource"
951-
single_datasource._id = "9dbd2263-16b5-46e1-9c43-a76bb8ab65fb"
952-
single_datasource.certified = True
953-
single_datasource.certification_note = "Warning, here be dragons."
954-
single_datasource.description = "Sample description"
955-
_ = server.datasources.publish(single_datasource, TEST_ASSET_DIR / "SampleDS.tds", server.PublishMode.CreateNew)
956-
957-
history = m.request_history[0]
958-
boundary = history.body[: history.body.index(b"\r\n")].strip()
959-
parts = history.body.split(boundary)
960-
request_payload = next(part for part in parts if b"request_payload" in part)
961-
xml_payload = request_payload.strip().split(b"\r\n")[-1]
962-
body = fromstring(xml_payload)
963-
ds_elem = body.find(".//datasource")
964-
assert ds_elem is not None
965-
assert ds_elem.attrib["description"] == "Sample description"
966-
967-
968-
def test_get_datasource_no_owner(server: TSC.Server) -> None:
969-
with requests_mock.mock() as m:
970-
m.get(server.datasources.baseurl, text=GET_NO_OWNER.read_text())
971-
datasources, _ = server.datasources.get()
972-
973-
datasource = datasources[0]
974-
assert datasource.owner is None
975-
assert datasource.project is None

0 commit comments

Comments
 (0)