require "helper" class LunarIndexTest < Test::Unit::TestCase setup do Lunar.redis(Redis.new(:host => '127.0.0.1', :port => '6380')) Lunar.redis.flushdb end context "given Item" do setup do @index = Lunar::Index.new('Item') end should "have a Lunar:Item ns" do assert_equal 'Lunar:Item', @index.ns end context "when setting attr :name, 'iphone'" do should "have attr(:name) iphone" do @index.attr :name, 'iphone' assert_equal 'iphone', @index.attr(:name) end end context "when setting attrs name iphone desc cellular mobile" do setup do @index.attrs = { :name => 'iphone', :desc => 'cellular mobile' } end should "be able to retrieve them via symbol or string" do assert_equal 'iphone', @index.attr(:name) assert_equal 'cellular mobile', @index.attr('desc') end end end context "on create" do setup do @index = Lunar::Index.create 'Item' do |i| i.key 1001 i.attr :name, 'iphone 3G' i.attr :description, 'the cellular mobile is the shiznit' end end should "store Lunar:Item:name:iphone 1001 1" do assert_equal "1", zscore("Lunar:Item:name:#{ encode('iphone') }", 1001) end should "store Lunar:Item:name:3g 1001 1" do assert_equal "1", zscore("Lunar:Item:name:#{ encode('3g') }", 1001) end should "store Lunar:Item:description:cellular 1001 1" do assert_equal "1", zscore("Lunar:Item:description:#{ encode('cellular') }", 1001) end should "store Lunar:Item:description:the 1001 2" do assert_equal "2", zscore("Lunar:Item:description:#{ encode('the') }", 1001) end should "store Lunar:Item:1001:name set" do set = Lunar.redis.smembers("Lunar:Item:1001:name") assert set.include?('iphone') assert set.include?('3g') end should "store Lunar:Item:1001:description of all words" do set = Lunar.redis.smembers("Lunar:Item:1001:description") assert set.include?('the') assert set.include?('cellular') assert set.include?('mobile') assert set.include?('is') assert set.include?('shiznit') end end context "when creating an index that already exists" do setup do @index = Lunar::Index.create 'Item' do |i| i.key 1001 i.attr :name, 'iphone 3G' i.attr :description, 'the cellular mobile is the shiznit' end @index = Lunar::Index.create 'Item' do |i| i.key 1001 i.attr :name, 'iphone 3GS' i.attr :description, 'kickass' end end should "update the references for Item:name" do set = Lunar.redis.smembers("Lunar:Item:1001:name") assert ! set.include?('3g'), "should not include 3G" assert set.include?('3gs'), "should include 3GS" end should "also remove the scores for the non-existent references for name" do assert_nil zscore("Lunar:Item:name:#{ encode('3g') }", 1001) end should "update the references for Item:description" do set = Lunar.redis.smembers("Lunar:Item:1001:description") assert ! set.include?('the') assert ! set.include?('cellular') assert ! set.include?('mobile') assert ! set.include?('is') assert ! set.include?('shiznit') assert set.include?('kickass') end should "also remove the scores for the non-existent references for desc" do assert_nil zscore("Lunar:Item:description:#{ encode('the') }", 1001) assert_nil zscore("Lunar:Item:description:#{ encode('cellular') }", 1001) assert_nil zscore("Lunar:Item:description:#{ encode('mobile') }", 1001) assert_nil zscore("Lunar:Item:description:#{ encode('is') }", 1001) assert_nil zscore("Lunar:Item:description:#{ encode('shiznit') }", 1001) assert_equal "1", zscore("Lunar:Item:description:#{ encode('kickass') }", 1001) end end context "on delete" do setup do @index = Lunar::Index.create 'Item' do |i| i.key 1001 i.attr :name, 'iphone 3G' i.attr :description, 'the cellular mobile is the shiznit' end Lunar::Index.delete('Item', 1001) end should "remove all references of the document words from the scores" do assert_nil zscore("Lunar:Item:name:#{ encode('iphone') }", 1001) assert_nil zscore("Lunar:Item:name:#{ encode('3g') }", 1001) assert_nil zscore("Lunar:Item:description:#{ encode('the') }", 1001) assert_nil zscore("Lunar:Item:description:#{ encode('cellular') }", 1001) assert_nil zscore("Lunar:Item:description:#{ encode('mobile') }", 1001) assert_nil zscore("Lunar:Item:description:#{ encode('is') }", 1001) assert_nil zscore("Lunar:Item:description:#{ encode('shiznit') }", 1001) end should "remove all Lunar:Item:* references" do assert ! Lunar.redis.exists("Lunar:Item:1001:name") assert ! Lunar.redis.exists("Lunar:Item:1001:description") end end context "on create of an index with numeric scores" do setup do @index = Lunar::Index.create 'Item' do |i| i.key 1001 i.integer :cost, 2700 i.float :voting_quotient, 35.5 end end should "store Lunar:Item:cost 1001 2700" do assert_equal "2700", zscore("Lunar:Item:cost", 1001) end should "store Lunar:Item:voting_quotient 1001 35.5" do assert_equal "35.5", zscore("Lunar:Item:voting_quotient", 1001) end end protected def encode(str) Lunar.encode(str) end def zscore(key, value) Lunar.redis.zscore(key, value) end end