1
# Copyright (c) 2008-2011 testtools developers. See LICENSE for details.
7
from testtools import TestCase
8
from testtools.compat import (
13
from testtools.content import (
21
from testtools.content_type import (
25
from testtools.matchers import (
31
from testtools.tests.helpers import an_exc_info
34
raises_value_error = Raises(MatchesException(ValueError))
37
class TestContent(TestCase):
39
def test___init___None_errors(self):
40
self.assertThat(lambda: Content(None, None), raises_value_error)
42
lambda: Content(None, lambda: ["traceback"]), raises_value_error)
44
lambda: Content(ContentType("text", "traceback"), None),
47
def test___init___sets_ivars(self):
48
content_type = ContentType("foo", "bar")
49
content = Content(content_type, lambda: ["bytes"])
50
self.assertEqual(content_type, content.content_type)
51
self.assertEqual(["bytes"], list(content.iter_bytes()))
53
def test___eq__(self):
54
content_type = ContentType("foo", "bar")
55
one_chunk = lambda: [_b("bytes")]
56
two_chunk = lambda: [_b("by"), _b("tes")]
57
content1 = Content(content_type, one_chunk)
58
content2 = Content(content_type, one_chunk)
59
content3 = Content(content_type, two_chunk)
60
content4 = Content(content_type, lambda: [_b("by"), _b("te")])
61
content5 = Content(ContentType("f", "b"), two_chunk)
62
self.assertEqual(content1, content2)
63
self.assertEqual(content1, content3)
64
self.assertNotEqual(content1, content4)
65
self.assertNotEqual(content1, content5)
67
def test___repr__(self):
68
content = Content(ContentType("application", "octet-stream"),
69
lambda: [_b("\x00bin"), _b("ary\xff")])
70
self.assertIn("\\x00binary\\xff", repr(content))
72
def test_iter_text_not_text_errors(self):
73
content_type = ContentType("foo", "bar")
74
content = Content(content_type, lambda: ["bytes"])
75
self.assertThat(content.iter_text, raises_value_error)
77
def test_iter_text_decodes(self):
78
content_type = ContentType("text", "strange", {"charset": "utf8"})
80
content_type, lambda: [_u("bytes\xea").encode("utf8")])
81
self.assertEqual([_u("bytes\xea")], list(content.iter_text()))
83
def test_iter_text_default_charset_iso_8859_1(self):
84
content_type = ContentType("text", "strange")
85
text = _u("bytes\xea")
86
iso_version = text.encode("ISO-8859-1")
87
content = Content(content_type, lambda: [iso_version])
88
self.assertEqual([text], list(content.iter_text()))
90
def test_from_file(self):
91
fd, path = tempfile.mkstemp()
92
self.addCleanup(os.remove, path)
93
os.write(fd, 'some data')
95
content = content_from_file(path, UTF8_TEXT, chunk_size=2)
97
list(content.iter_bytes()), Equals(['so', 'me', ' d', 'at', 'a']))
99
def test_from_nonexistent_file(self):
100
directory = tempfile.mkdtemp()
101
nonexistent = os.path.join(directory, 'nonexistent-file')
102
content = content_from_file(nonexistent)
103
self.assertThat(content.iter_bytes, raises(IOError))
105
def test_from_file_default_type(self):
106
content = content_from_file('/nonexistent/path')
107
self.assertThat(content.content_type, Equals(UTF8_TEXT))
109
def test_from_file_eager_loading(self):
110
fd, path = tempfile.mkstemp()
111
os.write(fd, 'some data')
113
content = content_from_file(path, UTF8_TEXT, buffer_now=True)
116
_b('').join(content.iter_bytes()), Equals('some data'))
118
def test_from_stream(self):
119
data = StringIO('some data')
120
content = content_from_stream(data, UTF8_TEXT, chunk_size=2)
122
list(content.iter_bytes()), Equals(['so', 'me', ' d', 'at', 'a']))
124
def test_from_stream_default_type(self):
125
data = StringIO('some data')
126
content = content_from_stream(data)
127
self.assertThat(content.content_type, Equals(UTF8_TEXT))
129
def test_from_stream_eager_loading(self):
130
fd, path = tempfile.mkstemp()
131
self.addCleanup(os.remove, path)
132
os.write(fd, 'some data')
133
stream = open(path, 'rb')
134
content = content_from_stream(stream, UTF8_TEXT, buffer_now=True)
135
os.write(fd, 'more data')
138
_b('').join(content.iter_bytes()), Equals('some data'))
140
def test_from_text(self):
141
data = _u("some data")
142
expected = Content(UTF8_TEXT, lambda: [data.encode('utf8')])
143
self.assertEqual(expected, text_content(data))
146
class TestTracebackContent(TestCase):
148
def test___init___None_errors(self):
150
lambda: TracebackContent(None, None), raises_value_error)
152
def test___init___sets_ivars(self):
153
content = TracebackContent(an_exc_info, self)
154
content_type = ContentType("text", "x-traceback",
155
{"language": "python", "charset": "utf8"})
156
self.assertEqual(content_type, content.content_type)
157
result = unittest.TestResult()
158
expected = result._exc_info_to_string(an_exc_info, self)
159
self.assertEqual(expected, ''.join(list(content.iter_text())))
162
class TestAttachFile(TestCase):
164
def make_file(self, data):
165
fd, path = tempfile.mkstemp()
166
self.addCleanup(os.remove, path)
171
def test_simple(self):
172
class SomeTest(TestCase):
175
test = SomeTest('test_foo')
177
path = self.make_file(data)
178
my_content = text_content(data)
179
attach_file(test, path, name='foo')
180
self.assertEqual({'foo': my_content}, test.getDetails())
182
def test_optional_name(self):
183
# If no name is provided, attach_file just uses the base name of the
185
class SomeTest(TestCase):
188
test = SomeTest('test_foo')
189
path = self.make_file('some data')
190
base_path = os.path.basename(path)
191
attach_file(test, path)
192
self.assertEqual([base_path], list(test.getDetails()))
194
def test_lazy_read(self):
195
class SomeTest(TestCase):
198
test = SomeTest('test_foo')
199
path = self.make_file('some data')
200
attach_file(test, path, name='foo', buffer_now=False)
201
content = test.getDetails()['foo']
202
content_file = open(path, 'w')
203
content_file.write('new data')
205
self.assertEqual(''.join(content.iter_bytes()), 'new data')
207
def test_eager_read_by_default(self):
208
class SomeTest(TestCase):
211
test = SomeTest('test_foo')
212
path = self.make_file('some data')
213
attach_file(test, path, name='foo')
214
content = test.getDetails()['foo']
215
content_file = open(path, 'w')
216
content_file.write('new data')
218
self.assertEqual(''.join(content.iter_bytes()), 'some data')
222
from unittest import TestLoader
223
return TestLoader().loadTestsFromName(__name__)