-
-
Save zaelcovsky/5436108a49452c19c08724faf615e17f to your computer and use it in GitHub Desktop.
Revisions
-
pitrk revised this gist
Sep 19, 2018 . 2 changed files with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,4 @@ # Python 3.7 import json from typing import List This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,4 @@ # Python 3.7 import unittest import unittest.mock -
pitrk revised this gist
Sep 19, 2018 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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) - [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) -
pitrk renamed this gist
Sep 19, 2018 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
pitrk renamed this gist
Sep 19, 2018 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
pitrk created this gist
Sep 19, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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) This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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]] )