Kouhei Sutou
null+****@clear*****
Fri Mar 3 21:26:25 JST 2017
Kouhei Sutou 2017-03-03 21:26:25 +0900 (Fri, 03 Mar 2017) New Revision: 6de4120318454e8742ca01625ab1d6ef4d996d02 https://github.com/ranguba/groonga-client-model/commit/6de4120318454e8742ca01625ab1d6ef4d996d02 Message: Support set_config and delete_config in migration Added files: test/unit/migration/test_config.rb test/unit/test_helper.rb Modified files: lib/groonga_client_model/migration.rb lib/groonga_client_model/test/groonga_server_runner.rb test/unit/run-test.rb Renamed files: test/unit/migration/test_create_table.rb (from test/unit/test_migration.rb) Modified: lib/groonga_client_model/migration.rb (+27 -0) =================================================================== --- lib/groonga_client_model/migration.rb 2017-03-03 21:06:46 +0900 (8031840) +++ lib/groonga_client_model/migration.rb 2017-03-03 21:26:25 +0900 (99a7c39) @@ -189,6 +189,33 @@ module GroongaClientModel end end + def set_config(key, value) + if @reverting + message = "can't revert set_config(#{key.inspect}, #{value.inspect})" + raise IrreversibleMigrationError, message + end + + report(__method__, [key, value]) do + @client.request(:config_set). + parameter(:key, key). + parameter(:value, value). + response + end + end + + def delete_config(key) + if @reverting + message = "can't revert delete_config(#{key.inspect})" + raise IrreversibleMigrationError, message + end + + report(__method__, [key]) do + @client.request(:config_delete). + parameter(:key, key). + response + end + end + private def puts(*args) if @output Modified: lib/groonga_client_model/test/groonga_server_runner.rb (+3 -3) =================================================================== --- lib/groonga_client_model/test/groonga_server_runner.rb 2017-03-03 21:06:46 +0900 (794c643) +++ lib/groonga_client_model/test/groonga_server_runner.rb 2017-03-03 21:26:25 +0900 (e2189ed) @@ -42,13 +42,13 @@ module GroongaClientModel end schema_path = base_dir + "db" + "schema.grn" + migrate_path = base_dir + "db" + "groonga" + "migrate" if schema_path.exist? schema_loader = SchemaLoader.new(schema_path) schema_loader.load - else + elsif migrate_path.exist? output = StringIO.new - migrator = Migrator.new(base_dir + "db" + "groonga" + "migrate", - nil) + migrator = Migrator.new(migrate_path, nil) migrator.output = output migrator.migrate end Added: test/unit/migration/test_config.rb (+62 -0) 100644 =================================================================== --- /dev/null +++ test/unit/migration/test_config.rb 2017-03-03 21:26:25 +0900 (dfb7666) @@ -0,0 +1,62 @@ +# 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 + +class TestMigrationConfig < Test::Unit::TestCase + include GroongaClientModel::TestHelper + include TestHelper::Migration + + test("#set_config") do + expected_up_report = <<-REPORT +-- set_config("alias.column", "aliases.real_name") + -> 0.0s + REPORT + expected_down_report = nil + expected_dump = <<-DUMP.chomp +config_set alias.column aliases.real_name + DUMP + assert_migrate(expected_up_report, + expected_down_report, + expected_dump) do |migration| + migration.instance_eval do + set_config("alias.column", "aliases.real_name") + end + end + end + + test("#delete_config") do + open_client do |client| + client.request(:config_set). + parameter(:key, "alias.column"). + parameter(:value, "aliases.real_name"). + response + end + + expected_up_report = <<-REPORT +-- delete_config("alias.column") + -> 0.0s + REPORT + expected_down_report = nil + expected_dump = <<-DUMP.chomp + DUMP + assert_migrate(expected_up_report, + expected_down_report, + expected_dump) do |migration| + migration.instance_eval do + delete_config("alias.column") + end + end + end +end Renamed: test/unit/migration/test_create_table.rb (+131 -180) 58% =================================================================== --- test/unit/test_migration.rb 2017-03-03 21:06:46 +0900 (2e512f6) +++ test/unit/migration/test_create_table.rb 2017-03-03 21:26:25 +0900 (d1c9e47) @@ -14,259 +14,211 @@ # 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 +class TestMigrationCreateTable < Test::Unit::TestCase include GroongaClientModel::TestHelper + include TestHelper::Migration - def open_client(&block) - GroongaClientModel::Client.open(&block) - 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 - - def assert_migrate(expected_up_report, - expected_down_report, - expected_dump) - migration_class = Class.new(GroongaClientModel::Migration) do |klass| - define_method(:change) do - yield(self) + test("default") do + expected_up_report = <<-REPORT +-- create_table(:posts, {:type=>"TABLE_NO_KEY"}) + -> 0.0s + REPORT + expected_down_report = <<-REPORT +-- remove_table(:posts) + -> 0.0s + REPORT + expected_dump = <<-DUMP.chomp +table_create posts TABLE_NO_KEY + DUMP + assert_migrate(expected_up_report, + expected_down_report, + expected_dump) do |migration| + migration.instance_eval do + create_table(:posts) end end - - up_output = StringIO.new - open_client do |client| - migration = migration_class.new(client) - migration.output = up_output - migration.up - end - assert_equal(expected_up_report, normalize_report(up_output.string)) - - assert_equal(expected_dump, dump) - - down_output = StringIO.new - open_client do |client| - migration = migration_class.new(client) - migration.output = down_output - migration.down - end - assert_equal(expected_down_report, normalize_report(down_output.string)) - - assert_equal("", dump) end - sub_test_case("#create_table") do + sub_test_case(":type => :patricia_trie") do test("default") do expected_up_report = <<-REPORT --- create_table(:posts, {:type=>"TABLE_NO_KEY"}) +-- create_table(:terms, {:type=>"TABLE_PAT_KEY", :key_type=>"ShortText"}) -> 0.0s REPORT expected_down_report = <<-REPORT --- remove_table(:posts) +-- remove_table(:terms) -> 0.0s REPORT expected_dump = <<-DUMP.chomp -table_create posts TABLE_NO_KEY +table_create terms TABLE_PAT_KEY ShortText DUMP assert_migrate(expected_up_report, expected_down_report, expected_dump) do |migration| migration.instance_eval do - create_table(:posts) + create_table(:terms, :type => :patricia_trie) end end end - sub_test_case(":type => :patricia_trie") do - test("default") do - expected_up_report = <<-REPORT --- create_table(:terms, {:type=>"TABLE_PAT_KEY", :key_type=>"ShortText"}) - -> 0.0s - REPORT - expected_down_report = <<-REPORT --- remove_table(:terms) - -> 0.0s - REPORT - expected_dump = <<-DUMP.chomp -table_create terms TABLE_PAT_KEY ShortText - DUMP - assert_migrate(expected_up_report, - expected_down_report, - expected_dump) do |migration| - migration.instance_eval do - create_table(:terms, :type => :patricia_trie) - end - end - end - - test("tokenizer") do - expected_up_report = <<-REPORT + test("tokenizer") do + expected_up_report = <<-REPORT -- create_table(:terms, {:type=>"TABLE_PAT_KEY", :key_type=>"ShortText", :tokenizer=>"TokenBigram"}) -> 0.0s - REPORT - expected_down_report = <<-REPORT + REPORT + expected_down_report = <<-REPORT -- remove_table(:terms) -> 0.0s - REPORT - expected_dump = <<-DUMP.chomp + REPORT + expected_dump = <<-DUMP.chomp table_create terms TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram DUMP - assert_migrate(expected_up_report, - expected_down_report, - expected_dump) do |migration| - migration.instance_eval do - create_table(:terms, - :type => :patricia_trie, - :tokenizer => :bigram) - end + assert_migrate(expected_up_report, + expected_down_report, + expected_dump) do |migration| + migration.instance_eval do + create_table(:terms, + :type => :patricia_trie, + :tokenizer => :bigram) end end + end - test("normalizer") do - expected_up_report = <<-REPORT + test("normalizer") do + expected_up_report = <<-REPORT -- create_table(:terms, {:type=>"TABLE_PAT_KEY", :key_type=>"ShortText", :normalizer=>"NormalizerAuto"}) -> 0.0s - REPORT - expected_down_report = <<-REPORT + REPORT + expected_down_report = <<-REPORT -- remove_table(:terms) -> 0.0s - REPORT - expected_dump = <<-DUMP.chomp + REPORT + expected_dump = <<-DUMP.chomp table_create terms TABLE_PAT_KEY ShortText --normalizer NormalizerAuto DUMP - assert_migrate(expected_up_report, - expected_down_report, - expected_dump) do |migration| - migration.instance_eval do - create_table(:terms, - :type => :patricia_trie, - :normalizer => :auto) - end + assert_migrate(expected_up_report, + expected_down_report, + expected_dump) do |migration| + migration.instance_eval do + create_table(:terms, + :type => :patricia_trie, + :normalizer => :auto) end end end + end - sub_test_case("propose") do - test("full_text_search") do - expected_up_report = <<-REPORT + sub_test_case("propose") do + test("full_text_search") do + expected_up_report = <<-REPORT -- create_table(:terms, {:type=>"TABLE_PAT_KEY", :key_type=>"ShortText", :tokenizer=>"TokenBigram", :normalizer=>"NormalizerAuto"}) -> 0.0s - REPORT - expected_down_report = <<-REPORT + REPORT + expected_down_report = <<-REPORT -- remove_table(:terms) -> 0.0s - REPORT - expected_dump = <<-DUMP.chomp + REPORT + expected_dump = <<-DUMP.chomp table_create terms TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto DUMP - assert_migrate(expected_up_report, - expected_down_report, - expected_dump) do |migration| - migration.instance_eval do - create_table(:terms, :propose => :full_text_search) - end + assert_migrate(expected_up_report, + expected_down_report, + expected_dump) do |migration| + migration.instance_eval do + create_table(:terms, :propose => :full_text_search) end end end + end - sub_test_case("columns") do - sub_test_case("#short_text") do - test("default") do - expected_up_report = <<-REPORT + sub_test_case("columns") do + sub_test_case("#short_text") do + test("default") do + expected_up_report = <<-REPORT -- create_table(:posts, {:type=>"TABLE_NO_KEY"}) -> 0.0s -- add_column(:posts, :title, {:flags=>["COLUMN_SCALAR"], :value_type=>"ShortText"}) -> 0.0s - REPORT - expected_down_report = <<-REPORT + REPORT + expected_down_report = <<-REPORT -- remove_table(:posts) -> 0.0s - REPORT - expected_dump = <<-DUMP.chomp + REPORT + expected_dump = <<-DUMP.chomp table_create posts TABLE_NO_KEY column_create posts title COLUMN_SCALAR ShortText - DUMP - assert_migrate(expected_up_report, - expected_down_report, - expected_dump) do |migration| - migration.instance_eval do - create_table(:posts) do |table| - table.short_text(:title) - end + DUMP + assert_migrate(expected_up_report, + expected_down_report, + expected_dump) do |migration| + migration.instance_eval do + create_table(:posts) do |table| + table.short_text(:title) end end end end + end - sub_test_case("#text") do - test("default") do - expected_up_report = <<-REPORT + sub_test_case("#text") do + test("default") do + expected_up_report = <<-REPORT -- create_table(:posts, {:type=>"TABLE_NO_KEY"}) -> 0.0s -- add_column(:posts, :content, {:flags=>["COLUMN_SCALAR"], :value_type=>"Text"}) -> 0.0s - REPORT - expected_down_report = <<-REPORT + REPORT + expected_down_report = <<-REPORT -- remove_table(:posts) -> 0.0s - REPORT - expected_dump = <<-DUMP.chomp + REPORT + expected_dump = <<-DUMP.chomp table_create posts TABLE_NO_KEY column_create posts content COLUMN_SCALAR Text - DUMP - assert_migrate(expected_up_report, - expected_down_report, - expected_dump) do |migration| - migration.instance_eval do - create_table(:posts) do |table| - table.text(:content) - end + DUMP + assert_migrate(expected_up_report, + expected_down_report, + expected_dump) do |migration| + migration.instance_eval do + create_table(:posts) do |table| + table.text(:content) end end end end + end - sub_test_case("#long_text") do - test("default") do - expected_up_report = <<-REPORT + sub_test_case("#long_text") do + test("default") do + expected_up_report = <<-REPORT -- create_table(:posts, {:type=>"TABLE_NO_KEY"}) -> 0.0s -- add_column(:posts, :content, {:flags=>["COLUMN_SCALAR"], :value_type=>"LongText"}) -> 0.0s - REPORT - expected_down_report = <<-REPORT + REPORT + expected_down_report = <<-REPORT -- remove_table(:posts) -> 0.0s - REPORT - expected_dump = <<-DUMP.chomp + REPORT + expected_dump = <<-DUMP.chomp table_create posts TABLE_NO_KEY column_create posts content COLUMN_SCALAR LongText - DUMP - assert_migrate(expected_up_report, - expected_down_report, - expected_dump) do |migration| - migration.instance_eval do - create_table(:posts) do |table| - table.long_text(:content) - end + DUMP + assert_migrate(expected_up_report, + expected_down_report, + expected_dump) do |migration| + migration.instance_eval do + create_table(:posts) do |table| + table.long_text(:content) end end end end + end - sub_test_case("#index") do - test("for full text search") do - expected_up_report = <<-REPORT + sub_test_case("#index") do + test("for full text search") do + expected_up_report = <<-REPORT -- create_table(:posts, {:type=>"TABLE_NO_KEY"}) -> 0.0s -- add_column(:posts, :content, {:flags=>["COLUMN_SCALAR"], :value_type=>"Text"}) @@ -275,35 +227,34 @@ column_create posts content COLUMN_SCALAR LongText -> 0.0s -- add_column(:terms, "posts_content", {:flags=>["COLUMN_INDEX", "WITH_POSITION"], :value_type=>:posts, :sources=>[:content]}) -> 0.0s - REPORT - expected_down_report = <<-REPORT + REPORT + expected_down_report = <<-REPORT -- remove_table(:terms) -> 0.0s -- remove_table(:posts) -> 0.0s - REPORT - expected_dump = <<-DUMP.chomp + REPORT + expected_dump = <<-DUMP.chomp table_create posts TABLE_NO_KEY column_create posts content COLUMN_SCALAR Text table_create terms TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto column_create terms posts_content COLUMN_INDEX|WITH_POSITION posts content - DUMP - assert_migrate(expected_up_report, - expected_down_report, - expected_dump) do |migration| - migration.instance_eval do - create_table(:posts) do |table| - table.text(:content) - end + DUMP + assert_migrate(expected_up_report, + expected_down_report, + expected_dump) do |migration| + migration.instance_eval do + create_table(:posts) do |table| + table.text(:content) + end - create_table(:terms, - :type => :patricia_trie, - :tokenizer => :bigram, - :normalizer => :auto) do |table| - table.index(:posts, :content) - end + create_table(:terms, + :type => :patricia_trie, + :tokenizer => :bigram, + :normalizer => :auto) do |table| + table.index(:posts, :content) end end end Modified: test/unit/run-test.rb (+1 -5) =================================================================== --- test/unit/run-test.rb 2017-03-03 21:06:46 +0900 (f37d9ff) +++ test/unit/run-test.rb 2017-03-03 21:26:25 +0900 (3b3cf59) @@ -27,11 +27,7 @@ test_dir = base_dir + "test/unit" $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" +require_relative "test_helper" Thread.abort_on_exception = true Added: test/unit/test_helper.rb (+83 -0) 100644 =================================================================== --- /dev/null +++ test/unit/test_helper.rb 2017-03-03 21:26:25 +0900 (4b17ff3) @@ -0,0 +1,83 @@ +# 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 "test-unit" + +require "groonga-client-model" +require "groonga_client_model/test_helper" +require "groonga_client_model/migration" + +GroongaClientModel::Client.url = "http://127.0.0.1:20041" + +module TestHelper + module Migration + def open_client(&block) + GroongaClientModel::Client.open(&block) + 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 + + def assert_migrate(expected_up_report, + expected_down_report, + expected_dump) + migration_class = Class.new(GroongaClientModel::Migration) do |klass| + define_method(:change) do + yield(self) + end + end + + up_output = StringIO.new + open_client do |client| + migration = migration_class.new(client) + migration.output = up_output + migration.up + end + assert_equal(expected_up_report, normalize_report(up_output.string)) + + assert_equal(expected_dump, dump) + + if expected_down_report + down_output = StringIO.new + open_client do |client| + migration = migration_class.new(client) + migration.output = down_output + migration.down + end + assert_equal(expected_down_report, normalize_report(down_output.string)) + assert_equal("", dump) + else + open_client do |client| + migration = migration_class.new(client) + assert_raise(GroongaClientModel::IrreversibleMigrationError) do + migration.down + end + end + assert_equal(expected_dump, dump) + end + + end + end +end -------------- next part -------------- HTML����������������������������... 下載