-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Use env var instead of License to determine instance type (#20189)
* fix: Use env var instead of License to determine instance type * Improve code formatting * Fix * Remove unused `type: ignore`s * Fix `TestAutoProjectMiddleware`
- Loading branch information
Showing
10 changed files
with
33 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,7 @@ | |
from django.core.cache import cache | ||
from django.db import connection, connections | ||
from django.db.migrations.executor import MigrationExecutor | ||
from django.test import TestCase, TransactionTestCase, override_settings | ||
from django.test import SimpleTestCase, TestCase, TransactionTestCase, override_settings | ||
from django.test.utils import CaptureQueriesContext | ||
from rest_framework.test import APITestCase as DRFTestCase | ||
|
||
|
@@ -30,9 +30,7 @@ | |
from posthog.clickhouse.client.connection import ch_pool | ||
from posthog.clickhouse.plugin_log_entries import TRUNCATE_PLUGIN_LOG_ENTRIES_TABLE_SQL | ||
from posthog.cloud_utils import ( | ||
TEST_clear_cloud_cache, | ||
TEST_clear_instance_license_cache, | ||
is_cloud, | ||
) | ||
from posthog.models import Dashboard, DashboardTile, Insight, Organization, Team, User | ||
from posthog.models.channel_type.sql import ( | ||
|
@@ -186,7 +184,7 @@ def validation_error_response( | |
} | ||
|
||
|
||
class TestMixin: | ||
class PostHogTestCase(SimpleTestCase): | ||
CONFIG_ORGANIZATION_NAME: str = "Test" | ||
CONFIG_EMAIL: Optional[str] = "[email protected]" | ||
CONFIG_PASSWORD: Optional[str] = "testpassword12345" | ||
|
@@ -234,17 +232,22 @@ def tearDown(self): | |
) | ||
global persons_ordering_int | ||
persons_ordering_int = 0 | ||
super().tearDown() # type: ignore | ||
super().tearDown() | ||
|
||
def validate_basic_html(self, html_message, site_url, preheader=None): | ||
# absolute URLs are used | ||
self.assertIn(f"{site_url}/static/posthog-logo.png", html_message) # type: ignore | ||
self.assertIn(f"{site_url}/static/posthog-logo.png", html_message) | ||
|
||
# CSS is inlined | ||
self.assertIn('style="display: none;', html_message) # type: ignore | ||
self.assertIn('style="display: none;', html_message) | ||
|
||
if preheader: | ||
self.assertIn(preheader, html_message) # type: ignore | ||
self.assertIn(preheader, html_message) | ||
|
||
@contextmanager | ||
def is_cloud(self, value: bool): | ||
with self.settings(REGION="US" if value else None): | ||
yield value | ||
|
||
@contextmanager | ||
def retry_assertion(self, max_retries=5, delay=0.1) -> Generator[None, None, None]: | ||
|
@@ -296,24 +299,17 @@ def _callTestMethod(self, method): | |
) | ||
|
||
|
||
class BaseTest(TestMixin, ErrorResponsesMixin, TestCase): | ||
class BaseTest(PostHogTestCase, ErrorResponsesMixin, TestCase): | ||
""" | ||
Base class for performing Postgres-based backend unit tests on. | ||
Each class and each test is wrapped inside an atomic block to rollback DB commits after each test. | ||
Read more: https://docs.djangoproject.com/en/3.1/topics/testing/tools/#testcase | ||
""" | ||
|
||
@contextmanager | ||
def is_cloud(self, value: bool): | ||
previous_value = is_cloud() | ||
try: | ||
TEST_clear_cloud_cache(value) | ||
yield value | ||
finally: | ||
TEST_clear_cloud_cache(previous_value) | ||
pass | ||
|
||
|
||
class NonAtomicBaseTest(TestMixin, ErrorResponsesMixin, TransactionTestCase): | ||
class NonAtomicBaseTest(PostHogTestCase, ErrorResponsesMixin, TransactionTestCase): | ||
""" | ||
Django wraps tests in TestCase inside atomic transactions to speed up the run time. TransactionTestCase is the base | ||
class for TestCase that doesn't implement this atomic wrapper. | ||
|
@@ -325,18 +321,15 @@ def setUpClass(cls): | |
cls.setUpTestData() | ||
|
||
|
||
class APIBaseTest(TestMixin, ErrorResponsesMixin, DRFTestCase): | ||
class APIBaseTest(PostHogTestCase, ErrorResponsesMixin, DRFTestCase): | ||
""" | ||
Functional API tests using Django REST Framework test suite. | ||
""" | ||
|
||
initial_cloud_mode: Optional[bool] = False | ||
|
||
def setUp(self): | ||
super().setUp() | ||
|
||
cache.clear() | ||
TEST_clear_cloud_cache(self.initial_cloud_mode) | ||
TEST_clear_instance_license_cache() | ||
|
||
# Sets the cloud mode to stabilise things tests, especially num query counts | ||
|
@@ -357,16 +350,6 @@ def assertFasterThan(self, duration_ms: float): | |
with assert_faster_than(duration_ms): | ||
yield | ||
|
||
@contextmanager | ||
def is_cloud(self, value: bool): | ||
# Typically the is_cloud setting is controlled by License but we need to be able to override it for tests | ||
previous_value = is_cloud() | ||
try: | ||
TEST_clear_cloud_cache(value) | ||
yield value | ||
finally: | ||
TEST_clear_cloud_cache(previous_value) | ||
|
||
|
||
def stripResponse(response, remove=("action", "label", "persons_urls", "filter")): | ||
if len(response): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters