[Groonga-commit] ranguba/chupa-text at a57a2cd [master] Add a test for HTTP server decomposer

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


Kouhei Sutou	2019-03-27 17:24:59 +0900 (Wed, 27 Mar 2019)

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

  Message:
    Add a test for HTTP server decomposer

  Added files:
    test/decomposers/test-http-server.rb
  Modified files:
    test/helper.rb

  Added: test/decomposers/test-http-server.rb (+87 -0) 100644
===================================================================
--- /dev/null
+++ test/decomposers/test-http-server.rb    2019-03-27 17:24:59 +0900 (1f5e8fc)
@@ -0,0 +1,87 @@
+# Copyright (C) 2019  Kouhei Sutou <kou****@clear*****>
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+
+class TestDecomposersHTTPServer < Test::Unit::TestCase
+  include Helper
+
+  def setup
+    @port = 40080
+    @path = "/extraction.json"
+    @server_url = "http://127.0.0.1:#{@port}#{@path}"
+    logger = WEBrick::Log.new
+    logger.level = logger.class::ERROR
+    @server = WEBrick::HTTPServer.new(Port: @port,
+                                      Logger: logger,
+                                      AccessLog: [])
+    @server.mount_proc(@path) do |request, response|
+      response["Content-Type"] = "application/json"
+      response.body = JSON.generate(@actual)
+    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
+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,
+        "texts" => [
+          {
+            "mime-type" => "text/plain",
+            "uri" => "file:///tmp/hello.txt",
+            "path" => "/tmp/hello.txt",
+            "size" => extracted.bytesize,
+            "source-mime-types" => [
+              "text/csv",
+            ],
+            "body" => extracted,
+          },
+        ],
+      }
+      assert_equal([extracted],
+                   decompose(csv).collect(&:body))
+    end
+
+    private
+    def decompose(csv)
+      data = ChupaText::Data.new
+      data.path = "/tmp/hello.csv"
+      data.mime_type = "text/csv"
+      data.body = csv
+
+      decomposed = []
+      @decomposer.decompose(data) do |decomposed_data|
+        decomposed << decomposed_data
+      end
+      decomposed
+    end
+  end
+end

  Modified: test/helper.rb (+1 -0)
===================================================================
--- test/helper.rb    2019-03-27 16:29:50 +0900 (9e068e6)
+++ test/helper.rb    2019-03-27 17:24:59 +0900 (7fea6f1)
@@ -17,6 +17,7 @@
 require "pathname"
 require "tempfile"
 require "uri"
+require "webrick"
 
 module Helper
   def fixture_path(*components)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.osdn.me/mailman/archives/groonga-commit/attachments/20190327/1dde85f0/attachment-0001.html>


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