forked from PythonCharmers/python-future
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_requests.py
More file actions
102 lines (87 loc) · 3.1 KB
/
Copy pathtest_requests.py
File metadata and controls
102 lines (87 loc) · 3.1 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
"""
Tests for whether the standard library hooks in ``future`` are compatible with
the ``requests`` package.
"""
from __future__ import absolute_import, unicode_literals, print_function
from future import standard_library
from future.tests.base import unittest, CodeHandler
import textwrap
import sys
import os
with standard_library.suspend_hooks():
try:
import requests
except ImportError:
requests = None
class write_module(object):
"""
A context manager to streamline the tests. Creates a temp file for a
module designed to be imported by the ``with`` block, then removes it
afterwards.
"""
def __init__(self, code, tempdir):
self.code = code
self.tempdir = tempdir
def __enter__(self):
print('Creating {0}/test_imports_future_stdlib ...'.format(self.tempdir))
with open(self.tempdir + 'test_imports_future_stdlib.py', 'w') as f:
f.write(textwrap.dedent(self.code))
sys.path.insert(0, self.tempdir)
return self
def __exit__(self, exc_type, exc_val, exc_tb):
"""
If an exception occurred, we leave the file for inspection.
"""
sys.path.remove(self.tempdir)
if exc_type is None:
# No exception occurred
os.remove(self.tempdir + 'test_imports_future_stdlib.py')
class TestRequests(CodeHandler):
"""
This class tests whether the requests module conflicts with the
standard library import hooks, as in issue #19.
"""
@unittest.skipIf(requests is None, 'Install ``requests`` if you would like' \
+ ' to test ``requests`` + future compatibility (issue #19)')
def test_requests(self):
code = """
from future import standard_library
standard_library.install_hooks()
import html.parser
"""
with write_module(code, self.tempdir):
import test_imports_future_stdlib
standard_library.remove_hooks()
import requests
r = requests.get('http://google.com')
self.assertTrue(r)
# Was:
# try:
# (code)
# except Exception as e:
# raise e
# else:
# print('Succeeded!')
# finally:
# sys.path.remove(self.tempdir)
@unittest.skipIf(requests is None, 'Install ``requests`` if you would like' \
+ ' to test ``requests`` + future compatibility (issue #19)')
def test_requests_cm(self):
"""
Tests whether requests can be used importing standard_library modules
previously with the hooks context manager
"""
code = """
from future import standard_library
with standard_library.hooks():
import builtins
import html.parser
import http.client
"""
with write_module(code, self.tempdir):
import test_imports_future_stdlib
import requests
r = requests.get('http://google.com')
self.assertTrue(True)
if __name__ == '__main__':
unittest.main()