UNPKG

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