def works_with_mock_callable(self): """ Covers a case where StrictMock would fail if mock_callable() was used on a class method. """ self.mock_callable( self.template, "class_method").to_return_value(None) strict_mock2 = StrictMock(self.template) strict_mock2.instance_method = lambda *args, **kwargs: None
def test_get_download_file_exception(self): fake_cursor = StrictMock(Cursor, runtime_attrs=["fetchone"]) fake_cursor.fetchone = lambda: [] self.mock_callable(utils, "db_execute").for_call( """ "SELECT song_path \ FROM downloads \ WHERE download_url=? AND accessed=False""", (self.fake_download_url, )).to_raise(FileNotFoundError).and_assert_called_once() with patch.object(pathlib.Path, "exists") as fake_download_db: fake_download_db.return_value = True self.assertIsNone(utils.get_download_file(self.fake_download_url))
def before(self): target = StrictMock(template=Target) self.original_callable = None self.target_arg = target self.mock_callable_dsl = mock_callable(self.target_arg, self.callable_arg) self.callable_target = target.instance_method
def before(self): target = StrictMock(template=Target, runtime_attrs=["dynamic_instance_method"]) self.original_callable = None self.target_arg = target self.mock_callable_dsl = mock_callable(self.target_arg, self.callable_arg) self.callable_target = target.dynamic_instance_method
def before(self): target = StrictMock(template=Target) self.original_callable = None self.target_arg = target self.callable_arg = "__str__" self.mock_callable_dsl = mock_callable(self.target_arg, self.callable_arg) self.callable_target = lambda: str(target)
def test_get_download_file_successful(self): fake_song = 'path/to/song1' fake_cursor = StrictMock(Cursor, runtime_attrs=["fetchone"]) fake_cursor.fetchone = lambda: [(fake_song)] self.mock_callable(utils, "db_execute").for_call( """ "SELECT song_path \ FROM downloads \ WHERE download_url=? AND accessed=False""", (self.fake_download_url, )).to_return_value(fake_cursor).and_assert_called_once() with patch.object(pathlib.Path, "exists") as fake_download_db: fake_download_db.return_value = True self.assertEqual(pathlib.Path(fake_song), utils.get_download_file(self.fake_download_url))
def test_zip_files_successful(self): fake_zipfile_manager = StrictMock(zipfile.ZipFile, runtime_attrs=["write"], default_context_manager=True) fake_zipfile_manager.write = lambda x, arcname: True self.mock_callable( fake_zipfile_manager, 'write').to_return_value(None).and_assert_called_exactly(4) self.mock_constructor(zipfile, "ZipFile").for_call( self.fake_download_path, 'w').to_return_value(fake_zipfile_manager) fake_zipfile_manager.__enter__ = lambda: fake_zipfile_manager scandir_iterator = StrictMock(FakeIterator, default_context_manager=True, runtime_attrs=["__iter__"]) fake_song_path = pathlib.Path( str(self.fake_song_path).replace(".mp3", "")) scandir_iterator.__iter__ = self._fake_iter self.mock_callable(os, "scandir").for_call( fake_song_path).to_return_value(scandir_iterator) download_path = utils.zip_files(self.fake_model, self.fake_song_path.stem) # we want to know if we can resolve the same path we expect self.assertEqual(download_path.resolve(), self.fake_download_path.resolve())
def test_db_exceute_failed_to_execute(self): fake_db = StrictMock(sqlite3.Connection, runtime_attrs=["execute"], default_context_manager=True) fake_db.execute = lambda: True self.mock_callable(fake_db, "execute").for_call( """ INSERT INTO downloads (song_path, download_url, accessed) values (?, ?, False)""", ( self.fake_song_path, self.fake_download_url, )).to_raise(Exception).and_assert_called_once() self.mock_callable(sqlite3, "connect").for_call( self.fake_download_db).to_return_value(fake_db) with patch.object(pathlib.Path, "exists") as fake_download_db: fake_download_db.return_value = True with self.assertRaises(Exception): utils.db_execute(self.fake_query, self.fake_parameters)
def other_instances_are_not_mocked(self): mock_callable( self.target_arg, self.callable_arg).to_return_value("mocked value") self.assertEqual( self.callable_target(*self.call_args, **self.call_kwargs), "mocked value", ) other_strict_mock = StrictMock(template=Target, runtime_attrs=runtime_attrs) mock_callable( other_strict_mock, self.callable_arg).to_return_value("other mocked value") self.assertEqual( getattr(other_strict_mock, self.callable_arg)(*self.call_args, **self.call_kwargs), "other mocked value", )
def making_copies(context): context.memoize("strict_mock", lambda self: StrictMock(template=Template)) context.memoize("key", lambda self: 1) context.memoize("value", lambda self: 2) context.memoize("attr", lambda self: {self.key: self.value}) @context.before def set_attributes(self): self.strict_mock.attr = self.attr self.strict_mock.instance_method = lambda arg: "mock" self.strict_mock.__eq__ = lambda other: True @context.example("copy.copy()") def copy_copy(self): strict_mock_copy = copy.copy(self.strict_mock) self.assertEqual(id(self.strict_mock.attr), id(strict_mock_copy.attr)) self.assertEqual( id(self.strict_mock.instance_method), id(strict_mock_copy.instance_method), ) self.assertEqual( self.strict_mock.instance_method("hello"), strict_mock_copy.instance_method("hello"), ) @context.example("copy.deepcopy()") def copy_deepcopy(self): strict_mock_copy = copy.deepcopy(self.strict_mock) self.assertEqual(self.strict_mock.attr, strict_mock_copy.attr) self.assertNotEqual(id(self.strict_mock.attr), id(strict_mock_copy.attr)) self.assertEqual((self.strict_mock.attr), (strict_mock_copy.attr)) self.assertEqual( self.strict_mock.instance_method("hello"), strict_mock_copy.instance_method("hello"), ) self.assertEqual(self.strict_mock.instance_method(1), "mock")
def test_zip_files_failure(self): fake_zipfile_manager = StrictMock(zipfile.ZipFile, runtime_attrs=["write"], default_context_manager=True) fake_zipfile_manager.write = lambda x, arcname: True self.mock_constructor(zipfile, "ZipFile").for_call( self.fake_download_path, 'w').to_return_value(fake_zipfile_manager) fake_zipfile_manager.__enter__ = lambda: fake_zipfile_manager scandir_iterator = StrictMock(FakeIterator, default_context_manager=True, runtime_attrs=["__iter__"]) fake_song_path = pathlib.Path( str(self.fake_song_path).replace(".mp3", "")) scandir_iterator.__iter__ = self._fake_iter self.mock_callable( os, "scandir").for_call(fake_song_path).to_raise(FileNotFoundError) # we want to know if we can resolve the same path we expect self.assertIsNone( utils.zip_files(self.fake_model, self.fake_song_path.stem))
async def strict_mock(self): return StrictMock(template=Template, default_context_manager=True)
async def strict_mock(self): return StrictMock( template=Template, signature_validation=self.get_signature_validation( ), )
def strict_mock(self): return StrictMock( template=self.template, default_context_manager=False, )
def strict_mock(self): return StrictMock( self.template, runtime_attrs=[self.runtime_attr], signature_validation=self.get_signature_validation(), )
def test_video_to_mp3_successful(self): fake_youtube_instance = StrictMock(pytube.YouTube, runtime_attrs=["streams"]) fake_strean_query = StrictMock(pytube.query.StreamQuery, runtime_attrs=["filter"]) fake_sorted_stream_query = StrictMock( pytube.query.StreamQuery, runtime_attrs=["order_by", "desc", "first", "download"]) fake_stream = StrictMock(pytube.streams.Stream, runtime_attrs=["download"]) fake_sorted_stream_query.order_by = lambda x: fake_sorted_stream_query fake_sorted_stream_query.desc = lambda: fake_sorted_stream_query fake_sorted_stream_query.first = lambda: fake_stream fake_stream.download = lambda output_path: self.fake_youtube_song_name fake_strean_query.filter = \ lambda progressive, file_extension: fake_sorted_stream_query fake_youtube_instance.streams = fake_strean_query self.mock_constructor(pytube, "YouTube").for_call( self.fake_url).to_return_value(fake_youtube_instance) self.mock_callable(utils, "mp4_to_mp3").for_call( self.fake_youtube_song_name).to_return_value( self.fake_youtube_song_mp3).and_assert_called_once() self.assertEqual(utils.video_to_mp3(self.fake_url), self.fake_youtube_song_mp3)
def strict_mock(self): return StrictMock(template=self.template)
def without_template(context): context.memoize("strict_mock", lambda _: StrictMock()) @context.memoize def strict_mock_rgx(self): return ("<StrictMock 0x{:02X} ".format(id(self.strict_mock)) + re.escape(self.caller_filename) + ":\d+>") context.memoize("value", lambda _: 3241234123) context.memoize("test_method_name", lambda _: "some_method") context.memoize("mock_function", lambda _: lambda: None) context.merge_context("can access attributes") @context.example def raises_when_an_undefined_attribute_is_accessed(self): name = "undefined_attribute" with self.assertRaisesWithRegexMessage( AttributeError, f"'{name}' was not set for {self.strict_mock}."): getattr(self.strict_mock, name) @context.example def allows_mocking_any_attribute(self): self.strict_mock.any_attribute = self.value self.assertEqual(self.strict_mock.any_attribute, self.value) @context.example def allows_deleting_a_mocked_attribute(self): name = "attr_name" setattr(self.strict_mock, name, self.value) self.assertTrue(hasattr(self.strict_mock, name)) delattr(self.strict_mock, name) with self.assertRaisesWithRegexMessage( AttributeError, f"'{name}' was not set for {self.strict_mock}."): getattr(self.strict_mock, name) @context.example def allows_mocking_any_method(self): def value_plus(b): return self.value + b self.strict_mock.any_method = value_plus plus = 2341 self.assertEqual(self.strict_mock.any_method(plus), self.value + plus) @context.example def allows_mocking_context_manager_methods(self): enter_mock = "something" self.strict_mock.__enter__ = lambda: enter_mock self.strict_mock.__exit__ = lambda exc_type, exc_value, traceback: None with self.strict_mock as target: self.assertEqual(target, enter_mock) @context.example def attribute_type_is_maintained(self): callable_attr = CallableObject() self.strict_mock.callable_attr = callable_attr attr = {1: 2} self.strict_mock.attr = attr self.assertEqual(type(self.strict_mock.callable_attr), type(callable_attr)) self.assertEqual(type(self.strict_mock.attr), type(attr))
def get_target_mock(self): return StrictMock(template=self.get_target_class())
def _fake_iter(self): for dir in ["bass.wav", "voice.wav", "drums.wav", "other.wav"]: tmp_dir = StrictMock(os.DirEntry, runtime_attrs=["path", "name"]) tmp_dir.path = dir tmp_dir.name = dir yield tmp_dir