1 | async = require 'async'
|
2 | assert = require 'assert'
|
3 | _ = require 'underscore'
|
4 | Clever = require "#{__dirname}/../index"
|
5 | nock = require 'nock'
|
6 |
|
7 | _([
|
8 | 'DEMO_KEY'
|
9 | {api_key: 'DEMO_KEY'}
|
10 | {token: 'DEMO_TOKEN'}
|
11 | ]).each (auth) ->
|
12 | describe "query #{JSON.stringify auth}", ->
|
13 |
|
14 | before -> @clever = Clever auth, 'https://api.clever.com'
|
15 |
|
16 | it 'throws an error if you try to instantiate without an api key', ->
|
17 | assert.throws -> Clever()
|
18 |
|
19 | it 'find with no arguments', (done) ->
|
20 | @clever.District.find (err, districts) =>
|
21 | _(districts).each (district) =>
|
22 | assert (district instanceof @clever.District), "Incorrect type on district object"
|
23 | assert district.get('name')
|
24 | done()
|
25 |
|
26 | it 'find with conditions', (done) ->
|
27 | @clever.District.find { id: "4fd43cc56d11340000000005" }, (err, districts) =>
|
28 | assert.equal districts.length, 1
|
29 | district = districts[0]
|
30 | assert (district instanceof @clever.District), "Incorrect type on district object"
|
31 | assert.equal district.get('name'), 'Demo District'
|
32 | done()
|
33 |
|
34 | it 'find with conditions and exec', (done) ->
|
35 | @clever.District.find(id: "4fd43cc56d11340000000005").exec (err, districts) =>
|
36 | assert.equal districts.length, 1
|
37 | district = districts[0]
|
38 | assert (district instanceof @clever.District), "Incorrect type on district object"
|
39 | assert.equal district.get('name'), 'Demo District'
|
40 | done()
|
41 |
|
42 | it 'findOne with no arguments', (done) ->
|
43 | @clever.District.findOne (err, district) =>
|
44 | assert not _(district).isArray()
|
45 | assert (district instanceof @clever.District), "Incorrect type on district object"
|
46 | assert.equal district.get('name'), 'Demo District'
|
47 | done()
|
48 |
|
49 | it 'findOne with conditions', (done) ->
|
50 | @clever.District.findOne { id: "4fd43cc56d11340000000005" }, (err, district) =>
|
51 | assert not _(district).isArray()
|
52 | assert (district instanceof @clever.District), "Incorrect type on district object"
|
53 | assert.equal district.get('name'), 'Demo District'
|
54 | done()
|
55 |
|
56 | it 'findOne with conditions and exec', (done) ->
|
57 | @clever.District.findOne(id: "4fd43cc56d11340000000005").exec (err, district) =>
|
58 | assert not _(district).isArray()
|
59 | assert (district instanceof @clever.District), "Incorrect type on district object"
|
60 | assert.equal district.get('name'), 'Demo District'
|
61 | done()
|
62 |
|
63 | it 'findById with no conditions throws', (done) ->
|
64 | assert.throws(
|
65 | () =>
|
66 | @clever.District.findById (err, district) -> assert false
|
67 | (err) ->
|
68 | ret = (err instanceof Error) and /must specify an ID/.test(err)
|
69 | setTimeout(done, 1000) if ret
|
70 | return ret
|
71 | )
|
72 |
|
73 | it 'findById', (done) ->
|
74 | @clever.District.findById "4fd43cc56d11340000000005", (err, district) =>
|
75 | assert not _(district).isArray()
|
76 | assert (district instanceof @clever.District), "Incorrect type on district object"
|
77 | assert.equal district.get('name'), 'Demo District'
|
78 | done()
|
79 |
|
80 |
|
81 | it.skip 'findById with exec', (done) ->
|
82 | @clever.District.findById("4fd43cc56d11340000000005").exec (err, district) =>
|
83 | assert not _(district).isArray()
|
84 | assert (district instanceof @clever.District), "Incorrect type on district object"
|
85 | assert.equal district.get('name'), 'Demo District'
|
86 | done()
|
87 |
|
88 |
|
89 | it.skip 'find with a where condition', (done) ->
|
90 | @clever.School.find().where('name').equals('Clever High School').exec (err, schools) =>
|
91 | assert.equal schools.length, 1
|
92 | school = schools[0]
|
93 | assert (school instanceof @clever.School), "Incorrect type on school object"
|
94 | assert.equal school.get('name'), 'Clever High School'
|
95 | done()
|
96 |
|
97 | it 'successfully generates correct link with ending_before', (done) ->
|
98 | @timeout 30000
|
99 | clever = Clever 'FAKE_KEY', 'http://fake_api.com'
|
100 | scope = nock('http://fake_api.com')
|
101 | .get('/v1.1/students?where=%7B%7D&ending_before=last')
|
102 | .reply(401, {error: 'test succeeded'})
|
103 | clever.Student.find().ending_before('last').exec (err, students) ->
|
104 | assert not students
|
105 | assert.equal err.message, "received statusCode 401 instead of 200"
|
106 | assert.deepEqual err.body, {error: 'test succeeded'}
|
107 | scope.done()
|
108 | done()
|
109 |
|
110 | it 'successfully generates correct link with starting_after', (done) ->
|
111 | @timeout 30000
|
112 | clever = Clever 'FAKE_KEY', 'http://fake_api.com'
|
113 | scope = nock('http://fake_api.com')
|
114 | .get('/v1.1/students?where=%7B%7D&starting_after=12345')
|
115 | .reply(401, {error: 'test succeeded'})
|
116 | clever.Student.find().starting_after('12345').exec (err, students) ->
|
117 | assert not students
|
118 | assert.equal err.message, "received statusCode 401 instead of 200"
|
119 | assert.deepEqual err.body, {error: 'test succeeded'}
|
120 | scope.done()
|
121 | done()
|
122 |
|
123 | it 'find with starting_after and ending_before', (done) ->
|
124 | before_students = []
|
125 | async.waterfall [
|
126 | (cb_wf) =>
|
127 |
|
128 | @clever.Student.find().limit(2).ending_before('last').exec (err, students) =>
|
129 | before_students = students
|
130 | assert.equal students.length, 2
|
131 | assert (students[0] instanceof @clever.Student), "Incorrect type on student object"
|
132 | cb_wf()
|
133 | (cb_wf) =>
|
134 |
|
135 | @clever.Student.find().starting_after(before_students[0].get 'id').exec (err, students) =>
|
136 | assert.equal students.length, 1
|
137 | assert.equal students[0].get('id'), before_students[1].get('id')
|
138 | assert (students[0] instanceof @clever.Student), "Incorrect type on student object"
|
139 | cb_wf()
|
140 | ], (err) =>
|
141 | assert.ifError err
|
142 | done()
|
143 |
|
144 |
|
145 | it.skip 'count works', (done) ->
|
146 | @clever.School.find().where('name').equals('Clever High School').count().exec (err, count) ->
|
147 | assert.equal count, 1
|
148 | done()
|
149 |
|
150 | it 'supports custom resources', (done) ->
|
151 | clever = Clever "FAKE_KEY", "http://fake_api.com"
|
152 | class clever.NewResource extends clever.Resource
|
153 | @path: '/resource/path'
|
154 | scope = nock("http://fake_api.com")
|
155 | .get('/resource/path?where=%7B%7D')
|
156 | .reply(200, {data: [{uri: '/resource/path/some_id', data: {some_key: 'some_val'}}]})
|
157 | clever.NewResource.find (err, resources) ->
|
158 | assert.equal resources.length, 1
|
159 | resource = resources[0]
|
160 | assert resource instanceof clever.NewResource
|
161 | assert.equal resource.get('some_key'), 'some_val'
|
162 | scope.done()
|
163 | done err
|
164 |
|
165 | it 'exists true with where works', (done) ->
|
166 | @clever.School.find().where('name').exists(true).count().exec (err, count) ->
|
167 | assert.equal count, 3
|
168 | done()
|
169 |
|
170 | it 'exists without args works', (done) ->
|
171 | @clever.School.find().where('name').exists().count().exec (err, count) ->
|
172 | assert.equal count, 3
|
173 | done()
|
174 |
|
175 | it 'exists true works', (done) ->
|
176 | @clever.School.find().exists('name', true).count().exec (err, count) ->
|
177 | assert.equal count, 3
|
178 | done()
|
179 |
|
180 | it 'exists path works', (done) ->
|
181 | @clever.School.find().exists('name').count().exec (err, count) ->
|
182 | assert.equal count, 3
|
183 | done()
|
184 |
|
185 | it 'exists false with where works', (done) ->
|
186 | @clever.School.find().where('name').exists(false).count().exec (err, count) ->
|
187 | assert.equal count, 0
|
188 | done()
|
189 |
|
190 | it 'exists false works', (done) ->
|
191 | @clever.School.find().exists('name', false).count().exec (err, count) ->
|
192 | assert.equal count, 0
|
193 | done()
|
194 |
|
195 | it 'successfully handles invalid get requests that return a json', (done) ->
|
196 | @timeout 30000
|
197 | clever = Clever 'FAKE_KEY', 'http://fake_api.com'
|
198 | scope = nock('http://fake_api.com')
|
199 | .get('/v1.1/districts?where=%7B%22id%22%3A%2212345%22%7D&limit=1')
|
200 | .reply(401, {error: 'unauthorized'})
|
201 | clever.District.findById '12345', (err, district) ->
|
202 | assert not district
|
203 | assert.equal err.message, "received statusCode 401 instead of 200"
|
204 | assert.deepEqual err.body, {error: 'unauthorized'}
|
205 | scope.done()
|
206 | done()
|
207 |
|
208 | it 'successfully handles invalid get requests that return a json with exec', (done) ->
|
209 | @timeout 30000
|
210 | clever = Clever 'FAKE_KEY', 'http://fake_api.com'
|
211 | scope = nock('http://fake_api.com')
|
212 | .get('/v1.1/districts?where=%7B%22id%22%3A%2212345%22%7D&limit=1')
|
213 | .reply(401, {error: 'unauthorized'})
|
214 | clever.District.findById('12345').exec (err, district) ->
|
215 | assert not district
|
216 | assert.equal err.message, "received statusCode 401 instead of 200"
|
217 | assert.deepEqual err.body, {error: 'unauthorized'}
|
218 | scope.done()
|
219 | done()
|
220 |
|
221 | it 'successfully handles invalid get requests that return a string', (done) ->
|
222 | @timeout 30000
|
223 | clever = Clever 'FAKE_KEY', 'http://fake_api.com'
|
224 | scope = nock('http://fake_api.com')
|
225 | .get('/v1.1/districts?where=%7B%22id%22%3A%2212345%22%7D&limit=1')
|
226 | .reply(401, 'unauthorized')
|
227 | clever.District.findById '12345', (err, district) ->
|
228 | assert not district
|
229 | assert.equal err.message, "received statusCode 401 instead of 200"
|
230 | assert.equal err.body, 'unauthorized'
|
231 | scope.done()
|
232 | done()
|