-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest_utils.py
93 lines (75 loc) · 3.58 KB
/
test_utils.py
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
# pylint: disable=missing-docstring,redefined-outer-name,no-self-use
import os
import pytest
from mock import patch
from dump2polarion import utils
from dump2polarion.exceptions import Dump2PolarionException
from tests import conf
# pylint: disable=too-many-public-methods
class TestUtils:
def test_get_unicode_char(self):
unicode_str = utils.get_unicode_str("®")
assert unicode_str == "®"
def test_get_unicode_num(self):
unicode_str = utils.get_unicode_str(42)
assert unicode_str == "42"
def test_get_unicode_basestring(self):
unicode_str = utils.get_unicode_str(b"@")
assert unicode_str == "@"
def test_write_xml_gen(self, tmpdir):
dirname = str(tmpdir)
utils.write_xml("<xml />", output_loc=dirname)
assert "output-" in os.listdir(dirname)[0]
def test_write_xml_loc(self, tmpdir):
dirname = str(tmpdir)
utils.write_xml("<xml />", output_loc=os.path.join(dirname, "output123.xml"))
assert "output123.xml" in os.listdir(dirname)[0]
def test_write_xml_file(self, tmpdir):
dirname = str(tmpdir)
utils.write_xml("<xml />", filename=os.path.join(dirname, "output123.xml"))
assert "output123.xml" in os.listdir(dirname)[0]
def test_write_xml_root_none(self, tmpdir):
dirname = str(tmpdir)
with pytest.raises(Dump2PolarionException) as excinfo:
utils.write_xml_root(None, filename=os.path.join(dirname, "output123.xml"))
assert "No data to write" in str(excinfo.value)
def test_write_xml_root_file(self, tmpdir):
fname = "complete_transform_noresponse.xml"
xml_root = utils.get_xml_root(os.path.join(conf.DATA_PATH, fname))
dirname = str(tmpdir)
utils.write_xml_root(xml_root, filename=os.path.join(dirname, "output123.xml"))
assert "output123.xml" in os.listdir(dirname)[0]
def test_write_xml_no_data(self, tmpdir):
dirname = str(tmpdir)
with pytest.raises(Dump2PolarionException) as excinfo:
utils.write_xml("", filename=os.path.join(dirname, "output123.xml"))
assert "No data to write" in str(excinfo.value)
def test_invalid_xml_root(self):
with pytest.raises(Dump2PolarionException) as excinfo:
utils.get_xml_root("NONEXISTENT.xml")
assert "Failed to parse XML file" in str(excinfo.value)
def test_invalid_xml_str(self):
with pytest.raises(Dump2PolarionException) as excinfo:
utils.get_xml_root_from_str(None)
assert "Failed to parse XML string" in str(excinfo.value)
def test_get_session_oldauth(self, config_prop):
if "auth_url" in config_prop:
del config_prop["auth_url"]
with patch("requests.Session"):
session = utils.get_session("foo", config_prop)
assert session.auth == "foo"
def test_get_session_cookie_failed(self, config_prop):
config_prop["auth_url"] = "http://example.com"
with patch("requests.Session") as mock:
instance = mock.return_value
instance.post.return_value = None
with pytest.raises(Dump2PolarionException) as excinfo:
utils.get_session("foo", config_prop)
assert "Cookie was not retrieved" in str(excinfo.value)
def test_get_session_cookie_success(self, config_prop):
config_prop["auth_url"] = "http://example.com"
with patch("requests.Session") as mock:
instance = mock.return_value
instance.post.return_value = True
session = utils.get_session("foo", config_prop)
assert session.auth != "foo"