|
| 1 | +import logging |
1 | 2 | from unittest.mock import PropertyMock, patch |
2 | 3 |
|
3 | 4 | from rest_framework import status |
|
10 | 11 | ) |
11 | 12 | from shared.reports.resources import Report, ReportFile, Session, SessionType |
12 | 13 | from shared.reports.types import ReportLine, ReportTotals |
| 14 | +from shared.yaml import UserYaml |
13 | 15 |
|
14 | 16 |
|
15 | 17 | def sample_report(): |
@@ -1131,6 +1133,212 @@ def test_unknown_report_flag_badge(self, full_report_mock): |
1131 | 1133 | assert expected_badge == badge |
1132 | 1134 | assert response.status_code == status.HTTP_200_OK |
1133 | 1135 |
|
| 1136 | + @patch("core.models.Commit.full_report", new_callable=PropertyMock) |
| 1137 | + @patch("graphs.views.commit_components") |
| 1138 | + def test_component_badge(self, commit_components_mock, full_report_mock): |
| 1139 | + gh_owner = OwnerFactory(service="github") |
| 1140 | + repo = RepositoryFactory( |
| 1141 | + author=gh_owner, |
| 1142 | + active=True, |
| 1143 | + private=False, |
| 1144 | + name="repo1", |
| 1145 | + yaml={ |
| 1146 | + "component_management": { |
| 1147 | + "individual_components": [ |
| 1148 | + { |
| 1149 | + "component_id": "file_1", |
| 1150 | + "name": "File1", |
| 1151 | + "paths": ["file_1.go"], |
| 1152 | + } |
| 1153 | + ] |
| 1154 | + } |
| 1155 | + }, |
| 1156 | + ) |
| 1157 | + CommitFactory(repository=repo, author=gh_owner) |
| 1158 | + commit_components_mock.return_value = UserYaml.get_final_yaml( |
| 1159 | + repo_yaml=repo.yaml |
| 1160 | + ).get_components() |
| 1161 | + full_report_mock.return_value = sample_report() |
| 1162 | + |
| 1163 | + # test default precision |
| 1164 | + response = self._get( |
| 1165 | + kwargs={ |
| 1166 | + "service": "gh", |
| 1167 | + "owner_username": gh_owner.username, |
| 1168 | + "repo_name": "repo1", |
| 1169 | + "ext": "svg", |
| 1170 | + }, |
| 1171 | + data={"component": "File1"}, |
| 1172 | + ) |
| 1173 | + |
| 1174 | + expected_badge = """<svg xmlns="http://www.w3.org/2000/svg" width="112" height="20"> |
| 1175 | + <linearGradient id="b" x2="0" y2="100%"> |
| 1176 | + <stop offset="0" stop-color="#bbb" stop-opacity=".1" /> |
| 1177 | + <stop offset="1" stop-opacity=".1" /> |
| 1178 | + </linearGradient> |
| 1179 | + <mask id="a"> |
| 1180 | + <rect width="112" height="20" rx="3" fill="#fff" /> |
| 1181 | + </mask> |
| 1182 | + <g mask="url(#a)"> |
| 1183 | + <path fill="#555" d="M0 0h73v20H0z" /> |
| 1184 | + <path fill="#e05d44" d="M73 0h39v20H73z" /> |
| 1185 | + <path fill="url(#b)" d="M0 0h112v20H0z" /> |
| 1186 | + </g> |
| 1187 | + <g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11"> |
| 1188 | + <text x="46" y="15" fill="#010101" fill-opacity=".3">codecov</text> |
| 1189 | + <text x="46" y="14">codecov</text> |
| 1190 | + <text x="93" y="15" fill="#010101" fill-opacity=".3">62%</text> |
| 1191 | + <text x="93" y="14">62%</text> |
| 1192 | + </g> |
| 1193 | + <svg viewBox="120 -8 60 60"> |
| 1194 | + <path d="M23.013 0C10.333.009.01 10.22 0 22.762v.058l3.914 2.275.053-.036a11.291 11.291 0 0 1 8.352-1.767 10.911 10.911 0 0 1 5.5 2.726l.673.624.38-.828c.368-.802.793-1.556 1.264-2.24.19-.276.398-.554.637-.851l.393-.49-.484-.404a16.08 16.08 0 0 0-7.453-3.466 16.482 16.482 0 0 0-7.705.449C7.386 10.683 14.56 5.016 23.03 5.01c4.779 0 9.272 1.84 12.651 5.18 2.41 2.382 4.069 5.35 4.807 8.591a16.53 16.53 0 0 0-4.792-.723l-.292-.002a16.707 16.707 0 0 0-1.902.14l-.08.012c-.28.037-.524.074-.748.115-.11.019-.218.041-.327.063-.257.052-.51.108-.75.169l-.265.067a16.39 16.39 0 0 0-.926.276l-.056.018c-.682.23-1.36.511-2.016.838l-.052.026c-.29.145-.584.305-.899.49l-.069.04a15.596 15.596 0 0 0-4.061 3.466l-.145.175c-.29.36-.521.666-.723.96-.17.247-.34.513-.552.864l-.116.199c-.17.292-.32.57-.449.824l-.03.057a16.116 16.116 0 0 0-.843 2.029l-.034.102a15.65 15.65 0 0 0-.786 5.174l.003.214a21.523 21.523 0 0 0 .04.754c.009.119.02.237.032.355.014.145.032.29.049.432l.01.08c.01.067.017.133.026.197.034.242.074.48.119.72.463 2.419 1.62 4.836 3.345 6.99l.078.098.08-.095c.688-.81 2.395-3.38 2.539-4.922l.003-.029-.014-.025a10.727 10.727 0 0 1-1.226-4.956c0-5.76 4.545-10.544 10.343-10.89l.381-.014a11.403 11.403 0 0 1 6.651 1.957l.054.036 3.862-2.237.05-.03v-.056c.006-6.08-2.384-11.793-6.729-16.089C34.932 2.361 29.16 0 23.013 0" fill="#F01F7A" fill-rule="evenodd"/> |
| 1195 | + </svg> |
| 1196 | + </svg>""" |
| 1197 | + |
| 1198 | + badge = response.content.decode("utf-8") |
| 1199 | + badge = [line.strip() for line in badge.split("\n")] |
| 1200 | + expected_badge = [line.strip() for line in expected_badge.split("\n")] |
| 1201 | + assert expected_badge == badge |
| 1202 | + assert response.status_code == status.HTTP_200_OK |
| 1203 | + |
| 1204 | + @patch("core.models.Commit.full_report", new_callable=PropertyMock) |
| 1205 | + @patch("graphs.views.commit_components") |
| 1206 | + def test_flag_component_badge(self, commit_components_mock, full_report_mock): |
| 1207 | + gh_owner = OwnerFactory(service="github") |
| 1208 | + repo = RepositoryFactory( |
| 1209 | + author=gh_owner, |
| 1210 | + active=True, |
| 1211 | + private=False, |
| 1212 | + name="repo1", |
| 1213 | + yaml={ |
| 1214 | + "component_management": { |
| 1215 | + "individual_components": [ |
| 1216 | + { |
| 1217 | + "component_id": "unittests", |
| 1218 | + "name": "Unit tests", |
| 1219 | + "flag_regexes": ["unittests"], |
| 1220 | + } |
| 1221 | + ] |
| 1222 | + } |
| 1223 | + }, |
| 1224 | + ) |
| 1225 | + CommitFactory(repository=repo, author=gh_owner) |
| 1226 | + commit_components_mock.return_value = UserYaml.get_final_yaml( |
| 1227 | + repo_yaml=repo.yaml |
| 1228 | + ).get_components() |
| 1229 | + full_report_mock.return_value = sample_report() |
| 1230 | + |
| 1231 | + # test default precision |
| 1232 | + response = self._get( |
| 1233 | + kwargs={ |
| 1234 | + "service": "gh", |
| 1235 | + "owner_username": gh_owner.username, |
| 1236 | + "repo_name": "repo1", |
| 1237 | + "ext": "svg", |
| 1238 | + }, |
| 1239 | + data={"component": "unittests"}, |
| 1240 | + ) |
| 1241 | + |
| 1242 | + expected_badge = """<svg xmlns="http://www.w3.org/2000/svg" width="122" height="20"> |
| 1243 | + <linearGradient id="b" x2="0" y2="100%"> |
| 1244 | + <stop offset="0" stop-color="#bbb" stop-opacity=".1" /> |
| 1245 | + <stop offset="1" stop-opacity=".1" /> |
| 1246 | + </linearGradient> |
| 1247 | + <mask id="a"> |
| 1248 | + <rect width="122" height="20" rx="3" fill="#fff" /> |
| 1249 | + </mask> |
| 1250 | + <g mask="url(#a)"> |
| 1251 | + <path fill="#555" d="M0 0h76v20H0z" /> |
| 1252 | + <path fill="#4c1" d="M76 0h46v20H76z" /> |
| 1253 | + <path fill="url(#b)" d="M0 0h122v20H0z" /> |
| 1254 | + </g> |
| 1255 | + <g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11"> |
| 1256 | + <text x="46" y="15" fill="#010101" fill-opacity=".3">codecov</text> |
| 1257 | + <text x="46" y="14">codecov</text> |
| 1258 | + <text x="98" y="15" fill="#010101" fill-opacity=".3">100%</text> |
| 1259 | + <text x="98" y="14">100%</text> |
| 1260 | + </g> |
| 1261 | + <svg viewBox="140 -8 60 60"> |
| 1262 | + <path d="M23.013 0C10.333.009.01 10.22 0 22.762v.058l3.914 2.275.053-.036a11.291 11.291 0 0 1 8.352-1.767 10.911 10.911 0 0 1 5.5 2.726l.673.624.38-.828c.368-.802.793-1.556 1.264-2.24.19-.276.398-.554.637-.851l.393-.49-.484-.404a16.08 16.08 0 0 0-7.453-3.466 16.482 16.482 0 0 0-7.705.449C7.386 10.683 14.56 5.016 23.03 5.01c4.779 0 9.272 1.84 12.651 5.18 2.41 2.382 4.069 5.35 4.807 8.591a16.53 16.53 0 0 0-4.792-.723l-.292-.002a16.707 16.707 0 0 0-1.902.14l-.08.012c-.28.037-.524.074-.748.115-.11.019-.218.041-.327.063-.257.052-.51.108-.75.169l-.265.067a16.39 16.39 0 0 0-.926.276l-.056.018c-.682.23-1.36.511-2.016.838l-.052.026c-.29.145-.584.305-.899.49l-.069.04a15.596 15.596 0 0 0-4.061 3.466l-.145.175c-.29.36-.521.666-.723.96-.17.247-.34.513-.552.864l-.116.199c-.17.292-.32.57-.449.824l-.03.057a16.116 16.116 0 0 0-.843 2.029l-.034.102a15.65 15.65 0 0 0-.786 5.174l.003.214a21.523 21.523 0 0 0 .04.754c.009.119.02.237.032.355.014.145.032.29.049.432l.01.08c.01.067.017.133.026.197.034.242.074.48.119.72.463 2.419 1.62 4.836 3.345 6.99l.078.098.08-.095c.688-.81 2.395-3.38 2.539-4.922l.003-.029-.014-.025a10.727 10.727 0 0 1-1.226-4.956c0-5.76 4.545-10.544 10.343-10.89l.381-.014a11.403 11.403 0 0 1 6.651 1.957l.054.036 3.862-2.237.05-.03v-.056c.006-6.08-2.384-11.793-6.729-16.089C34.932 2.361 29.16 0 23.013 0" fill="#F01F7A" fill-rule="evenodd"/> |
| 1263 | + </svg> |
| 1264 | +</svg>""" |
| 1265 | + |
| 1266 | + badge = response.content.decode("utf-8") |
| 1267 | + badge = [line.strip() for line in badge.split("\n")] |
| 1268 | + expected_badge = [line.strip() for line in expected_badge.split("\n")] |
| 1269 | + assert expected_badge == badge |
| 1270 | + assert response.status_code == status.HTTP_200_OK |
| 1271 | + |
| 1272 | + @patch("core.models.Commit.full_report", new_callable=PropertyMock) |
| 1273 | + @patch("graphs.views.commit_components") |
| 1274 | + def test_unknown_component_badge(self, commit_components_mock, full_report_mock): |
| 1275 | + gh_owner = OwnerFactory(service="github") |
| 1276 | + repo = RepositoryFactory( |
| 1277 | + author=gh_owner, |
| 1278 | + active=True, |
| 1279 | + private=False, |
| 1280 | + name="repo1", |
| 1281 | + yaml={ |
| 1282 | + "component_management": { |
| 1283 | + "individual_components": [ |
| 1284 | + { |
| 1285 | + "component_id": "unittests", |
| 1286 | + "name": "Unit tests", |
| 1287 | + "flag_regexes": ["unittests"], |
| 1288 | + } |
| 1289 | + ] |
| 1290 | + } |
| 1291 | + }, |
| 1292 | + ) |
| 1293 | + CommitFactory(repository=repo, author=gh_owner) |
| 1294 | + commit_components_mock.return_value = UserYaml.get_final_yaml( |
| 1295 | + repo_yaml=repo.yaml |
| 1296 | + ).get_components() |
| 1297 | + full_report_mock.return_value = sample_report() |
| 1298 | + |
| 1299 | + # test default precision |
| 1300 | + response = self._get( |
| 1301 | + kwargs={ |
| 1302 | + "service": "gh", |
| 1303 | + "owner_username": gh_owner.username, |
| 1304 | + "repo_name": "repo1", |
| 1305 | + "ext": "svg", |
| 1306 | + }, |
| 1307 | + data={"component": "idk"}, |
| 1308 | + ) |
| 1309 | + |
| 1310 | + expected_badge = """<svg xmlns="http://www.w3.org/2000/svg" width="137" height="20"> |
| 1311 | + <linearGradient id="b" x2="0" y2="100%"> |
| 1312 | + <stop offset="0" stop-color="#bbb" stop-opacity=".1" /> |
| 1313 | + <stop offset="1" stop-opacity=".1" /> |
| 1314 | + </linearGradient> |
| 1315 | + <mask id="a"> |
| 1316 | + <rect width="137" height="20" rx="3" fill="#fff" /> |
| 1317 | + </mask> |
| 1318 | + <g mask="url(#a)"> |
| 1319 | + <path fill="#555" d="M0 0h76v20H0z" /> |
| 1320 | + <path fill="#9f9f9f" d="M76 0h61v20H76z" /> |
| 1321 | + <path fill="url(#b)" d="M0 0h137v20H0z" /> |
| 1322 | + </g> |
| 1323 | + <g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11"> |
| 1324 | + <text x="46" y="15" fill="#010101" fill-opacity=".3">codecov</text> |
| 1325 | + <text x="46" y="14">codecov</text> |
| 1326 | + <text x="105.5" y="15" fill="#010101" fill-opacity=".3">unknown</text> |
| 1327 | + <text x="105.5" y="14">unknown</text> |
| 1328 | + </g> |
| 1329 | + <svg viewBox="161 -8 60 60"> |
| 1330 | + <path d="M23.013 0C10.333.009.01 10.22 0 22.762v.058l3.914 2.275.053-.036a11.291 11.291 0 0 1 8.352-1.767 10.911 10.911 0 0 1 5.5 2.726l.673.624.38-.828c.368-.802.793-1.556 1.264-2.24.19-.276.398-.554.637-.851l.393-.49-.484-.404a16.08 16.08 0 0 0-7.453-3.466 16.482 16.482 0 0 0-7.705.449C7.386 10.683 14.56 5.016 23.03 5.01c4.779 0 9.272 1.84 12.651 5.18 2.41 2.382 4.069 5.35 4.807 8.591a16.53 16.53 0 0 0-4.792-.723l-.292-.002a16.707 16.707 0 0 0-1.902.14l-.08.012c-.28.037-.524.074-.748.115-.11.019-.218.041-.327.063-.257.052-.51.108-.75.169l-.265.067a16.39 16.39 0 0 0-.926.276l-.056.018c-.682.23-1.36.511-2.016.838l-.052.026c-.29.145-.584.305-.899.49l-.069.04a15.596 15.596 0 0 0-4.061 3.466l-.145.175c-.29.36-.521.666-.723.96-.17.247-.34.513-.552.864l-.116.199c-.17.292-.32.57-.449.824l-.03.057a16.116 16.116 0 0 0-.843 2.029l-.034.102a15.65 15.65 0 0 0-.786 5.174l.003.214a21.523 21.523 0 0 0 .04.754c.009.119.02.237.032.355.014.145.032.29.049.432l.01.08c.01.067.017.133.026.197.034.242.074.48.119.72.463 2.419 1.62 4.836 3.345 6.99l.078.098.08-.095c.688-.81 2.395-3.38 2.539-4.922l.003-.029-.014-.025a10.727 10.727 0 0 1-1.226-4.956c0-5.76 4.545-10.544 10.343-10.89l.381-.014a11.403 11.403 0 0 1 6.651 1.957l.054.036 3.862-2.237.05-.03v-.056c.006-6.08-2.384-11.793-6.729-16.089C34.932 2.361 29.16 0 23.013 0" fill="#F01F7A" fill-rule="evenodd"/> |
| 1331 | + </svg> |
| 1332 | +</svg> |
| 1333 | +""" |
| 1334 | + |
| 1335 | + badge = response.content.decode("utf-8") |
| 1336 | + logging.getLogger(__name__).warning(badge) |
| 1337 | + badge = [line.strip() for line in badge.split("\n")] |
| 1338 | + expected_badge = [line.strip() for line in expected_badge.split("\n")] |
| 1339 | + assert expected_badge == badge |
| 1340 | + assert response.status_code == status.HTTP_200_OK |
| 1341 | + |
1134 | 1342 | def test_yaml_range(self): |
1135 | 1343 | gh_owner = OwnerFactory(service="github") |
1136 | 1344 | repo = RepositoryFactory( |
|
0 commit comments