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
5329aaa7
Kaydet (Commit)
5329aaa7
authored
Tem 17, 2015
tarafından
Robert Collins
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Issue #21750: mock_open.read_data can now be read from each instance, as it
could in Python 3.3.
üst
579db160
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
61 additions
and
35 deletions
+61
-35
mock.py
Lib/unittest/mock.py
+37
-33
testmock.py
Lib/unittest/test/testmock/testmock.py
+5
-0
testwith.py
Lib/unittest/test/testmock/testwith.py
+16
-2
NEWS
Misc/NEWS
+3
-0
No files found.
Lib/unittest/mock.py
Dosyayı görüntüle @
5329aaa7
...
...
@@ -2265,6 +2265,7 @@ def _iterate_read_data(read_data):
for
line
in
data_as_list
:
yield
line
def
mock_open
(
mock
=
None
,
read_data
=
''
):
"""
A helper function to create a mock to replace the use of `open`. It works
...
...
@@ -2277,24 +2278,6 @@ def mock_open(mock=None, read_data=''):
`read_data` is a string for the `read` methoddline`, and `readlines` of the
file handle to return. This is an empty string by default.
"""
def
_readlines_side_effect
(
*
args
,
**
kwargs
):
if
handle
.
readlines
.
return_value
is
not
None
:
return
handle
.
readlines
.
return_value
return
list
(
_data
)
def
_read_side_effect
(
*
args
,
**
kwargs
):
if
handle
.
read
.
return_value
is
not
None
:
return
handle
.
read
.
return_value
return
''
.
join
(
_data
)
def
_readline_side_effect
():
if
handle
.
readline
.
return_value
is
not
None
:
while
True
:
yield
handle
.
readline
.
return_value
for
line
in
_data
:
yield
line
global
file_spec
if
file_spec
is
None
:
import
_io
...
...
@@ -2303,21 +2286,42 @@ def mock_open(mock=None, read_data=''):
if
mock
is
None
:
mock
=
MagicMock
(
name
=
'open'
,
spec
=
open
)
handle
=
MagicMock
(
spec
=
file_spec
)
handle
.
__enter__
.
return_value
=
handle
_data
=
_iterate_read_data
(
read_data
)
handle
.
write
.
return_value
=
None
handle
.
read
.
return_value
=
None
handle
.
readline
.
return_value
=
None
handle
.
readlines
.
return_value
=
None
handle
.
read
.
side_effect
=
_read_side_effect
handle
.
readline
.
side_effect
=
_readline_side_effect
()
handle
.
readlines
.
side_effect
=
_readlines_side_effect
mock
.
return_value
=
handle
def
make_handle
(
*
args
,
**
kwargs
):
# Arg checking is handled by __call__
def
_readlines_side_effect
(
*
args
,
**
kwargs
):
if
handle
.
readlines
.
return_value
is
not
None
:
return
handle
.
readlines
.
return_value
return
list
(
_data
)
def
_read_side_effect
(
*
args
,
**
kwargs
):
if
handle
.
read
.
return_value
is
not
None
:
return
handle
.
read
.
return_value
return
''
.
join
(
_data
)
def
_readline_side_effect
():
if
handle
.
readline
.
return_value
is
not
None
:
while
True
:
yield
handle
.
readline
.
return_value
for
line
in
_data
:
yield
line
handle
=
MagicMock
(
spec
=
file_spec
)
handle
.
__enter__
.
return_value
=
handle
_data
=
_iterate_read_data
(
read_data
)
handle
.
write
.
return_value
=
None
handle
.
read
.
return_value
=
None
handle
.
readline
.
return_value
=
None
handle
.
readlines
.
return_value
=
None
handle
.
read
.
side_effect
=
_read_side_effect
handle
.
readline
.
side_effect
=
_readline_side_effect
()
handle
.
readlines
.
side_effect
=
_readlines_side_effect
_check_and_set_parent
(
mock
,
handle
,
None
,
'()'
)
return
handle
mock
.
side_effect
=
make_handle
return
mock
...
...
Lib/unittest/test/testmock/testmock.py
Dosyayı görüntüle @
5329aaa7
...
...
@@ -1326,6 +1326,11 @@ class MockTest(unittest.TestCase):
self
.
assertEqual
(
m
.
mock_calls
,
[
call
.
__int__
(),
call
.
__float__
()])
self
.
assertEqual
(
m
.
method_calls
,
[])
def
test_mock_open_reuse_issue_21750
(
self
):
mocked_open
=
mock
.
mock_open
(
read_data
=
'data'
)
f1
=
mocked_open
(
'a-name'
)
f2
=
mocked_open
(
'another-name'
)
self
.
assertEqual
(
f1
.
read
(),
f2
.
read
())
def
test_mock_parents
(
self
):
for
Klass
in
Mock
,
MagicMock
:
...
...
Lib/unittest/test/testmock/testwith.py
Dosyayı görüntüle @
5329aaa7
...
...
@@ -141,7 +141,6 @@ class TestMockOpen(unittest.TestCase):
def
test_mock_open_context_manager
(
self
):
mock
=
mock_open
()
handle
=
mock
.
return_value
with
patch
(
'
%
s.open'
%
__name__
,
mock
,
create
=
True
):
with
open
(
'foo'
)
as
f
:
f
.
read
()
...
...
@@ -149,8 +148,23 @@ class TestMockOpen(unittest.TestCase):
expected_calls
=
[
call
(
'foo'
),
call
()
.
__enter__
(),
call
()
.
read
(),
call
()
.
__exit__
(
None
,
None
,
None
)]
self
.
assertEqual
(
mock
.
mock_calls
,
expected_calls
)
self
.
assertIs
(
f
,
handle
)
# mock_open.return_value is no longer static, because
# readline support requires that it mutate state
def
test_mock_open_context_manager_multiple_times
(
self
):
mock
=
mock_open
()
with
patch
(
'
%
s.open'
%
__name__
,
mock
,
create
=
True
):
with
open
(
'foo'
)
as
f
:
f
.
read
()
with
open
(
'bar'
)
as
f
:
f
.
read
()
expected_calls
=
[
call
(
'foo'
),
call
()
.
__enter__
(),
call
()
.
read
(),
call
()
.
__exit__
(
None
,
None
,
None
),
call
(
'bar'
),
call
()
.
__enter__
(),
call
()
.
read
(),
call
()
.
__exit__
(
None
,
None
,
None
)]
self
.
assertEqual
(
mock
.
mock_calls
,
expected_calls
)
def
test_explicit_mock
(
self
):
mock
=
MagicMock
()
...
...
Misc/NEWS
Dosyayı görüntüle @
5329aaa7
...
...
@@ -66,6 +66,9 @@ Core and Builtins
Library
-------
-
Issue
#
21750
:
mock_open
.
read_data
can
now
be
read
from
each
instance
,
as
it
could
in
Python
3.3
.
-
Issue
#
23247
:
Fix
a
crash
in
the
StreamWriter
.
reset
()
of
CJK
codecs
.
-
Issue
#
18622
:
unittest
.
mock
.
mock_open
().
reset_mock
would
recurse
infinitely
.
...
...
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