Permalink
Browse files

tests are awesome (foudn a bug with slice usage)

  • Loading branch information...
1 parent ae5a295 commit 308d8770576a9928036a8a3d692381928b8b33de @PragTob committed May 8, 2012
Showing with 35 additions and 10 deletions.
  1. +1 −1 lib/recommender.coffee
  2. +14 −7 spec/helper/itemStorageMock.coffee
  3. +20 −1 spec/recommender.spec.coffee
  4. +0 −1 spec/serverAcceptance.spec.coffee
View
@@ -37,7 +37,7 @@ class Recommender
limit = requestObject.config.limit
itemsWithoutRequested = _.reject @itemStorage[domainId], (item) ->
item.id == requestObject.item.id
- items = @sortItemsByHitCount(itemsWithoutRequested).slice(0, limit - 1)
+ items = @sortItemsByHitCount(itemsWithoutRequested).slice(0, limit)
recommendations = _.map items, (item) -> id: item.id
sortItemsByHitCount: (items) ->
@@ -5,26 +5,33 @@ class ItemStorageMock
constructor: ->
# the structure here is domain_id: item_id: item
@[1] =
- 1:
+ '1':
title: 'casual'
+ id: '1'
hitcount: 1
- 2:
+ '2':
title: 'uninteresting'
+ id: '2'
hitcount: 2
- 4:
+ '4':
title: 'exciting'
+ id: '4'
hitcount: 5
- 7:
+ '7':
title: 'nobody knows this'
+ id: '7'
hitcount: 1
- 8:
+ '8':
title: 'a pair'
+ id: '8'
hitcount: 2
- 9:
+ '9':
title: 'cool'
+ id: '9'
hitcount: 4
- 17:
+ '17':
title: 'top of the pops'
+ id: '17'
hitcount: 100
exports.ItemStorageMock = ItemStorageMock
@@ -2,6 +2,17 @@ _ = require 'underscore'
{Recommender} = require './../lib/recommender'
{ItemStorageMock} = require './helper/itemStorageMock'
+LIMIT = 4
+DOMAIN_ID = 1
+
+requestObject = (itemId = "5") ->
+ domain:
+ id: DOMAIN_ID
+ item:
+ id: itemId
+ config:
+ limit: LIMIT
+
describe 'Recommender', ->
beforeEach ->
@@ -12,9 +23,17 @@ describe 'Recommender', ->
expect(@recommender).toBeDefined()
it 'can sort items by hitcount', ->
- items = @recommender.sortItemsByHitCount(@storage[1])
+ items = @recommender.sortItemsByHitCount(@storage[DOMAIN_ID])
last = null
_.each items, (each) ->
expect(each.hitcount).not.toBeGreaterThan(last.hitcount) if last?
last = each
+ describe 'findRecommendations', ->
+
+ it 'responds with an array of an appropriate size', ->
+ recommendations = @recommender.findRecommendations(requestObject())
+ expect(recommendations.length).toEqual LIMIT
+
+
+
@@ -1,7 +1,6 @@
{Recommender} = require './../lib/recommender'
{ItemStorage} = require './../lib/itemStorage'
{ServerTester} = require './helper/serverTester'
-{ItemStorageMock} = require './helper/itemStorageMock'
PORT = 4050
ITEM_ID = "I'm a String ID - weird"

0 comments on commit 308d877

Please sign in to comment.