forked from tableau/server-client-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_utils.py
More file actions
65 lines (46 loc) · 1.61 KB
/
Copy path_utils.py
File metadata and controls
65 lines (46 loc) · 1.61 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
import os.path
import unittest
from typing import Optional
from xml.etree import ElementTree as ET
from contextlib import contextmanager
TEST_ASSET_DIR = os.path.join(os.path.dirname(__file__), "assets")
def asset(filename):
return os.path.join(TEST_ASSET_DIR, filename)
def read_xml_asset(filename):
with open(asset(filename), "rb") as f:
return f.read().decode("utf-8")
def read_xml_assets(*args):
return map(read_xml_asset, args)
def server_response_error_factory(code: str, summary: str, detail: str) -> str:
root = ET.Element("tsResponse")
error = ET.SubElement(root, "error")
error.attrib["code"] = code
summary_element = ET.SubElement(error, "summary")
summary_element.text = summary
detail_element = ET.SubElement(error, "detail")
detail_element.text = detail
return ET.tostring(root, encoding="utf-8").decode("utf-8")
def server_response_factory(tag: str, **attributes: str) -> bytes:
ns = "http://tableau.com/api"
ET.register_namespace("", ns)
root = ET.Element(
f"{{{ns}}}tsResponse",
)
if attributes is None:
attributes = {}
elem = ET.SubElement(root, f"{{{ns}}}{tag}", attrib=attributes)
return ET.tostring(root, encoding="utf-8")
@contextmanager
def mocked_time():
mock_time = 0
def sleep_mock(interval):
nonlocal mock_time
mock_time += interval
def get_time():
return mock_time
try:
patch = unittest.mock.patch
except AttributeError:
from unittest.mock import patch
with patch("time.sleep", sleep_mock), patch("time.time", get_time):
yield get_time