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

Use fuzzy integer for tests which assert of number of queries #4606

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion kobo/apps/accounts/tests/test_current_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from rest_framework import status
from rest_framework.test import APITestCase

from kpi.utils.fuzzy_int import FuzzyInt


class CurrentUserAPITestCase(APITestCase):
def setUp(self):
Expand All @@ -24,7 +26,7 @@ def test_social_accounts(self):
other_social_account = baker.make('socialaccount.SocialAccount')
# This modifies the user account
self.client.get(self.url)
with self.assertNumQueries(4):
with self.assertNumQueries(FuzzyInt(3, 5)):
res = self.client.get(self.url)
for social_account in social_accounts:
self.assertContains(res, social_account.uid)
Expand Down
4 changes: 3 additions & 1 deletion kobo/apps/accounts/tests/test_social_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from model_bakery import baker
from rest_framework.test import APITestCase

from kpi.utils.fuzzy_int import FuzzyInt


class AccountsEmailTestCase(APITestCase):
def setUp(self):
Expand All @@ -13,7 +15,7 @@ def test_list(self):
account1 = baker.make('socialaccount.SocialAccount', user=self.user)
account2 = baker.make('socialaccount.SocialAccount')
# Auth, Count, Queryset
with self.assertNumQueries(4):
with self.assertNumQueries(FuzzyInt(3, 5)):
res = self.client.get(self.url_list)
self.assertContains(res, account1.uid)
self.assertNotContains(res, account2.uid)
Expand Down
6 changes: 4 additions & 2 deletions kobo/apps/organizations/tests/test_organizations_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from kpi.tests.kpi_test_case import BaseTestCase
from kpi.urls.router_api_v2 import URL_NAMESPACE

from kpi.utils.fuzzy_int import FuzzyInt


class OrganizationTestCase(BaseTestCase):
fixtures = ['test_data']
Expand Down Expand Up @@ -42,7 +44,7 @@ def test_list(self):
self._insert_data()
organization2 = baker.make(Organization, id='org_abcd123')
organization2.add_user(user=self.user, is_admin=True)
with self.assertNumQueries(3):
with self.assertNumQueries(FuzzyInt(2, 4)):
res = self.client.get(self.url_list)
self.assertContains(res, organization2.name)

Expand All @@ -61,7 +63,7 @@ def test_api_returns_org_data(self):
def test_update(self):
self._insert_data()
data = {'name': 'edit'}
with self.assertNumQueries(5):
with self.assertNumQueries(FuzzyInt(4, 6)):
res = self.client.patch(self.url_detail, data)
self.assertContains(res, data['name'])

Expand Down
10 changes: 4 additions & 6 deletions kobo/apps/subsequences/tests/test_submission_extras_api_post.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@

from constance.test import override_config
from jsonschema import validate
from rest_framework.test import APIClient, APITestCase
from rest_framework.test import APITestCase

from kobo.apps.languages.models.language import Language, LanguageRegion
from kobo.apps.languages.models.transcription import (
TranscriptionService, TranscriptionServiceLanguageM2M)
from kpi.models import Asset

from kpi.utils.fuzzy_int import FuzzyInt
from ..constants import GOOGLETS, make_async_cache_key
from ..models import SubmissionExtras
from .test_submission_extras_content import sample_asset
Expand Down Expand Up @@ -206,7 +205,6 @@ def test_date_created_is_pulled_from_last_revision(self):
assert 'revisions' not in revision
assert field['tx1']['dateCreated'] == 'A'


def test_second_translation_comes_in(self):
field = self.txi.revise_field({
'tx1': {
Expand Down Expand Up @@ -302,10 +300,10 @@ def test_google_transcript_post(self, m1, m2):
'submission': submission_id,
'q1': {GOOGLETS: {'status': 'requested', 'languageCode': ''}}
}
with self.assertNumQueries(53):
with self.assertNumQueries(FuzzyInt(51, 55)):
res = self.client.post(url, data, format='json')
self.assertContains(res, 'complete')
with self.assertNumQueries(22):
with self.assertNumQueries(FuzzyInt(20, 24)):
self.client.post(url, data, format='json')

@override_settings(CACHES={'default': {'BACKEND': 'django.core.cache.backends.locmem.LocMemCache'}})
Expand Down