Skip to content

Commit f895256

Browse files
produc grading
1 parent ebfcaa7 commit f895256

2 files changed

Lines changed: 45 additions & 17 deletions

File tree

unittests/dojo_test_case.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,17 @@ def system_settings(
7878
disable_jira_webhook_secret=False,
7979
jira_webhook_secret=None,
8080
enable_product_tag_inehritance=False,
81+
enable_product_grade=True,
82+
enable_webhooks_notifications=False,
8183
):
8284
ss = System_Settings.objects.get()
8385
ss.enable_jira = enable_jira
8486
ss.enable_jira_web_hook = enable_jira_web_hook
8587
ss.disable_jira_webhook_secret = disable_jira_webhook_secret
8688
ss.jira_webhook_secret = jira_webhook_secret
8789
ss.enable_product_tag_inheritance = enable_product_tag_inehritance
90+
ss.enable_product_grade = enable_product_grade
91+
ss.enable_webhooks_notifications = enable_webhooks_notifications
8892
ss.save()
8993

9094
def create_product_type(self, name, *args, description="dummy description", **kwargs):

unittests/test_importers_performance.py

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,11 @@
2020

2121

2222
class TestDojoImporterPerformance(DojoTestCase):
23-
fixtures = ["dojo_testdata.json"]
2423

2524
def setUp(self):
2625
super().setUp()
27-
user = User.objects.get(username="admin")
28-
user.usercontactinfo.block_execution = True
29-
user.save()
26+
self.system_settings(enable_webhooks_notifications=False)
27+
self.system_settings(enable_product_grade=False)
3028

3129
@contextmanager
3230
def assertNumAsyncTask(self, num):
@@ -46,7 +44,14 @@ def assertNumAsyncTask(self, num):
4644
raise self.failureException(msg)
4745

4846
def import_reimport_performance(self, expected_num_queries1, expected_num_async_tasks1, expected_num_queries2, expected_num_async_tasks2, expected_num_queries3, expected_num_async_tasks3):
49-
"""Despite all efforts, these imports here run in async mode, so celery tasks are executed in the background"""
47+
"""
48+
Log output can be quite large as when the assertNumQueries fails, all queries are printed.
49+
It could be usefule to capture the output in `less`:
50+
./run-unittest.sh --test-case unittests.test_importers_performance.TestDojoImporterPerformance 2>&1 | less
51+
Then search for `expected` to find the lines where the expected number of queries is printed.
52+
Or you can use `grep` to filter the output:
53+
./run-unittest.sh --test-case unittests.test_importers_performance.TestDojoImporterPerformance 2>&1 | grep expected
54+
"""
5055
product_type, _created = Product_Type.objects.get_or_create(name="test")
5156
product, _created = Product.objects.get_or_create(
5257
name="TestDojoDefaultImporter",
@@ -127,12 +132,12 @@ def import_reimport_performance(self, expected_num_queries1, expected_num_async_
127132

128133
def test_import_reimport_reimport_performance(self):
129134
self.import_reimport_performance(
130-
expected_num_queries1=617,
131-
expected_num_async_tasks1=18,
132-
expected_num_queries2=496,
133-
expected_num_async_tasks2=25,
134-
expected_num_queries3=348,
135-
expected_num_async_tasks3=21,
135+
expected_num_queries1=606,
136+
expected_num_async_tasks1=15,
137+
expected_num_queries2=489,
138+
expected_num_async_tasks2=23,
139+
expected_num_queries3=347,
140+
expected_num_async_tasks3=20,
136141
)
137142

138143
@patch("dojo.decorators.we_want_async", return_value=False)
@@ -145,10 +150,29 @@ def test_import_reimport_reimport_performance_no_async(self, mock):
145150
so we patch the we_want_async decorator to always return False.
146151
"""
147152
self.import_reimport_performance(
148-
expected_num_queries1=708,
149-
expected_num_async_tasks1=29,
150-
expected_num_queries2=566,
151-
expected_num_async_tasks2=32,
152-
expected_num_queries3=400,
153-
expected_num_async_tasks3=26,
153+
expected_num_queries1=613,
154+
expected_num_async_tasks1=15,
155+
expected_num_queries2=496,
156+
expected_num_async_tasks2=23,
157+
expected_num_queries3=352,
158+
expected_num_async_tasks3=20,
159+
)
160+
161+
@patch("dojo.decorators.we_want_async", return_value=False)
162+
def test_import_reimport_reimport_performance_no_async_with_product_grading(self, mock):
163+
"""
164+
This test checks the performance of the importers when they are run in sync mode.
165+
The reason for this is that we also want to be aware of when a PR affects the number of queries
166+
or async tasks created by a background task.
167+
The impersonate context manager above does not work as expected for disabling async,
168+
so we patch the we_want_async decorator to always return False.
169+
"""
170+
self.system_settings(enable_product_grade=True)
171+
self.import_reimport_performance(
172+
expected_num_queries1=673,
173+
expected_num_async_tasks1=25,
174+
expected_num_queries2=544,
175+
expected_num_async_tasks2=30,
176+
expected_num_queries3=387,
177+
expected_num_async_tasks3=25,
154178
)

0 commit comments

Comments
 (0)