UNPKG

9.46 kBJavaScriptView Raw
1process.env.NODE_TLS_REJECT_UNAUTHORIZED=0
2import oada from '../src/index'
3import chai from 'chai';
4var expect = chai.expect;
5
6let token = 'def';
7let domain = 'https://vip3.ecn.purdue.edu';
8let contentType = 'application/vnd.oada.yield.1+json';
9let connectTime = 30 * 1000; // seconds to click through oauth
10let connOne;
11let connTwo;
12let tree = {
13 '_type': 'application/vnd.oada.harvest.1+json',
14 '_rev': '0-0',
15 'tiled-maps': {
16 '_type': 'application/vnd.oada.tiled-maps.1+json',
17 '_rev': '0-0',
18 'dry-yield-map': {
19 '_type': 'application/vnd.oada.tiled-maps.dry-yield-map.1+json',
20 '_rev': '0-0',
21 'crop-index': {
22 '*': {
23 '_type': 'application/vnd.oada.tiled-maps.dry-yield-map.1+json',
24 '_rev': '0-0',
25 'geohash-length-index': {
26 '*': {
27 '_type': 'application/vnd.oada.tiled-maps.dry-yield-map.1+json',
28 '_rev': '0-0',
29 }
30 }
31 }
32 }
33 }
34 }
35}
36describe('~~~~~~~~~~Cache testing~~~~~~~~~~', () => {
37
38
39 it('Make the connection. Cache + websocket enabled.', function() {
40 this.timeout(connectTime);
41 return oada.connect({
42 domain,
43 token: 'def',
44 cache: {name: 'testDb'}
45 }).then((result) => {
46 connOne = result;
47 expect(result).to.have.keys(['token', 'cache', 'socket', 'disconnect', 'get', 'put', 'post', 'delete', 'resetCache'])
48 expect(result.cache).to.not.equal(undefined);
49 expect(result.socket).to.not.equal(undefined);
50 })
51 })
52
53 it('reset cache and DELETE to initialize the state', async () => {
54 return connOne.resetCache().then(() => {
55 return connOne.delete({
56 path: '/bookmarks/test',
57 }).then((response) => {
58 expect(response.status).to.equal(204)
59 expect(response.headers).to.include.keys(['content-location', 'x-oada-rev'])
60 })
61 })
62 })
63})
64
65
66for (var i = 0; i < 2; i++) {
67 describe(`Basic GET/PUT/POST/DELETE calls. Running ${i+1} of 2 times (to test cache)`, function() {
68 this.timeout(connectTime);
69 it('GET using a path', () => {
70 return connOne.get({
71 path: '/bookmarks',
72 }).then((response) => {
73 expect(response.cached).to.equal(true)
74 expect(response.status).to.equal(200)
75 expect(response.headers).to.include.keys(['content-location', 'x-oada-rev'])
76 expect(response.data).to.include.keys(['_id', '_rev'])
77 })
78 })
79
80 it('GET using a url. This second GET should come from cache.', () => {
81 return connOne.get({
82 url: domain+'/bookmarks',
83 }).then((response) => {
84 expect(response.status).to.equal(200)
85 expect(response.headers).to.include.keys(['content-location', 'x-oada-rev'])
86 expect(response.data).to.include.keys(['_id', '_rev'])
87 })
88 })
89
90 it('PUT using a path', ()=> {
91 return connOne.put({
92 path: '/bookmarks/test1',
93 type: contentType,
94 data: "123",
95 }).then((response) => {
96 expect(response.status).to.equal(204)
97 expect(response.headers).to.include.keys(['content-location', 'x-oada-rev', 'location'])
98 return connOne.get({
99 path: '/bookmarks/test1'
100 }).then((res) => {
101 expect(res.data).to.equal(123);
102 })
103 })
104 })
105
106 it('PUT using a url', ()=> {
107 return connOne.put({
108 url: domain+'/bookmarks/test',
109 type: contentType,
110 data: {testA: "123"},
111 }).then((response) => {
112 expect(response.status).to.equal(204)
113 expect(response.headers).to.include.keys(['content-location', 'x-oada-rev', 'location'])
114 return connOne.get({
115 path: '/bookmarks/test'
116 }).then((res) => {
117 expect(res.data).to.be.an('object');
118 expect(res.data.testA).to.equal('123');
119 })
120 })
121 })
122
123 it('POST using a path', ()=> {
124 return connOne.post({
125 path: '/bookmarks/test',
126 type: contentType,
127 data:"123"
128 }).then((response) => {
129 expect(response.status).to.equal(204)
130 expect(response.headers).to.include.keys(['content-location', 'x-oada-rev', 'location'])
131 })
132 })
133
134 it('POST using a url', ()=> {
135 return connOne.post({
136 url: domain+'/bookmarks/test',
137 type: contentType,
138 data:"123",
139 }).then((response) => {
140 expect(response.status).to.equal(204)
141 expect(response.headers).to.include.keys(['content-location', 'x-oada-rev', 'location'])
142 })
143 })
144
145 it('DELETE using a path', ()=> {
146 return connOne.delete({
147 path: '/bookmarks/test1',
148 }).then((response) => {
149 expect(response.status).to.equal(204)
150 return connOne.get({
151 path: '/bookmarks/test1'
152 }).catch((err) => {
153 expect(err.response.status).to.equal(404)
154 })
155 })
156 })
157
158 it('DELETE using a url', ()=> {
159 return connOne.delete({
160 url: domain+'/bookmarks/test',
161 }).then((response) => {
162 expect(response.status).to.equal(204)
163 return connOne.get({
164 path: '/bookmarks/test'
165 }).catch((err) => {
166 expect(err.response.status).to.equal(404)
167 return connOne.get({
168 path: '/bookmarks'
169 }).then((resp) => {
170 expect(resp.status).to.equal(200)
171 expect(resp.data).to.include.keys(['_id', '_rev'])
172 expect(resp.data).to.not.include.keys(['test'])
173 })
174 })
175 })
176 })
177 })
178
179 describe('Recursive GETs with a tree supplied.', () => {
180 it('Recursive tree get with harvest data tree.', (done) => {
181 connOne.get({
182 path: '/bookmarks/harvest',
183 tree,
184 }).then((response) => {
185 expect(response.status).to.equal(200)
186 expect(response.data).to.include.keys(['_id', '_rev'])
187 expect(response.data['tiled-maps']).to.include.keys(['_id', '_rev'])
188 expect(response.data['tiled-maps']['dry-yield-map']).to.include.keys(['_id', '_rev'])
189 Object.keys(response.data['tiled-maps']['dry-yield-map']['crop-index']).forEach((key) => {
190 expect(response.data['tiled-maps']['dry-yield-map']['crop-index'][key]).to.include.keys(['_id', '_rev']);
191 Object.keys(response.data['tiled-maps']['dry-yield-map']['crop-index'][key]['geohash-length-index']).forEach((i) => {
192 expect(response.data['tiled-maps']['dry-yield-map']['crop-index'][key]['geohash-length-index'][i]).to.include.keys(['_id', '_rev']);
193 })
194 })
195 done()
196 })
197 })
198
199 it('Recursive tree get with harvest data tree.', (done) => {
200 connOne.get({
201 path: '/bookmarks',
202 tree: {
203 _type: 'application/vnd.oada.bookmarks.1+json',
204 _rev: '0-0',
205 harvest: tree,
206 },
207 }).then((response) => {
208 console.log(response);
209 expect(response.status).to.equal(200)
210 expect(response.data).to.include.keys(['_id', '_rev'])
211 expect(response.data['tiled-maps']).to.include.keys(['_id', '_rev'])
212 expect(response.data['tiled-maps']['dry-yield-map']).to.include.keys(['_id', '_rev'])
213 Object.keys(response.data['tiled-maps']['dry-yield-map']['crop-index']).forEach((key) => {
214 expect(response.data['tiled-maps']['dry-yield-map']['crop-index'][key]).to.include.keys(['_id', '_rev']);
215 Object.keys(response.data['tiled-maps']['dry-yield-map']['crop-index'][key]['geohash-length-index']).forEach((i) => {
216 expect(response.data['tiled-maps']['dry-yield-map']['crop-index'][key]['geohash-length-index'][i]).to.include.keys(['_id', '_rev']);
217 })
218 })
219 done()
220 })
221 })
222 })
223}
224
225describe('make a second connection, put over that connection, and check first cache', () => {
226 it('Make second connection.', () => {
227 return oada.connect({
228 domain,
229 token: 'def',
230 cache: {name: 'testDbTwo'}
231 }).then((result) => {
232 connTwo = result;
233 return connTwo.resetCache()
234 })
235 })
236
237 it('PUT over second connection to a new endpoint.', () => {
238 return connTwo.put({
239 path: '/bookmarks/test2',
240 type: contentType,
241 data: "123",
242 })
243 })
244
245 it('PUT over second connection to an old endpoint.', () => {
246 return connTwo.put({
247 path: '/bookmarks/test1',
248 type: contentType,
249 data: "765",
250 })
251 })
252
253 it('PUT over first connection to a new endpoint.', () => {
254 return connOne.put({
255 path: '/bookmarks/test3',
256 type: contentType,
257 data: "999",
258 })
259 })
260
261 it('GET new endpoint on first connection.', () => {
262 return connOne.get({
263 path: '/bookmarks/test2'
264 }).then((response) => {
265 expect(response.cached).is.equal(true)
266 })
267 })
268
269 it('GET old endpoint on first connection.', () => {
270 return connOne.get({
271 path: '/bookmarks/test1'
272 }).then((response) => {
273 expect(response.cached).is.equal(true)
274 })
275 })
276
277 it('GET new endpoint on second connection.', () => {
278 return connTwo.get({
279 path: '/bookmarks/test3'
280 }).then((response) => {
281 expect(response.cached).is.equal(false)
282 })
283 })
284
285 it('Clean up the server.', () => {
286 return connOne.delete({
287 path: '/bookmarks/test1'
288 }).then((response) => {
289 return connOne.delete({
290 path: '/bookmarks/test2'
291 }).then((response) => {
292 return connOne.delete({
293 path: '/bookmarks/test3'
294 }).then(() =>{
295 connOne.disconnect();
296 }).then(() => {
297 connTwo.disconnect();
298 })
299 })
300 })
301 })
302})