This repository was archived by the owner on Jan 23, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathtest.srmRoundsForProblem.js
304 lines (283 loc) · 9.69 KB
/
test.srmRoundsForProblem.js
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
* Copyright (C) 2014 TopCoder Inc., All Rights Reserved.
*
* @version 1.0
* @author panoptimum
*/
/*global describe, it, before, beforeEach, after, afterEach*/
/*jslint node: true, nomen: true*/
"use strict";
/**
* Module dependencies.
*/
var _ = require('underscore'),
async = require('async'),
request = require('supertest'),
chai = require('chai'),
testHelper = require('./helpers/testHelper'),
util = require('util');
chai.Assertion.includeStack = true;
var assert = chai.assert;
/**
* Constants
*/
var API_ENDPOINT = process.env.API_ENDPOINT || 'http://localhost:8080',
SQL_DIR = __dirname + '/sqls/srmRoundsForProblem/',
ROUTE = '/v2/data/srm/problems/%s/rounds',
DB = ['topcoder_dw'];
/* This function returns a function that takes a callback and runs a sql file
* @param {String} suffix "clean" or "insert"
* @param {String} db the database name, e.g. "common_oltp", "informixoltp"
* @return {Function} function that takes a callback and runs a sql file
*/
function runSqlFile(suffix, db) {
return _.bind(
testHelper.runSqlFile,
testHelper,
util.format("%s%s__%s", SQL_DIR, db, suffix),
db
);
}
/**
* Runs the sql file of a given suffix for each db in a list.
* @param {String} suffix the suffix, e.g. clean
* @param {Array} dbs list of database names, e.g. informixoltp
* @param {Function} done callback function
*/
function runSqlFiles(suffix, dbs, done) {
async.series(
_.map(
dbs,
function (db) {
return runSqlFile(suffix, db);
}
),
done
);
}
/**
* Create and return GET request
* @param {Object} data the data to be queried
* @return {Object} request object
*/
function createGetRequest(data) {
var result = request(API_ENDPOINT)
.get(data.route)
.query(_.omit(data.request, 'problemId'))
.set('Accept', 'application/json');
return result;
}
/**
* Send request and check if response conforms to API contract
* @param {Object} testData configuration object
*/
function assertResponse(testData) {
var status = testData.status,
responseData = testData.response,
createRequest = createGetRequest;
return function (done) {
createRequest(testData)
.expect(status)
.expect('Content-Type', /json/)
.end(
function (error, response) {
var result;
if (error) {
return done(error);
}
if (!response) {
return done(new Error("Server unresponsive."));
}
if (status === 200) {
result = testHelper.getTrimmedData(response.res.text);
assert.deepEqual(result, responseData, "Responses must match.");
return done();
}
result = testHelper.getTrimmedData(response.res.text);
assert.deepEqual(result, responseData,
'response does not conform to expected value');
return done(error);
}
);
};
}
/**
* Assert requests to the Rounds For Problem API
*
* @param {Object} request - an object representing the request to be send
* @param {Object} response - an object representing the expected response
* @param {Integer} status - an int representing the expected status
*
* @return {Function} a function that takes a callback and does the assertion
*/
function rfp(request, response, status) {
return assertResponse({
request: request,
response: response,
status: status,
route: util.format(ROUTE, request.problemId)
});
}
describe('SRM Rounds For Problem', function () {
this.timeout(60000); // Wait a minute, remote db might be slow.
var clearDb = _.partial(runSqlFiles, "clean", DB),
fillDb = _.partial(runSqlFiles, "insert", DB);
before(
function (done) {
async.series(
[
clearDb,
fillDb
],
done
);
}
);
after(clearDb);
describe('Invalid Requests', function () {
it(
"Invalid problemId - NaN",
rfp(
{
problemId: "foobar"
},
{
"error": {
"name": "Bad Request",
"value": 400,
"description": "The request was invalid. An accompanying message will explain why.",
"details": "problemId should be number."
}
},
400
)
);
it(
"Invalid problemId - too small",
rfp(
{
problemId: "0"
},
{
"error": {
"name": "Bad Request",
"value": 400,
"description": "The request was invalid. An accompanying message will explain why.",
"details": "problemId should be positive."
}
},
400
)
);
it(
"Invalid problemId - too small",
rfp(
{
problemId: "2147483648"
},
{
"error": {
"name": "Bad Request",
"value": 400,
"description": "The request was invalid. An accompanying message will explain why.",
"details": "problemId should be less or equal to 2147483647."
}
},
400
)
);
it(
"Invalid problemId - problem doesn't exist",
rfp(
{
problemId: 2003
},
{
"error": {
"name": "Not Found",
"value": 404,
"description": "The URI requested is invalid or the requested resource does not exist.",
"details": "The problem doesn't exist."
}
},
404
)
);
});
describe('Valid Requests', function () {
// The following round refers to problem with problem_id 2002:
// round_id: 3001 - active srm round - omit
it(
"Round exists, but is not finished, empty result.",
rfp(
{
problemId: 2002
},
{
rounds: []
},
200
)
);
// The following rounds refer to problem with problem_id 2001:
// round_id: 3000 - active srm round - omit
// round_id: 3002 - active tournament round - omit
// round_id: 3003 - active long round - omit
// round_id: 3004 - active info event round - omit
// round_id: 3005 - past srm round - show
// round_id: 3006 - past tournament round - show
// round_id: 3007 - past long round - show
// round_id: 3008 - past info event round - show
// round_id: 3009 - past practice round - omit
// round_id: 3010 - past tournament practice round - omit
it(
"Don't report non-finished rounds, test rounds.",
rfp(
{
problemId: 2001
},
{
rounds: [
{
"contestId": 3005,
"contestName": "Contest 3005",
"roundId": 6,
"roundName": "Past Srm Round",
"divisionDescription": "Unrated Division",
"levelDescription": "Level 2001",
"editorialLink": "http://editlink6"
},
{
"contestId": 3006,
"contestName": "Contest 3006",
"roundId": 7,
"roundName": "Past Tournament Round",
"divisionDescription": "Division-I",
"levelDescription": "Level 2001",
"editorialLink": "http://editlink7"
},
{
"contestId": 3007,
"contestName": "Contest 3007",
"roundId": 8,
"roundName": "Past Long Round",
"divisionDescription": "Division-II",
"levelDescription": "Level 2001",
"editorialLink": "http://editlink8"
},
{
"contestId": 3008,
"contestName": "Contest 3008",
"roundId": 9,
"roundName": "Past Event Round",
"divisionDescription": "No Division Applicable",
"levelDescription": "Level 2001",
"editorialLink": "http://editlink9"
}
]
},
200
)
);
});
});