[Groonga-commit] ranguba/groonga-client-rails at d72df0f [master] test rails5 activerecord: create searcher

Back to archive index

Kouhei Sutou null+****@clear*****
Mon Dec 12 11:08:21 JST 2016


Kouhei Sutou	2016-12-12 11:08:21 +0900 (Mon, 12 Dec 2016)

  New Revision: d72df0f93c04bdb3ccfb5c5ad39b69abd9f4f0a4
  https://github.com/ranguba/groonga-client-rails/commit/d72df0f93c04bdb3ccfb5c5ad39b69abd9f4f0a4

  Message:
    test rails5 activerecord: create searcher

  Added files:
    test/apps/rails5-activerecord/app/searchers/application_searcher.rb
    test/apps/rails5-activerecord/app/searchers/posts_searcher.rb
    test/apps/rails5-activerecord/config/groonga_client.yml
    test/apps/rails5-activerecord/test/factories/posts.rb
    test/apps/rails5-activerecord/test/searchers/posts_searcher_test.rb
  Removed files:
    test/apps/rails5-activerecord/test/fixtures/.keep
    test/apps/rails5-activerecord/test/fixtures/files/.keep
    test/apps/rails5-activerecord/test/fixtures/posts.yml
  Modified files:
    Gemfile
    test/apps/rails5-activerecord/Gemfile
    test/apps/rails5-activerecord/Gemfile.lock
    test/apps/rails5-activerecord/app/models/post.rb
    test/apps/rails5-activerecord/test/controllers/posts_controller_test.rb
    test/apps/rails5-activerecord/test/test_helper.rb

  Modified: Gemfile (+1 -3)
===================================================================
--- Gemfile    2016-12-12 10:22:39 +0900 (b8940b4)
+++ Gemfile    2016-12-12 11:08:21 +0900 (242fc22)
@@ -20,6 +20,4 @@ source "https://rubygems.org/"
 
 gemspec
 
-if File.exist?("../groonga-client")
-  gem "groonga-client", path: "../groonga-client"
-end
+gem "groonga-client", path: "../groonga-client"

  Modified: test/apps/rails5-activerecord/Gemfile (+5 -0)
===================================================================
--- test/apps/rails5-activerecord/Gemfile    2016-12-12 10:22:39 +0900 (4aa8f6b)
+++ test/apps/rails5-activerecord/Gemfile    2016-12-12 11:08:21 +0900 (8aeb9bc)
@@ -30,9 +30,14 @@ gem 'jbuilder', '~> 2.5'
 # Use Capistrano for deployment
 # gem 'capistrano-rails', group: :development
 
+gem 'groonga-client-rails', path: '../../../'
+gem 'groonga-client', path: '../../../../groonga-client'
+
 group :development, :test do
   # Call 'byebug' anywhere in the code to stop execution and get a debugger console
   gem 'byebug', platform: :mri
+
+  gem 'factory_girl_rails'
 end
 
 group :development do

  Modified: test/apps/rails5-activerecord/Gemfile.lock (+28 -0)
===================================================================
--- test/apps/rails5-activerecord/Gemfile.lock    2016-12-12 10:22:39 +0900 (9016f3f)
+++ test/apps/rails5-activerecord/Gemfile.lock    2016-12-12 11:08:21 +0900 (abcf155)
@@ -1,3 +1,18 @@
+PATH
+  remote: ../../../../groonga-client
+  specs:
+    groonga-client (0.3.4)
+      gqtp (>= 1.0.4)
+      groonga-command (>= 1.2.8)
+      hashie
+
+PATH
+  remote: ../../../
+  specs:
+    groonga-client-rails (0.9.5)
+      groonga-client (>= 0.3.4)
+      rails
+
 GEM
   remote: https://rubygems.org/
   specs:
@@ -52,9 +67,18 @@ GEM
     debug_inspector (0.0.2)
     erubis (2.7.0)
     execjs (2.7.0)
+    factory_girl (4.7.0)
+      activesupport (>= 3.0.0)
+    factory_girl_rails (4.7.0)
+      factory_girl (~> 4.7.0)
+      railties (>= 3.0.0)
     ffi (1.9.14)
     globalid (0.3.7)
       activesupport (>= 4.1.0)
+    gqtp (1.0.6)
+    groonga-command (1.3.1)
+      json
+    hashie (3.4.6)
     i18n (0.7.0)
     jbuilder (2.6.1)
       activesupport (>= 3.0.0, < 5.1)
@@ -63,6 +87,7 @@ GEM
       rails-dom-testing (>= 1, < 3)
       railties (>= 4.2.0)
       thor (>= 0.14, < 2.0)
+    json (2.0.2)
     listen (3.0.8)
       rb-fsevent (~> 0.9, >= 0.9.4)
       rb-inotify (~> 0.9, >= 0.9.7)
@@ -154,6 +179,9 @@ PLATFORMS
 DEPENDENCIES
   byebug
   coffee-rails (~> 4.2)
+  factory_girl_rails
+  groonga-client!
+  groonga-client-rails!
   jbuilder (~> 2.5)
   jquery-rails
   listen (~> 3.0.5)

  Modified: test/apps/rails5-activerecord/app/models/post.rb (+5 -0)
===================================================================
--- test/apps/rails5-activerecord/app/models/post.rb    2016-12-12 10:22:39 +0900 (b2a8b46)
+++ test/apps/rails5-activerecord/app/models/post.rb    2016-12-12 11:08:21 +0900 (d617e0f)
@@ -1,2 +1,7 @@
 class Post < ApplicationRecord
+  PostsSearcher.source(self).title = :title
+  PostsSearcher.source(self).body = lambda do |model|
+    model.body.gsub(/<.*?>/, "")
+  end
+  PostsSearcher.source(self).updated_at = true
 end

  Added: test/apps/rails5-activerecord/app/searchers/application_searcher.rb (+2 -0) 100644
===================================================================
--- /dev/null
+++ test/apps/rails5-activerecord/app/searchers/application_searcher.rb    2016-12-12 11:08:21 +0900 (457c4d6)
@@ -0,0 +1,2 @@
+class ApplicationSearcher < Groonga::Client::Searcher
+end

  Added: test/apps/rails5-activerecord/app/searchers/posts_searcher.rb (+16 -0) 100644
===================================================================
--- /dev/null
+++ test/apps/rails5-activerecord/app/searchers/posts_searcher.rb    2016-12-12 11:08:21 +0900 (7919a39)
@@ -0,0 +1,16 @@
+class PostsSearcher < ApplicationSearcher
+  schema.column :title, {
+    type: "ShortText",
+    index: true,
+    index_type: :full_text_search,
+  }
+  schema.column :body, {
+    type: "Text",
+    index: true,
+    index_type: :full_text_search,
+  }
+  schema.column :updated_at, {
+    type: "Time",
+    index: true,
+  }
+end

  Added: test/apps/rails5-activerecord/config/groonga_client.yml (+22 -0) 100644
===================================================================
--- /dev/null
+++ test/apps/rails5-activerecord/config/groonga_client.yml    2016-12-12 11:08:21 +0900 (807a647)
@@ -0,0 +1,22 @@
+default: &default
+  protocol: http
+  # protocol: https
+  host: 127.0.0.1
+  port: 10041
+  # user: alice
+  # password: secret
+  read_timeout: -1
+  # read_timeout: 3
+  backend: synchronous
+
+development:
+  <<: *default
+
+test:
+  <<: *default
+  port: 20041
+
+production:
+  <<: *default
+  host: 127.0.0.1
+  read_timeout: 10

  Modified: test/apps/rails5-activerecord/test/controllers/posts_controller_test.rb (+3 -1)
===================================================================
--- test/apps/rails5-activerecord/test/controllers/posts_controller_test.rb    2016-12-12 10:22:39 +0900 (8cd9d5d)
+++ test/apps/rails5-activerecord/test/controllers/posts_controller_test.rb    2016-12-12 11:08:21 +0900 (fa55cf7)
@@ -1,8 +1,10 @@
 require 'test_helper'
 
 class PostsControllerTest < ActionDispatch::IntegrationTest
+  include Groonga::Client::Rails::TestHelper
+
   setup do
-    @post = posts(:one)
+    @post = create(:post)
   end
 
   test "should get index" do

  Added: test/apps/rails5-activerecord/test/factories/posts.rb (+6 -0) 100644
===================================================================
--- /dev/null
+++ test/apps/rails5-activerecord/test/factories/posts.rb    2016-12-12 11:08:21 +0900 (28b7437)
@@ -0,0 +1,6 @@
+FactoryGirl.define do
+  factory :post do
+    title "MyString"
+    body "MyText"
+  end
+end

  Deleted: test/apps/rails5-activerecord/test/fixtures/.keep (+0 -0) 100644
===================================================================
--- test/apps/rails5-activerecord/test/fixtures/.keep    2016-12-12 10:22:39 +0900 (e69de29)
+++ /dev/null

  Deleted: test/apps/rails5-activerecord/test/fixtures/files/.keep (+0 -0) 100644
===================================================================
--- test/apps/rails5-activerecord/test/fixtures/files/.keep    2016-12-12 10:22:39 +0900 (e69de29)
+++ /dev/null

  Deleted: test/apps/rails5-activerecord/test/fixtures/posts.yml (+0 -9) 100644
===================================================================
--- test/apps/rails5-activerecord/test/fixtures/posts.yml    2016-12-12 10:22:39 +0900 (e192dee)
+++ /dev/null
@@ -1,9 +0,0 @@
-# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
-
-one:
-  title: MyString
-  body: MyText
-
-two:
-  title: MyString
-  body: MyText

  Added: test/apps/rails5-activerecord/test/searchers/posts_searcher_test.rb (+119 -0) 100644
===================================================================
--- /dev/null
+++ test/apps/rails5-activerecord/test/searchers/posts_searcher_test.rb    2016-12-12 11:08:21 +0900 (6b181fe)
@@ -0,0 +1,119 @@
+require 'test_helper'
+
+class PostsSearcherTest < ActionController::TestCase
+  include Groonga::Client::Rails::TestHelper
+
+  setup do
+    @searcher = PostsSearcher.new
+  end
+
+  test "should be untagged" do
+    create(:post, body: "<p>Hello <em>World</em></p>")
+    result_set =****@searc*****_set
+    assert_equal(["Hello World"],
+                 result_set.records.collect {|record| record["body"]})
+  end
+
+  test "should be searchable without match_columns" do
+    create(:post, body: "Hello World")
+    create(:post, body: "Hello Rails")
+    result_set =****@searc*****("World").result_set
+    assert_equal(["Hello World"],
+                 result_set.records.collect {|record| record["body"]})
+  end
+
+  test "should be searchable by a filter" do
+    create(:post, body: "Hello World")
+    create(:post, body: "Hello Rails")
+    result_set = @searcher.
+      search.
+      filter("body @ %{keyword}", {keyword: "World"}).
+      result_set
+    assert_equal(["Hello World"],
+                 result_set.records.collect {|record| record["body"]})
+  end
+
+  test "should be searchable by filters" do
+    create(:post, body: "Hello World")
+    create(:post, body: "Hello Rails")
+    create(:post, body: "Hi World")
+    result_set = @searcher.
+      search.
+      filter("body @ %{keyword}", {keyword: "Hello"}).
+      filter("body @ %{keyword}", {keyword: "World"}).
+      result_set
+    assert_equal(["Hello World"],
+                 result_set.records.collect {|record| record["body"]})
+  end
+
+  test "should be searchable with special characters by a filter" do
+    create(:post, body: "Hello \"Wo\\rld\"")
+    create(:post, body: "Hello Rails")
+    result_set = @searcher.
+      search.
+      filter("body @ %{keyword}", {keyword: "\"Wo\\rld\""}).
+      result_set
+    assert_equal(["Hello \"Wo\\rld\""],
+                 result_set.records.collect {|record| record["body"]})
+  end
+
+  test "should support snippet_html in output_columns" do
+    create(:post, body: "Hello World")
+    create(:post, body: "Hi Rails! Hello!")
+    result_set = @searcher.
+      search.
+      query("Hello").
+      output_columns("snippet_html(body)").
+      result_set
+    snippet_htmls = result_set.records.collect do |record|
+      record["snippet_html"]
+    end
+    assert_equal([
+                   ["<span class=\"keyword\">Hello</span> World"],
+                   ["Hi Rails! <span class=\"keyword\">Hello</span>!"],
+                 ],
+                 snippet_htmls)
+  end
+
+  test "should support Array for output_columns" do
+    post = create(:post, body: "Hello World")
+    result_set = @searcher.
+      search.
+      query("World").
+      output_columns(["_key", "body"]).
+      result_set
+    data = result_set.records.collect do |record|
+      [
+        record["_id"],
+        record["_key"],
+        record["body"],
+      ]
+    end
+    assert_equal([
+                   [
+                     nil,
+                     "#{post.class}-#{post.id}",
+                     "Hello World",
+                   ],
+                 ],
+                 data)
+  end
+
+  test "should support pagination" do
+    100.times do |i|
+      create(:post, body: "Hello #{i}")
+    end
+    result_set =****@searc*****(3, per_page: 5).result_set
+    data = result_set.records.collect do |record|
+      record["body"]
+    end
+    assert_equal([
+                   "Hello 10",
+                   "Hello 11",
+                   "Hello 12",
+                   "Hello 13",
+                   "Hello 14",
+                 ],
+                 data)
+  end
+end

  Modified: test/apps/rails5-activerecord/test/test_helper.rb (+2 -3)
===================================================================
--- test/apps/rails5-activerecord/test/test_helper.rb    2016-12-12 10:22:39 +0900 (92e39b2)
+++ test/apps/rails5-activerecord/test/test_helper.rb    2016-12-12 11:08:21 +0900 (d2aca34)
@@ -1,10 +1,9 @@
 ENV['RAILS_ENV'] ||= 'test'
 require File.expand_path('../../config/environment', __FILE__)
 require 'rails/test_help'
+require 'groonga/client/rails/test_helper'
 
 class ActiveSupport::TestCase
-  # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
-  fixtures :all
-
   # Add more helper methods to be used by all tests here...
+  include FactoryGirl::Syntax::Methods
 end
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index