This repository was archived by the owner on May 5, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change 1
1
from datetime import timedelta
2
2
from enum import Enum
3
3
4
+ from django .db import transaction
4
5
from django .db .models import Q
5
6
from django .utils import timezone
6
7
@@ -35,6 +36,18 @@ def query_monthly_coverage_measurements(plan_service: PlanService) -> int:
35
36
return queryset [:monthly_limit ].count ()
36
37
37
38
39
+ def bulk_insert_coverage_measurements (
40
+ measurements : list [UserMeasurement ],
41
+ ) -> list [UserMeasurement ]:
42
+ """
43
+ This function takes measurements as input and bulk_creates them into the DB.
44
+ The atomic transaction ensures either all transactions are inserted or none
45
+ if there's an error
46
+ """
47
+ with transaction .atomic ():
48
+ return UserMeasurement .objects .bulk_create (measurements )
49
+
50
+
38
51
def insert_coverage_measurement (
39
52
owner_id : int ,
40
53
repo_id : int ,
Original file line number Diff line number Diff line change 14
14
from shared .django_apps .user_measurements .models import UserMeasurement
15
15
from shared .plan .service import PlanService
16
16
from shared .upload .utils import (
17
+ bulk_insert_coverage_measurements ,
17
18
insert_coverage_measurement ,
18
19
query_monthly_coverage_measurements ,
19
20
)
@@ -145,3 +146,28 @@ def test_query_monthly_coverage_measurements_beyond_monthly_limit(
145
146
)
146
147
# 10 uploads total, max 3 returned
147
148
assert monthly_measurements == 3
149
+
150
+ def test_bulk_insert_user_measurements (self ):
151
+ owner = OwnerFactory ()
152
+ measurements = []
153
+ for _ in range (5 ):
154
+ repo = RepositoryFactory .create (author = owner )
155
+ commit = CommitFactory .create (repository = repo )
156
+ report = CommitReportFactory .create (commit = commit )
157
+ upload = UploadFactory .create (report = report )
158
+ measurements .append (
159
+ UserMeasurement (
160
+ owner_id = owner .ownerid ,
161
+ repo_id = repo .repoid ,
162
+ commit_id = commit .id ,
163
+ upload_id = upload .id ,
164
+ uploader_used = "CLI" ,
165
+ private_repo = repo .private ,
166
+ report_type = report .report_type ,
167
+ )
168
+ )
169
+
170
+ inserted_measurements = bulk_insert_coverage_measurements (
171
+ measurements = measurements
172
+ )
173
+ assert len (inserted_measurements ) == 5
You can’t perform that action at this time.
0 commit comments