def test_can_check_mime(self, mock_magic): "Validator must call `magic.from_file` using it's file_path attribute" validator = DocumentValidatorProvider(*self.initial_data) validator._has_right_type() mock_magic.from_file.assert_called_once_with( self.initial_data[0], mime=True, )
def test_can_detect_invalid_mime(self, mock_magic): """ If given an invalid document `validator.has_right_type` method should return False """ # magic.from_file return bytes mock_magic.from_file.return_value = b'invalid/mime' # Run validation validator = DocumentValidatorProvider(*self.initial_data) result = validator._has_right_type() self.assertFalse(result)
def test_can_dispatch_validation(self, _has_right_type, validate): """ Validator must call methods to check the file type and format. """ # Patch `open` built-in on target module m = mock_open() import runner.document_validation with patch.object(runner.document_validation, 'open', m, create=True): # Create a mock file (for the context manager) mock_file_pointer = m.return_value validator = DocumentValidatorProvider(*self.initial_data) validator.run() # Assert that the required methods were called _has_right_type.assert_called_once_with() validate.assert_called_with(mock_file_pointer)