|
3 | 3 | from unittest.mock import patch |
4 | 4 |
|
5 | 5 | from crum import impersonate |
| 6 | +from django.contrib.contenttypes.models import ContentType |
6 | 7 | from django.utils import timezone |
7 | 8 |
|
8 | 9 | from dojo.decorators import dojo_async_task_counter |
@@ -39,6 +40,12 @@ def setUp(self): |
39 | 40 | self.system_settings(enable_webhooks_notifications=False) |
40 | 41 | self.system_settings(enable_product_grade=False) |
41 | 42 | self.system_settings(enable_github=False) |
| 43 | + # Warm up ContentType cache for relevant models. This is needed if we want to be able to run the test in isolation |
| 44 | + # As part of the test suite the ContentTYpe ids will already be cached and won't affect the query count. |
| 45 | + # But if we run the test in isolation, the ContentType ids will not be cached and will result in more queries. |
| 46 | + # By warming up the cache here, these queries are executed before we start counting queries |
| 47 | + for model in [Development_Environment, Dojo_User, Endpoint, Endpoint_Status, Engagement, Finding, Product, Product_Type, User]: |
| 48 | + ContentType.objects.get_for_model(model) |
42 | 49 |
|
43 | 50 | @contextmanager |
44 | 51 | def assertNumAsyncTask(self, num): |
@@ -80,45 +87,6 @@ def import_reimport_performance(self, expected_num_queries1, expected_num_async_ |
80 | 87 | lead, _ = User.objects.get_or_create(username="admin") |
81 | 88 | environment, _ = Development_Environment.objects.get_or_create(name="Development") |
82 | 89 |
|
83 | | - # first we do a bogus import to make sure any caches are loaded. |
84 | | - # without this the number of queries will be higher as the audit log will load content_type ids from the db |
85 | | - |
86 | | - engagement_dummy, _created = Engagement.objects.get_or_create( |
87 | | - name="Test Create Dummy Engagement", |
88 | | - product=product, |
89 | | - target_start=timezone.now(), |
90 | | - target_end=timezone.now(), |
91 | | - ) |
92 | | - import_options = { |
93 | | - "user": lead, |
94 | | - "lead": lead, |
95 | | - "scan_date": None, |
96 | | - "environment": environment, |
97 | | - "minimum_severity": "Info", |
98 | | - "active": True, |
99 | | - "verified": True, |
100 | | - "sync": True, |
101 | | - "scan_type": NPM_AUDIT_SCAN_TYPE, |
102 | | - "engagement": engagement_dummy, |
103 | | - } |
104 | | - importer = DefaultImporter(**import_options) |
105 | | - test, _, _len_new_findings, _len_closed_findings, _, _, _ = importer.process_scan(NPM_AUDIT_NO_VULN_FILENAME.open(encoding="utf-8")) |
106 | | - |
107 | | - # if True: |
108 | | - # exit(0) # this is to make sure the importers are not run when running the tests in the IDE, as it will take too long |
109 | | - |
110 | | - finding = Finding.objects.filter(test=test).first() |
111 | | - |
112 | | - endpoint = Endpoint.objects.create(host="foo.bar", product=product) |
113 | | - Endpoint_Status.objects.create( |
114 | | - finding=finding, |
115 | | - endpoint=endpoint, |
116 | | - ) |
117 | | - |
118 | | - finding.endpoints.add(endpoint) |
119 | | - finding.title = "Dummy finding to ensure audit log is created" |
120 | | - finding.save() |
121 | | - |
122 | 90 | # first import the subset which missed one finding and a couple of endpoints on some of the findings |
123 | 91 | with ( |
124 | 92 | self.subTest("import1"), impersonate(Dojo_User.objects.get(username="admin")), |
@@ -185,7 +153,7 @@ def import_reimport_performance(self, expected_num_queries1, expected_num_async_ |
185 | 153 |
|
186 | 154 | def test_import_reimport_reimport_performance(self): |
187 | 155 | self.import_reimport_performance( |
188 | | - expected_num_queries1=602, |
| 156 | + expected_num_queries1=604, |
189 | 157 | expected_num_async_tasks1=15, |
190 | 158 | expected_num_queries2=489, |
191 | 159 | expected_num_async_tasks2=23, |
|
0 commit comments