Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a recipe in unittest.mock examples about mock_open per file #82338

Closed
tirkarthi opened this issue Sep 13, 2019 · 2 comments
Closed

Add a recipe in unittest.mock examples about mock_open per file #82338

tirkarthi opened this issue Sep 13, 2019 · 2 comments
Labels
3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes docs Documentation in the Doc dir type-bug An unexpected behavior, bug, or error

Comments

@tirkarthi
Copy link
Member

BPO 38157
Nosy @cjw296, @voidspace, @lisroach, @mariocj89, @tirkarthi
PRs
  • bpo-38157: Add example about per file output for mock_open. #16090
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2019-09-13.10:54:43.734>
    labels = ['3.8', 'type-bug', '3.7', '3.9', 'docs']
    title = 'Add a recipe in unittest.mock examples about mock_open per file'
    updated_at = <Date 2019-09-13.11:44:22.369>
    user = 'https://github.com/tirkarthi'

    bugs.python.org fields:

    activity = <Date 2019-09-13.11:44:22.369>
    actor = 'xtreak'
    assignee = 'docs@python'
    closed = False
    closed_date = None
    closer = None
    components = ['Documentation']
    creation = <Date 2019-09-13.10:54:43.734>
    creator = 'xtreak'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 38157
    keywords = ['patch']
    message_count = 1.0
    messages = ['352285']
    nosy_count = 6.0
    nosy_names = ['cjw296', 'michael.foord', 'docs@python', 'lisroach', 'mariocj89', 'xtreak']
    pr_nums = ['16090']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue38157'
    versions = ['Python 3.7', 'Python 3.8', 'Python 3.9']

    @tirkarthi
    Copy link
    Member Author

    With bpo-37669 it was proposed to refactor out the mock_open handler to return different mocks per file and an API change to make sure read_data accepts a dictionary of file and return values it can only land on master if accepter. It's already possible now with using side_effect to return per file content. Adding it would be a good example like below so that users can know this usage. I can prepare a PR for this.

    from unittest.mock import mock_open, patch
    
    DEFAULT_MOCK_DATA = "default mock data"
    data_dict = {"file1": "data1",
                 "file2": "data2"}
    
    def open_side_effect(name):
        return mock_open(read_data=data_dict.get(name, DEFAULT_MOCK_DATA))()
    
    with patch(f"{__name__}.open", side_effect=open_side_effect):
        with open("file1") as file1:
            assert file1.read() == "data1"
    
            with open("file2") as file2:
                assert file2.read() == "data2"
    
                with open("file1") as file3:
                    assert file3.read(1) == "d"
        assert [file1](https://bugs.python.org/file1).read() == ""
    
        with open("defaultfile") as file4:
            assert file4.read() == "default mock data"

    @tirkarthi tirkarthi added 3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes labels Sep 13, 2019
    @tirkarthi tirkarthi added docs Documentation in the Doc dir type-bug An unexpected behavior, bug, or error labels Sep 13, 2019
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @hugovk
    Copy link
    Member

    hugovk commented Sep 7, 2023

    Fixed via #16090 and backports to 3.11. Thanks all!

    @hugovk hugovk closed this as completed Sep 7, 2023
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes docs Documentation in the Doc dir type-bug An unexpected behavior, bug, or error
    Projects
    Status: Done
    Development

    No branches or pull requests

    2 participants