-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathindex.test.ts
150 lines (140 loc) · 4.31 KB
/
index.test.ts
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
import {
AirbyteLogLevel,
AirbyteSourceLogger,
AirbyteSpec,
SyncMode,
} from 'faros-airbyte-cdk';
import fs from 'fs-extra';
import {VError} from 'verror';
import {BitbucketServer} from '../src/bitbucket-server';
import {Prefix as MEP} from '../src/bitbucket-server/more-endpoint-methods';
import * as sut from '../src/index';
function readResourceFile(fileName: string): any {
return JSON.parse(fs.readFileSync(`resources/${fileName}`, 'utf8'));
}
describe('index', () => {
const logger = new AirbyteSourceLogger(
// Shush messages in tests, unless in debug
process.env.LOG_LEVEL === 'debug'
? AirbyteLogLevel.DEBUG
: AirbyteLogLevel.FATAL
);
test('spec', async () => {
const source = new sut.BitbucketServerSource(logger);
await expect(source.spec()).resolves.toStrictEqual(
new AirbyteSpec(readResourceFile('spec.json'))
);
});
test('check connection - invalid', async () => {
const source = new sut.BitbucketServerSource(logger);
await expect(source.checkConnection({} as any)).resolves.toStrictEqual([
false,
new VError('server_url must be a valid url'),
]);
});
test('check connection', async () => {
BitbucketServer.instance = jest.fn().mockImplementation(() => {
return new BitbucketServer(
{api: {getUsers: jest.fn().mockResolvedValue({})}} as any,
{} as any,
100,
5,
logger,
new Date('2010-03-27T14:03:51-0800'),
1,
1
);
});
const source = new sut.BitbucketServerSource(logger);
await expect(
source.checkConnection({
server_url: 'localhost',
token: 'token',
projects: ['PLAYG'],
cutoff_days: 90,
})
).resolves.toStrictEqual([true, undefined]);
});
test('streams - projects, use full_refresh sync mode', async () => {
const fnProjectsFunc = jest.fn();
const testProject = {key: 'PROJ1', name: 'Project1'};
BitbucketServer.instance = jest.fn().mockImplementation(() => {
return new BitbucketServer(
{
[MEP]: {
projects: {
getProjects: fnProjectsFunc.mockResolvedValue({
data: {values: [testProject]},
}),
getProject: jest.fn().mockResolvedValue({data: testProject}),
},
},
hasNextPage: jest.fn(),
} as any,
{} as any,
100,
5,
logger,
new Date('2010-03-27T14:03:51-0800'),
1,
1
);
});
const source = new sut.BitbucketServerSource(logger);
const streams = source.streams({} as any);
const projectsStream = streams[2];
const iter = projectsStream.readRecords(SyncMode.FULL_REFRESH);
const projects = [];
for await (const project of iter) {
projects.push(project);
}
expect(fnProjectsFunc).toHaveBeenCalledTimes(1);
expect(JSON.parse(JSON.stringify(projects))).toStrictEqual([testProject]);
});
test('streams - repositories, use full_refresh sync mode with bucketing', async () => {
const fnReposFunc = jest.fn();
BitbucketServer.instance = jest.fn().mockImplementation(() => {
return new BitbucketServer(
{
[MEP]: {
projects: {
getRepositories: fnReposFunc.mockResolvedValue({
data: {values: [{slug: 'repo1'}, {slug: 'repo2'}]},
}),
},
},
repos: {
getDefaultBranch: jest
.fn()
.mockResolvedValue({data: {displayId: 'main'}}),
},
hasNextPage: jest.fn(),
} as any,
{} as any,
100,
5,
logger,
new Date('2010-03-27T14:03:51-0800'),
1,
2
);
});
const source = new sut.BitbucketServerSource(logger);
const streams = source.streams({repositories: ['PROJ1/repo1']} as any);
const projectsStream = streams[6];
const iter = projectsStream.readRecords(SyncMode.FULL_REFRESH, null, {
projectKey: 'PROJ1',
});
const repos = [];
for await (const repo of iter) {
repos.push(repo);
}
expect(fnReposFunc).toHaveBeenCalledTimes(1);
expect(JSON.parse(JSON.stringify(repos))).toStrictEqual([
{
slug: 'repo1',
computedProperties: {fullName: 'proj1/repo1', mainBranch: 'main'},
},
]);
});
});