UNPKG

13 kBtext/coffeescriptView Raw
1ger_tests = (ESM) ->
2 ns = global.default_namespace
3
4 describe '#list_namespaces', ->
5 it 'should work', ->
6 init_ger(ESM)
7 .then (ger) ->
8 ger.list_namespaces()
9 .then((list) ->
10 _.isArray(list).should.be.true
11 )
12
13 describe '#event', ->
14 it 'should add events', ->
15 init_ger(ESM)
16 .then (ger) ->
17 bb.all([
18 ger.event(ns,'p1','buy','c')
19 ])
20 .then(-> ger.count_events(ns))
21 .then((count) ->
22 count.should.equal 1
23 )
24
25 describe '#events', ->
26 it 'should work same events', ->
27 exp_date = new Date().toISOString()
28 init_ger(ESM)
29 .then (ger) ->
30 ger.events([
31 {namespace: ns, person: 'p1', action: 'a', thing: 't1'}
32 {namespace: ns, person: 'p1', action: 'a', thing: 't2', created_at: new Date().toISOString()}
33 {namespace: ns, person: 'p1', action: 'a', thing: 't3', expires_at: exp_date}
34 ])
35 .then(-> ger.count_events(ns))
36 .then((count) ->
37 count.should.equal 3
38 )
39
40 describe '#count_events', ->
41 it 'should return 2 for 2 events', ->
42 init_ger(ESM)
43 .then (ger) ->
44 bb.all([
45 ger.event(ns,'p1','buy','c'),
46 ger.event(ns,'p1','view','c'),
47 ])
48 .then(-> ger.count_events(ns))
49 .then((count) ->
50 count.should.equal 2
51 )
52
53 describe 'recommendations_for_person', ->
54
55 it 'should recommend basic things', ->
56 init_ger(ESM)
57 .then (ger) ->
58 bb.all([
59 ger.event(ns,'p1','buy','a', expires_at: tomorrow),
60 ger.event(ns,'p1','view','a', expires_at: tomorrow),
61
62 ger.event(ns,'p2','view','a', expires_at: tomorrow),
63 ])
64 .then(-> ger.recommendations_for_person(ns, 'p2', actions: {view:1, buy:1}))
65 .then((recommendations) ->
66 item_weights = recommendations.recommendations
67 item_weights[0].thing.should.equal 'a'
68 item_weights.length.should.equal 1
69 )
70
71 it 'should recommend things based on user history', ->
72 init_ger(ESM)
73 .then (ger) ->
74 bb.all([
75 ger.event(ns,'p1','buy','a', expires_at: tomorrow),
76 ger.event(ns,'p1','view','a', expires_at: tomorrow),
77
78 ger.event(ns,'p2','view','a', expires_at: tomorrow),
79 ger.event(ns,'p2','buy','c', expires_at: tomorrow),
80 ger.event(ns,'p2','buy','d', expires_at: tomorrow),
81
82 ger.event(ns,'p3','view','a', expires_at: tomorrow),
83 ger.event(ns,'p3','buy','c', expires_at: tomorrow)
84 ])
85 .then(-> ger.recommendations_for_person(ns, 'p1', actions: {view:1, buy:1}))
86 .then((recommendations) ->
87 item_weights = recommendations.recommendations
88 #p1 is similar to (p1 by 1), p2 by .5, and (p3 by .5)
89 #p1 buys a (a is 1), p2 and p3 buys c (.5 + .5=1) and p2 buys d (.5)
90 items = (i.thing for i in item_weights)
91 (items[0] == 'a' or items[0] == 'c').should.equal true
92 items[2].should.equal 'd'
93 )
94
95 it 'should take a person and reccommend some things', ->
96 init_ger(ESM)
97 .then (ger) ->
98 bb.all([
99
100 ger.event(ns,'p1','view','a', expires_at: tomorrow),
101
102 ger.event(ns,'p2','view','a', expires_at: tomorrow),
103 ger.event(ns,'p2','view','c', expires_at: tomorrow),
104 ger.event(ns,'p2','view','d', expires_at: tomorrow),
105
106 ger.event(ns,'p3','view','a', expires_at: tomorrow),
107 ger.event(ns,'p3','view','c', expires_at: tomorrow)
108 ])
109 .then(-> ger.recommendations_for_person(ns, 'p1', actions: {view: 1}))
110 .then((recommendations) ->
111 item_weights = recommendations.recommendations
112 item_weights[0].thing.should.equal 'a'
113 item_weights[1].thing.should.equal 'c'
114 item_weights[2].thing.should.equal 'd'
115
116 )
117
118 it 'should filter previously actioned things based on filter events option', ->
119 init_ger(ESM)
120 .then (ger) ->
121 bb.all([
122 ger.event(ns,'p1','buy','a'),
123 ])
124 .then(-> ger.recommendations_for_person(ns, 'p1', filter_previous_actions: ['buy'], actions: {buy: 1}))
125 .then((recommendations) ->
126 item_weights = recommendations.recommendations
127 item_weights.length.should.equal 0
128 )
129
130 it 'should filter actioned things from other people', ->
131 init_ger(ESM)
132 .then (ger) ->
133 bb.all([
134 ger.event(ns,'p1','buy','a', expires_at: tomorrow),
135 ger.event(ns,'p1','buy','b', expires_at: tomorrow),
136 ger.event(ns,'p2','buy','b', expires_at: tomorrow),
137 ger.event(ns,'p2','buy','c', expires_at: tomorrow),
138 ])
139 .then(-> ger.recommendations_for_person(ns, 'p1', filter_previous_actions: ['buy'], actions: {buy: 1}))
140 .then((recommendations) ->
141 item_weights = recommendations.recommendations
142 item_weights.length.should.equal 1
143 item_weights[0].thing.should.equal 'c'
144 )
145
146 it 'should filter previously actioned by someone else', ->
147 init_ger(ESM)
148 .then (ger) ->
149 bb.all([
150 ger.event(ns,'p1','buy','a', expires_at: tomorrow),
151 ger.event(ns,'p2','buy','a', expires_at: tomorrow),
152 ])
153 .then(-> ger.recommendations_for_person(ns, 'p1', filter_previous_actions: ['buy'], actions: {buy: 1, view: 1}))
154 .then((recommendations) ->
155 item_weights = recommendations.recommendations
156 item_weights.length.should.equal 0
157 )
158
159 it 'should not filter non actioned things', ->
160 init_ger(ESM)
161 .then (ger) ->
162 bb.all([
163 ger.event(ns,'p1','view','a', expires_at: tomorrow),
164 ger.event(ns,'p2','view','a', expires_at: tomorrow),
165 ger.event(ns,'p2','buy','a', expires_at: tomorrow),
166 ])
167 .then(-> ger.recommendations_for_person(ns, 'p1', filter_previous_actions: ['buy'], actions: {buy: 1, view: 1}))
168 .then((recommendations) ->
169 item_weights = recommendations.recommendations
170 item_weights.length.should.equal 1
171 item_weights[0].thing.should.equal 'a'
172 )
173
174 it 'should not break with weird names (SQL INJECTION)', ->
175 init_ger(ESM)
176 .then (ger) ->
177 bb.all([
178 ger.event(ns,"'p\n,1};","v'i\new","'a\n;", expires_at: tomorrow),
179 ger.event(ns,"'p\n2};","v'i\new","'a\n;", expires_at: tomorrow),
180 ])
181 .then(-> ger.recommendations_for_person(ns, "'p\n,1};", actions: {"v'i\new": 1}))
182 .then((recommendations) ->
183 item_weights = recommendations.recommendations
184 item_weights[0].thing.should.equal "'a\n;"
185 item_weights.length.should.equal 1
186 )
187
188 it 'should return the last_actioned_at date it was actioned at', ->
189
190 init_ger(ESM)
191 .then (ger) ->
192 bb.all([
193 ger.event(ns,'p1','view','a', expires_at: tomorrow),
194 ger.event(ns,'p2','view','a', expires_at: tomorrow),
195 ger.event(ns,'p3','view','a', expires_at: tomorrow),
196 ger.event(ns,'p2','view','b', created_at: soon, expires_at: tomorrow),
197 ger.event(ns,'p3','view','b', created_at: yesterday, expires_at: tomorrow),
198 ])
199 .then(-> ger.recommendations_for_person(ns, "p1", actions : {view: 1}))
200 .then((recommendations) ->
201 item_weights = recommendations.recommendations
202 item_weights.length.should.equal 2
203 item_weights[0].thing.should.equal "a"
204 item_weights[1].thing.should.equal "b"
205 (item_weights[1].last_actioned_at.replace(".","")).should.equal soon.format()
206 )
207
208 it 'should return the last_expires_at date it was expires at', ->
209
210
211 init_ger(ESM)
212 .then (ger) ->
213 bb.all([
214 ger.event(ns,'p1','view','a', expires_at: tomorrow),
215 ger.event(ns,'p2','view','a', expires_at: tomorrow),
216 ger.event(ns,'p3','view','a', expires_at: tomorrow),
217 ger.event(ns,'p2','view','b', expires_at: tomorrow),
218 ger.event(ns,'p2','view','b', expires_at: tomorrow),
219 ger.event(ns,'p3','view','b', expires_at: next_week)
220 ])
221 .then(-> ger.recommendations_for_person(ns, "p1", actions : {view: 1}))
222 .then((recommendations) ->
223 item_weights = recommendations.recommendations
224 item_weights.length.should.equal 2
225 item_weights[0].thing.should.equal "a"
226 (item_weights[0].last_expires_at.replace(".","")).should.equal tomorrow.format()
227 item_weights[1].thing.should.equal "b"
228 (item_weights[1].last_expires_at.replace(".","")).should.equal next_week.format()
229 )
230
231 it 'should people that contributed to recommendation', ->
232 init_ger(ESM)
233 .then (ger) ->
234 bb.all([
235 ger.event(ns,'p2','buy','a', expires_at: tomorrow),
236 ger.event(ns,'p2','view','a'),
237
238 ger.event(ns,'p1','view','a'),
239 ])
240 .then(-> ger.recommendations_for_person(ns, 'p1', actions: {view:1, buy:1}))
241 .then((recommendations) ->
242 item_weights = recommendations.recommendations
243 item_weights[0].thing.should.equal 'a'
244 item_weights[0].people.should.include 'p2'
245 item_weights[0].people.length.should.equal 1
246 )
247
248 it 'should return neighbourhood', ->
249 init_ger(ESM)
250 .then (ger) ->
251 bb.all([
252
253 ger.event(ns,'p1','view','a'),
254
255 ger.event(ns,'p2','buy','a', expires_at: tomorrow),
256 ger.event(ns,'p2','view','a'),
257
258 ger.event(ns,'p3','buy','b', expires_at: tomorrow),
259 ger.event(ns,'p3','view','a'),
260 ger.event(ns,'p3','view','d'),
261 ])
262 .then(-> ger.recommendations_for_person(ns, 'p1', {actions: {view:1, buy:1}}))
263 .then((recommendations) ->
264
265 recommendations.neighbourhood['p2'].should.exist
266 )
267
268 describe 'thing_neighbourhood', ->
269 it 'should list things of people who actioned a thing', ->
270 init_ger(ESM)
271 .then (ger) ->
272 bb.all([
273 ger.event(ns,'p1','v','a', expires_at: tomorrow),
274 ger.event(ns,'p1','v','b', expires_at: tomorrow),
275 ])
276 .then(-> ger.thing_neighbourhood(ns, 'a', {'v': 1}))
277 .then((neighbourhood) ->
278 neighbourhood.length.should.equal 1
279 neighbourhood.map( (x) -> x.thing).should.include 'b'
280 )
281
282 it 'should not list things twice', ->
283 init_ger(ESM)
284 .then (ger) ->
285 bb.all([
286 ger.event(ns,'p1','v','a', expires_at: tomorrow),
287 ger.event(ns,'p1','b','b', expires_at: tomorrow),
288 ger.event(ns,'p1','v','b', expires_at: tomorrow),
289 ])
290 .then(-> ger.thing_neighbourhood(ns, 'a', {'v': 1, 'b': 1}))
291 .then((neighbourhood) ->
292 neighbourhood.length.should.equal 1
293 neighbourhood.map( (x) -> x.thing).should.include 'b'
294 )
295
296 it 'should list things which cannot be recommended', ->
297 init_ger(ESM)
298 .then (ger) ->
299 bb.all([
300 ger.event(ns,'p1','v','a', expires_at: tomorrow),
301 ger.event(ns,'p1','v','b', expires_at: yesterday),
302 ger.event(ns,'p1','v','c', expires_at: tomorrow)
303 ])
304 .then(-> ger.thing_neighbourhood(ns, 'a', {'v': 1}))
305 .then((neighbourhood) ->
306 neighbourhood.length.should.equal 1
307 neighbourhood.map( (x) -> x.thing).should.include 'c'
308 )
309
310
311 describe 'person_neighbourhood', ->
312 it 'should return a list of similar people', ->
313 init_ger(ESM)
314 .then (ger) ->
315 bb.all([
316 ger.event(ns,'p1','action1','a', expires_at: tomorrow),
317 ger.event(ns,'p2','action1','a', expires_at: tomorrow),
318 ger.event(ns,'p3','action1','a', expires_at: tomorrow),
319
320 ger.event(ns,'p1','action1','b', expires_at: tomorrow),
321 ger.event(ns,'p3','action1','b', expires_at: tomorrow),
322
323 ger.event(ns,'p4','action1','d', expires_at: tomorrow)
324 ])
325 .then(-> ger.person_neighbourhood(ns, 'p1', {'action1': 1}))
326 .then((similar_people) ->
327 similar_people.should.include 'p2'
328 similar_people.should.include 'p3'
329 )
330
331 it 'should handle a non associated person', ->
332 init_ger(ESM)
333 .then (ger) ->
334 bb.all([
335 ger.event(ns,'p1','action1','not', expires_at: tomorrow)
336 ger.event(ns,'p1','action1','a', expires_at: tomorrow),
337 ger.event(ns,'p2','action1','a', expires_at: tomorrow),
338 ger.event(ns,'p3','action1','a', expires_at: tomorrow),
339
340 ger.event(ns,'p1','action1','b', expires_at: tomorrow),
341 ger.event(ns,'p3','action1','b', expires_at: tomorrow),
342
343 ger.event(ns,'p4','action1','d', expires_at: tomorrow)
344 ])
345 .then(-> ger.person_neighbourhood(ns, 'p1', {'action1': 1}))
346 .then((similar_people) ->
347 similar_people.length.should.equal 2
348 )
349
350module.exports = ger_tests;
\No newline at end of file