Changeset 1818:89490275e1c5


Ignore:
Timestamp:
Jun 18, 2019, 7:40:07 PM (2 months ago)
Author:
Stefan Schwarzer <sschwarzer@…>
Branch:
default
Message:
Use `ScriptedSession` in `TestFileContextManager`
File:
1 edited

Legend:

Unmodified
Added
Removed
  • test/test_with_statement.py

    r1816 r1818  
    44
    55import ftplib
     6import io
    67import pytest
    78
     
    7172
    7273    def test_normal_operation(self):
    73         with test_base.ftp_host_factory(
    74                session_factory=ReadMockSession) as host:
     74        host_script = [
     75          Call(method_name="__init__"),
     76          Call(method_name="pwd", result="/"),
     77          Call(method_name="close")
     78        ]
     79        file_script = [
     80          Call(method_name="__init__"),
     81          Call(method_name="pwd", result="/"),
     82          Call(method_name="cwd", result=None, args=("/",)),
     83          Call(method_name="voidcmd", result=None, args=("TYPE I",)),
     84          Call(method_name="transfercmd",
     85               result=io.BytesIO(b"line 1\nline 2"),
     86               args=("RETR dummy", None)),
     87          Call(method_name="voidresp", result=None),
     88          Call(method_name="close")
     89        ]
     90        multisession_factory = scripted_session.factory(host_script, file_script)
     91        with test_base.ftp_host_factory(multisession_factory) as host:
    7592            with host.open("dummy", "r") as fobj:
    7693                assert fobj.closed is False
     
    8198
    8299    def test_ftputil_exception(self):
    83         with test_base.ftp_host_factory(
    84                session_factory=InaccessibleDirSession) as host:
     100        host_script = [
     101          Call(method_name="__init__"),
     102          Call(method_name="pwd", result="/"),
     103          Call(method_name="close")
     104        ]
     105        file_script = [
     106          Call(method_name="__init__"),
     107          Call(method_name="pwd", result="/"),
     108          Call(method_name="cwd", result=None, args=("/",)),
     109          Call(method_name="voidcmd", result=None, args=("TYPE I",)),
     110          # Raise exception. `voidresp` therefore won't be called, but `close`
     111          # will be called by the context manager.
     112          Call(method_name="transfercmd",
     113               result=ftplib.error_perm,
     114               args=("STOR inaccessible", None)),
     115          # Call(method_name="voidresp", result=None),
     116          Call(method_name="close")
     117        ]
     118        multisession_factory = scripted_session.factory(host_script, file_script)
     119        with test_base.ftp_host_factory(multisession_factory) as host:
    85120            with pytest.raises(ftputil.error.FTPIOError):
    86                 # This should fail since the directory isn't
    87                 # accessible by definition.
    88                 with host.open("/inaccessible/new_file", "w") as fobj:
     121                # This should fail.
     122                with host.open("/inaccessible", "w") as fobj:
    89123                    pass
    90124            # The file construction shouldn't have succeeded, so `fobj`
     
    93127
    94128    def test_client_code_exception(self):
    95         with test_base.ftp_host_factory(
    96                session_factory=ReadMockSession) as host:
     129        host_script = [
     130          Call(method_name="__init__"),
     131          Call(method_name="pwd", result="/"),
     132          Call(method_name="close")
     133        ]
     134        file_script = [
     135          Call(method_name="__init__"),
     136          Call(method_name="pwd", result="/"),
     137          Call(method_name="cwd", result=None, args=("/",)),
     138          Call(method_name="voidcmd", result=None, args=("TYPE I",)),
     139          Call(method_name="transfercmd",
     140               result=io.BytesIO(b""),
     141               args=("RETR dummy", None)),
     142          Call(method_name="voidresp", result=None),
     143          Call(method_name="close")
     144        ]
     145        multisession_factory = scripted_session.factory(host_script, file_script)
     146        with test_base.ftp_host_factory(multisession_factory) as host:
    97147            try:
    98148                with host.open("dummy", "r") as fobj:
Note: See TracChangeset for help on using the changeset viewer.