Kouhei Sutou
null+****@clear*****
Fri Mar 3 08:54:53 JST 2017
Kouhei Sutou 2017-03-03 08:54:53 +0900 (Fri, 03 Mar 2017) New Revision: e0847d44a749a63a681303ac0c4abd137c95914c https://github.com/ranguba/groonga-client-model/commit/e0847d44a749a63a681303ac0c4abd137c95914c Message: Add a test for migration Added files: test/unit/test_migration.rb Modified files: groonga-client-model.gemspec lib/groonga_client_model/migration.rb test/unit/run-test.rb Modified: groonga-client-model.gemspec (+1 -1) =================================================================== --- groonga-client-model.gemspec 2017-03-03 08:40:25 +0900 (817f998) +++ groonga-client-model.gemspec 2017-03-03 08:54:53 +0900 (7364ab5) @@ -43,7 +43,7 @@ Gem::Specification.new do |spec| spec.files += Dir.glob("doc/text/*") spec.test_files += Dir.glob("test/**/*") - spec.add_runtime_dependency("groonga-client", ">= 0.4.1") + spec.add_runtime_dependency("groonga-client", ">= 0.4.2") spec.add_runtime_dependency("groonga-command-parser") spec.add_runtime_dependency("activemodel") Modified: lib/groonga_client_model/migration.rb (+13 -0) =================================================================== --- lib/groonga_client_model/migration.rb 2017-03-03 08:40:25 +0900 (1ea36df) +++ lib/groonga_client_model/migration.rb 2017-03-03 08:54:53 +0900 (bf99578) @@ -14,6 +14,8 @@ # License along with this library; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +require "benchmark" + module GroongaClientModel class MigrationError < Error end @@ -22,8 +24,11 @@ module GroongaClientModel end class Migration + attr_accessor :output + def initialize(client) @client = client + @output = nil @reverting = false end @@ -106,6 +111,14 @@ module GroongaClientModel end private + def puts(*args) + if @output + @output.puts(*args) + else + super + end + end + def report(method_name, arguments) argument_list = arguments.collect(&:inspect).join(", ") puts("-- #{method_name}(#{argument_list})") Modified: test/unit/run-test.rb (+3 -0) =================================================================== --- test/unit/run-test.rb 2017-03-03 08:40:25 +0900 (305958c) +++ test/unit/run-test.rb 2017-03-03 08:54:53 +0900 (f37d9ff) @@ -29,6 +29,9 @@ $LOAD_PATH.unshift(lib_dir.to_s) require "test-unit" require "groonga-client-model" +require "groonga_client_model/test_helper" + +GroongaClientModel::Client.url = "http://127.0.0.1:20041" Thread.abort_on_exception = true Added: test/unit/test_migration.rb (+69 -0) 100644 =================================================================== --- /dev/null +++ test/unit/test_migration.rb 2017-03-03 08:54:53 +0900 (d585066) @@ -0,0 +1,69 @@ +# Copyright (C) 2017 Kouhei Sutou <kou �� clear-code.com> +# +# 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 + +require "stringio" + +require "groonga_client_model/migration" + +class TestMigration < Test::Unit::TestCase + include GroongaClientModel::TestHelper + + def open_client(&block) + GroongaClientModel::Client.open(&block) + end + + def migrate(direction) + migration_class = Class.new(GroongaClientModel::Migration) do |klass| + define_method(:change) do + yield(self) + end + end + output = StringIO.new + open_client do |client| + migration = migration_class.new(client) + migration.output = output + migration.up + end + normalize_report(output.string) + end + + def normalize_report(report) + report.gsub(/[0-9]+\.[0-9]+s/, "0.0s") + end + + def dump + open_client do |client| + client.dump.body + end + end + + sub_test_case("#create_table") do + test("default") do + report = migrate(:up) do |migration| + migration.instance_eval do + create_table(:posts) + end + end + assert_equal(<<-REPORT, report) +-- create_table(:posts, {:type=>"TABLE_NO_KEY", :key_type=>nil}) + -> 0.0s + REPORT + assert_equal(<<-DUMP.chomp, dump) +table_create posts TABLE_NO_KEY + DUMP + end + end +end -------------- next part -------------- HTML����������������������������... Download