Add a model for parsing buildbot JSON with unit tests
[WebKit-https.git] / Websites / perf.webkit.org / unit-tests / test-groups-tests.js
1 'use strict';
2
3 var assert = require('assert');
4
5 require('../tools/js/v3-models.js');
6 require('./resources/mock-v3-models.js');
7
8 function sampleTestGroup() {
9     return {
10         "testGroups": [{
11             "id": "2128",
12             "task": "1376",
13             "name": "Confirm",
14             "author": "rniwa",
15             "createdAt": 1458688514000,
16             "hidden": false,
17             "buildRequests": ["16985", "16986", "16987", "16988", "16989", "16990", "16991", "16992"],
18             "rootSets": ["4255", "4256", "4255", "4256", "4255", "4256", "4255", "4256"]
19         }
20         ],
21         "buildRequests": [{
22             "id": "16985",
23             "triggerable": "3",
24             "test": "844",
25             "platform": "31",
26             "testGroup": "2128",
27             "order": "0",
28             "rootSet": "4255",
29             "status": "pending",
30             "url": null,
31             "build": null,
32             "createdAt": 1458688514000
33         }, {
34             "id": "16986",
35             "triggerable": "3",
36             "test": "844",
37             "platform": "31",
38             "testGroup": "2128",
39             "order": "1",
40             "rootSet": "4256",
41             "status": "pending",
42             "url": null,
43             "build": null,
44             "createdAt": 1458688514000
45         },
46         {
47             "id": "16987",
48             "triggerable": "3",
49             "test": "844",
50             "platform": "31",
51             "testGroup": "2128",
52             "order": "2",
53             "rootSet": "4255",
54             "status": "pending",
55             "url": null,
56             "build": null,
57             "createdAt": 1458688514000
58         }, {
59             "id": "16988",
60             "triggerable": "3",
61             "test": "844",
62             "platform": "31",
63             "testGroup": "2128",
64             "order": "3",
65             "rootSet": "4256",
66             "status": "pending",
67             "url": null,
68             "build": null,
69             "createdAt": 1458688514000
70         }
71         ],
72         "rootSets": [{
73             "id": "4255",
74             "roots": ["87832", "93116"]
75         }, {
76             "id": "4256",
77             "roots": ["87832", "96336"]
78         }
79         ],
80         "roots": [{
81             "id": "87832",
82             "repository": "9",
83             "revision": "10.11 15A284",
84             "time": 0
85         }, {
86             "id": "93116",
87             "repository": "11",
88             "revision": "191622",
89             "time": 1445945816878
90         }, {
91             "id": "87832",
92             "repository": "9",
93             "revision": "10.11 15A284",
94             "time": 0
95         }, {
96             "id": "96336",
97             "repository": "11",
98             "revision": "192736",
99             "time": 1448225325650
100         }
101         ],
102         "status": "OK"
103     };
104 }
105
106 describe('TestGroup', function () {
107
108     describe('_createModelsFromFetchedTestGroups', function () {
109         it('should create test groups', function () {
110             var groups = TestGroup._createModelsFromFetchedTestGroups(sampleTestGroup());
111             assert.equal(groups.length, 1);
112
113             var group = groups[0];
114             assert.ok(group instanceof TestGroup);
115             assert.equal(group.id(), 2128);
116             assert.ok(group.createdAt() instanceof Date);
117             assert.equal(group.isHidden(), false);
118             assert.equal(+group.createdAt(), 1458688514000);
119             assert.equal(group.repetitionCount(), sampleTestGroup()['buildRequests'].length / 2);
120             assert.ok(group.hasPending());
121             assert.ok(!group.hasFinished());
122             assert.ok(!group.hasStarted());
123         });
124
125         it('should not create a new instance of TestGroup object if there is a matching entry', function () {
126             var firstObject = TestGroup._createModelsFromFetchedTestGroups(sampleTestGroup())[0];
127             assert.ok(firstObject instanceof TestGroup);
128             assert.equal(TestGroup._createModelsFromFetchedTestGroups(sampleTestGroup())[0], firstObject);
129         });
130
131         it('should create build requests for each group', function () {
132             var groups = TestGroup._createModelsFromFetchedTestGroups(sampleTestGroup());
133             assert.equal(groups.length, 1);
134             assert.equal(groups[0].buildRequests().length, sampleTestGroup()['buildRequests'].length);
135
136             var buildRequests = groups[0].buildRequests();
137             assert.equal(buildRequests[0].id(), 16985);
138             assert.equal(buildRequests[0].order(), 0);
139             assert.ok(!buildRequests[0].hasFinished());
140             assert.ok(!buildRequests[0].hasStarted());
141             assert.ok(buildRequests[0].hasPending());
142             assert.equal(buildRequests[0].statusLabel(), 'Waiting to be scheduled');
143             assert.equal(buildRequests[0].buildId(), null);
144             assert.equal(buildRequests[0].result(), null);
145
146             assert.equal(buildRequests[1].id(), 16986);
147             assert.equal(buildRequests[1].order(), 1);
148             assert.ok(!buildRequests[1].hasFinished());
149             assert.ok(!buildRequests[1].hasStarted());
150             assert.ok(buildRequests[1].hasPending());
151             assert.equal(buildRequests[1].statusLabel(), 'Waiting to be scheduled');
152             assert.equal(buildRequests[1].buildId(), null);
153             assert.equal(buildRequests[1].result(), null);
154         });
155
156         it('should create root sets for each group', function () {
157             var buildRequests = TestGroup._createModelsFromFetchedTestGroups(sampleTestGroup())[0].buildRequests();
158
159             var firstSet = buildRequests[0].rootSet();
160             assert.ok(firstSet instanceof RootSet);
161             assert.equal(firstSet, buildRequests[2].rootSet());
162
163             var secondSet = buildRequests[1].rootSet();
164             assert.ok(secondSet instanceof RootSet);
165             assert.equal(secondSet, buildRequests[3].rootSet());
166
167             assert.equal(firstSet.revisionForRepository(webkit), '191622');
168             var firstWebKitCommit = firstSet.commitForRepository(webkit);
169             assert.ok(firstWebKitCommit instanceof CommitLog);
170             assert.ok(firstWebKitCommit, buildRequests[2].rootSet().commitForRepository(webkit));
171             assert.ok(firstWebKitCommit.repository(), webkit);
172             assert.ok(firstWebKitCommit.revision(), '191622');
173             assert.ok(firstWebKitCommit.time() instanceof Date);
174             assert.ok(+firstWebKitCommit.time(), 1445945816878);
175
176             assert.equal(secondSet.revisionForRepository(webkit), '192736');
177             var secondWebKitCommit = secondSet.commitForRepository(webkit);
178             assert.ok(secondWebKitCommit instanceof CommitLog);
179             assert.ok(secondWebKitCommit, buildRequests[3].rootSet().commitForRepository(webkit));
180             assert.ok(secondWebKitCommit.repository(), webkit);
181             assert.ok(secondWebKitCommit.revision(), '192736');
182             assert.ok(secondWebKitCommit.time() instanceof Date);
183             assert.ok(+secondWebKitCommit.time(), 1445945816878);
184
185             assert.equal(firstSet.revisionForRepository(osx), '10.11 15A284');
186             var osxCommit = firstSet.commitForRepository(osx);
187             assert.ok(osxCommit instanceof CommitLog);
188             assert.equal(osxCommit, buildRequests[1].rootSet().commitForRepository(osx));
189             assert.equal(osxCommit, buildRequests[2].rootSet().commitForRepository(osx));
190             assert.equal(osxCommit, buildRequests[3].rootSet().commitForRepository(osx));
191             assert.ok(osxCommit.repository(), osx);
192             assert.ok(osxCommit.revision(), '10.11 15A284');
193         });
194     });
195
196     function testGroupWithStatusList(list) {
197         var data = sampleTestGroup();
198         data.buildRequests[0].status = list[0];
199         data.buildRequests[1].status = list[1];
200         data.buildRequests[2].status = list[2];
201         data.buildRequests[3].status = list[3];
202         return TestGroup._createModelsFromFetchedTestGroups(data)[0];
203     }
204
205     describe('hasFinished', function () {
206         it('should return true if all build requests have completed', function () {
207             assert.ok(testGroupWithStatusList(['completed', 'completed', 'completed', 'completed']).hasFinished());
208         });
209
210         it('should return true if all build requests have failed', function () {
211             assert.ok(testGroupWithStatusList(['failed', 'failed', 'failed', 'failed']).hasFinished());
212         });
213
214         it('should return true if all build requests have been canceled', function () {
215             assert.ok(testGroupWithStatusList(['canceled', 'canceled', 'canceled', 'canceled']).hasFinished());
216         });
217
218         it('should return true if all build requests have completed or failed', function () {
219             assert.ok(testGroupWithStatusList(['failed', 'completed', 'failed', 'failed']).hasFinished());
220         });
221
222         it('should return true if all build requests have completed, failed, or canceled', function () {
223             assert.ok(testGroupWithStatusList(['failed', 'completed', 'canceled', 'canceled']).hasFinished());
224         });
225
226         it('should return false if all build requests are pending', function () {
227             assert.ok(!testGroupWithStatusList(['pending', 'pending', 'pending', 'pending']).hasFinished());
228         });
229
230         it('should return false if some build requests are pending', function () {
231             assert.ok(!testGroupWithStatusList(['completed', 'completed', 'completed', 'pending']).hasFinished());
232         });
233
234         it('should return false if some build requests are scheduled', function () {
235             assert.ok(!testGroupWithStatusList(['completed', 'completed', 'completed', 'scheduled']).hasFinished());
236         });
237
238         it('should return false if some build requests are running', function () {
239             assert.ok(!testGroupWithStatusList(['completed', 'canceled', 'completed', 'running']).hasFinished());
240         });
241     });
242
243     describe('hasStarted', function () {
244         it('should return true if all build requests have completed', function () {
245             assert.ok(testGroupWithStatusList(['completed', 'completed', 'completed', 'completed']).hasStarted());
246         });
247
248         it('should return true if all build requests have failed', function () {
249             assert.ok(testGroupWithStatusList(['failed', 'failed', 'failed', 'failed']).hasStarted());
250         });
251
252         it('should return true if all build requests have been canceled', function () {
253             assert.ok(testGroupWithStatusList(['canceled', 'canceled', 'canceled', 'canceled']).hasStarted());
254         });
255
256         it('should return true if all build requests have completed or failed', function () {
257             assert.ok(testGroupWithStatusList(['failed', 'completed', 'failed', 'failed']).hasStarted());
258         });
259
260         it('should return true if all build requests have completed, failed, or cancled', function () {
261             assert.ok(testGroupWithStatusList(['failed', 'completed', 'canceled', 'canceled']).hasStarted());
262         });
263
264         it('should return false if all build requests are pending', function () {
265             assert.ok(!testGroupWithStatusList(['pending', 'pending', 'pending', 'pending']).hasStarted());
266         });
267
268         it('should return true if some build requests have completed', function () {
269             assert.ok(testGroupWithStatusList(['completed', 'pending', 'pending', 'pending']).hasStarted());
270         });
271
272         it('should return true if some build requests are scheduled', function () {
273             assert.ok(testGroupWithStatusList(['scheduled', 'pending', 'pending', 'pending']).hasStarted());
274         });
275
276         it('should return true if some build requests are running', function () {
277             assert.ok(testGroupWithStatusList(['running', 'pending', 'pending', 'pending']).hasStarted());
278         });
279     });
280
281     describe('hasPending', function () {
282         it('should return false if all build requests have completed', function () {
283             assert.ok(!testGroupWithStatusList(['completed', 'completed', 'completed', 'completed']).hasPending());
284         });
285
286         it('should return false if all build requests have failed', function () {
287             assert.ok(!testGroupWithStatusList(['failed', 'failed', 'failed', 'failed']).hasPending());
288         });
289
290         it('should return false if all build requests have been canceled', function () {
291             assert.ok(!testGroupWithStatusList(['canceled', 'canceled', 'canceled', 'canceled']).hasPending());
292         });
293
294         it('should return false if all build requests have completed or failed', function () {
295             assert.ok(!testGroupWithStatusList(['failed', 'completed', 'failed', 'failed']).hasPending());
296         });
297
298         it('should return false if all build requests have completed, failed, or cancled', function () {
299             assert.ok(!testGroupWithStatusList(['failed', 'completed', 'canceled', 'canceled']).hasPending());
300         });
301
302         it('should return true if all build requests are pending', function () {
303             assert.ok(testGroupWithStatusList(['pending', 'pending', 'pending', 'pending']).hasPending());
304         });
305
306         it('should return true if some build requests are pending', function () {
307             assert.ok(testGroupWithStatusList(['completed', 'failed', 'canceled', 'pending']).hasPending());
308         });
309
310         it('should return false if some build requests are scheduled and others have completed', function () {
311             assert.ok(!testGroupWithStatusList(['completed', 'completed', 'completed', 'scheduled']).hasPending());
312         });
313     });
314
315 });