This repository was archived by the owner on Jun 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathtest_comparison.py
More file actions
1854 lines (1586 loc) · 66.4 KB
/
test_comparison.py
File metadata and controls
1854 lines (1586 loc) · 66.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import asyncio
import enum
import json
from collections import Counter
from datetime import datetime
from unittest.mock import PropertyMock, patch
import minio
import pytest
import pytz
from django.test import TestCase
from shared.django_apps.core.tests.factories import (
CommitFactory,
OwnerFactory,
PullFactory,
RepositoryFactory,
)
from shared.reports.api_report_service import SerializableReport
from shared.reports.resources import ReportFile
from shared.reports.types import ReportTotals
from shared.utils.merge import LineType
from compare.models import CommitComparison
from compare.tests.factories import CommitComparisonFactory
from core.models import Commit
from reports.tests.factories import CommitReportFactory
from services.comparison import (
CommitComparisonService,
Comparison,
ComparisonReport,
CreateChangeSummaryVisitor,
CreateLineComparisonVisitor,
FileComparison,
FileComparisonTraverseManager,
ImpactedFile,
LineComparison,
MissingComparisonReport,
PullRequestComparison,
Segment,
)
# Pulled from shared.django_apps.core.tests.factories.CommitFactory files.
# Contents don't actually matter, it's just for providing a format
# compatible with what SerializableReport expects. Used in
# ComparisonTests.
file_data = [
2,
[0, 10, 8, 2, 0, "80.00000", 0, 0, 0, 0, 0, 0, 0],
[[0, 10, 8, 2, 0, "80.00000", 0, 0, 0, 0, 0, 0, 0]],
[0, 2, 1, 1, 0, "50.00000", 0, 0, 0, 0, 0, 0, 0],
]
class MockOrderValue(object):
def __init__(self, value):
self.value = value
def __getitem__(self, key):
return getattr(self, key)
class OrderingDirection(enum.Enum):
ASC = "ascending"
DESC = "descending"
class LineNumberCollector:
"""
A visitor for testing line traversal.
"""
def __init__(self):
self.line_numbers = []
def __call__(self, base_ln, head_ln, value, is_diff):
self.line_numbers.append((base_ln, head_ln))
class FileComparisonTraverseManagerTests(TestCase):
def test_no_diff_results_in_no_line_number_adjustments(self):
manager = FileComparisonTraverseManager(head_file_eof=3, base_file_eof=3)
expected_result = [(1, 1), (2, 2)]
visitor = LineNumberCollector()
manager.apply(visitors=[visitor])
assert visitor.line_numbers == expected_result
def test_diff_with_added_lines_adjusts_lines(self):
# A line added at line 1 -- note header is string values, that's how
# torngit returns it
segments = [{"header": ["1", "1", "1", "2"], "lines": ["+"]}]
manager = FileComparisonTraverseManager(
head_file_eof=4, base_file_eof=3, segments=segments
)
expected_result = [(None, 1), (1, 2), (2, 3)]
visitor = LineNumberCollector()
manager.apply(visitors=[visitor])
assert visitor.line_numbers == expected_result
def test_diff_with_removed_lines_adjusts_lines(self):
# A line removed at line 1
segments = [{"header": ["1", "1", "1", "2"], "lines": ["-"]}]
manager = FileComparisonTraverseManager(
head_file_eof=3, base_file_eof=4, segments=segments
)
expected_result = [(1, None), (2, 1), (3, 2)]
visitor = LineNumberCollector()
manager.apply(visitors=[visitor])
assert visitor.line_numbers == expected_result
def test_diff_with_1_line_added_file_adjusts_lines(self):
segments = [{"header": ["0", "0", "1", ""], "lines": ["+"]}]
manager = FileComparisonTraverseManager(
head_file_eof=2, base_file_eof=0, segments=segments
)
expected_result = [(None, 1)]
visitor = LineNumberCollector()
manager.apply(visitors=[visitor])
assert visitor.line_numbers == expected_result
def test_diff_with_1_line_removed_file_adjusts_lines(self):
segments = [{"header": ["1", "1", "0", ""], "lines": ["-"]}]
manager = FileComparisonTraverseManager(
head_file_eof=0, base_file_eof=2, segments=segments
)
expected_result = [(1, None)]
visitor = LineNumberCollector()
manager.apply(visitors=[visitor])
assert visitor.line_numbers == expected_result
def test_pop_line_returns_none_if_no_diff_or_src(self):
manager = FileComparisonTraverseManager()
assert manager.pop_line() is None
def test_pop_line_pops_first_line_in_segment_if_traversing_that_segment(self):
expected_line_value = "+this is a line!"
segments = [
{
"header": [1, 2, 1, 3],
"lines": [expected_line_value, "this is another line"],
}
]
manager = FileComparisonTraverseManager(segments=segments)
assert manager.pop_line() == expected_line_value
def test_pop_line_returns_line_at_head_ln_index_in_src_if_not_in_segment(self):
expected_line_value = "a line from src!"
manager = FileComparisonTraverseManager(
head_file_eof=2, src=[expected_line_value]
)
assert manager.pop_line() == expected_line_value
def test_traversing_diff_returns_true_if_head_ln_within_segment_at_position_0(self):
manager = FileComparisonTraverseManager(
segments=[{"header": ["1", "6", "12", "4"]}]
)
manager.head_ln = 14
manager.base_ln = 1000
assert manager.traversing_diff() is True
manager.head_ln = 11
assert manager.traversing_diff() is False
def test_traversing_diff_handles_added_one_line_file_segment_header(self):
segment = {"header": ["0", "0", "1", ""], "lines": ["+"]}
manager = FileComparisonTraverseManager(segments=[segment])
assert manager.traversing_diff() is True
def test_traversing_diff_handles_removed_one_line_file_segment_header(self):
segment = {"header": ["1", "1", "0", ""], "lines": ["-"]}
manager = FileComparisonTraverseManager(segments=[segment])
assert manager.traversing_diff() is True
def test_traversing_diff_returns_true_if_base_ln_within_segment_at_position_0(self):
manager = FileComparisonTraverseManager(
segments=[{"header": ["4", "43", "4", "3"]}]
)
manager.head_ln = 7
manager.base_ln = 44
assert manager.traversing_diff() is True
def test_traverse_finished_returns_false_even_both_line_counters_at_eof_and_traversing_diff(
self,
):
# This accounts for an edge case wherein you remove a multi-line expression
# (which codecov counts as a single line, coverage-wise) at the
# end of a file. The git-diff counts these as multiple lines, so
# in order to not truncate the diff we need to continue popping
# lines off the segment even of line counters are technically both
# at EOF.
manager = FileComparisonTraverseManager(
head_file_eof=7,
base_file_eof=43,
segments=[{"header": ["4", "43", "4", "3"]}],
)
manager.base_ln = 45 # higher than eof, but still traversing_diff
manager.head_ln = 7 # highest it can go in this segment
assert manager.traverse_finished() is False
def test_no_indexerror_if_basefile_longer_than_headfile_and_src_provided(self):
manager = FileComparisonTraverseManager(
head_file_eof=3,
base_file_eof=4,
src=["hey"] * 2, # head file eof minus 1, which is the typical case
)
# No indexerror should occur
manager.apply([lambda a, b, c, d: None])
def test_can_traverse_diff_with_line_numbers_greater_than_file_eof(self):
# This can happen when we have a file ending in a large multi-line
# expression, and a diff is made somewhere within that expression,
# but the start of the diff occurs after the start of the expression.
# The previous implementation of "traverse_finished" would end the traverse
# on account of not "traversing_diff", and having the line indices be
# greater than the respective files' EOF. The fix for this bug is stronger
# than that of the above comment and should handle both cases.
segments = [
{
"header": ["3", "4", "3", "4"],
"lines": ["-Pro Team (billed monthly)", "+Pro Team"],
}
]
manager = FileComparisonTraverseManager(
head_file_eof=2, base_file_eof=2, segments=segments
)
visitor = LineNumberCollector()
manager.apply([visitor])
assert visitor.line_numbers == [(1, 1), (2, 2), (3, None), (None, 3)]
def test_can_traverse_diff_with_diff_like_lines(self):
src = [
"- line 1", # not part of diff
"- line 2", # not part of diff
"line 3",
]
# this is the diff
segments = [
{
"header": ["3", "4", "3", "4"],
"lines": ["-line 3", "+line 3"],
}
]
manager = FileComparisonTraverseManager(
head_file_eof=3, base_file_eof=3, segments=segments, src=src
)
visitor = LineNumberCollector()
manager.apply([visitor])
assert visitor.line_numbers == [(1, 1), (2, 2), (3, None), (None, 3)]
class CreateLineComparisonVisitorTests(TestCase):
def setUp(self):
self.head_file = ReportFile(
"file1", lines=[[0, "", [], 0, 0], None, [0, "", [], 0, 0]]
)
self.base_file = ReportFile(
"file1", lines=[None, [0, "", [], 0, 0], None, [0, "", [], 0, 0]]
)
def test_skips_if_line_value_is_none(self):
visitor = CreateLineComparisonVisitor(self.base_file, self.head_file)
visitor(0, 0, None, False)
assert visitor.lines == []
def test_appends_line_comparison_with_relevant_fields_if_line_value_not_none(self):
base_ln = 2
head_ln = 1
base_line = self.base_file._lines[base_ln - 1]
head_line = self.head_file._lines[head_ln - 1]
value = "sup dood"
is_diff = True
visitor = CreateLineComparisonVisitor(self.base_file, self.head_file)
visitor(base_ln, head_ln, value, is_diff)
line = visitor.lines[0]
assert line.head_ln == head_ln
assert line.base_ln == base_ln
assert line.head_line == head_line
assert line.base_line == base_line
assert line.value == value
assert line.is_diff == is_diff
def test_appends_line_comparison_with_no_base_line_if_no_base_file_or_line_not_in_base_file(
self,
):
visitor = CreateLineComparisonVisitor(self.base_file, self.head_file)
visitor(100, 1, "", False) # 100 is not a line in the base file
assert visitor.lines[0].base_line is None
visitor.base_file = None
visitor(
2, 1, "", False
) # all valid line numbers, but still expect none without base_file
assert visitor.lines[1].base_line is None
def test_appends_line_comparison_with_no_head_line_if_no_head_file_or_line_not_in_head_file(
self,
):
visitor = CreateLineComparisonVisitor(self.base_file, self.head_file)
visitor(2, 100, "", False)
assert visitor.lines[0].head_line is None
visitor.head_file = None
visitor(1, 2, "", False)
assert visitor.lines[1].head_line is None
class CreateChangeSummaryVisitorTests(TestCase):
def setUp(self):
self.head_file = ReportFile("file1", lines=[[1, "", [], 0, 0]])
self.base_file = ReportFile("file1", lines=[[0, "", [], 0, 0]])
def test_changed_lines_in_diff_do_not_affect_change_summary(self):
visitor = CreateChangeSummaryVisitor(self.base_file, self.head_file)
visitor(1, 1, "+", False)
assert visitor.summary == {}
visitor(1, 1, "-", False)
assert visitor.summary == {}
def test_summary_with_one_less_miss_and_one_more_hit(self):
visitor = CreateChangeSummaryVisitor(self.base_file, self.head_file)
visitor(1, 1, "", True)
assert visitor.summary == {"misses": -1, "hits": 1}
def test_summary_with_one_less_hit_and_one_more_partial(self):
self.base_file._lines[0][0] = 1
self.head_file._lines[0][0] = "1/2"
visitor = CreateChangeSummaryVisitor(self.base_file, self.head_file)
visitor(1, 1, "", True)
assert visitor.summary == {"hits": -1, "partials": 1}
class LineComparisonTests(TestCase):
def test_number_shows_number_from_base_and_head(self):
base_ln = 3
head_ln = 4
lc = LineComparison(
[0, "", [], 0, 0], [0, "", [], 0, 0], base_ln, head_ln, "", False
)
assert lc.number == {"base": base_ln, "head": head_ln}
def test_number_shows_none_for_base_if_added(self):
head_ln = 4
lc = LineComparison(None, [0, "", [], 0, 0], 0, head_ln, "+", True)
assert lc.number == {"base": None, "head": head_ln}
def test_number_shows_none_for_base_if_plus_not_part_of_diff(self):
base_ln = 3
head_ln = 4
lc = LineComparison(None, [0, "", [], 0, 0], base_ln, head_ln, "+", False)
assert lc.number == {"base": base_ln, "head": head_ln}
def test_number_shows_none_for_base_if_minus_not_part_of_diff(self):
base_ln = 3
head_ln = 4
lc = LineComparison(None, [0, "", [], 0, 0], base_ln, head_ln, "-", False)
assert lc.number == {"base": base_ln, "head": head_ln}
def test_number_shows_none_for_head_if_removed(self):
base_ln = 3
lc = LineComparison([0, "", [], 0, 0], None, base_ln, 0, "-", True)
assert lc.number == {"base": base_ln, "head": None}
def test_coverage_shows_coverage_for_base_and_head(self):
base_cov, head_cov = 0, 1
lc = LineComparison(
[base_cov, "", [], 0, 0], [head_cov, "", [], 0, 0], 0, 0, "", False
)
assert lc.coverage == {"base": LineType.miss, "head": LineType.hit}
def test_coverage_shows_none_for_base_if_added(self):
head_cov = 1
lc = LineComparison(None, [head_cov, "", [], 0, 0], 0, 0, "+", False)
assert lc.coverage == {"base": None, "head": LineType.hit}
def test_coverage_shows_none_for_head_if_removed(self):
base_cov = 0
lc = LineComparison([base_cov, "", [], 0, 0], None, 0, 0, "-", False)
assert lc.coverage == {"base": LineType.miss, "head": None}
def test_hit_count_returns_sessions_hit_in_head(self):
lc = LineComparison(
None,
[
1,
"",
[
[0, 1, 0, 0, 0],
[1, 2, 0, 0, 0],
[2, 0, 0, 0, 0],
[3, "2/2", 0, 0, 0],
],
0,
0,
],
0,
0,
"",
False,
)
assert lc.hit_count == 3
def test_hit_count_returns_none_if_no_coverage(self):
lc = LineComparison(None, [0, "", [[0, 0, 0, 0, 0]], 0, 0], 0, 0, "", False)
assert lc.hit_count is None
def test_hit_session_ids(self):
lc = LineComparison(
None,
[
1,
"",
[
[0, 1, 0, 0, 0],
[1, 2, 0, 0, 0],
[2, 0, 0, 0, 0],
[3, "2/2", 0, 0, 0],
],
0,
0,
],
0,
0,
"",
False,
)
assert lc.hit_session_ids == [0, 1, 3]
def test_hit_session_ids_no_coverage(self):
lc = LineComparison(None, [0, "", [[0, 0, 0, 0, 0]], 0, 0], 0, 0, "", False)
assert lc.hit_session_ids is None
def test_hit_session_ids_no_head_line(self):
lc = LineComparison(None, None, 0, 0, "", False)
assert lc.hit_session_ids is None
class FileComparisonConstructorTests(TestCase):
def test_constructor_no_keyError_if_diff_data_segements_is_missing(self):
FileComparison(
head_file=ReportFile("file1"), base_file=ReportFile("file1"), diff_data={}
)
class FileComparisonTests(TestCase):
def setUp(self):
self.file_comparison = FileComparison(
head_file=ReportFile("file1"), base_file=ReportFile("file1")
)
def test_name_shows_name_for_base_and_head(self):
assert self.file_comparison.name == {
"base": self.file_comparison.base_file.name,
"head": self.file_comparison.head_file.name,
}
def test_name_none_if_base_or_head_if_files_none(self):
self.file_comparison.head_file = None
assert self.file_comparison.name == {
"base": self.file_comparison.base_file.name,
"head": None,
}
self.file_comparison.base_file = None
assert self.file_comparison.name == {"base": None, "head": None}
def test_totals_shows_totals_for_base_and_head(self):
assert self.file_comparison.totals == {
"base": self.file_comparison.base_file.totals,
"head": self.file_comparison.head_file.totals,
"diff": None,
}
def test_totals_shows_totals_for_base_head_and_diff(self):
diff_totals = ReportTotals.default_totals()
self.file_comparison.diff_data = {
"totals": diff_totals,
}
assert self.file_comparison.totals == {
"base": self.file_comparison.base_file.totals,
"head": self.file_comparison.head_file.totals,
"diff": diff_totals,
}
def test_totals_base_is_none_if_missing_basefile(self):
self.file_comparison.base_file = None
assert self.file_comparison.totals == {
"base": None,
"head": self.file_comparison.head_file.totals,
"diff": None,
}
def test_totals_head_is_none_if_missing_headfile(self):
self.file_comparison.head_file = None
assert self.file_comparison.totals == {
"base": self.file_comparison.base_file.totals,
"head": None,
"diff": None,
}
def test_totals_includes_diff_totals_if_diff(self):
totals = "these are the totals"
self.file_comparison.diff_data = {"totals": totals}
assert self.file_comparison.totals["head"].diff == totals
def test_has_diff_returns_true_iff_diff_data_not_none(self):
assert self.file_comparison.has_diff is False
self.file_comparison.diff_data = {}
assert self.file_comparison.has_diff is True
def test_stats_returns_none_if_no_diff_data(self):
assert self.file_comparison.has_diff is False
assert self.file_comparison.stats is None
def test_stats_returns_diff_stats_if_diff_data(self):
expected_stats = "yep"
self.file_comparison.diff_data = {"stats": expected_stats}
assert self.file_comparison.stats == expected_stats
def test_lines_returns_empty_list_if_no_diff_or_src(self):
assert self.file_comparison.lines == []
# essentially a smoke/integration test
def test_lines(self):
head_lines = [
[1, "", [], 0, None],
["1/2", "", [], 0, None],
[1, "", [], 0, None],
]
base_lines = [[0, "", [], 0, None], [1, "", [], 0, None], [0, "", [], 0, None]]
first_line_val = "unchanged line from src"
second_line_val = "+this is an added line"
third_line_val = "-this is a removed line"
last_line_val = "this is the third line"
segment = {
"header": ["2", "2", "2", "2"],
"lines": [second_line_val, third_line_val],
}
src = [first_line_val, "this is an added line", last_line_val]
self.file_comparison.head_file._lines = head_lines
self.file_comparison.base_file._lines = base_lines
self.file_comparison.diff_data = {"segments": [segment]}
self.file_comparison.src = src
assert self.file_comparison.lines[0].value == first_line_val
assert self.file_comparison.lines[0].number == {"base": 1, "head": 1}
assert self.file_comparison.lines[0].coverage == {
"base": LineType.miss,
"head": LineType.hit,
}
assert self.file_comparison.lines[1].value == second_line_val
assert self.file_comparison.lines[1].number == {"base": None, "head": 2}
assert self.file_comparison.lines[1].coverage == {
"base": None,
"head": LineType.partial,
}
assert self.file_comparison.lines[2].value == third_line_val
assert self.file_comparison.lines[2].number == {"base": 2, "head": None}
assert self.file_comparison.lines[2].coverage == {
"base": LineType.hit,
"head": None,
}
assert self.file_comparison.lines[3].value == last_line_val
assert self.file_comparison.lines[3].number == {"base": 3, "head": 3}
assert self.file_comparison.lines[3].coverage == {
"base": LineType.miss,
"head": LineType.hit,
}
@patch("services.comparison.FileComparison.lines", new_callable=PropertyMock)
def test_segments_diff_only(self, lines):
lines.return_value = [
LineComparison([1], [1], 1, 1, "first line", False),
LineComparison(None, [1], None, 2, "+this is an added line", True),
LineComparison([1], None, 2, None, "-this is a removed line", True),
LineComparison([1], [1], 3, 3, "last line", False),
]
segments = self.file_comparison.segments
assert len(segments) == 1
assert segments[0].lines == self.file_comparison.lines
assert segments[0].header == (1, 3, 1, 3)
assert segments[0].has_diff_changes == True
assert segments[0].has_unintended_changes == False
@patch("services.comparison.FileComparison.lines", new_callable=PropertyMock)
def test_segments_changes_only(self, lines):
lines.return_value = [
LineComparison([1], [1], 1, 1, "first line", False),
LineComparison([0], [1], 2, 2, "middle line", False), # coverage added
LineComparison([1], [1], 3, 3, "last line", False),
]
segments = self.file_comparison.segments
assert len(segments) == 1
assert segments[0].lines == self.file_comparison.lines
assert segments[0].header == (1, 3, 1, 3)
assert segments[0].has_diff_changes == False
assert segments[0].has_unintended_changes
@patch("services.comparison.FileComparison.lines", new_callable=PropertyMock)
def test_segments_no_changes_no_diff(self, lines):
lines.return_value = [
LineComparison([1], [1], 1, 1, "first line", False),
LineComparison([1], [1], 2, 2, "middle line", False),
LineComparison([1], [1], 3, 3, "last line", False),
]
segments = self.file_comparison.segments
assert len(segments) == 0
def test_change_summary(self):
head_lines = [
[1, "", [], 0, None],
["3/4", "", [], 0, None],
[1, "", [], 0, None],
]
base_lines = [[0, "", [], 0, None], [1, "", [], 0, None], [0, "", [], 0, None]]
first_line_val = "unchanged line from src"
second_line_val = "+this is an added line"
third_line_val = "-this is a removed line"
last_line_val = "this is the third line"
segment = {
"header": ["2", "2", "2", "2"],
"lines": [second_line_val, third_line_val],
}
src = [first_line_val, "this is an added line", last_line_val]
self.file_comparison.head_file._lines = head_lines
self.file_comparison.base_file._lines = base_lines
self.file_comparison.diff_data = {"segments": [segment]}
self.file_comparison.src = src
assert self.file_comparison.change_summary == {"hits": 2, "misses": -2}
@patch(
"services.comparison.FileComparison.change_summary", new_callable=PropertyMock
)
def test_has_changes(self, change_summary_mock):
change_summary_mock.return_value = Counter()
assert self.file_comparison.has_changes == False
change_summary_mock.return_value = Counter({"hits": 0, "misses": 0})
assert self.file_comparison.has_changes == False
change_summary_mock.return_value = Counter({"hits": 1, "misses": -1})
assert self.file_comparison.has_changes == True
@patch("services.comparison.FileComparisonTraverseManager.apply")
def test_does_not_calculate_changes_if_no_diff_and_should_search_for_changes_is_False(
self, mocked_apply_traverse
):
self.file_comparison.should_search_for_changes = False
self.file_comparison._calculated_changes_and_lines
mocked_apply_traverse.assert_not_called()
@patch("services.comparison.FileComparisonTraverseManager.apply")
def test_calculates_changes_if_no_diff_and_should_search_for_changes_is_None(
self, mocked_apply_traverse
):
self.file_comparison.should_search_for_changes = None
self.file_comparison._calculated_changes_and_lines
mocked_apply_traverse.assert_called_once()
@patch("services.comparison.FileComparisonTraverseManager.apply")
def test_calculates_changes_should_search_for_changes_is_True(
self, mocked_apply_traverse
):
self.file_comparison.should_search_for_changes = True
self.file_comparison._calculated_changes_and_lines
mocked_apply_traverse.assert_called_once()
@patch("services.comparison.FileComparisonTraverseManager.apply")
def test_calculates_changes_if_traversing_src(self, mocked_apply_traverse):
self.file_comparison.should_search_for_changes = False
self.file_comparison.src = ["a truthy list"]
self.file_comparison._calculated_changes_and_lines
mocked_apply_traverse.assert_called_once()
@patch("services.comparison.Comparison.git_comparison", new_callable=PropertyMock)
@patch("services.comparison.Comparison.head_report", new_callable=PropertyMock)
@patch("services.comparison.Comparison.base_report", new_callable=PropertyMock)
class ComparisonTests(TestCase):
def setUp(self):
owner = OwnerFactory()
base, head = CommitFactory(author=owner), CommitFactory(author=owner)
self.comparison = Comparison(user=owner, base_commit=base, head_commit=head)
def test_files_gets_file_comparison_for_each_file_in_head_report(
self, base_report_mock, head_report_mock, git_comparison_mock
):
head_report_files = {"file1": file_data, "file2": file_data}
head_report_mock.return_value = SerializableReport(files=head_report_files)
base_report_mock.return_value = SerializableReport(files={})
git_comparison_mock.return_value = {"diff": {"files": {}}}
assert sum(1 for x in self.comparison.files) == 2
for fc in self.comparison.files:
assert isinstance(fc, FileComparison)
assert fc.head_file.name in head_report_files
assert fc.base_file is None
def test_get_file_comparison_adds_in_file_from_base_report_if_exists(
self, base_report_mock, head_report_mock, git_comparison_mock
):
git_comparison_mock.return_value = {"diff": {"files": {}}}
files = {"both.py": file_data}
base_report_mock.return_value = SerializableReport(files=files)
head_report_mock.return_value = SerializableReport(files=files)
fc = self.comparison.get_file_comparison("both.py")
assert fc.head_file.name == "both.py"
assert fc.base_file.name == "both.py"
def test_get_file_comparison_accounts_for_renamed_files(
self, base_report_mock, head_report_mock, git_comparison_mock
):
file_name = "myfile.py"
previous_name = "previous.py"
base_report_files = {previous_name: file_data}
base_report_mock.return_value = SerializableReport(files=base_report_files)
head_report_files = {file_name: file_data}
head_report_mock.return_value = SerializableReport(files=head_report_files)
git_comparison_mock.return_value = {
"diff": {"files": {file_name: {"before": previous_name, "segments": []}}},
"commits": [],
}
fc = self.comparison.get_file_comparison(file_name)
assert fc.head_file.name == file_name
assert fc.base_file.name == previous_name
def test_get_file_comparison_includes_diff_data_if_exists(
self, base_report_mock, head_report_mock, git_comparison_mock
):
file_name = "f"
diff_data = {
"segments": [{"header": ["4", "6", "7", "3"], "lines": []}],
"stats": {"added": 3, "removed": 2},
}
base_report_mock.return_value = SerializableReport(files={})
head_report_files = {file_name: file_data}
head_report_mock.return_value = SerializableReport(files=head_report_files)
git_comparison_mock.return_value = {"diff": {"files": {file_name: diff_data}}}
fc = self.comparison.get_file_comparison(file_name)
assert fc.diff_data == diff_data
@patch("services.repo_providers.RepoProviderService.get_adapter")
def test_get_file_comparison_includes_src_if_with_src_is_true(
self,
mocked_comparison_adapter,
base_report_mock,
head_report_mock,
git_comparison_mock,
):
from api.internal.tests.views.test_compare_viewset import (
MockedComparisonAdapter,
)
src = b"two\nlines"
git_comparison_mock.return_value = {"diff": {"files": {}}}
mocked_comparison_adapter.return_value = MockedComparisonAdapter(
{"diff": {"files": {}}}, test_lines=src
)
file_name = "f"
base_report_mock.return_value = SerializableReport(files={})
head_report_files = {file_name: file_data}
head_report_mock.return_value = SerializableReport(files=head_report_files)
fc = self.comparison.get_file_comparison(file_name, with_src=True)
assert fc.src == ["two", "lines"]
@patch("services.repo_providers.RepoProviderService.get_adapter")
def test_get_file_comparison_can_parse_string_src(
self,
mocked_comparison_adapter,
base_report_mock,
head_report_mock,
git_comparison_mock,
):
from api.internal.tests.views.test_compare_viewset import (
MockedComparisonAdapter,
)
src = "two\nlines"
git_comparison_mock.return_value = {"diff": {"files": {}}}
mocked_comparison_adapter.return_value = MockedComparisonAdapter(
{"diff": {"files": {}}}, test_lines=src
)
file_name = "f"
base_report_mock.return_value = SerializableReport(files={})
head_report_files = {file_name: file_data}
head_report_mock.return_value = SerializableReport(files=head_report_files)
fc = self.comparison.get_file_comparison(file_name, with_src=True)
assert fc.src == ["two", "lines"]
def test_get_file_comparison_with_no_base_report_doesnt_crash(
self, base_report_mock, head_report_mock, git_comparison_mock
):
git_comparison_mock.return_value = {"diff": {"files": {}}}
files = {"both.py": file_data}
base_report_mock.return_value = None
head_report_mock.return_value = SerializableReport(files=files)
fc = self.comparison.get_file_comparison("both.py")
assert fc.head_file.name == "both.py"
@pytest.mark.xfail # TODO(pierce): investigate this feature
def test_files_adds_deleted_files_that_were_tracked_in_base_report(
self, base_report_mock, head_report_mock, git_comparison_mock
):
deleted_file_name = "deleted.py"
base_report_files = {deleted_file_name: file_data}
base_report_mock.return_value = SerializableReport(files=base_report_files)
head_report_files = {}
head_report_mock.return_value = SerializableReport(files=head_report_files)
git_comparison_mock.return_value = {
"diff": {"files": {deleted_file_name: {"type": "deleted"}}},
"commits": [],
}
assert self.comparison.files[0].base_file.name == deleted_file_name
assert self.comparison.files[0].head_file is None
assert self.comparison.files[0].diff_data == {"type": "deleted"}
def test_totals_returns_head_totals_if_exists(
self, base_report_mock, head_report_mock, git_comparison_mock
):
base_report_mock.return_value = None
head_report_mock.return_value = SerializableReport()
assert self.comparison.totals["head"] == head_report_mock.return_value.totals
assert self.comparison.totals["base"] is None
def test_totals_returns_base_totals_if_exists(
self, base_report_mock, head_report_mock, git_comparison_mock
):
head_report_mock.return_value = None
base_report_mock.return_value = SerializableReport()
assert self.comparison.totals["base"] == base_report_mock.return_value.totals
assert self.comparison.totals["head"] is None
def test_totals_returns_diff_totals_if_exists(
self, base_report_mock, head_report_mock, git_comparison_mock
):
head_report = SerializableReport()
head_report_mock.return_value = head_report
base_report_mock.return_value = None
diff_totals = ReportTotals.default_totals()
git_comparison_mock.return_value = {"diff": {"totals": diff_totals}}
assert self.comparison.totals["base"] is None
assert self.comparison.totals["head"] == head_report.totals
assert self.comparison.totals["diff"] is diff_totals
def test_head_and_base_reports_have_cff_sessions(
self, base_report_mock, head_report_mock, _
):
# Only relevant files keys to the session object
head_report_sessions = {"0": {"st": "carriedforward"}}
head_report = SerializableReport(sessions=head_report_sessions)
head_report_mock.return_value = head_report
base_report_sessions = {"0": {"st": "carriedforward"}}
base_report = SerializableReport(sessions=base_report_sessions)
base_report_mock.return_value = base_report
fc = self.comparison.has_different_number_of_head_and_base_sessions
assert fc == False
@patch(
"services.comparison.Comparison.head_report_without_applied_diff",
new_callable=PropertyMock,
)
def test_head_and_base_reports_have_different_number_of_reports(
self, head_report_no_diff_mock, base_report_mock, head_report_mock, _
):
# Only relevant files keys to the session object
head_report_sessions = {"0": {"st": "uploaded"}, "1": {"st": "uploaded"}}
head_report = SerializableReport(sessions=head_report_sessions)
head_report_no_diff_mock.return_value = head_report
base_report_sessions = {"0": {"st": "uploaded"}}
base_report = SerializableReport(sessions=base_report_sessions)
base_report_mock.return_value = base_report
fc = self.comparison.has_different_number_of_head_and_base_sessions
assert fc == True
def test_head_and_base_reports_have_same_number_of_reports(
self, base_report_mock, head_report_mock, _
):
# Only relevant files keys to the session object
head_report_sessions = {"0": {"st": "uploaded"}}
head_report = SerializableReport(sessions=head_report_sessions)
head_report_mock.return_value = head_report
base_report_sessions = {"0": {"st": "uploaded"}}
base_report = SerializableReport(sessions=base_report_sessions)
base_report_mock.return_value = base_report
fc = self.comparison.has_different_number_of_head_and_base_sessions
assert fc == False
class PullRequestComparisonTests(TestCase):
def setUp(self):
owner = OwnerFactory()
repo = RepositoryFactory(author=owner)
base, head, compared_to = (
CommitFactory(repository=repo),
CommitFactory(repository=repo),
CommitFactory(repository=repo),
)
self.pull = PullFactory(
repository=repo,
base=base.commitid,
head=head.commitid,
compared_to=compared_to.commitid,
)
self.comparison = PullRequestComparison(user=owner, pull=self.pull)
def test_files_with_changes_hash_key(self):
assert self.comparison._files_with_changes_hash_key == "/".join(
(
"compare-changed-files",
self.pull.repository.author.service,
self.pull.repository.author.username,
self.pull.repository.name,
str(self.pull.pullid),
)
)
@patch("redis.Redis.get")
def test_files_with_changes_retrieves_from_redis(self, mocked_get):
filename = "something.py"
mocked_get.return_value = json.dumps([filename])
assert self.comparison._files_with_changes == [filename]
@patch("redis.Redis.get")
def test_files_with_changes_returns_none_if_no_files_with_changes(self, mocked_get):
mocked_get.return_value = None
assert self.comparison._files_with_changes is None