Skip to content

Commit 4f0134b

Browse files
author
testgen-ci-bot
committed
Merge remote-tracking branch 'origin/enterprise' into misc-fixes
2 parents a13cce0 + c38f573 commit 4f0134b

11 files changed

Lines changed: 98 additions & 67 deletions

File tree

testgen/commands/job_runner.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,12 @@ def _print_run_summary(job_id: UUID, job_key: str) -> None:
7373
case "run-profile":
7474
run = session.scalars(select(ProfilingRun).where(ProfilingRun.job_execution_id == job_id)).first()
7575
if run:
76-
status_msg = "Profiling encountered an error. Check log for details." if run.status == "Error" else "Profiling completed."
76+
status_msg = "Profiling encountered an error. Check log for details." if run.job_execution.status == JobStatus.ERROR else "Profiling completed."
7777
click.echo(f"\n {status_msg}\n Run ID: {run.id}\n ")
7878
case "run-tests" | "run-monitors":
7979
run = session.scalars(select(TestRun).where(TestRun.job_execution_id == job_id)).first()
8080
if run:
81-
status_msg = "Test execution encountered an error. Check log for details." if run.status == "Error" else "Test execution completed."
81+
status_msg = "Test execution encountered an error. Check log for details." if run.job_execution.status == JobStatus.ERROR else "Test execution completed."
8282
click.echo(f"\n {status_msg}\n Run ID: {run.id}\n ")
8383
case "run-test-generation":
8484
click.echo("Test generation completed.")

testgen/common/models/profiling_run.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from sqlalchemy import BigInteger, Column, Float, Integer, String, desc, func, select, text, update
88
from sqlalchemy.dialects import postgresql
9-
from sqlalchemy.orm import InstrumentedAttribute
9+
from sqlalchemy.orm import InstrumentedAttribute, foreign, relationship
1010
from sqlalchemy.orm.attributes import flag_modified
1111
from sqlalchemy.sql.expression import case
1212

@@ -117,6 +117,13 @@ class ProfilingRun(Entity):
117117
process_id: int = Column(Integer)
118118
job_execution_id: UUID | None = Column(postgresql.UUID(as_uuid=True), nullable=True)
119119

120+
job_execution = relationship(
121+
JobExecution,
122+
primaryjoin=foreign(job_execution_id) == JobExecution.id,
123+
uselist=False,
124+
viewonly=True,
125+
)
126+
120127
_default_order_by = (desc(profiling_starttime),)
121128
_minimal_columns = (
122129
id,

testgen/common/models/test_run.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from sqlalchemy import BigInteger, Column, Float, ForeignKey, Integer, String, Text, desc, func, select, text, update
77
from sqlalchemy.dialects import postgresql
8+
from sqlalchemy.orm import foreign, relationship
89
from sqlalchemy.orm.attributes import flag_modified
910
from sqlalchemy.sql.expression import case
1011

@@ -121,6 +122,13 @@ class TestRun(Entity):
121122
process_id: int = Column(Integer)
122123
job_execution_id: UUID | None = Column(postgresql.UUID(as_uuid=True), nullable=True)
123124

125+
job_execution = relationship(
126+
JobExecution,
127+
primaryjoin=foreign(job_execution_id) == JobExecution.id,
128+
uselist=False,
129+
viewonly=True,
130+
)
131+
124132
_default_order_by = (desc(test_starttime),)
125133
_minimal_columns = (
126134
id,

testgen/common/notifications/monitor_run.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ def send_monitor_notifications(test_run: TestRun, result_list_ct=20):
236236
notification.recipients,
237237
{
238238
"summary": {
239-
"test_endtime": test_run.test_endtime,
239+
"test_endtime": test_run.job_execution.completed_at,
240240
"table_groups_name": table_group.table_groups_name,
241241
"project_name": project.project_name,
242242
"table_name": table_name,

testgen/common/notifications/notifications.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import math
22
from datetime import datetime
33

4-
from testgen.common.models.profiling_run import ProfilingRunStatus
5-
from testgen.common.models.test_definition import TestRunStatus
64
from testgen.common.notifications.base import BaseEmailTemplate
75
from testgen.utils import friendly_score
86

@@ -29,12 +27,6 @@ def format_duration_helper(self, start_time: datetime, end_time: datetime) -> st
2927
formatted = " ".join([ f"{unit[0]}{unit[1]}" for unit in units if unit[0] ])
3028
return formatted.strip() or "< 1s"
3129

32-
def format_status_helper(self, status: TestRunStatus | ProfilingRunStatus) -> str:
33-
return {
34-
"Complete": "Completed",
35-
"Cancelled": "Canceled",
36-
}.get(status, status)
37-
3830
def format_score_helper(self, score: float) -> str:
3931
return friendly_score(score)
4032

testgen/common/notifications/profiling_run.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from sqlalchemy import select
55

66
from testgen import settings
7+
from testgen.common.enums import JOB_STATUS_LABEL, JobStatus
78
from testgen.common.models import get_current_session, with_database_session
89
from testgen.common.models.hygiene_issue import HygieneIssue
910
from testgen.common.models.notification_settings import (
@@ -23,7 +24,7 @@ class ProfilingRunEmailTemplate(BaseNotificationTemplate):
2324

2425
def get_subject_template(self) -> str:
2526
return (
26-
"[TestGen] Profiling Run {{format_status profiling_run.status}}: {{table_groups_name}}"
27+
"[TestGen] Profiling Run {{profiling_run.status_label}}: {{table_groups_name}}"
2728
"{{#if issue_count}}"
2829
' | {{format_number issue_count}} hygiene {{pluralize issue_count "issue" "issues"}}'
2930
"{{/if}}"
@@ -32,9 +33,9 @@ def get_subject_template(self) -> str:
3233
def get_title_template(self):
3334
return """
3435
TestGen Profiling Run - <span class="
35-
{{#if (eq profiling_run.status 'Error')}} text-red {{/if}}
36-
{{#if (eq profiling_run.status 'Cancelled')}} text-purple {{/if}}
37-
">{{format_status profiling_run.status}}</span>
36+
{{#if (eq profiling_run.status 'error')}} text-red {{/if}}
37+
{{#if (eq profiling_run.status 'canceled')}} text-purple {{/if}}
38+
">{{profiling_run.status_label}}</span>
3839
"""
3940

4041
def get_main_content_template(self):
@@ -85,7 +86,7 @@ def get_main_content_template(self):
8586
</tr>
8687
<tr>
8788
<td class="summary__subtitle">
88-
{{#if (eq profiling_run.status 'Complete')}}
89+
{{#if (eq profiling_run.status 'completed')}}
8990
{{#if (eq notification_trigger 'on_changes')}}
9091
Profiling run detected new hygiene issues.
9192
{{/if}}
@@ -95,15 +96,15 @@ def get_main_content_template(self):
9596
{{/if}}
9697
{{/if}}
9798
{{/if}}
98-
{{#if (eq profiling_run.status 'Error')}}
99+
{{#if (eq profiling_run.status 'error')}}
99100
Profiling encountered an error.
100101
{{/if}}
101-
{{#if (eq profiling_run.status 'Cancelled')}}
102+
{{#if (eq profiling_run.status 'canceled')}}
102103
Profiling run was canceled.
103104
{{/if}}
104105
</td>
105106
</tr>
106-
{{#if (eq profiling_run.status 'Complete')}}
107+
{{#if (eq profiling_run.status 'completed')}}
107108
<tr>
108109
<td colspan="2" style="padding-top: 12px; padding-bottom: 12px;">
109110
<table cellspacing="0" cellpadding="0">
@@ -131,12 +132,12 @@ def get_main_content_template(self):
131132
</td>
132133
</tr>
133134
{{/if}}
134-
{{#if (eq profiling_run.status 'Error')}}
135+
{{#if (eq profiling_run.status 'error')}}
135136
<tr>
136137
<td><div class="code">{{profiling_run.log_message}}</div></td>
137138
</tr>
138139
{{/if}}
139-
{{#if (eq profiling_run.status 'Complete')}}
140+
{{#if (eq profiling_run.status 'completed')}}
140141
<tr>
141142
<td>
142143
<a class="link" href="{{profiling_run.issues_url}}" target="_blank">View {{format_number issue_count}} issues &gt;</a>
@@ -241,6 +242,8 @@ def send_profiling_run_notifications(profiling_run: ProfilingRun, result_list_ct
241242
if not notifications:
242243
return
243244

245+
job_execution = profiling_run.job_execution
246+
244247
previous_run = profiling_run.get_previous()
245248
issues = list(
246249
HygieneIssue.select_with_diff(
@@ -251,7 +254,7 @@ def send_profiling_run_notifications(profiling_run: ProfilingRun, result_list_ct
251254
)
252255

253256
triggers = {ProfilingRunNotificationTrigger.always}
254-
if profiling_run.status in ("Error", "Cancelled") or {None, True} & {is_new for _, is_new in issues}:
257+
if job_execution.status in (JobStatus.ERROR, JobStatus.CANCELED) or {None, True} & {is_new for _, is_new in issues}:
255258
triggers.add(ProfilingRunNotificationTrigger.on_changes)
256259

257260
notifications = [ns for ns in notifications if ns.trigger in triggers]
@@ -320,10 +323,11 @@ def send_profiling_run_notifications(profiling_run: ProfilingRun, result_list_ct
320323
"&source=email"
321324
)
322325
),
323-
"start_time": profiling_run.profiling_starttime,
324-
"end_time": profiling_run.profiling_endtime,
325-
"status": profiling_run.status,
326-
"log_message": profiling_run.log_message,
326+
"start_time": job_execution.started_at,
327+
"end_time": job_execution.completed_at,
328+
"status": job_execution.status,
329+
"status_label": JOB_STATUS_LABEL.get(job_execution.status, job_execution.status),
330+
"log_message": job_execution.error_message,
327331
"table_ct": profiling_run.table_ct,
328332
"column_ct": profiling_run.column_ct,
329333
},

testgen/common/notifications/test_run.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from sqlalchemy import case, literal, select
44

55
from testgen import settings
6+
from testgen.common.enums import JobStatus
67
from testgen.common.models import get_current_session, with_database_session
78
from testgen.common.models.notification_settings import (
89
TestRunNotificationSettings,
@@ -261,7 +262,7 @@ def send_test_run_notifications(test_run: TestRun, result_list_ct=20, result_sta
261262
changed_td_id_list.extend(td_id_list)
262263

263264
triggers = {TestRunNotificationTrigger.always}
264-
if test_run.status in ("Error", "Cancelled"):
265+
if test_run.job_execution.status in (JobStatus.ERROR, JobStatus.CANCELED):
265266
triggers.update(TestRunNotificationTrigger)
266267
else:
267268
if test_run.error_ct + test_run.failed_ct:

tests/unit/common/notifications/test_monitor_run_notifications.py

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ def create_test_result(table_name, test_type, message, result_code=0):
2929
return mock
3030

3131

32+
def make_monitor_run(completed_at="2024-01-15T10:30:00Z"):
33+
run = TestRun(id="monitor-run-id", test_suite_id="monitor-suite-id")
34+
run.job_execution = Mock(completed_at=completed_at)
35+
return run
36+
37+
3238
@pytest.fixture
3339
def ns_select_result():
3440
return [
@@ -110,11 +116,7 @@ def test_send_monitor_notifications(
110116
send_mock,
111117
persisted_setting_mock,
112118
):
113-
test_run = TestRun(
114-
id="monitor-run-id",
115-
test_suite_id="monitor-suite-id",
116-
test_endtime="2024-01-15T10:30:00Z",
117-
)
119+
test_run = make_monitor_run()
118120

119121
table_group = Mock(spec=TableGroup)
120122
table_group.id = "tg-id"
@@ -193,11 +195,7 @@ def test_send_monitor_notifications_early_exit(
193195
test_result_select_where_mock,
194196
send_mock,
195197
):
196-
test_run = TestRun(
197-
id="monitor-run-id",
198-
test_suite_id="monitor-suite-id",
199-
test_endtime="2024-01-15T10:30:00Z",
200-
)
198+
test_run = make_monitor_run()
201199

202200
if not has_notifications:
203201
ns_select_patched.return_value = []
@@ -219,11 +217,7 @@ def test_send_monitor_notifications_anomaly_counts(
219217
send_mock,
220218
persisted_setting_mock,
221219
):
222-
test_run = TestRun(
223-
id="monitor-run-id",
224-
test_suite_id="monitor-suite-id",
225-
test_endtime="2024-01-15T10:30:00Z",
226-
)
220+
test_run = make_monitor_run()
227221

228222
table_group = Mock(spec=TableGroup)
229223
table_group.id = "tg-id"
@@ -270,11 +264,7 @@ def test_send_monitor_notifications_url_construction(
270264
send_mock,
271265
persisted_setting_mock,
272266
):
273-
test_run = TestRun(
274-
id="monitor-run-id",
275-
test_suite_id="monitor-suite-id",
276-
test_endtime="2024-01-15T10:30:00Z",
277-
)
267+
test_run = make_monitor_run()
278268

279269
table_group = Mock(spec=TableGroup)
280270
table_group.id = "tg-123"

tests/unit/common/notifications/test_profiling_run_notifications.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import pytest
66

7+
from testgen.common.enums import JOB_STATUS_LABEL, JobStatus
78
from testgen.common.models.hygiene_issue import IssueCount
89
from testgen.common.models.notification_settings import (
910
ProfilingRunNotificationSettings,
@@ -15,6 +16,10 @@
1516
pytestmark = pytest.mark.unit
1617

1718

19+
def make_job_execution(status, started_at=None, completed_at=None, error_message=None):
20+
return Mock(status=status, started_at=started_at, completed_at=completed_at, error_message=error_message)
21+
22+
1823
def create_ns(**kwargs):
1924
with patch("testgen.common.notifications.profiling_run.ProfilingRunNotificationSettings.save"):
2025
return ProfilingRunNotificationSettings.create("proj", None, **kwargs)
@@ -79,12 +84,12 @@ def get_prev_mock():
7984
@pytest.mark.parametrize(
8085
("profiling_run_status", "has_prev_run", "issue_count", "new_issue_count", "expected_triggers"),
8186
(
82-
("Error", True, 25, 0, ("always", "on_changes")),
83-
("Error", True, 0, 0, ("always", "on_changes")),
84-
("Cancelled", True, 50, 10, ("always", "on_changes")),
85-
("Complete", True, 50, 10, ("always", "on_changes")),
86-
("Complete", True, 15, 0, ("always",)),
87-
("Complete", False, 15, 15, ("always", "on_changes")),
87+
(JobStatus.ERROR, True, 25, 0, ("always", "on_changes")),
88+
(JobStatus.ERROR, True, 0, 0, ("always", "on_changes")),
89+
(JobStatus.CANCELED, True, 50, 10, ("always", "on_changes")),
90+
(JobStatus.COMPLETED, True, 50, 10, ("always", "on_changes")),
91+
(JobStatus.COMPLETED, True, 15, 0, ("always",)),
92+
(JobStatus.COMPLETED, False, 15, 15, ("always", "on_changes")),
8893
),
8994
)
9095
def test_send_profiling_run_notification(
@@ -104,9 +109,9 @@ def test_send_profiling_run_notification(
104109
id="pr-id",
105110
job_execution_id="pr-id",
106111
table_groups_id="tg-id",
107-
status=profiling_run_status,
108112
project_code="proj",
109113
)
114+
profiling_run.job_execution = make_job_execution(profiling_run_status)
110115
get_prev_mock.return_value = ProfilingRun(id="pr-prev-id") if has_prev_run else None
111116
new_count = iter(count())
112117
priorities = ("Definite", "Likely", "Possible", "High", "Moderate")
@@ -144,6 +149,7 @@ def test_send_profiling_run_notification(
144149
"start_time": None,
145150
"end_time": None,
146151
"status": profiling_run_status,
152+
"status_label": JOB_STATUS_LABEL[profiling_run_status],
147153
"log_message": None,
148154
"table_ct": None,
149155
"column_ct": None,

0 commit comments

Comments
 (0)