1- import json
2- from unittest .mock import Mock , patch
1+ from unittest .mock import patch
32
43from django .utils import timezone
54from rest_framework .reverse import reverse
1211from core .models import Repository
1312from core .tests .factories import (
1413 CommitFactory ,
15- CommitWithReportFactory ,
1614 RepositoryFactory ,
1715)
1816from utils .test_utils import Client
@@ -732,166 +730,31 @@ def test_retrieve_with_no_commits_doesnt_crash(self, mocked_get_permissions):
732730 def test_retrieve_returns_latest_commit_data (self , mocked_get_permissions ):
733731 self .maxDiff = None
734732 mocked_get_permissions .return_value = True , True
735- commit = CommitWithReportFactory (
733+ commit = CommitFactory (
736734 repository = self .repo ,
737- )
738-
739- report_details = commit .reports .first ().reportdetails
740- report_details ._files_array = [
741- {
742- "filename" : "test_file_1.py" ,
743- "file_index" : 2 ,
744- "file_totals" : [1 , 10 , 8 , 2 , 5 , "80.00000" , 6 , 7 , 9 , 8 , 20 , 40 , 13 ],
745- "diff_totals" : [0 , 2 , 1 , 1 , 0 , "50.00000" , 0 , 0 , 0 , 0 , 0 , 0 , 0 ],
746- },
747- {
748- "filename" : "test_file_2.py" ,
749- "file_index" : 0 ,
750- "file_totals" : [1 , 3 , 2 , 1 , 0 , "66.66667" , 0 , 0 , 0 , 0 , 0 , 0 , 0 ],
751- "diff_totals" : None ,
752- },
753- ]
754- report_details .save ()
755-
756- from api .internal .commit .serializers import CommitWithFileLevelReportSerializer
757-
758- expected_commit_payload = CommitWithFileLevelReportSerializer (commit ).data
759-
760- response = self ._retrieve ()
761- assert response .status_code == 200
762- assert (
763- response .data ["latest_commit" ]["report" ]["totals" ]
764- == expected_commit_payload ["report" ]["totals" ]
765- )
766- self .assertEqual (
767- response .data ["latest_commit" ]["report" ]["files" ],
768- [
769- {
770- "name" : "test_file_1.py" ,
771- "totals" : {
772- "files" : 1 ,
773- "lines" : 10 ,
774- "hits" : 8 ,
775- "misses" : 2 ,
776- "partials" : 5 ,
777- "coverage" : 80.0 ,
778- "branches" : 6 ,
779- "methods" : 7 ,
780- "sessions" : 8 ,
781- "complexity" : 20.0 ,
782- "complexity_total" : 40.0 ,
783- "complexity_ratio" : 50.0 ,
784- "diff" : 0 ,
785- },
786- },
787- {
788- "name" : "test_file_2.py" ,
789- "totals" : {
790- "files" : 1 ,
791- "lines" : 3 ,
792- "hits" : 2 ,
793- "misses" : 1 ,
794- "partials" : 0 ,
795- "coverage" : 66.66 ,
796- "branches" : 0 ,
797- "methods" : 0 ,
798- "sessions" : 0 ,
799- "complexity" : 0 ,
800- "complexity_total" : 0 ,
801- "complexity_ratio" : 0 ,
802- "diff" : 0 ,
803- },
735+ _report = {
736+ "files" : {
737+ "test_file_1.py" : [
738+ 2 ,
739+ [1 , 10 , 8 , 2 , 5 , "80.00000" , 6 , 7 , 9 , 8 , 20 , 40 , 13 ],
740+ [[0 , 10 , 8 , 2 , 0 , "80.00000" , 0 , 0 , 0 , 0 , 0 , 0 , 0 ]],
741+ [0 , 2 , 1 , 1 , 0 , "50.00000" , 0 , 0 , 0 , 0 , 0 , 0 , 0 ],
742+ ],
743+ "test_file_2.py" : [
744+ 0 ,
745+ [1 , 3 , 2 , 1 , 0 , "66.66667" , 0 , 0 , 0 , 0 , 0 , 0 , 0 ],
746+ [[0 , 3 , 2 , 1 , 0 , "66.66667" , 0 , 0 , 0 , 0 , 0 , 0 , 0 ]],
747+ None ,
748+ ],
804749 },
805- ],
806- )
807-
808- @patch ("services.archive.ArchiveService.read_chunks" , lambda obj , _ : "" )
809- @patch ("shared.django_apps.utils.model_utils.ArchiveService" )
810- def test_retrieve_returns_latest_commit_data_report_details_from_storage (
811- self , mocker_archive_service , mocked_get_permissions
812- ):
813- self .maxDiff = None
814- mocked_get_permissions .return_value = True , True
815- files_array_storage_path = "https://storage/path/to/details/files_array.json"
816- commit = CommitWithReportFactory (
817- repository = self .repo ,
750+ "sessions" : {},
751+ },
818752 )
819753
820- def side_effect (path , * args , ** kwargs ):
821- if path == files_array_storage_path :
822- return json .dumps (
823- [
824- {
825- "filename" : "test_file_1.py" ,
826- "file_index" : 2 ,
827- "file_totals" : [
828- 1 ,
829- 10 ,
830- 8 ,
831- 2 ,
832- 5 ,
833- "80.00000" ,
834- 6 ,
835- 7 ,
836- 9 ,
837- 8 ,
838- 20 ,
839- 40 ,
840- 13 ,
841- ],
842- "diff_totals" : [
843- 0 ,
844- 2 ,
845- 1 ,
846- 1 ,
847- 0 ,
848- "50.00000" ,
849- 0 ,
850- 0 ,
851- 0 ,
852- 0 ,
853- 0 ,
854- 0 ,
855- 0 ,
856- ],
857- },
858- {
859- "filename" : "test_file_2.py" ,
860- "file_index" : 0 ,
861- "file_totals" : [
862- 1 ,
863- 3 ,
864- 2 ,
865- 1 ,
866- 0 ,
867- "66.66667" ,
868- 0 ,
869- 0 ,
870- 0 ,
871- 0 ,
872- 0 ,
873- 0 ,
874- 0 ,
875- ],
876- "diff_totals" : None ,
877- },
878- ]
879- )
880- else :
881- return ""
882-
883- mock_read_file = Mock (side_effect = side_effect )
884- mocker_archive_service .return_value = Mock (read_file = mock_read_file )
885- report_details = commit .reports .first ().reportdetails
886- report_details ._files_array = None
887- report_details ._files_array_storage_path = files_array_storage_path
888- report_details .save ()
889-
890754 from api .internal .commit .serializers import CommitWithFileLevelReportSerializer
891755
892756 expected_commit_payload = CommitWithFileLevelReportSerializer (commit ).data
893- mocker_archive_service .assert_called_with (repository = commit .repository )
894- mock_read_file .assert_called_with (files_array_storage_path )
757+
895758 response = self ._retrieve ()
896759 assert response .status_code == 200
897760 assert (
@@ -946,10 +809,8 @@ def test_retrieve_returns_latest_commit_of_default_branch_if_branch_not_specifie
946809 ):
947810 mocked_get_permissions .return_value = True , True
948811
949- commit = CommitWithReportFactory (repository = self .repo )
950- more_recent_commit = CommitWithReportFactory (
951- repository = self .repo , branch = "other-branch"
952- )
812+ commit = CommitFactory (repository = self .repo )
813+ more_recent_commit = CommitFactory (repository = self .repo , branch = "other-branch" )
953814
954815 response = self ._retrieve ()
955816
@@ -962,8 +823,8 @@ def test_retrieve_accepts_branch_query_param_to_specify_latest_commit(
962823 ):
963824 mocked_get_permissions .return_value = True , True
964825
965- commit = CommitWithReportFactory (repository = self .repo , branch = "other-branch" )
966- more_recent_commit = CommitWithReportFactory (repository = self .repo )
826+ commit = CommitFactory (repository = self .repo , branch = "other-branch" )
827+ more_recent_commit = CommitFactory (repository = self .repo )
967828
968829 response = self ._retrieve (data = {"branch" : "other-branch" })
969830
0 commit comments