Skip to content

Commit dc1964e

Browse files
committed
Fix Contributors Dependency data source tests
Signed-off-by: Raúl Santos <4837+borfast@users.noreply.github.com>
1 parent 9e4afcd commit dc1964e

File tree

2 files changed

+184
-0
lines changed

2 files changed

+184
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import {
2+
describe, test, expect, vi, beforeEach
3+
} from 'vitest';
4+
import {DateTime} from "luxon";
5+
import {mockTimeseries} from '../../mocks/tinybird-contributors-dependency-response.mock';
6+
import {mockTimeseries as mockLeaderboardTimeseries} from '../../mocks/tinybird-contributors-leaderboard-response.mock';
7+
import type {ContributorDependencyResponse} from "~~/server/data/tinybird/contributors-dependency-data-source";
8+
9+
const mockFetchFromTinybird = vi.fn();
10+
11+
describe('Contributors Dependency Data Source', () => {
12+
beforeEach(() => {
13+
mockFetchFromTinybird.mockClear();
14+
15+
// Here be dragons! vi.doMock is not hoisted, and thus it is executed after the original import statement.
16+
// This means that the import for tinybird.ts inside active-contributors-data-source.ts would still be used,
17+
// and thus not mocked. This means we need to import the module again after the mock is set, whenever we want to
18+
// use it.
19+
vi.doMock(import("./tinybird"), () => ({
20+
fetchFromTinybird: mockFetchFromTinybird,
21+
}));
22+
})
23+
24+
test('should fetch contributors dependency data with correct parameters', async () => {
25+
// We have to import this here again because vi.doMock is not hoisted. See the explanation in beforeEach().
26+
const {fetchContributorDependency} = await import("~~/server/data/tinybird/contributors-dependency-data-source");
27+
28+
mockFetchFromTinybird.mockResolvedValueOnce(mockTimeseries).mockResolvedValueOnce(mockLeaderboardTimeseries);
29+
30+
const startDate = DateTime.utc(2024, 3, 20);
31+
const endDate = DateTime.utc(2025, 3, 20);
32+
33+
const filter = {
34+
project: 'the-linux-kernel-organization',
35+
startDate,
36+
endDate
37+
};
38+
39+
const result = await fetchContributorDependency(filter);
40+
41+
expect(mockFetchFromTinybird).toHaveBeenNthCalledWith(
42+
1,
43+
'/v0/pipes/contributor_dependency.json',
44+
filter
45+
)
46+
expect(mockFetchFromTinybird).toHaveBeenNthCalledWith(
47+
2,
48+
'/v0/pipes/contributors_leaderboard.json',
49+
{
50+
...filter,
51+
limit: 5
52+
}
53+
);
54+
55+
const topContributorsCount = mockTimeseries.data.length;
56+
const lastContributor = mockTimeseries.data.at(-1);
57+
const topContributorsPercentage = lastContributor?.contributionPercentageRunningTotal || 0;
58+
const totalContributorCount = mockTimeseries.data[0]?.totalContributorCount || 0;
59+
60+
const expectedResult: ContributorDependencyResponse = {
61+
topContributors: {
62+
count: topContributorsCount,
63+
percentage: topContributorsPercentage
64+
},
65+
otherContributors: {
66+
count: Math.max(0, (totalContributorCount || 0) - topContributorsCount),
67+
percentage: 100 - topContributorsPercentage
68+
},
69+
list: mockLeaderboardTimeseries.data.map((item) => ({
70+
avatar: item.avatar,
71+
name: item.displayName,
72+
contributions: item.contributionCount,
73+
percentage: item.contributionPercentage
74+
}))
75+
};
76+
77+
expect(result).toEqual(expectedResult);
78+
});
79+
80+
// TODO: Add checks for invalid dates, invalid data, sql injections, and other edge cases.
81+
});
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
export const mockTimeseries = {
2+
meta: [
3+
{
4+
name: "id",
5+
type: "String"
6+
},
7+
{
8+
name: "displayName",
9+
type: "String"
10+
},
11+
{
12+
name: "contributionPercentage",
13+
type: "Float64"
14+
},
15+
{
16+
name: "contributionPercentageRunningTotal",
17+
type: "Float64"
18+
},
19+
{
20+
name: "totalContributorCount",
21+
type: "UInt64"
22+
}
23+
],
24+
data: [
25+
{
26+
id: "7f65feb0-589b-11ee-bf26-d732180a3416",
27+
displayName: "Mark Brown",
28+
contributionPercentage: 2,
29+
contributionPercentageRunningTotal: 7,
30+
totalContributorCount: 6754
31+
},
32+
{
33+
id: "a3bbff07-7cec-4885-b688-d8b377a580c6",
34+
displayName: "Alex Deucher",
35+
contributionPercentage: 2,
36+
contributionPercentageRunningTotal: 9,
37+
totalContributorCount: 6754
38+
},
39+
{
40+
id: "b17e3beb-09e2-447f-a12d-00c716dd00db",
41+
displayName: "Jakub Kicinski",
42+
contributionPercentage: 3,
43+
contributionPercentageRunningTotal: 3,
44+
totalContributorCount: 6754
45+
},
46+
{
47+
id: "6ac1a927-efc8-4b42-9cd4-683f7ec2e4c8",
48+
displayName: "Len Brown",
49+
contributionPercentage: 2,
50+
contributionPercentageRunningTotal: 5,
51+
totalContributorCount: 6754
52+
},
53+
{
54+
id: "d47386a6-3890-42c3-8c26-6872a8e38c2c",
55+
displayName: "Linus Torvalds",
56+
contributionPercentage: 2,
57+
contributionPercentageRunningTotal: 11,
58+
totalContributorCount: 6754
59+
},
60+
{
61+
id: "eb7fc260-77d6-11ef-b115-a7c57b0467ff",
62+
displayName: "Greg Kroah-Hartman",
63+
contributionPercentage: 2,
64+
contributionPercentageRunningTotal: 13,
65+
totalContributorCount: 6754
66+
},
67+
{
68+
id: "03a88390-f8f7-11ee-9733-23f194e8fceb",
69+
displayName: "Kent Overstreet",
70+
contributionPercentage: 1,
71+
contributionPercentageRunningTotal: 14,
72+
totalContributorCount: 6754
73+
},
74+
{
75+
id: "e068c75f-716e-4248-b8fa-4e97aaa76fc9",
76+
displayName: "Christian Brauner",
77+
contributionPercentage: 1,
78+
contributionPercentageRunningTotal: 16,
79+
totalContributorCount: 6754
80+
},
81+
{
82+
id: "7b2a3b66-d42a-442c-99c0-5cdc4f0c5a56",
83+
displayName: "Krzysztof Kozlowski",
84+
contributionPercentage: 1,
85+
contributionPercentageRunningTotal: 15,
86+
totalContributorCount: 6754
87+
},
88+
{
89+
id: "f215f342-dd22-4f08-b70a-f2cb589b9f41",
90+
displayName: "Bjorn Andersson",
91+
contributionPercentage: 1,
92+
contributionPercentageRunningTotal: 17,
93+
totalContributorCount: 6754
94+
}
95+
],
96+
rows: 10,
97+
rows_before_limit_at_least: 6754,
98+
statistics: {
99+
elapsed: 1.899921774,
100+
rows_read: 2301274,
101+
bytes_read: 189901864
102+
}
103+
};

0 commit comments

Comments
 (0)