[Groonga-commit] ranguba/chupa-text at c3e120e [master] Add HTTPServer.default_url{, =}

Back to archive index
Kouhei Sutou null+****@clear*****
Wed Mar 27 17:31:00 JST 2019


Kouhei Sutou	2019-03-27 17:31:00 +0900 (Wed, 27 Mar 2019)

  Revision: c3e120eb0e56629ab4604f4550332717556972ae
  https://github.com/ranguba/chupa-text/commit/c3e120eb0e56629ab4604f4550332717556972ae

  Message:
    Add HTTPServer.default_url{,=}

  Modified files:
    lib/chupa-text/decomposers/http-server.rb
    test/decomposers/test-http-server.rb

  Modified: lib/chupa-text/decomposers/http-server.rb (+15 -2)
===================================================================
--- lib/chupa-text/decomposers/http-server.rb    2019-03-27 17:24:59 +0900 (6b3483b)
+++ lib/chupa-text/decomposers/http-server.rb    2019-03-27 17:31:00 +0900 (fde562c)
@@ -22,9 +22,22 @@ module ChupaText
     class HTTPServer < Decomposer
       registry.register("http-server", self)
 
+      @@default_url = nil
+      class << self
+        def default_url
+          @@default_url
+        end
+
+        def default_url=(url)
+          @@default_url = url
+        end
+      end
+
       def initialize(options)
         super
-        @url = @options[:url] || ENV["CHUPA_TEXT_HTTP_SERVER_URL"]
+        @url = @options[:url] ||
+               self.class.default_url ||
+               ENV["CHUPA_TEXT_HTTP_SERVER_URL"]
         @url = URI(@url) if @url
       end
 
@@ -36,7 +49,7 @@ module ChupaText
 
       def target_score(data)
         if target?(data)
-          -100
+          100
         else
           nil
         end

  Modified: test/decomposers/test-http-server.rb (+56 -28)
===================================================================
--- test/decomposers/test-http-server.rb    2019-03-27 17:24:59 +0900 (1f5e8fc)
+++ test/decomposers/test-http-server.rb    2019-03-27 17:31:00 +0900 (ec2c226)
@@ -18,6 +18,13 @@ class TestDecomposersHTTPServer < Test::Unit::TestCase
   include Helper
 
   def setup
+    ChupaText::Decomposers::HTTPServer.default_url = nil
+    setup_server
+    setup_data
+    setup_decomposer
+  end
+
+  def setup_server
     @port = 40080
     @path = "/extraction.json"
     @server_url = "http://127.0.0.1:#{@port}#{@path}"
@@ -28,54 +35,75 @@ class TestDecomposersHTTPServer < Test::Unit::TestCase
                                       AccessLog: [])
     @server.mount_proc(@path) do |request, response|
       response["Content-Type"] = "application/json"
-      response.body = JSON.generate(@actual)
+      response.body = JSON.generate(@extracted)
     end
     @server_thread = Thread.new do
       @server.start
     end
-    @decomposer = ChupaText::Decomposers::HTTPServer.new(:url => @server_url)
-  end
-
-  def teardown
-    @server.shutdown
-    @server_thread.join
   end
 
-  sub_test_case("decompose") do
-    def test_valid
-      csv = <<-CSV
+  def setup_data
+    @input_data = <<-CSV
 Hello,World
 Ruby,ChupaText
-      CSV
-      extracted = csv.gsub(/,/, "\t")
-      @actual = {
-        "mime-type" => "text/csv",
-        "uri" => "file:///tmp/hello.csv",
-        "path" => "/tmp/hello.csv",
-        "size" => csv.bytesize,
+    CSV
+    @input_mime_type = "text/csv"
+    @input_path = "/tmp/hello.csv"
+    @extracted_text = @input_data.gsub(/,/, "\t")
+    @extracted_path = @input_path.gsub(/\.csv\z/, ".txt")
+    @extracted = {
+        "mime-type" => @input_mime_type,
+        "uri" => "file://#{@input_path}",
+        "path" => @input_path,
+        "size" => @input_data.bytesize,
         "texts" => [
           {
             "mime-type" => "text/plain",
-            "uri" => "file:///tmp/hello.txt",
-            "path" => "/tmp/hello.txt",
-            "size" => extracted.bytesize,
+            "uri" => "file://#{@extracted_path}",
+            "path" => @extracted_path,
+            "size" => @extracted_text.bytesize,
             "source-mime-types" => [
-              "text/csv",
+              @input_mime_type,
             ],
-            "body" => extracted,
+            "body" => @extracted_text,
           },
         ],
       }
-      assert_equal([extracted],
-                   decompose(csv).collect(&:body))
+
+  end
+
+  def setup_decomposer
+    @decomposer = ChupaText::Decomposers::HTTPServer.new(:url => @server_url)
+  end
+
+  def teardown
+    teardown_server
+  end
+
+  def teardown_server
+    @server.shutdown
+    @server_thread.join
+  end
+
+  sub_test_case("decompose") do
+    def test_valid
+      assert_equal([@extracted_text],
+                   decompose.collect(&:body))
+    end
+
+    def test_default_url
+      ChupaText::Decomposers::HTTPServer.default_url = @server_url
+      @decomposer = ChupaText::Decomposers::HTTPServer.new({})
+      assert_equal([@extracted_text],
+                   decompose.collect(&:body))
     end
 
     private
-    def decompose(csv)
+    def decompose
       data = ChupaText::Data.new
-      data.path = "/tmp/hello.csv"
-      data.mime_type = "text/csv"
-      data.body = csv
+      data.path = @input_path
+      data.mime_type = @input_mime_type
+      data.body = @input_data
 
       decomposed = []
       @decomposer.decompose(data) do |decomposed_data|
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.osdn.me/mailman/archives/groonga-commit/attachments/20190327/caf4997f/attachment-0001.html>


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