1package repositories_test
2
3import (
4	"fmt"
5	"testing"
6
7	"github.com/stretchr/testify/assert"
8
9	c "repodiff/constants"
10	e "repodiff/entities"
11	repoSQL "repodiff/persistence/sql"
12	"repodiff/repositories"
13)
14
15const arbitraryTimestamp = e.RepoTimestamp(1525978906)
16
17var fakeTarget = e.DiffTarget{
18	Upstream: e.Project{
19		URL:    "https://keystone-qcom.googlesource.com/platform/manifest",
20		Branch: "p-fs-release",
21	},
22	Downstream: e.Project{
23		URL:    "https://keystone-qcom.googlesource.com/platform/manifest",
24		Branch: "p-keystone-qcom",
25	},
26}
27var fakeMappedTarget = e.MappedDiffTarget{
28	UpstreamTarget:   1,
29	DownstreamTarget: 2,
30}
31
32func init() {
33	clearTable("denormalized_view_recent_project")
34	clearTable("denormalized_view_changes_over_time")
35}
36
37func getRowCountAtTable(tableName string) int {
38	db, _ := repoSQL.GetDBConnectionPool()
39	var count int
40	db.QueryRow(
41		fmt.Sprintf("SELECT COUNT(*) FROM %s", tableName),
42	).Scan(&count)
43	return count
44}
45
46func TestInsertDenormalizedDiffRows(t *testing.T) {
47	tableName := "denormalized_view_recent_project"
48	defer clearTable(tableName)
49
50	assert.Equal(t, 0, getRowCountAtTable(tableName), "Rows should start empty")
51
52	d, err := repositories.NewScopedDenormalizerRepository(fakeTarget, fakeMappedTarget)
53	assert.Equal(t, nil, err, "Error should not be nil")
54
55	fixtures := fakeFixtures()
56	err = d.DenormalizeToRecentView(fixtures)
57	assert.Equal(t, nil, err, "Error should be nil")
58	assert.Equal(t, len(fixtures), getRowCountAtTable(tableName), "Rows should be inserted")
59}
60
61func TestDenormalizeToChangesOverTime(t *testing.T) {
62	defer clearTable("denormalized_view_changes_over_time")
63
64	d, _ := repositories.NewScopedDenormalizerRepository(fakeTarget, fakeMappedTarget)
65	fixtures := fakeFixtures()
66	fixtures[0].DBInsertTimestamp = 1519666754
67	err := d.DenormalizeToChangesOverTime(fixtures)
68	assert.Equal(t, nil, err, "Error should be nil")
69}
70
71func TestDenormalizeToRecentCommits(t *testing.T) {
72	tableName := "denormalized_view_recent_commit"
73	defer clearTable(tableName)
74	d, _ := repositories.NewScopedDenormalizerRepository(fakeTarget, fakeMappedTarget)
75	fixture := e.AnalyzedCommitRow{
76		CommitRow: e.CommitRow{
77			Date:              "2018/02/20",
78			Commit:            "61d5e61b6b6dfbf52d0d433759da964db31cc106",
79			DownstreamProject: "platform/vendor/unbundled_google/packages/Ears",
80			Author:            "slobdell@google.com",
81			// Actual commit subject!
82			Subject: "Import translations. DO NOT MERGE",
83		},
84	}
85	fixtures := []e.AnalyzedCommitRow{
86		fixture,
87	}
88	err := d.DenormalizeToRecentCommits(
89		fixtures,
90		map[string]e.RepoTimestamp{
91			"61d5e61b6b6dfbf52d0d433759da964db31cc106": arbitraryTimestamp,
92		},
93	)
94	assert.Equal(t, nil, err, "Error should be nil")
95	assert.Equal(t, len(fixtures), getRowCountAtTable(tableName), "Rows should be inserted")
96}
97
98func TestDenormalizeToTopCommitter(t *testing.T) {
99	tableName := "denormalized_view_top_committer"
100	defer clearTable(tableName)
101	defer clearTable("denormalized_view_recent_commit")
102	defer clearTable("project_commit")
103
104	fakeCommitRows := []e.AnalyzedCommitRow{
105		e.AnalyzedCommitRow{
106			CommitRow: e.CommitRow{
107				Date:              "2018/03/20",
108				Commit:            "540eecd728a407e4b31a38f4ea9416dea7d05c0c",
109				DownstreamProject: "platform/tools/external/gradle",
110				Author:            "jeffrey.lebowski@google.com",
111				Subject:           "Hand off the briefcase",
112			},
113			Type: c.Empty,
114		},
115		e.AnalyzedCommitRow{
116			CommitRow: e.CommitRow{
117				Date:              "2018/03/19",
118				Commit:            "ea999655a8af4b7d6a8033d1c864ca87617d0ede",
119				DownstreamProject: "platform/tools/external/gradle",
120				Author:            "brandt@google.com",
121				Subject:           "We Just Don't Know",
122			},
123			Type: c.Empty,
124		},
125		e.AnalyzedCommitRow{
126			CommitRow: e.CommitRow{
127				Date:              "2018/03/19",
128				Commit:            "4cc9725c953f57f8abe63b729e26125feac1be4e",
129				DownstreamProject: "platform/tools/external/gradle",
130				Author:            "jeffrey.lebowski@google.com",
131				Subject:           "Take any rug in the house",
132			},
133			Type: c.Empty,
134		},
135	}
136	commitRepo, err := repositories.NewCommitRepository(fakeMappedTarget)
137	err = commitRepo.InsertCommitRows(fakeCommitRows)
138	assert.Equal(t, nil, err, "Error should be nil")
139	assert.Equal(t, 3, getRowCountAtTable("project_commit"), "Rows should be inserted")
140
141	scopedD, _ := repositories.NewScopedDenormalizerRepository(fakeTarget, fakeMappedTarget)
142
143	err = scopedD.DenormalizeToRecentCommits(
144		fakeCommitRows,
145		map[string]e.RepoTimestamp{
146			"540eecd728a407e4b31a38f4ea9416dea7d05c0c": arbitraryTimestamp,
147			"ea999655a8af4b7d6a8033d1c864ca87617d0ede": arbitraryTimestamp,
148			"4cc9725c953f57f8abe63b729e26125feac1be4e": arbitraryTimestamp,
149		},
150	)
151	assert.Equal(t, nil, err, "Error should be nil")
152	assert.Equal(t, 3, getRowCountAtTable("denormalized_view_recent_commit"), "Rows should be inserted")
153
154	d, _ := repositories.NewGlobalDenormalizerRepository()
155	err = d.DenormalizeToTopCommitter()
156	assert.Equal(t, nil, err, "Error should be nil")
157	assert.Equal(t, 2, getRowCountAtTable(tableName), "Rows should be inserted")
158}
159
160func TestDenormalizeToTopTechArea(t *testing.T) {
161	tableName := "denormalized_view_top_tech_area"
162	defer clearTable(tableName)
163	defer clearTable("denormalized_view_recent_commit")
164	defer clearTable("project_commit")
165	fakeCommitRows := []e.AnalyzedCommitRow{
166		e.AnalyzedCommitRow{
167			CommitRow: e.CommitRow{
168				Date:              "2018/03/20",
169				Commit:            "540eecd728a407e4b31a38f4ea9416dea7d05c0c",
170				DownstreamProject: "platform/tools/external/gradle",
171				Author:            "jeffrey.lebowski@google.com",
172				Subject:           "Hand off the briefcase",
173			},
174			Type: c.Empty,
175		},
176		e.AnalyzedCommitRow{
177			CommitRow: e.CommitRow{
178				Date:              "2018/03/19",
179				Commit:            "ea999655a8af4b7d6a8033d1c864ca87617d0ede",
180				DownstreamProject: "platform/tools/external/gradle",
181				Author:            "brandt@google.com",
182				Subject:           "We Just Don't Know",
183			},
184			Type: c.Empty,
185		},
186		e.AnalyzedCommitRow{
187			CommitRow: e.CommitRow{
188				Date:              "2018/03/19",
189				Commit:            "4cc9725c953f57f8abe63b729e26125feac1be4e",
190				DownstreamProject: "platform/tools/external/gradle",
191				Author:            "jeffrey.lebowski@google.com",
192				Subject:           "Take any rug in the house",
193			},
194			Type: c.Empty,
195		},
196	}
197	commitRepo, err := repositories.NewCommitRepository(fakeMappedTarget)
198	err = commitRepo.InsertCommitRows(fakeCommitRows)
199	assert.Equal(t, nil, err, "Error should be nil")
200	assert.Equal(t, 3, getRowCountAtTable("project_commit"), "Rows should be inserted")
201
202	scopedD, _ := repositories.NewScopedDenormalizerRepository(fakeTarget, fakeMappedTarget)
203
204	err = scopedD.DenormalizeToRecentCommits(
205		fakeCommitRows,
206		map[string]e.RepoTimestamp{
207			"540eecd728a407e4b31a38f4ea9416dea7d05c0c": arbitraryTimestamp,
208			"ea999655a8af4b7d6a8033d1c864ca87617d0ede": arbitraryTimestamp,
209			"4cc9725c953f57f8abe63b729e26125feac1be4e": arbitraryTimestamp,
210		},
211	)
212	assert.Equal(t, nil, err, "Error should be nil")
213	assert.Equal(t, 3, getRowCountAtTable("denormalized_view_recent_commit"), "Rows should be inserted")
214
215	d, _ := repositories.NewGlobalDenormalizerRepository()
216	err = d.DenormalizeToTopTechArea()
217	assert.Equal(t, nil, err, "Error should be nil")
218	assert.Equal(t, 2, getRowCountAtTable(tableName), "Rows should be inserted")
219}
220