-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_temporal_correlation.py
135 lines (105 loc) · 4.12 KB
/
test_temporal_correlation.py
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
import unittest
import pprint
from unittest import mock
from temporal_correlation import TemporalCorrelationAnalysis, GitCommit
# See https://dzone.com/articles/temporal-correlation-git
# http://michaelfeathers.typepad.com/michael_feathers_blog/2011/09/temporal-correlation-of-class-changes.html
class GitCommitTest(unittest.TestCase):
@mock.patch('subprocess.Popen.communicate')
def test_create_git_commit(self, mocked_instance):
# Given.
class StdOutMock(object):
def splitlines(self):
return ["file1.txt", "file2.txt"]
git_commit = GitCommit(commit_id="abc")
mocked_instance.return_value = (StdOutMock(), None)
# When
file_list = git_commit.read_files_belonging_to_commit()
# Then
self.assertEqual(['file1.txt', 'file2.txt'], file_list,
"Files list is present")
def test_commit_score_score(self):
# Given.
git_commit = GitCommit(commit_id="abc")
git_commit.__dict__["_entries"] = ["file1.txt", "file2.txt"]
# When
score = git_commit._commit_score()
# Then
self.assertEqual(0.5, score)
def test_commit_score_oeps(self):
# Given.
git_commit = GitCommit(commit_id="abc")
# When
score = git_commit._commit_score()
# Then
self.assertEqual(0, score)
def test_construct_key(self):
# Given.
git_commit = GitCommit(commit_id="abc")
# When
key = git_commit.construct_key("file_a", "file_b")
# Then
self.assertEqual("file_a -> file_b", key)
class TemporalCorrelationTest(unittest.TestCase):
def setUp(self):
self.analysis = TemporalCorrelationAnalysis()
self.commit_ids = """bcce420a2d27558a5eb65ce5641cd978d86fb1f0
2bd49b18e4af1a0ef134c81755b7a2cad2683c8e
c85cda1f6ae899acb48b895adf8917ef7d5d4446"""
def test_convert_to_git_commits(self):
# When
array = self.analysis.convert_to_git_commits(self.commit_ids)
pprint.pprint(array)
# Then
expected = [
GitCommit("bcce420a2d27558a5eb65ce5641cd978d86fb1f0"),
GitCommit("2bd49b18e4af1a0ef134c81755b7a2cad2683c8e"),
GitCommit("c85cda1f6ae899acb48b895adf8917ef7d5d4446")
]
self.assertEqual(expected, array)
def test_to_weighted_matrix(self):
# Given
git_commit1 = GitCommit(commit_id="abc")
git_commit1.__dict__["_entries"] = ["file1.txt", "file2.txt"]
git_commit2 = GitCommit(commit_id="abc")
git_commit2.__dict__["_entries"] = ["file1.txt", "file2.txt"]
git_commit3 = GitCommit(commit_id="abc")
git_commit3.__dict__["_entries"] = ["file1.txt", "file2.txt"]
git_commits = [
git_commit1,
git_commit2,
git_commit3
]
# When
matrix = self.analysis._to_weighted_matrix(git_commits)
# Then
expected_matrix = {}
self.assertEqual(expected_matrix, matrix)
#
# def test_print_top_10(self):
# # Given
# array = [
# ["gall/apps/cms/tests/test_views.py"],
# ["config/base.cfg", "setup.cfg"],
# ["config/a.log", "setup.cfg"],
# ["config/a.log", "setup.cfg"],
# ["config/a.log", "setup.cfg"]
# ]
#
# # When
# result = self.analysis.print_top10(array)
# pprint.pprint(result)
#
# # Then
# expected = [('setup.cfg -> setup.cfg', 2.0),
# ('config/a.log -> setup.cfg', 1.5),
# ('setup.cfg -> config/a.log', 1.5),
# ('config/a.log -> config/a.log', 1.5),
# ('gall/apps/cms/tests/test_views.py -> gall/apps/cms/tests/test_views.py',
# 1.0),
# ('setup.cfg -> config/base.cfg', 0.5),
# ('config/base.cfg -> setup.cfg', 0.5),
# ('config/base.cfg -> config/base.cfg', 0.5)]
# self.assertSetEqual(expected, result, msg="The top most element")
if __name__ == '__main__':
unittest.main()