-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
132 lines (113 loc) · 3.49 KB
/
script.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
var assert = require('assert');
var express = require('express');
var request = require('request');
var async = require('async');
var path = require('path');
var fs = require('fs');
// Will launch the server in preperation for the tests
var app = require('./app.js');
var baseURI = 'http://localhost:3000/';
// user field for the recommendations query, will be used as follows /recommendations?user=<userToQuery>
var userToQuery = 'a';
// Base options object for requests
var options = { method: 'POST'
, uri: baseURI
, headers: { 'Content-Type': 'application/json' , 'Accept': 'application/json' }
, json: {}
};
/**
* SendRequest
* Helper Function/Iterator for async.each
*
*/
function SendRequest(item, callback){
// Attach JSON
options.json=item;
request(options, function(err, res, body){
HandleResponse(err, res, body);
callback(err);
});
}
/**
* HandleResponse
* Helper Function for requests
*
*/
function HandleResponse(err, res, body){
if (!err){
console.log(res.statusCode);
console.log(body);
} else {
console.log("NO RESPONSE");
}
}
describe('Fictional MVP', function() {
describe('#Reset', function() {
it('Resets the database in preperation for the rest of the code', function(done){
// Setup Options JSON for the requests to be sent
app.resetdb(function(err){
done();
});
});
});
describe('#Listen', function() {
it('Executes the listen command with the provided JSON', function(done){
// Setup Options JSON for the requests to be sent
options.uri = baseURI+'listen';
options.method = 'POST';
options.headers={'Content-Type': 'application/json'};
// Read data from jsonFile
var listenJSON;
var fileContent = fs.readFileSync('./test/listen.json');
try{
listenJSON = JSON.parse(fileContent);
} catch (e){
console.log("Could not parse JSON");
done();
}
// Build array of requests to be sent to server, and store in array
var listenRequests =[];
for (var key in listenJSON.userIds){
for (var i = 0; i<listenJSON.userIds[key].length; i++){
listenRequests.push({user:key, music:listenJSON.userIds[key][i]});
}
}
// Execute requests, calling done when all requests are finished
async.each(listenRequests, SendRequest, done);
});
});
describe('#Follow', function() {
it('Executes the follow command with the provided JSON', function(done){
// Setup Options JSON for the requests to be sent
options.uri = baseURI+'follow';
options.method = 'POST';
options.headers={'Content-Type': 'application/json'};
// Read data from jsonFile
var followsJSON;
var fileContent = fs.readFileSync('./test/follows.json');
try{
followsJSON = JSON.parse(fileContent);
} catch (e){
console.log("Could not parse JSON");
done();
}
// Build array of requests to be sent to server, and store in array
var followsRequests =[];
for (var i = 0; i<followsJSON.operations.length; i++){
followsRequests.push({from:followsJSON.operations[i][0], to:followsJSON.operations[i][1]});
}
// Execute requests, calling done when all requests are finished
async.each(followsRequests, SendRequest, done);
});
});
describe('#Recommendations', function() {
it('Executes the follow command with the provided JSON', function(done){
// Setup Options JSON for the requests to be sent
options.uri = baseURI+'recommendations?user='+userToQuery;
options.method = 'GET';
options.headers={'Content-Type': 'application/json'};
// Execute request
SendRequest(null, function(err){done()});
});
});
});