Просмотр исходного кода

perf: move tests/sentry_plugins to use orjson (#71050)

Part of `orjson` move to get rid of `sentry.utils.json`
Yagiz Nizipli 9 месяцев назад
Родитель
Сommit
8c090965b6

+ 16 - 7
tests/sentry_plugins/amazon_sqs/test_plugin.py

@@ -1,11 +1,11 @@
 from functools import cached_property
 from functools import cached_property
 from unittest.mock import patch
 from unittest.mock import patch
 
 
+import orjson
 import pytest
 import pytest
 from botocore.client import ClientError
 from botocore.client import ClientError
 
 
 from sentry.testutils.cases import PluginTestCase
 from sentry.testutils.cases import PluginTestCase
-from sentry.utils import json
 from sentry_plugins.amazon_sqs.plugin import AmazonSQSPlugin
 from sentry_plugins.amazon_sqs.plugin import AmazonSQSPlugin
 
 
 
 
@@ -53,7 +53,9 @@ class AmazonSQSPluginTest(PluginTestCase):
         )
         )
         mock_client.return_value.send_message.assert_called_once_with(
         mock_client.return_value.send_message.assert_called_once_with(
             QueueUrl="https://sqs.us-east-1.amazonaws.com/12345678/myqueue",
             QueueUrl="https://sqs.us-east-1.amazonaws.com/12345678/myqueue",
-            MessageBody=json.dumps(self.plugin.get_event_payload(event)),
+            MessageBody=orjson.dumps(
+                self.plugin.get_event_payload(event), option=orjson.OPT_UTC_Z
+            ).decode(),
         )
         )
 
 
     @patch("sentry_plugins.amazon_sqs.plugin.logger")
     @patch("sentry_plugins.amazon_sqs.plugin.logger")
@@ -106,7 +108,9 @@ class AmazonSQSPluginTest(PluginTestCase):
 
 
         mock_client.return_value.send_message.assert_called_once_with(
         mock_client.return_value.send_message.assert_called_once_with(
             QueueUrl="https://sqs.us-east-1.amazonaws.com/12345678/myqueue",
             QueueUrl="https://sqs.us-east-1.amazonaws.com/12345678/myqueue",
-            MessageBody=json.dumps(self.plugin.get_event_payload(event)),
+            MessageBody=orjson.dumps(
+                self.plugin.get_event_payload(event), option=orjson.OPT_UTC_Z
+            ).decode(),
             MessageGroupId="my_group",
             MessageGroupId="my_group",
             MessageDeduplicationId="abc123",
             MessageDeduplicationId="abc123",
         )
         )
@@ -120,16 +124,21 @@ class AmazonSQSPluginTest(PluginTestCase):
 
 
         mock_client.return_value.send_message.assert_called_once_with(
         mock_client.return_value.send_message.assert_called_once_with(
             QueueUrl="https://sqs.us-east-1.amazonaws.com/12345678/myqueue",
             QueueUrl="https://sqs.us-east-1.amazonaws.com/12345678/myqueue",
-            MessageBody=json.dumps(
+            MessageBody=orjson.dumps(
                 {
                 {
                     "s3Url": f"https://my_bucket.s3-us-east-1.amazonaws.com/{key}",
                     "s3Url": f"https://my_bucket.s3-us-east-1.amazonaws.com/{key}",
                     "eventID": event.event_id,
                     "eventID": event.event_id,
-                }
-            ),
+                },
+                option=orjson.OPT_UTC_Z,
+            ).decode(),
         )
         )
 
 
         mock_client.return_value.put_object.assert_called_once_with(
         mock_client.return_value.put_object.assert_called_once_with(
-            Bucket="my_bucket", Body=json.dumps(self.plugin.get_event_payload(event)), Key=key
+            Bucket="my_bucket",
+            Body=orjson.dumps(
+                self.plugin.get_event_payload(event), option=orjson.OPT_UTC_Z
+            ).decode(),
+            Key=key,
         )
         )
 
 
     @patch("sentry_plugins.amazon_sqs.plugin.logger")
     @patch("sentry_plugins.amazon_sqs.plugin.logger")

+ 3 - 3
tests/sentry_plugins/asana/test_plugin.py

@@ -1,5 +1,6 @@
 from functools import cached_property
 from functools import cached_property
 
 
+import orjson
 import pytest
 import pytest
 import responses
 import responses
 from django.contrib.auth.models import AnonymousUser
 from django.contrib.auth.models import AnonymousUser
@@ -7,7 +8,6 @@ from django.test import RequestFactory
 
 
 from sentry.exceptions import PluginError
 from sentry.exceptions import PluginError
 from sentry.testutils.cases import PluginTestCase
 from sentry.testutils.cases import PluginTestCase
-from sentry.utils import json
 from sentry_plugins.asana.plugin import AsanaPlugin
 from sentry_plugins.asana.plugin import AsanaPlugin
 
 
 
 
@@ -65,7 +65,7 @@ class AsanaPluginTest(PluginTestCase):
 
 
         assert self.plugin.create_issue(request, group, form_data) == 1
         assert self.plugin.create_issue(request, group, form_data) == 1
         request = responses.calls[0].request
         request = responses.calls[0].request
-        payload = json.loads(request.body)
+        payload = orjson.loads(request.body)
         assert payload == {"data": {"notes": "Fix this.", "name": "Hello", "workspace": "12345678"}}
         assert payload == {"data": {"notes": "Fix this.", "name": "Hello", "workspace": "12345678"}}
 
 
     @responses.activate
     @responses.activate
@@ -119,5 +119,5 @@ class AsanaPluginTest(PluginTestCase):
 
 
         assert self.plugin.link_issue(request, group, form_data) == {"title": "Hello"}
         assert self.plugin.link_issue(request, group, form_data) == {"title": "Hello"}
         request = responses.calls[-1].request
         request = responses.calls[-1].request
-        payload = json.loads(request.body)
+        payload = orjson.loads(request.body)
         assert payload == {"data": {"text": "please fix this"}}
         assert payload == {"data": {"text": "please fix this"}}

+ 3 - 3
tests/sentry_plugins/github/test_plugin.py

@@ -1,5 +1,6 @@
 from functools import cached_property
 from functools import cached_property
 
 
+import orjson
 import pytest
 import pytest
 import responses
 import responses
 from django.contrib.auth.models import AnonymousUser
 from django.contrib.auth.models import AnonymousUser
@@ -7,7 +8,6 @@ from django.test import RequestFactory
 
 
 from sentry.exceptions import PluginError
 from sentry.exceptions import PluginError
 from sentry.testutils.cases import PluginTestCase
 from sentry.testutils.cases import PluginTestCase
-from sentry.utils import json
 from sentry_plugins.github.plugin import GitHubPlugin
 from sentry_plugins.github.plugin import GitHubPlugin
 
 
 
 
@@ -66,7 +66,7 @@ class GitHubPluginTest(PluginTestCase):
         assert self.plugin.create_issue(request, group, form_data) == 1
         assert self.plugin.create_issue(request, group, form_data) == 1
         request = responses.calls[0].request
         request = responses.calls[0].request
         assert request.headers["Authorization"] == "Bearer foo"
         assert request.headers["Authorization"] == "Bearer foo"
-        payload = json.loads(request.body)
+        payload = orjson.loads(request.body)
         assert payload == {"title": "Hello", "body": "Fix this.", "assignee": None}
         assert payload == {"title": "Hello", "body": "Fix this.", "assignee": None}
 
 
     @responses.activate
     @responses.activate
@@ -100,5 +100,5 @@ class GitHubPluginTest(PluginTestCase):
         assert self.plugin.link_issue(request, group, form_data) == {"title": "Hello world"}
         assert self.plugin.link_issue(request, group, form_data) == {"title": "Hello world"}
         request = responses.calls[-1].request
         request = responses.calls[-1].request
         assert request.headers["Authorization"] == "Bearer foo"
         assert request.headers["Authorization"] == "Bearer foo"
-        payload = json.loads(request.body)
+        payload = orjson.loads(request.body)
         assert payload == {"body": "Hello"}
         assert payload == {"body": "Hello"}

+ 6 - 6
tests/sentry_plugins/github/test_provider.py

@@ -1,6 +1,7 @@
 from functools import cached_property
 from functools import cached_property
 from unittest.mock import patch
 from unittest.mock import patch
 
 
+import orjson
 import responses
 import responses
 
 
 from sentry.models.integrations.organization_integration import OrganizationIntegration
 from sentry.models.integrations.organization_integration import OrganizationIntegration
@@ -8,7 +9,6 @@ from sentry.models.repository import Repository
 from sentry.silo.base import SiloMode
 from sentry.silo.base import SiloMode
 from sentry.testutils.cases import TestCase
 from sentry.testutils.cases import TestCase
 from sentry.testutils.silo import assume_test_silo_mode
 from sentry.testutils.silo import assume_test_silo_mode
-from sentry.utils import json
 from sentry_plugins.github.client import GithubPluginAppsClient, GithubPluginClient
 from sentry_plugins.github.client import GithubPluginAppsClient, GithubPluginClient
 from sentry_plugins.github.plugin import GitHubAppsRepositoryProvider, GitHubRepositoryProvider
 from sentry_plugins.github.plugin import GitHubAppsRepositoryProvider, GitHubRepositoryProvider
 from sentry_plugins.github.testutils import (
 from sentry_plugins.github.testutils import (
@@ -27,7 +27,7 @@ class GitHubPluginTest(TestCase):
     def test_compare_commits(self):
     def test_compare_commits(self):
         repo = Repository.objects.create(provider="github", name="example", organization_id=1)
         repo = Repository.objects.create(provider="github", name="example", organization_id=1)
 
 
-        res = self.provider._format_commits(repo, json.loads(COMPARE_COMMITS_EXAMPLE)["commits"])
+        res = self.provider._format_commits(repo, orjson.loads(COMPARE_COMMITS_EXAMPLE)["commits"])
 
 
         assert res == [
         assert res == [
             {
             {
@@ -42,7 +42,7 @@ class GitHubPluginTest(TestCase):
     def test_get_last_commits(self):
     def test_get_last_commits(self):
         repo = Repository.objects.create(provider="github", name="example", organization_id=1)
         repo = Repository.objects.create(provider="github", name="example", organization_id=1)
 
 
-        res = self.provider._format_commits(repo, json.loads(GET_LAST_COMMITS_EXAMPLE)[:10])
+        res = self.provider._format_commits(repo, orjson.loads(GET_LAST_COMMITS_EXAMPLE)[:10])
 
 
         assert res == [
         assert res == [
             {
             {
@@ -80,7 +80,7 @@ class GitHubPluginTest(TestCase):
         }
         }
 
 
         request = responses.calls[-1].request
         request = responses.calls[-1].request
-        req_json = json.loads(request.body)
+        req_json = orjson.loads(request.body)
         assert req_json == {
         assert req_json == {
             "active": True,
             "active": True,
             "config": {
             "config": {
@@ -178,12 +178,12 @@ class GitHubAppsProviderTest(TestCase):
     @patch.object(
     @patch.object(
         GithubPluginAppsClient,
         GithubPluginAppsClient,
         "get_repositories",
         "get_repositories",
-        return_value=json.loads(INSTALLATION_REPOSITORIES_API_RESPONSE),
+        return_value=orjson.loads(INSTALLATION_REPOSITORIES_API_RESPONSE),
     )
     )
     @patch.object(
     @patch.object(
         GithubPluginClient,
         GithubPluginClient,
         "get_installations",
         "get_installations",
-        return_value=json.loads(LIST_INSTALLATION_API_RESPONSE),
+        return_value=orjson.loads(LIST_INSTALLATION_API_RESPONSE),
     )
     )
     def test_link_auth(self, *args):
     def test_link_auth(self, *args):
         user = self.create_user()
         user = self.create_user()

+ 4 - 4
tests/sentry_plugins/gitlab/test_plugin.py

@@ -1,11 +1,11 @@
 from functools import cached_property
 from functools import cached_property
 
 
+import orjson
 import responses
 import responses
 from django.test import RequestFactory
 from django.test import RequestFactory
 from django.urls import reverse
 from django.urls import reverse
 
 
 from sentry.testutils.cases import PluginTestCase
 from sentry.testutils.cases import PluginTestCase
-from sentry.utils import json
 from sentry_plugins.gitlab.plugin import GitLabPlugin
 from sentry_plugins.gitlab.plugin import GitLabPlugin
 
 
 
 
@@ -67,7 +67,7 @@ class GitLabPluginTest(PluginTestCase):
 
 
         assert self.plugin.create_issue(request, group, form_data) == 1
         assert self.plugin.create_issue(request, group, form_data) == 1
         request = responses.calls[0].request
         request = responses.calls[0].request
-        payload = json.loads(request.body)
+        payload = orjson.loads(request.body)
         assert payload == {
         assert payload == {
             "title": "Hello",
             "title": "Hello",
             "description": "Fix this.",
             "description": "Fix this.",
@@ -101,7 +101,7 @@ class GitLabPluginTest(PluginTestCase):
 
 
         assert self.plugin.link_issue(request, group, form_data) == {"title": "Hello world"}
         assert self.plugin.link_issue(request, group, form_data) == {"title": "Hello world"}
         request = responses.calls[-1].request
         request = responses.calls[-1].request
-        payload = json.loads(request.body)
+        payload = orjson.loads(request.body)
         assert payload == {"body": "Hello"}
         assert payload == {"body": "Hello"}
 
 
     def test_no_secrets(self):
     def test_no_secrets(self):
@@ -117,7 +117,7 @@ class GitLabPluginTest(PluginTestCase):
             "sentry-api-0-project-plugin-details", args=[self.org.slug, self.project.slug, "gitlab"]
             "sentry-api-0-project-plugin-details", args=[self.org.slug, self.project.slug, "gitlab"]
         )
         )
         res = self.client.get(url)
         res = self.client.get(url)
-        config = json.loads(res.content)["config"]
+        config = orjson.loads(res.content)["config"]
         token_config = [item for item in config if item["name"] == "gitlab_token"][0]
         token_config = [item for item in config if item["name"] == "gitlab_token"][0]
         assert token_config.get("type") == "secret"
         assert token_config.get("type") == "secret"
         assert token_config.get("value") is None
         assert token_config.get("value") is None

+ 6 - 6
tests/sentry_plugins/heroku/test_plugin.py

@@ -4,6 +4,7 @@ from datetime import timedelta
 from typing import Any
 from typing import Any
 from unittest.mock import Mock, patch
 from unittest.mock import Mock, patch
 
 
+import orjson
 import pytest
 import pytest
 from django.utils import timezone
 from django.utils import timezone
 
 
@@ -17,7 +18,6 @@ from sentry.models.releasecommit import ReleaseCommit
 from sentry.models.releaseheadcommit import ReleaseHeadCommit
 from sentry.models.releaseheadcommit import ReleaseHeadCommit
 from sentry.models.repository import Repository
 from sentry.models.repository import Repository
 from sentry.testutils.cases import TestCase
 from sentry.testutils.cases import TestCase
-from sentry.utils import json
 from sentry_plugins.heroku.plugin import HerokuReleaseHook
 from sentry_plugins.heroku.plugin import HerokuReleaseHook
 
 
 
 
@@ -146,7 +146,7 @@ class HookHandleTest(TestCase):
             },
             },
             "action": "update",
             "action": "update",
         }
         }
-        req.body = json.dumps(body).encode()
+        req.body = orjson.dumps(body)
         hook.handle(req)
         hook.handle(req)
         assert Release.objects.filter(version=body["data"]["slug"]["commit"]).exists()
         assert Release.objects.filter(version=body["data"]["slug"]["commit"]).exists()
         assert set_refs_mock.call_count == 1
         assert set_refs_mock.call_count == 1
@@ -167,7 +167,7 @@ class HookHandleTest(TestCase):
             },
             },
             "action": "create",
             "action": "create",
         }
         }
-        req.body = json.dumps(body).encode()
+        req.body = orjson.dumps(body)
         hook.handle(req)
         hook.handle(req)
         assert not Release.objects.filter(version=body["data"]["slug"]["commit"]).exists()
         assert not Release.objects.filter(version=body["data"]["slug"]["commit"]).exists()
         assert set_refs_mock.call_count == 0
         assert set_refs_mock.call_count == 0
@@ -188,7 +188,7 @@ class HookHandleTest(TestCase):
             },
             },
             "action": "update",
             "action": "update",
         }
         }
-        req.body = json.dumps(body).encode()
+        req.body = orjson.dumps(body)
         hook.handle(req)
         hook.handle(req)
         assert Release.objects.filter(version=body["data"]["slug"]["commit"]).exists()
         assert Release.objects.filter(version=body["data"]["slug"]["commit"]).exists()
         assert set_refs_mock.call_count == 1
         assert set_refs_mock.call_count == 1
@@ -208,7 +208,7 @@ class HookHandleTest(TestCase):
             },
             },
             "action": "update",
             "action": "update",
         }
         }
-        req.body = json.dumps(body).encode()
+        req.body = orjson.dumps(body)
         hook.handle(req)
         hook.handle(req)
         assert Release.objects.filter(version=body["data"]["slug"]["commit"]).exists()
         assert Release.objects.filter(version=body["data"]["slug"]["commit"]).exists()
 
 
@@ -226,6 +226,6 @@ class HookHandleTest(TestCase):
             },
             },
             "action": "update",
             "action": "update",
         }
         }
-        req.body = json.dumps(body).encode()
+        req.body = orjson.dumps(body)
         with pytest.raises(HookValidationError):
         with pytest.raises(HookValidationError):
             hook.handle(req)
             hook.handle(req)

+ 2 - 2
tests/sentry_plugins/jira/test_plugin.py

@@ -3,13 +3,13 @@ from __future__ import annotations
 from functools import cached_property
 from functools import cached_property
 from typing import Any
 from typing import Any
 
 
+import orjson
 import responses
 import responses
 from django.contrib.auth.models import AnonymousUser
 from django.contrib.auth.models import AnonymousUser
 from django.test import RequestFactory
 from django.test import RequestFactory
 from django.urls import reverse
 from django.urls import reverse
 
 
 from sentry.testutils.cases import TestCase
 from sentry.testutils.cases import TestCase
-from sentry.utils import json
 from sentry_plugins.jira.plugin import JiraPlugin
 from sentry_plugins.jira.plugin import JiraPlugin
 
 
 create_meta_response = {
 create_meta_response = {
@@ -273,7 +273,7 @@ class JiraPluginTest(TestCase):
             "sentry-api-0-project-plugin-details", args=[self.org.slug, self.project.slug, "jira"]
             "sentry-api-0-project-plugin-details", args=[self.org.slug, self.project.slug, "jira"]
         )
         )
         res = self.client.get(url)
         res = self.client.get(url)
-        config = json.loads(res.content)["config"]
+        config = orjson.loads(res.content)["config"]
         password_config = [item for item in config if item["name"] == "password"][0]
         password_config = [item for item in config if item["name"] == "password"][0]
         assert password_config.get("type") == "secret"
         assert password_config.get("type") == "secret"
         assert password_config.get("value") is None
         assert password_config.get("value") is None

+ 2 - 2
tests/sentry_plugins/opgsenie/test_plugin.py

@@ -1,11 +1,11 @@
 from functools import cached_property
 from functools import cached_property
 
 
+import orjson
 import responses
 import responses
 
 
 from sentry.models.rule import Rule
 from sentry.models.rule import Rule
 from sentry.plugins.base import Notification
 from sentry.plugins.base import Notification
 from sentry.testutils.cases import PluginTestCase
 from sentry.testutils.cases import PluginTestCase
-from sentry.utils import json
 from sentry_plugins.opsgenie.plugin import OpsGeniePlugin
 from sentry_plugins.opsgenie.plugin import OpsGeniePlugin
 
 
 
 
@@ -54,7 +54,7 @@ class OpsGeniePluginTest(PluginTestCase):
             self.plugin.notify(notification)
             self.plugin.notify(notification)
 
 
         request = responses.calls[0].request
         request = responses.calls[0].request
-        payload = json.loads(request.body)
+        payload = orjson.loads(request.body)
         group_id = str(group.id)
         group_id = str(group.id)
         assert payload == {
         assert payload == {
             "recipients": "me",
             "recipients": "me",

+ 3 - 3
tests/sentry_plugins/pagerduty/test_plugin.py

@@ -1,12 +1,12 @@
 from functools import cached_property
 from functools import cached_property
 
 
+import orjson
 import responses
 import responses
 from django.urls import reverse
 from django.urls import reverse
 
 
 from sentry.models.rule import Rule
 from sentry.models.rule import Rule
 from sentry.plugins.base import Notification
 from sentry.plugins.base import Notification
 from sentry.testutils.cases import PluginTestCase
 from sentry.testutils.cases import PluginTestCase
-from sentry.utils import json
 from sentry_plugins.pagerduty.plugin import PagerDutyPlugin
 from sentry_plugins.pagerduty.plugin import PagerDutyPlugin
 
 
 INVALID_METHOD = (
 INVALID_METHOD = (
@@ -70,7 +70,7 @@ class PagerDutyPluginTest(PluginTestCase):
             self.plugin.notify(notification)
             self.plugin.notify(notification)
 
 
         request = responses.calls[0].request
         request = responses.calls[0].request
-        payload = json.loads(request.body)
+        payload = orjson.loads(request.body)
         assert payload == {
         assert payload == {
             "client_url": "http://example.com",
             "client_url": "http://example.com",
             "event_type": "trigger",
             "event_type": "trigger",
@@ -109,7 +109,7 @@ class PagerDutyPluginTest(PluginTestCase):
             args=[self.org.slug, self.project.slug, "pagerduty"],
             args=[self.org.slug, self.project.slug, "pagerduty"],
         )
         )
         res = self.client.get(url)
         res = self.client.get(url)
-        config = json.loads(res.content)["config"]
+        config = orjson.loads(res.content)["config"]
         key_config = [item for item in config if item["name"] == "service_key"][0]
         key_config = [item for item in config if item["name"] == "service_key"][0]
         assert key_config.get("type") == "secret"
         assert key_config.get("type") == "secret"
         assert key_config.get("value") is None
         assert key_config.get("value") is None

+ 2 - 2
tests/sentry_plugins/pivotal/test_pivotal_plugin.py

@@ -1,9 +1,9 @@
 from functools import cached_property
 from functools import cached_property
 
 
+import orjson
 from django.urls import reverse
 from django.urls import reverse
 
 
 from sentry.testutils.cases import PluginTestCase
 from sentry.testutils.cases import PluginTestCase
-from sentry.utils import json
 from sentry_plugins.pivotal.plugin import PivotalPlugin
 from sentry_plugins.pivotal.plugin import PivotalPlugin
 
 
 
 
@@ -44,7 +44,7 @@ class PivotalPluginTest(PluginTestCase):
             args=[self.org.slug, self.project.slug, "pivotal"],
             args=[self.org.slug, self.project.slug, "pivotal"],
         )
         )
         res = self.client.get(url)
         res = self.client.get(url)
-        config = json.loads(res.content)["config"]
+        config = orjson.loads(res.content)["config"]
         token_config = [item for item in config if item["name"] == "token"][0]
         token_config = [item for item in config if item["name"] == "token"][0]
         assert token_config.get("type") == "secret"
         assert token_config.get("type") == "secret"
         assert token_config.get("value") is None
         assert token_config.get("value") is None

Некоторые файлы не были показаны из-за большого количества измененных файлов