Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
2087023f
Kaydet (Commit)
2087023f
authored
Eyl 12, 2018
tarafından
Tony Flury
Kaydeden (comit)
Berker Peksag
Eyl 12, 2018
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
bpo-32933: Implement __iter__ method on mock_open() (GH-5974)
üst
c7042224
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
37 additions
and
3 deletions
+37
-3
unittest.mock.rst
Doc/library/unittest.mock.rst
+4
-0
mock.py
Lib/unittest/mock.py
+6
-3
testmock.py
Lib/unittest/test/testmock/testmock.py
+10
-0
testwith.py
Lib/unittest/test/testmock/testwith.py
+15
-0
2018-04-30-22-43-31.bpo-32933.M3iI_y.rst
...S.d/next/Library/2018-04-30-22-43-31.bpo-32933.M3iI_y.rst
+2
-0
No files found.
Doc/library/unittest.mock.rst
Dosyayı görüntüle @
2087023f
...
@@ -2095,6 +2095,10 @@ mock_open
...
@@ -2095,6 +2095,10 @@ mock_open
.. versionchanged:: 3.5
.. versionchanged:: 3.5
*read_data* is now reset on each call to the *mock*.
*read_data* is now reset on each call to the *mock*.
.. versionchanged:: 3.8
Added :meth:`__iter__` to implementation so that iteration (such as in for
loops) correctly consumes *read_data*.
Using :func:`open` as a context manager is a great way to ensure your file handles
Using :func:`open` as a context manager is a great way to ensure your file handles
are closed properly and is becoming common::
are closed properly and is becoming common::
...
...
Lib/unittest/mock.py
Dosyayı görüntüle @
2087023f
...
@@ -2358,14 +2358,16 @@ def mock_open(mock=None, read_data=''):
...
@@ -2358,14 +2358,16 @@ def mock_open(mock=None, read_data=''):
return
type
(
read_data
)()
.
join
(
_state
[
0
])
return
type
(
read_data
)()
.
join
(
_state
[
0
])
def
_readline_side_effect
():
def
_readline_side_effect
():
yield
from
_iter_side_effect
()
while
True
:
yield
type
(
read_data
)()
def
_iter_side_effect
():
if
handle
.
readline
.
return_value
is
not
None
:
if
handle
.
readline
.
return_value
is
not
None
:
while
True
:
while
True
:
yield
handle
.
readline
.
return_value
yield
handle
.
readline
.
return_value
for
line
in
_state
[
0
]:
for
line
in
_state
[
0
]:
yield
line
yield
line
while
True
:
yield
type
(
read_data
)()
global
file_spec
global
file_spec
if
file_spec
is
None
:
if
file_spec
is
None
:
...
@@ -2389,6 +2391,7 @@ def mock_open(mock=None, read_data=''):
...
@@ -2389,6 +2391,7 @@ def mock_open(mock=None, read_data=''):
_state
[
1
]
=
_readline_side_effect
()
_state
[
1
]
=
_readline_side_effect
()
handle
.
readline
.
side_effect
=
_state
[
1
]
handle
.
readline
.
side_effect
=
_state
[
1
]
handle
.
readlines
.
side_effect
=
_readlines_side_effect
handle
.
readlines
.
side_effect
=
_readlines_side_effect
handle
.
__iter__
.
side_effect
=
_iter_side_effect
def
reset_data
(
*
args
,
**
kwargs
):
def
reset_data
(
*
args
,
**
kwargs
):
_state
[
0
]
=
_iterate_read_data
(
read_data
)
_state
[
0
]
=
_iterate_read_data
(
read_data
)
...
...
Lib/unittest/test/testmock/testmock.py
Dosyayı görüntüle @
2087023f
...
@@ -1450,6 +1450,16 @@ class MockTest(unittest.TestCase):
...
@@ -1450,6 +1450,16 @@ class MockTest(unittest.TestCase):
f2_data
=
f2
.
read
()
f2_data
=
f2
.
read
()
self
.
assertEqual
(
f1_data
,
f2_data
)
self
.
assertEqual
(
f1_data
,
f2_data
)
def
test_mock_open_dunder_iter_issue
(
self
):
# Test dunder_iter method generates the expected result and
# consumes the iterator.
mocked_open
=
mock
.
mock_open
(
read_data
=
'Remarkable
\n
Norwegian Blue'
)
f1
=
mocked_open
(
'a-name'
)
lines
=
[
line
for
line
in
f1
]
self
.
assertEqual
(
lines
[
0
],
'Remarkable
\n
'
)
self
.
assertEqual
(
lines
[
1
],
'Norwegian Blue'
)
self
.
assertEqual
(
list
(
f1
),
[])
def
test_mock_open_write
(
self
):
def
test_mock_open_write
(
self
):
# Test exception in file writing write()
# Test exception in file writing write()
mock_namedtemp
=
mock
.
mock_open
(
mock
.
MagicMock
(
name
=
'JLV'
))
mock_namedtemp
=
mock
.
mock_open
(
mock
.
MagicMock
(
name
=
'JLV'
))
...
...
Lib/unittest/test/testmock/testwith.py
Dosyayı görüntüle @
2087023f
...
@@ -188,6 +188,7 @@ class TestMockOpen(unittest.TestCase):
...
@@ -188,6 +188,7 @@ class TestMockOpen(unittest.TestCase):
def
test_readline_data
(
self
):
def
test_readline_data
(
self
):
# Check that readline will return all the lines from the fake file
# Check that readline will return all the lines from the fake file
# And that once fully consumed, readline will return an empty string.
mock
=
mock_open
(
read_data
=
'foo
\n
bar
\n
baz
\n
'
)
mock
=
mock_open
(
read_data
=
'foo
\n
bar
\n
baz
\n
'
)
with
patch
(
'
%
s.open'
%
__name__
,
mock
,
create
=
True
):
with
patch
(
'
%
s.open'
%
__name__
,
mock
,
create
=
True
):
h
=
open
(
'bar'
)
h
=
open
(
'bar'
)
...
@@ -197,6 +198,7 @@ class TestMockOpen(unittest.TestCase):
...
@@ -197,6 +198,7 @@ class TestMockOpen(unittest.TestCase):
self
.
assertEqual
(
line1
,
'foo
\n
'
)
self
.
assertEqual
(
line1
,
'foo
\n
'
)
self
.
assertEqual
(
line2
,
'bar
\n
'
)
self
.
assertEqual
(
line2
,
'bar
\n
'
)
self
.
assertEqual
(
line3
,
'baz
\n
'
)
self
.
assertEqual
(
line3
,
'baz
\n
'
)
self
.
assertEqual
(
h
.
readline
(),
''
)
# Check that we properly emulate a file that doesn't end in a newline
# Check that we properly emulate a file that doesn't end in a newline
mock
=
mock_open
(
read_data
=
'foo'
)
mock
=
mock_open
(
read_data
=
'foo'
)
...
@@ -204,6 +206,19 @@ class TestMockOpen(unittest.TestCase):
...
@@ -204,6 +206,19 @@ class TestMockOpen(unittest.TestCase):
h
=
open
(
'bar'
)
h
=
open
(
'bar'
)
result
=
h
.
readline
()
result
=
h
.
readline
()
self
.
assertEqual
(
result
,
'foo'
)
self
.
assertEqual
(
result
,
'foo'
)
self
.
assertEqual
(
h
.
readline
(),
''
)
def
test_dunder_iter_data
(
self
):
# Check that dunder_iter will return all the lines from the fake file.
mock
=
mock_open
(
read_data
=
'foo
\n
bar
\n
baz
\n
'
)
with
patch
(
'
%
s.open'
%
__name__
,
mock
,
create
=
True
):
h
=
open
(
'bar'
)
lines
=
[
l
for
l
in
h
]
self
.
assertEqual
(
lines
[
0
],
'foo
\n
'
)
self
.
assertEqual
(
lines
[
1
],
'bar
\n
'
)
self
.
assertEqual
(
lines
[
2
],
'baz
\n
'
)
self
.
assertEqual
(
h
.
readline
(),
''
)
def
test_readlines_data
(
self
):
def
test_readlines_data
(
self
):
...
...
Misc/NEWS.d/next/Library/2018-04-30-22-43-31.bpo-32933.M3iI_y.rst
0 → 100644
Dosyayı görüntüle @
2087023f
:func:`unittest.mock.mock_open` now supports iteration over the file
contents. Patch by Tony Flury.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment