|
1 | | -import os |
2 | | -import unittest |
| 1 | +from pathlib import Path |
3 | 2 |
|
| 3 | +import pytest |
4 | 4 | import requests_mock |
5 | 5 |
|
6 | 6 | import tableauserverclient as TSC |
7 | 7 |
|
8 | | -TEST_ASSET_DIR = os.path.join(os.path.dirname(__file__), "assets") |
9 | | - |
10 | | -CREATE_XML = os.path.join(TEST_ASSET_DIR, "subscription_create.xml") |
11 | | -GET_XML = os.path.join(TEST_ASSET_DIR, "subscription_get.xml") |
12 | | -GET_XML_BY_ID = os.path.join(TEST_ASSET_DIR, "subscription_get_by_id.xml") |
13 | | - |
14 | | - |
15 | | -class SubscriptionTests(unittest.TestCase): |
16 | | - def setUp(self) -> None: |
17 | | - self.server = TSC.Server("http://test", False) |
18 | | - self.server.version = "2.6" |
19 | | - |
20 | | - # Fake Signin |
21 | | - self.server._site_id = "dad65087-b08b-4603-af4e-2887b8aafc67" |
22 | | - self.server._auth_token = "j80k54ll2lfMZ0tv97mlPvvSCRyD0DOM" |
23 | | - |
24 | | - self.baseurl = self.server.subscriptions.baseurl |
25 | | - |
26 | | - def test_get_subscriptions(self) -> None: |
27 | | - with open(GET_XML, "rb") as f: |
28 | | - response_xml = f.read().decode("utf-8") |
29 | | - with requests_mock.mock() as m: |
30 | | - m.get(self.baseurl, text=response_xml) |
31 | | - all_subscriptions, pagination_item = self.server.subscriptions.get() |
32 | | - |
33 | | - self.assertEqual(2, pagination_item.total_available) |
34 | | - subscription = all_subscriptions[0] |
35 | | - self.assertEqual("382e9a6e-0c08-4a95-b6c1-c14df7bac3e4", subscription.id) |
36 | | - self.assertEqual("NOT FOUND!", subscription.message) |
37 | | - self.assertTrue(subscription.attach_image) |
38 | | - self.assertFalse(subscription.attach_pdf) |
39 | | - self.assertFalse(subscription.suspended) |
40 | | - self.assertFalse(subscription.send_if_view_empty) |
41 | | - self.assertIsNone(subscription.page_orientation) |
42 | | - self.assertIsNone(subscription.page_size_option) |
43 | | - self.assertEqual("Not Found Alert", subscription.subject) |
44 | | - self.assertEqual("cdd716ca-5818-470e-8bec-086885dbadee", subscription.target.id) |
45 | | - self.assertEqual("View", subscription.target.type) |
46 | | - self.assertEqual("c0d5fc44-ad8c-4957-bec0-b70ed0f8df1e", subscription.user_id) |
47 | | - self.assertEqual("7617c389-cdca-4940-a66e-69956fcebf3e", subscription.schedule_id) |
48 | | - |
49 | | - subscription = all_subscriptions[1] |
50 | | - self.assertEqual("23cb7630-afc8-4c8e-b6cd-83ae0322ec66", subscription.id) |
51 | | - self.assertEqual("overview", subscription.message) |
52 | | - self.assertFalse(subscription.attach_image) |
53 | | - self.assertTrue(subscription.attach_pdf) |
54 | | - self.assertTrue(subscription.suspended) |
55 | | - self.assertTrue(subscription.send_if_view_empty) |
56 | | - self.assertEqual("PORTRAIT", subscription.page_orientation) |
57 | | - self.assertEqual("A5", subscription.page_size_option) |
58 | | - self.assertEqual("Last 7 Days", subscription.subject) |
59 | | - self.assertEqual("2e6b4e8f-22dd-4061-8f75-bf33703da7e5", subscription.target.id) |
60 | | - self.assertEqual("Workbook", subscription.target.type) |
61 | | - self.assertEqual("c0d5fc44-ad8c-4957-bec0-b70ed0f8df1e", subscription.user_id) |
62 | | - self.assertEqual("3407cd38-7b39-4983-86a6-67a1506a5e3f", subscription.schedule_id) |
63 | | - |
64 | | - def test_get_subscription_by_id(self) -> None: |
65 | | - with open(GET_XML_BY_ID, "rb") as f: |
66 | | - response_xml = f.read().decode("utf-8") |
67 | | - with requests_mock.mock() as m: |
68 | | - m.get(self.baseurl + "/382e9a6e-0c08-4a95-b6c1-c14df7bac3e4", text=response_xml) |
69 | | - subscription = self.server.subscriptions.get_by_id("382e9a6e-0c08-4a95-b6c1-c14df7bac3e4") |
70 | | - |
71 | | - self.assertEqual("382e9a6e-0c08-4a95-b6c1-c14df7bac3e4", subscription.id) |
72 | | - self.assertEqual("View", subscription.target.type) |
73 | | - self.assertEqual("cdd716ca-5818-470e-8bec-086885dbadee", subscription.target.id) |
74 | | - self.assertEqual("c0d5fc44-ad8c-4957-bec0-b70ed0f8df1e", subscription.user_id) |
75 | | - self.assertEqual("Not Found Alert", subscription.subject) |
76 | | - self.assertEqual("7617c389-cdca-4940-a66e-69956fcebf3e", subscription.schedule_id) |
77 | | - |
78 | | - def test_create_subscription(self) -> None: |
79 | | - with open(CREATE_XML, "rb") as f: |
80 | | - response_xml = f.read().decode("utf-8") |
81 | | - with requests_mock.mock() as m: |
82 | | - m.post(self.baseurl, text=response_xml) |
83 | | - |
84 | | - target_item = TSC.Target("960e61f2-1838-40b2-bba2-340c9492f943", "workbook") |
85 | | - new_subscription = TSC.SubscriptionItem( |
86 | | - "subject", "4906c453-d5ec-4972-9ff4-789b629bdfa2", "8d30c8de-0a5f-4bee-b266-c621b4f3eed0", target_item |
87 | | - ) |
88 | | - new_subscription = self.server.subscriptions.create(new_subscription) |
89 | | - |
90 | | - self.assertEqual("78e9318d-2d29-4d67-b60f-3f2f5fd89ecc", new_subscription.id) |
91 | | - self.assertEqual("sub_name", new_subscription.subject) |
92 | | - self.assertEqual("960e61f2-1838-40b2-bba2-340c9492f943", new_subscription.target.id) |
93 | | - self.assertEqual("Workbook", new_subscription.target.type) |
94 | | - self.assertEqual("4906c453-d5ec-4972-9ff4-789b629bdfa2", new_subscription.schedule_id) |
95 | | - self.assertEqual("8d30c8de-0a5f-4bee-b266-c621b4f3eed0", new_subscription.user_id) |
96 | | - |
97 | | - def test_delete_subscription(self) -> None: |
98 | | - with requests_mock.mock() as m: |
99 | | - m.delete(self.baseurl + "/78e9318d-2d29-4d67-b60f-3f2f5fd89ecc", status_code=204) |
100 | | - self.server.subscriptions.delete("78e9318d-2d29-4d67-b60f-3f2f5fd89ecc") |
| 8 | +TEST_ASSET_DIR = Path(__file__).parent / "assets" |
| 9 | + |
| 10 | +CREATE_XML = TEST_ASSET_DIR / "subscription_create.xml" |
| 11 | +GET_XML = TEST_ASSET_DIR / "subscription_get.xml" |
| 12 | +GET_XML_BY_ID = TEST_ASSET_DIR / "subscription_get_by_id.xml" |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture(scope="function") |
| 16 | +def server(): |
| 17 | + """Fixture to create a TSC.Server instance for testing.""" |
| 18 | + server = TSC.Server("http://test", False) |
| 19 | + |
| 20 | + # Fake signin |
| 21 | + server._site_id = "dad65087-b08b-4603-af4e-2887b8aafc67" |
| 22 | + server._auth_token = "j80k54ll2lfMZ0tv97mlPvvSCRyD0DOM" |
| 23 | + server.version = "2.6" |
| 24 | + |
| 25 | + return server |
| 26 | + |
| 27 | + |
| 28 | +def test_get_subscriptions(server: TSC.Server) -> None: |
| 29 | + response_xml = GET_XML.read_text() |
| 30 | + with requests_mock.mock() as m: |
| 31 | + m.get(server.subscriptions.baseurl, text=response_xml) |
| 32 | + all_subscriptions, pagination_item = server.subscriptions.get() |
| 33 | + |
| 34 | + assert 2 == pagination_item.total_available |
| 35 | + subscription = all_subscriptions[0] |
| 36 | + assert "382e9a6e-0c08-4a95-b6c1-c14df7bac3e4" == subscription.id |
| 37 | + assert "NOT FOUND!" == subscription.message |
| 38 | + assert subscription.attach_image is True |
| 39 | + assert subscription.attach_pdf is False |
| 40 | + assert subscription.suspended is False |
| 41 | + assert subscription.send_if_view_empty is False |
| 42 | + assert subscription.page_orientation is None |
| 43 | + assert subscription.page_size_option is None |
| 44 | + assert "Not Found Alert" == subscription.subject |
| 45 | + assert "cdd716ca-5818-470e-8bec-086885dbadee" == subscription.target.id |
| 46 | + assert "View" == subscription.target.type |
| 47 | + assert "c0d5fc44-ad8c-4957-bec0-b70ed0f8df1e" == subscription.user_id |
| 48 | + assert "7617c389-cdca-4940-a66e-69956fcebf3e" == subscription.schedule_id |
| 49 | + |
| 50 | + subscription = all_subscriptions[1] |
| 51 | + assert "23cb7630-afc8-4c8e-b6cd-83ae0322ec66" == subscription.id |
| 52 | + assert "overview" == subscription.message |
| 53 | + assert subscription.attach_image is False |
| 54 | + assert subscription.attach_pdf is True |
| 55 | + assert subscription.suspended is True |
| 56 | + assert subscription.send_if_view_empty is True |
| 57 | + assert "PORTRAIT" == subscription.page_orientation |
| 58 | + assert "A5" == subscription.page_size_option |
| 59 | + assert "Last 7 Days" == subscription.subject |
| 60 | + assert "2e6b4e8f-22dd-4061-8f75-bf33703da7e5" == subscription.target.id |
| 61 | + assert "Workbook" == subscription.target.type |
| 62 | + assert "c0d5fc44-ad8c-4957-bec0-b70ed0f8df1e" == subscription.user_id |
| 63 | + assert "3407cd38-7b39-4983-86a6-67a1506a5e3f" == subscription.schedule_id |
| 64 | + |
| 65 | + |
| 66 | +def test_get_subscription_by_id(server: TSC.Server) -> None: |
| 67 | + response_xml = GET_XML_BY_ID.read_text() |
| 68 | + with requests_mock.mock() as m: |
| 69 | + m.get(server.subscriptions.baseurl + "/382e9a6e-0c08-4a95-b6c1-c14df7bac3e4", text=response_xml) |
| 70 | + subscription = server.subscriptions.get_by_id("382e9a6e-0c08-4a95-b6c1-c14df7bac3e4") |
| 71 | + |
| 72 | + assert "382e9a6e-0c08-4a95-b6c1-c14df7bac3e4" == subscription.id |
| 73 | + assert "View" == subscription.target.type |
| 74 | + assert "cdd716ca-5818-470e-8bec-086885dbadee" == subscription.target.id |
| 75 | + assert "c0d5fc44-ad8c-4957-bec0-b70ed0f8df1e" == subscription.user_id |
| 76 | + assert "Not Found Alert" == subscription.subject |
| 77 | + assert "7617c389-cdca-4940-a66e-69956fcebf3e" == subscription.schedule_id |
| 78 | + |
| 79 | + |
| 80 | +def test_create_subscription(server: TSC.Server) -> None: |
| 81 | + response_xml = CREATE_XML.read_text() |
| 82 | + with requests_mock.mock() as m: |
| 83 | + m.post(server.subscriptions.baseurl, text=response_xml) |
| 84 | + |
| 85 | + target_item = TSC.Target("960e61f2-1838-40b2-bba2-340c9492f943", "workbook") |
| 86 | + new_subscription = TSC.SubscriptionItem( |
| 87 | + "subject", "4906c453-d5ec-4972-9ff4-789b629bdfa2", "8d30c8de-0a5f-4bee-b266-c621b4f3eed0", target_item |
| 88 | + ) |
| 89 | + new_subscription = server.subscriptions.create(new_subscription) |
| 90 | + |
| 91 | + assert "78e9318d-2d29-4d67-b60f-3f2f5fd89ecc" == new_subscription.id |
| 92 | + assert "sub_name" == new_subscription.subject |
| 93 | + assert "960e61f2-1838-40b2-bba2-340c9492f943" == new_subscription.target.id |
| 94 | + assert "Workbook" == new_subscription.target.type |
| 95 | + assert "4906c453-d5ec-4972-9ff4-789b629bdfa2" == new_subscription.schedule_id |
| 96 | + assert "8d30c8de-0a5f-4bee-b266-c621b4f3eed0" == new_subscription.user_id |
| 97 | + |
| 98 | + |
| 99 | +def test_delete_subscription(server: TSC.Server) -> None: |
| 100 | + with requests_mock.mock() as m: |
| 101 | + m.delete(server.subscriptions.baseurl + "/78e9318d-2d29-4d67-b60f-3f2f5fd89ecc", status_code=204) |
| 102 | + server.subscriptions.delete("78e9318d-2d29-4d67-b60f-3f2f5fd89ecc") |
0 commit comments