Skip to content

tests(code_mappings): Remove usage of called_with assertions #43199

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

Merged
merged 6 commits into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 18 additions & 35 deletions tests/sentry/integrations/github/test_integration.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import logging
from unittest.mock import MagicMock, patch
from urllib.parse import urlencode, urlparse

import pytest
import responses
from django.urls import reverse

Expand Down Expand Up @@ -72,10 +70,6 @@ class GitHubIntegrationTest(IntegrationTestCase):
provider = GitHubIntegrationProvider
base_url = "https://api.github.com"

@pytest.fixture(autouse=True)
def inject_fixtures(self, caplog):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not needed anymore since we won't do assertions in the logger.

self._caplog = caplog

def setUp(self):
super().setUp()

Expand Down Expand Up @@ -643,33 +637,22 @@ def test_get_trees_for_org(self):
),
}

with patch("sentry.integrations.utils.code_mapping.logger") as logger:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is mostly a left indent since we won't be patching the logger anymore.

assert not cache.get("githubtrees:repositories:Test-Organization")
# This allows checking for caching related output
self._caplog.set_level(logging.INFO, logger="sentry")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing this line.

# Check that the cache is clear
repo_key = "github:repo:Test-Organization/foo:source-code"
assert cache.get("githubtrees:repositories:foo:Test-Organization") is None
assert cache.get(repo_key) is None
trees = installation.get_trees_for_org()

# These checks are useful since they will be available in the GCP logs
for msg in [
"The Github App does not have access to Test-Organization/baz.",
"Caching trees for Test-Organization",
]:
assert logger.info.called_with(msg)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed this block.


assert cache.get("githubtrees:repositories:foo:Test-Organization") == [
{"full_name": "Test-Organization/foo", "default_branch": "master"},
{"full_name": "Test-Organization/bar", "default_branch": "main"},
{"full_name": "Test-Organization/baz", "default_branch": "master"},
{"full_name": "Test-Organization/xyz", "default_branch": "master"},
]
assert cache.get(repo_key) == ["src/sentry/api/endpoints/auth_login.py"]
assert trees == expected_trees
assert not cache.get("githubtrees:repositories:Test-Organization")
# Check that the cache is clear
repo_key = "github:repo:Test-Organization/foo:source-code"
assert cache.get("githubtrees:repositories:foo:Test-Organization") is None
assert cache.get(repo_key) is None
trees = installation.get_trees_for_org()

assert cache.get("githubtrees:repositories:foo:Test-Organization") == [
{"full_name": "Test-Organization/foo", "default_branch": "master"},
{"full_name": "Test-Organization/bar", "default_branch": "main"},
{"full_name": "Test-Organization/baz", "default_branch": "master"},
{"full_name": "Test-Organization/xyz", "default_branch": "master"},
]
assert cache.get(repo_key) == ["src/sentry/api/endpoints/auth_login.py"]
assert trees == expected_trees

# Calling a second time should produce the same results
trees = installation.get_trees_for_org()
assert logger.info.called_with("Using cached trees for Test-Organization.")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed this line.

assert trees == expected_trees
# Calling a second time should produce the same results
trees = installation.get_trees_for_org()
assert trees == expected_trees
12 changes: 2 additions & 10 deletions tests/sentry/tasks/test_derive_code_mappings.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ def generate_data(self, frames: List[Dict[str, Union[str, bool]]], platform: str
class TestTaskBehavior(BaseDeriveCodeMappings):
"""Test task behavior that is not language specific."""

@pytest.fixture(autouse=True)
def inject_fixtures(self, caplog):
self._caplog = caplog

def setUp(self):
super().setUp()
self.platform = "any"
Expand All @@ -57,11 +53,9 @@ def test_does_not_raise_installation_removed(self):
side_effect=ApiError(
'{"message":"Not Found","documentation_url":"https://docs.github.com/rest/reference/apps#create-an-installation-access-token-for-an-app"}'
),
), patch("sentry.integrations.utils.code_mapping.logger") as logger:
):
assert derive_code_mappings(self.project.id, self.event_data) is None

assert logger.warning.called_with("The org has uninstalled the Sentry App.")

def test_raises_other_api_errors(self):
with patch(
"sentry.integrations.github.client.GitHubClientMixin.get_trees_for_org",
Expand All @@ -74,12 +68,10 @@ def test_unable_to_get_lock(self):
with patch(
"sentry.integrations.github.client.GitHubClientMixin.get_trees_for_org",
side_effect=UnableToAcquireLock,
), patch("sentry.integrations.utils.code_mapping.logger") as logger:
):
with pytest.raises(UnableToAcquireLock):
derive_code_mappings(self.project.id, self.event_data)

assert logger.warning.called_with("derive_code_mappings.getting_lock_failed")


class TestJavascriptDeriveCodeMappings(BaseDeriveCodeMappings):
def setUp(self):
Expand Down
18 changes: 5 additions & 13 deletions tests/sentry/tasks/test_post_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from sentry.models.activity import ActivityIntegration
from sentry.ownership.grammar import Matcher, Owner, Rule, dump_schema
from sentry.rules import init_registry
from sentry.tasks.derive_code_mappings import SUPPORTED_LANGUAGES
from sentry.tasks.merge import merge_groups
from sentry.tasks.post_process import post_process_group, process_event
from sentry.testutils import SnubaTestCase, TestCase
Expand Down Expand Up @@ -173,19 +174,10 @@ def test_derive_invalid_platform(self, mock_derive_code_mappings):
assert mock_derive_code_mappings.delay.call_count == 0

@patch("sentry.tasks.derive_code_mappings.derive_code_mappings")
def test_derive_python(self, mock_derive_code_mappings):
data = {"platform": "python"}
self._call_post_process_group(data)
assert mock_derive_code_mappings.delay.call_count == 1
assert mock_derive_code_mappings.delay.called_with(self.project.id, data, False)

@patch("sentry.tasks.derive_code_mappings.derive_code_mappings")
def test_derive_js(self, mock_derive_code_mappings):
data = {"platform": "javascript"}
self._call_post_process_group(data)
assert mock_derive_code_mappings.delay.call_count == 1
# Because we only run on dry run mode even if the official flag is set
assert mock_derive_code_mappings.delay.called_with(self.project.id, data, True)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried switching to assert_called_with, however, the NodeData object event though it was the same was not handling well comparing one of it's properties. IIRC correctly, a dictionary of properties would not compared to be the same because the key/values were out of order.

Instead of working to fix the comparison I decided to remove it. If we ever change the delay call, we should spend time to change it to not use a NodeData by extracting the information we need (e.g. stacktrace and project info).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing the test seems okay. FYI Python introduced ordering in dictionaries, based on order of insertion as of 3.6 or 3.7.

def test_derive_supported_languages(self, mock_derive_code_mappings):
for platform in SUPPORTED_LANGUAGES:
self._call_post_process_group({"platform": platform})
assert mock_derive_code_mappings.delay.call_count == 1


class RuleProcessorTestMixin(BasePostProgressGroupMixin):
Expand Down