Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Tests for PR#790 #792

Conversation

sentry-autofix[bot]
Copy link

This PR adds tests for #790

Commits:

  • Add unit tests for PrometheusCheckpointLoggerHandler and CheckpointLogger

Swatinem and others added 7 commits October 16, 2024 06:54
Co-authored-by: Arpad Borsos <arpad.borsos@sentry.io>
… `helpers/checkpoint_logger/tests/` directory. The file contains two test classes:

1. `TestPrometheusCheckpointLoggerHandler`: This class tests all the methods of the `PrometheusCheckpointLoggerHandler` class. Each test method corresponds to a method in the handler, mocking the appropriate Prometheus metric and verifying that it's called correctly.

2. `TestCheckpointLogger`: This class tests the changes made to the `CheckpointLogger` class, specifically the `submit_subflow` method. It mocks the necessary dependencies and verifies that both Sentry SDK and Prometheus metrics are updated correctly.

The tests use the `unittest` framework and extensively use mocking to isolate the code under test from its dependencies.
Copy link

This PR includes changes to shared. Please review them here: codecov/shared@b9e83b2...3cac029

@codecov-notifications
Copy link

codecov-notifications bot commented Oct 16, 2024

Codecov Report

Attention: Patch coverage is 89.57055% with 17 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
tasks/upload_finisher.py 54.54% 10 Missing ⚠️
...checkpoint_logger/tests/test_prometheus_handler.py 94.64% 3 Missing ⚠️
services/processing/merging.py 0.00% 2 Missing ⚠️
tasks/tests/unit/test_upload_processing_task.py 93.10% 2 Missing ⚠️

❗ There is a different number of reports uploaded between BASE (39fb207) and HEAD (a714733). Click for more details.

HEAD has 2 uploads less than BASE
Flag BASE (39fb207) HEAD (a714733)
unit 2 1
integration 2 1

Impacted file tree graph

@@                        Coverage Diff                         @@
##           2576-create-prometheus-handler     #792      +/-   ##
==================================================================
- Coverage                           98.01%   90.57%   -7.44%     
==================================================================
  Files                                 442      444       +2     
  Lines                               36582    36671      +89     
==================================================================
- Hits                                35855    33214    -2641     
- Misses                                727     3457    +2730     
Flag Coverage Δ
integration 90.57% <89.57%> (-7.44%) ⬇️
unit 90.57% <89.57%> (-7.44%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
NonTestCode 89.61% <81.81%> (-6.26%) ⬇️
OutsideTasks 95.60% <92.53%> (-2.40%) ⬇️
Files with missing lines Coverage Δ
services/repository.py 95.12% <100.00%> (-1.64%) ⬇️
tasks/tests/unit/test_upload_finisher_task.py 90.04% <100.00%> (-9.96%) ⬇️
tasks/upload_processor.py 92.22% <100.00%> (-7.20%) ⬇️
services/processing/merging.py 33.33% <0.00%> (-60.42%) ⬇️
tasks/tests/unit/test_upload_processing_task.py 75.73% <93.10%> (-24.27%) ⬇️
...checkpoint_logger/tests/test_prometheus_handler.py 94.64% <94.64%> (ø)
tasks/upload_finisher.py 81.90% <54.54%> (-15.77%) ⬇️

... and 58 files with indirect coverage changes

@codecov-qa
Copy link

codecov-qa bot commented Oct 16, 2024

❌ 1 Tests Failed:

Tests completed Failed Passed Skipped
1708 1 1707 0
View the top 1 failed tests by shortest run time
helpers.checkpoint_logger.tests.test_prometheus_handler.TestCheckpointLogger test_submit_subflow
Stack Traces | 0.03s run time
self = &lt;test_prometheus_handler.TestCheckpointLogger testMethod=test_submit_subflow&gt;
mock_set_measurement = &lt;MagicMock name='set_measurement' id='140675383214640'&gt;
mock_prometheus_handler = &lt;MagicMock name='PROMETHEUS_HANDLER' id='140675382273248'&gt;

    @patch('helpers.checkpoint_logger.PROMETHEUS_HANDLER')
    @patch('sentry_sdk.set_measurement')
    def test_submit_subflow(self, mock_set_measurement, mock_prometheus_handler):
        # We need to mock the CheckpointLogger class and its methods
        mock_checkpoint_logger = MagicMock()
        mock_checkpoint_logger.cls.__name__ = "TestFlow"
    
        # Mock the start and end events
        mock_start = MagicMock()
        mock_end = MagicMock()
    
        # Set up the mock timing
        mock_checkpoint_logger.timing.return_value = 5000  # 5 seconds in milliseconds
    
        # Call the submit_subflow method
        from helpers.checkpoint_logger import CheckpointLogger
&gt;       CheckpointLogger.submit_subflow(mock_checkpoint_logger, "test_metric", mock_start, mock_end)

.../checkpoint_logger/tests/test_prometheus_handler.py:72: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
.../local/lib/python3.12/unittest/mock.py:1139: in __call__
    return self._mock_call(*args, **kwargs)
.../local/lib/python3.12/unittest/mock.py:1143: in _mock_call
    return self._execute_mock_call(*args, **kwargs)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = &lt;Mock id='140675383214736'&gt;
args = (&lt;MagicMock id='140675383222896'&gt;, 'test_metric', &lt;MagicMock id='140675369231920'&gt;, &lt;MagicMock id='140675382273584'&gt;)
kwargs = {}
effect = &lt;function mock_checkpoint_submit.&lt;locals&gt;.mock_submit_fn at 0x7ff18a3cc4a0&gt;

    def _execute_mock_call(self, /, *args, **kwargs):
        # separate from _increment_mock_call so that awaited functions are
        # executed separately from their call, also AsyncMock overrides this method
    
        effect = self.side_effect
        if effect is not None:
            if _is_exception(effect):
                raise effect
            elif not _callable(effect):
                result = next(effect)
                if _is_exception(result):
                    raise result
            else:
&gt;               result = effect(*args, **kwargs)
E               TypeError: mock_checkpoint_submit.&lt;locals&gt;.mock_submit_fn() takes 3 positional arguments but 4 were given

.../local/lib/python3.12/unittest/mock.py:1204: TypeError

To view individual test run time comparison to the main branch, go to the Test Analytics Dashboard

Copy link

Test Failures Detected: Due to failing tests, we cannot provide coverage reports at this time.

❌ Failed Test Results:

Completed 1708 tests with 1 failed, 1707 passed and 0 skipped.

View the full list of failed tests

pytest

  • Class name: helpers.checkpoint_logger.tests.test_prometheus_handler.TestCheckpointLogger
    Test name: test_submit_subflow

    self = <test_prometheus_handler.TestCheckpointLogger testMethod=test_submit_subflow>
    mock_set_measurement = <MagicMock name='set_measurement' id='140675383214640'>
    mock_prometheus_handler = <MagicMock name='PROMETHEUS_HANDLER' id='140675382273248'>

    @patch('helpers.checkpoint_logger.PROMETHEUS_HANDLER')
    @patch('sentry_sdk.set_measurement')
    def test_submit_subflow(self, mock_set_measurement, mock_prometheus_handler):
    # We need to mock the CheckpointLogger class and its methods
    mock_checkpoint_logger = MagicMock()
    mock_checkpoint_logger.cls.__name__ = "TestFlow"

    # Mock the start and end events
    mock_start = MagicMock()
    mock_end = MagicMock()

    # Set up the mock timing
    mock_checkpoint_logger.timing.return_value = 5000 # 5 seconds in milliseconds

    # Call the submit_subflow method
    from helpers.checkpoint_logger import CheckpointLogger
    > CheckpointLogger.submit_subflow(mock_checkpoint_logger, "test_metric", mock_start, mock_end)

    .../checkpoint_logger/tests/test_prometheus_handler.py:72:
    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
    .../local/lib/python3.12/unittest/mock.py:1139: in __call__
    return self._mock_call(*args, **kwargs)
    .../local/lib/python3.12/unittest/mock.py:1143: in _mock_call
    return self._execute_mock_call(*args, **kwargs)
    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    self = <Mock id='140675383214736'>
    args = (<MagicMock id='140675383222896'>, 'test_metric', <MagicMock id='140675369231920'>, <MagicMock id='140675382273584'>)
    kwargs = {}
    effect = <function mock_checkpoint_submit.<locals>.mock_submit_fn at 0x7ff18a3cc4a0>

    def _execute_mock_call(self, /, *args, **kwargs):
    # separate from _increment_mock_call so that awaited functions are
    # executed separately from their call, also AsyncMock overrides this method

    effect = self.side_effect
    if effect is not None:
    if _is_exception(effect):
    raise effect
    elif not _callable(effect):
    result = next(effect)
    if _is_exception(result):
    raise result
    else:
    > result = effect(*args, **kwargs)
    E TypeError: mock_checkpoint_submit.<locals>.mock_submit_fn() takes 3 positional arguments but 4 were given

    .../local/lib/python3.12/unittest/mock.py:1204: TypeError

Copy link

codecov bot commented Oct 16, 2024

❌ 1 Tests Failed:

Tests completed Failed Passed Skipped
1708 1 1707 0
View the top 1 failed tests by shortest run time
helpers.checkpoint_logger.tests.test_prometheus_handler.TestCheckpointLogger test_submit_subflow
Stack Traces | 0.03s run time
self = &lt;test_prometheus_handler.TestCheckpointLogger testMethod=test_submit_subflow&gt;
mock_set_measurement = &lt;MagicMock name='set_measurement' id='140675383214640'&gt;
mock_prometheus_handler = &lt;MagicMock name='PROMETHEUS_HANDLER' id='140675382273248'&gt;

    @patch('helpers.checkpoint_logger.PROMETHEUS_HANDLER')
    @patch('sentry_sdk.set_measurement')
    def test_submit_subflow(self, mock_set_measurement, mock_prometheus_handler):
        # We need to mock the CheckpointLogger class and its methods
        mock_checkpoint_logger = MagicMock()
        mock_checkpoint_logger.cls.__name__ = "TestFlow"
    
        # Mock the start and end events
        mock_start = MagicMock()
        mock_end = MagicMock()
    
        # Set up the mock timing
        mock_checkpoint_logger.timing.return_value = 5000  # 5 seconds in milliseconds
    
        # Call the submit_subflow method
        from helpers.checkpoint_logger import CheckpointLogger
&gt;       CheckpointLogger.submit_subflow(mock_checkpoint_logger, "test_metric", mock_start, mock_end)

.../checkpoint_logger/tests/test_prometheus_handler.py:72: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
.../local/lib/python3.12/unittest/mock.py:1139: in __call__
    return self._mock_call(*args, **kwargs)
.../local/lib/python3.12/unittest/mock.py:1143: in _mock_call
    return self._execute_mock_call(*args, **kwargs)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = &lt;Mock id='140675383214736'&gt;
args = (&lt;MagicMock id='140675383222896'&gt;, 'test_metric', &lt;MagicMock id='140675369231920'&gt;, &lt;MagicMock id='140675382273584'&gt;)
kwargs = {}
effect = &lt;function mock_checkpoint_submit.&lt;locals&gt;.mock_submit_fn at 0x7ff18a3cc4a0&gt;

    def _execute_mock_call(self, /, *args, **kwargs):
        # separate from _increment_mock_call so that awaited functions are
        # executed separately from their call, also AsyncMock overrides this method
    
        effect = self.side_effect
        if effect is not None:
            if _is_exception(effect):
                raise effect
            elif not _callable(effect):
                result = next(effect)
                if _is_exception(result):
                    raise result
            else:
&gt;               result = effect(*args, **kwargs)
E               TypeError: mock_checkpoint_submit.&lt;locals&gt;.mock_submit_fn() takes 3 positional arguments but 4 were given

.../local/lib/python3.12/unittest/mock.py:1204: TypeError

To view individual test run time comparison to the main branch, go to the Test Analytics Dashboard

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants