Permalink
Browse files

threw in some tests for prefixes_for_phrase

  • Loading branch information...
1 parent 5daedd2 commit 3ab6d2b8706a8039c5483dc28276ebce850852ec @erwaller erwaller committed Oct 12, 2011
Showing with 11 additions and 3 deletions.
  1. +1 −1 lib/soulmate/helpers.rb
  2. +2 −2 lib/soulmate/loader.rb
  3. +8 −0 test/test_soulmate.rb
View
@@ -5,7 +5,7 @@ def prefixes_for_phrase(phrase)
words = normalize(phrase).split(' ')
words.map do |w|
(MIN_COMPLETE-1..(w.length-1)).map{ |l| w[0..l] }
- end.flatten
+ end.flatten.uniq
end
def normalize(str)
View
@@ -39,7 +39,7 @@ def add(item = {})
# store the raw data in a separate key to reduce memory usage
Soulmate.redis.hset(database, item["id"], JSON.dump(item))
phrase = ([item["term"]] + (item["aliases"] || [])).join(' ')
- prefixes_for_phrase(phrase).uniq.each do |p|
+ prefixes_for_phrase(phrase).each do |p|
Soulmate.redis.sadd(base, p) # remember this prefix in a master set
Soulmate.redis.zadd("#{base}:#{p}", item["score"], item["id"]) # store the id of this term in the index
end
@@ -52,7 +52,7 @@ def remove(id)
# undo the operations done in add
Soulmate.redis.hdel(database, prev_item["id"])
phrase = ([prev_item["term"]] + (prev_item["aliases"] || [])).join(' ')
- prefixes_for_phrase(phrase).uniq.each do |p|
+ prefixes_for_phrase(phrase).each do |p|
Soulmate.redis.srem(base, p)
Soulmate.redis.zrem("#{base}:#{p}", prev_item["id"])
end
View
@@ -90,4 +90,12 @@ def test_can_update_items
assert_equal 9, results.first["score"]
end
+
+ def test_prefixes_for_phrase
+ loader = Soulmate::Loader.new('venues')
+
+ assert_equal ["te", "tes", "test", "testi", "testin", "th", "thi", "this"], loader.prefixes_for_phrase("testin' this")
+ assert_equal ["te", "tes", "test"], loader.prefixes_for_phrase("test test")
+ assert_equal ["so", "sou", "soul", "soulm", "soulma", "soulmat", "soulmate"], loader.prefixes_for_phrase("SoUlmATE")
+ end
end

0 comments on commit 3ab6d2b

Please sign in to comment.