# -*- coding: utf-8 -*- # Copyright (C) 2009 Kouhei Sutou # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License version 2.1 as published by the Free Software Foundation. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA class PatriciaTrieTest < Test::Unit::TestCase include GroongaTestUtils setup :setup_database def test_encoding assert_equal(Groonga::Encoding.default, Groonga::PatriciaTrie.create.encoding) end def test_tokenizer trie = Groonga::PatriciaTrie.create assert_nil(trie.default_tokenizer) trie.default_tokenizer = "" assert_equal(Groonga::Context.default[""], trie.default_tokenizer) end def test_search omit("creating entry is broken.") users = Groonga::Array.create(:name => "") user_name = users.define_column("name", "") bookmarks = Groonga::PatriciaTrie.create(:name => "", :key_type => "") bookmark_user_id = bookmarks.define_column("user_id", users) daijiro = users.add daijiro["name"] = "daijiro" gunyarakun = users.add gunyarakun["name"] = "gunyarakun" groonga = bookmarks.add("http://groonga.org/") groonga["user_id"] = daijiro records = bookmarks.search("http://groonga.org/") assert_equal(["daijiro"], records.records.collect {|record| record[".user_id.name"]}) end def test_add users = Groonga::PatriciaTrie.create(:name => "") users.define_column("address", "") me = users.add("me", :address => "me@example.com") assert_equal("me@example.com", me[:address]) end def test_default_tokenizer_on_create terms = Groonga::PatriciaTrie.create(:name => "", :default_tokenizer => "") assert_equal(context[Groonga::Type::UNIGRAM], terms.default_tokenizer) end def test_duplicated_name Groonga::PatriciaTrie.create(:name => "") assert_raise(Groonga::InvalidArgument) do Groonga::PatriciaTrie.create(:name => "") end end def test_open_same_name users_created = Groonga::PatriciaTrie.create(:name => "") users_opened = Groonga::PatriciaTrie.open(:name => "") users_opened.add("morita") assert_equal(1, users_created.size) end def test_has_key? users = Groonga::PatriciaTrie.create(:name => "") assert_false(users.has_key?("morita")) users.add("morita") assert_true(users.has_key?("morita")) end def test_scan Groonga::Context.default_options = {:encoding => "utf-8"} words = Groonga::PatriciaTrie.create(:key_type => "ShortText", :key_normalize => true) words.add("リンク") adventure_of_link = words.add('リンクの冒険') words.add('冒険') gaxtu = words.add('ガッ') muteki = words.add('MUTEKI') assert_equal([[muteki, "muTEki", 0, 6], [adventure_of_link, "リンクの冒険", 7, 18], [gaxtu, "ガッ", 42, 6]], words.scan('muTEki リンクの冒険 ミリバール ガッ')) end def test_tag_keys Groonga::Context.default_options = {:encoding => "utf-8"} words = Groonga::PatriciaTrie.create(:key_type => "ShortText", :key_normalize => true) words.add("リンク") words.add('リンクの冒険') words.add('冒険') words.add('㍊') words.add('ガッ') words.add('MUTEKI') text = 'muTEki リンクの冒険 マッチしない ミリバール ガッ' actual = words.tag_keys(text) do |record, word| "<#{word}(#{record.key})>" end assert_equal(" " + "<リンクの冒険(リンクの冒険)> " + "マッチしない " + "<ミリバール(ミリバール)> " + "<ガッ(ガッ)>", actual) end end