code: def test_prediction_writer_hook_call_intervals():
      """Test that the `write_on_batch_end` and `write_on_epoch_end` hooks get invoked based on the defined interval."""
      DummyPredictionWriter.write_on_batch_end = Mock()
      DummyPredictionWriter.write_on_epoch_end = Mock()
  
      dataloader = DataLoader(RandomDataset(32, 64))
  
      model = BoringModel()
      cb = DummyPredictionWriter("batch_and_epoch")
      trainer = Trainer(limit_predict_batches=4, callbacks=cb)
      results = trainer.predict(model, dataloaders=dataloader)
      assert len(results) == 4
      assert cb.write_on_batch_end.call_count == 4
      assert cb.write_on_epoch_end.call_count == 1
  
      DummyPredictionWriter.write_on_batch_end.reset_mock()
      DummyPredictionWriter.write_on_epoch_end.reset_mock()
  
      cb = DummyPredictionWriter("batch_and_epoch")
      trainer = Trainer(limit_predict_batches=4, callbacks=cb)
      trainer.predict(
          model, dataloaders=dataloader, return_predictions=False
      )
      assert cb.write_on_batch_end.call_count == 4
      assert cb.write_on_epoch_end.call_count == 1
  
      DummyPredictionWriter.write_on_batch_end.reset_mock()
      DummyPredictionWriter.write_on_epoch_end.reset_mock()
  
      cb = DummyPredictionWriter("batch")
      trainer = Trainer(limit_predict_batches=4, callbacks=cb)
      trainer.predict(
          model, dataloaders=dataloader, return_predictions=False
      )
      assert cb.write_on_batch_end.call_count == 4
      assert cb.write_on_epoch_end.call_count == 0
  
      DummyPredictionWriter.write_on_batch_end.reset_mock()
      DummyPredictionWriter.write_on_epoch_end.reset_mock()
  
      cb = DummyPredictionWriter("epoch")
      trainer = Trainer(limit_predict_batches=4, callbacks=cb)
      trainer.predict(
          model, dataloaders=dataloader, return_predictions=False
      )
      assert cb.write_on_batch_end.call_count == 0
      assert cb.write_on_epoch_end.call_count == 1
  
file_path: pytorch-lightning/tests/tests_pytorch/callbacks/test_prediction_writer.py
imports: from unittest.mock import ANY, Mock, call
  import pytest
  from lightning.pytorch import Trainer
  from lightning.pytorch.callbacks import BasePredictionWriter
  from lightning.pytorch.demos.boring_classes import BoringModel, RandomDataset
  from lightning.pytorch.utilities.exceptions import MisconfigurationException
  from torch.utils.data import DataLoader
label_desc: Use image node_id 2 for calling a global function with example usage: test_prediction_writer_hook_call_intervals() without return types