Skip to content

Instantly share code, notes, and snippets.

@zaelcovsky
Forked from pitrk/_mock_file_with_string.md
Created October 23, 2023 19:22
Show Gist options
  • Select an option

  • Save zaelcovsky/5436108a49452c19c08724faf615e17f to your computer and use it in GitHub Desktop.

Select an option

Save zaelcovsky/5436108a49452c19c08724faf615e17f to your computer and use it in GitHub Desktop.

Revisions

  1. @pitrk pitrk revised this gist Sep 19, 2018. 2 changed files with 2 additions and 0 deletions.
    1 change: 1 addition & 0 deletions function.py
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    # Python 3.7
    import json
    from typing import List

    1 change: 1 addition & 0 deletions tests.py
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    # Python 3.7
    import unittest
    import unittest.mock

  2. @pitrk pitrk revised this gist Sep 19, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion _mock_file_with_string.md
    Original file line number Diff line number Diff line change
    @@ -16,5 +16,5 @@ Example with `@mock_patch` decorator and corresponding function are shown.

    ## Sources

    - [`mock.open`](https://docs.python.org/3/library/unittest.mock.html#mock-open)
    - [`mock_open`](https://docs.python.org/3/library/unittest.mock.html#mock-open)
    - [Michele d'Amico's answer on: How do I mock an open used in a with statement (using the Mock framework in Python)?](https://stackoverflow.com/a/34677735)
  3. @pitrk pitrk renamed this gist Sep 19, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. @pitrk pitrk renamed this gist Sep 19, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. @pitrk pitrk created this gist Sep 19, 2018.
    20 changes: 20 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    # Mock file with string in Python

    This is an example on how to mock a file with string.

    ## Use case

    Testing a function which opens a file and does some kind of operation on it.

    ## About the example

    Let us make a function which takes json from file and returns some data. In this example, we want to take data from [this API](https://www.edsm.net/api-system-v1/bodies?systemName=Sol) and return name of each planet and its surface temperature in a list.

    Instead of creating a mock file, we can limit ourselves to short string with only necessary information.

    Example with `@mock_patch` decorator and corresponding function are shown.

    ## Sources

    - [`mock.open`](https://docs.python.org/3/library/unittest.mock.html#mock-open)
    - [Michele d'Amico's answer on: How do I mock an open used in a with statement (using the Mock framework in Python)?](https://stackoverflow.com/a/34677735)
    14 changes: 14 additions & 0 deletions function.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    import json
    from typing import List


    def celestial_bodies_temperatures(filename: str) -> List(List(str, int)):
    return_list = []
    with open(filename) as f:
    data = json.load(f)
    for each_planet in data:
    return_list.append(
    [each_planet["name"],
    int(each_planet["surfaceTemperature"])]
    )
    return return_list
    33 changes: 33 additions & 0 deletions tests.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    import unittest
    import unittest.mock

    from .function import celestial_bodies_temperatures


    class TestFunction(unittest.TestCase):
    mock_file_content = """
    [{"name": "Earth","surfaceTemperature": 288},
    {"name": "Mars", "surfaceTemperature": 260}]
    """

    def test_celestial_bodies_temperatures_returns_list_correctly_with(self):
    with unittest.mock.patch(
    'builtins.open',
    new=unittest.mock.mock_open(read_data=self.mock_file_content),
    create=True
    ) as file_mock:
    self.assertEqual(
    celestial_bodies_temperatures('/dev/null'),
    [["Earth", 288], ["Mars", 260]]
    )

    @unittest.mock.patch(
    'builtins.open',
    new=unittest.mock.mock_open(read_data=mock_file_content),
    create=True
    )
    def test_celestial_bodies_temperatures_returns_list_correctly_decorator(self):
    self.assertEqual(
    celestial_bodies_temperatures('/dev/null'),
    [["Earth", 288], ["Mars", 260]]
    )