Skip to content

Commit

Permalink
ref: don't guess about str / bytes in tests (#53366)
Browse files Browse the repository at this point in the history
force_str / force_bytes really should not be used -- these are the easy
ones to fix




<!-- Describe your PR here. -->
  • Loading branch information
asottile-sentry authored Jul 21, 2023
1 parent 446ea57 commit 91f9f3c
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 23 deletions.
9 changes: 4 additions & 5 deletions tests/relay_integration/lang/javascript/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import pytest
import responses
from django.utils.encoding import force_bytes

from sentry.models import (
ArtifactBundle,
Expand Down Expand Up @@ -183,7 +182,7 @@ def test_source_expansion(self, mock_fetch_by_url):
},
}

mock_fetch_by_url.return_value.body = force_bytes("\n".join("hello world"))
mock_fetch_by_url.return_value.body = "\n".join("hello world").encode()
mock_fetch_by_url.return_value.encoding = None
mock_fetch_by_url.return_value.headers = {}

Expand Down Expand Up @@ -236,7 +235,7 @@ def test_inlined_sources(self, mock_discover_sourcemap, mock_fetch_by_url):
mock_discover_sourcemap.return_value = BASE64_SOURCEMAP

mock_fetch_by_url.return_value.url = "http://example.com/test.min.js"
mock_fetch_by_url.return_value.body = force_bytes("\n".join("<generated source>"))
mock_fetch_by_url.return_value.body = "\n".join("<generated source>").encode()
mock_fetch_by_url.return_value.encoding = None

event = self.post_and_retrieve_event(data)
Expand Down Expand Up @@ -283,7 +282,7 @@ def test_invalid_base64_sourcemap_returns_an_error(
mock_discover_sourcemap.return_value = INVALID_BASE64_SOURCEMAP

mock_fetch_by_url.return_value.url = "http://example.com/test.min.js"
mock_fetch_by_url.return_value.body = force_bytes("\n".join("<generated source>"))
mock_fetch_by_url.return_value.body = "\n".join("<generated source>").encode()
mock_fetch_by_url.return_value.encoding = None

event = self.post_and_retrieve_event(data)
Expand Down Expand Up @@ -342,7 +341,7 @@ def test_sourcemap_cache_is_constructed_only_once_if_an_error_is_raised(
mock_discover_sourcemap.return_value = BASE64_SOURCEMAP

mock_fetch_by_url.return_value.url = "http://example.com/test.min.js"
mock_fetch_by_url.return_value.body = force_bytes("\n".join("<generated source>"))
mock_fetch_by_url.return_value.body = "\n".join("<generated source>").encode()
mock_fetch_by_url.return_value.encoding = None

mock_from_bytes.side_effect = Exception()
Expand Down
9 changes: 4 additions & 5 deletions tests/sentry/buffer/test_redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import pytest
from django.utils import timezone
from django.utils.encoding import force_str
from freezegun import freeze_time

from sentry import options
Expand Down Expand Up @@ -141,8 +140,8 @@ def test_incr_saves_to_redis(self):
key = self.buf._make_key(model, filters=filters)
self.buf.incr(model, columns, filters, extra={"foo": "bar", "datetime": now})
result = client.hgetall(key)
# Force keys to strings
result = {force_str(k): v for k, v in result.items()}
if not self.buf.is_redis_cluster:
result = {k.decode(): v for k, v in result.items()}

f = result.pop("f")
if self.buf.is_redis_cluster:
Expand Down Expand Up @@ -171,8 +170,8 @@ def load_value(x):
assert pending == [key.encode("utf-8")]
self.buf.incr(model, columns, filters, extra={"foo": "baz", "datetime": now})
result = client.hgetall(key)
# Force keys to strings
result = {force_str(k): v for k, v in result.items()}
if not self.buf.is_redis_cluster:
result = {k.decode(): v for k, v in result.items()}
f = result.pop("f")
assert load_values(f) == {"pk": 1, "datetime": now}
assert load_value(result.pop("e+datetime")) == now
Expand Down
5 changes: 2 additions & 3 deletions tests/sentry/db/models/fields/test_jsonfield.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import pytest
from django import forms
from django.db import models
from django.utils.encoding import force_str

from sentry.db.models.fields.jsonfield import JSONField
from sentry.testutils import TestCase
Expand Down Expand Up @@ -91,7 +90,7 @@ def test_formfield_clean_blank(self):
formfield = field.formfield()
self.assertRaisesMessage(
forms.ValidationError,
force_str(formfield.error_messages["required"]),
str(formfield.error_messages["required"]),
formfield.clean,
value="",
)
Expand All @@ -101,7 +100,7 @@ def test_formfield_clean_none(self):
formfield = field.formfield()
self.assertRaisesMessage(
forms.ValidationError,
force_str(formfield.error_messages["required"]),
str(formfield.error_messages["required"]),
formfield.clean,
value=None,
)
Expand Down
14 changes: 6 additions & 8 deletions tests/sentry/db/postgres/test_base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from django.utils.encoding import force_bytes, force_str

from sentry.constants import MAX_CULPRIT_LENGTH
from sentry.testutils import TestCase

Expand All @@ -10,14 +8,14 @@ def test_null_bytes(self):

cursor = connection.cursor()
cursor.execute("SELECT %s", [b"Ma\x00tt"])
assert force_bytes(cursor.fetchone()[0]) == b"Matt"
assert bytes(cursor.fetchone()[0]) == b"Matt"

cursor.execute("SELECT %s", ["Ma\x00tt"])
assert cursor.fetchone()[0] == "Matt"

cursor = connection.cursor()
cursor.execute("SELECT %(name)s", {"name": b"Ma\x00tt"})
assert force_bytes(cursor.fetchone()[0]) == b"Matt"
assert bytes(cursor.fetchone()[0]) == b"Matt"

cursor.execute("SELECT %(name)s", {"name": "Ma\x00tt"})
assert cursor.fetchone()[0] == "Matt"
Expand All @@ -31,12 +29,12 @@ def test_null_bytes_at_max_len_bytes(self):
assert len(long_str) <= MAX_CULPRIT_LENGTH

cursor.execute("SELECT %s", [long_str])
long_str_from_db = force_bytes(cursor.fetchone()[0])
long_str_from_db = bytes(cursor.fetchone()[0])
assert long_str_from_db == (b"a" * (MAX_CULPRIT_LENGTH - 1))
assert len(long_str_from_db) <= MAX_CULPRIT_LENGTH

cursor.execute("SELECT %(long_str)s", {"long_str": long_str})
long_str_from_db = force_bytes(cursor.fetchone()[0])
long_str_from_db = bytes(cursor.fetchone()[0])
assert long_str_from_db == (b"a" * (MAX_CULPRIT_LENGTH - 1))
assert len(long_str_from_db) <= MAX_CULPRIT_LENGTH

Expand Down Expand Up @@ -65,9 +63,9 @@ def test_lone_surrogates(self):

bad_str = "Hello\ud83dWorld🇦🇹!"
cursor.execute("SELECT %s", [bad_str])
bad_str_from_db = force_str(cursor.fetchone()[0])
bad_str_from_db = cursor.fetchone()[0]
assert bad_str_from_db == "HelloWorld🇦🇹!"

cursor.execute("SELECT %(bad_str)s", {"bad_str": bad_str})
bad_str_from_db = force_str(cursor.fetchone()[0])
bad_str_from_db = cursor.fetchone()[0]
assert bad_str_from_db == "HelloWorld🇦🇹!"
3 changes: 1 addition & 2 deletions tests/sentry/web/frontend/test_account_identity.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import pytest
from django.urls import reverse
from django.utils.encoding import force_bytes

from sentry import identity
from sentry.identity.providers.dummy import DummyProvider
Expand Down Expand Up @@ -35,7 +34,7 @@ def test_associate_identity(self):
resp = self.client.post(path)

assert resp.status_code == 200
assert resp.content == force_bytes(DummyProvider.TEMPLATE)
assert resp.content == DummyProvider.TEMPLATE.encode()

resp = self.client.post(path, data={"email": "[email protected]"})
ident = Identity.objects.get(user=user)
Expand Down

0 comments on commit 91f9f3c

Please sign in to comment.