Skip to content

Commit

Permalink
Add some column migration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Mar 3, 2017
1 parent e0847d4 commit dd0597d
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 15 deletions.
4 changes: 4 additions & 0 deletions lib/groonga_client_model/migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,10 @@ def short_text(column_name, options={})
def text(column_name, options={})
@migration.add_column(@table_name, column_name, :text, options)
end

def long_text(column_name, options={})
@migration.add_column(@table_name, column_name, :long_text, options)
end
end
end
end
137 changes: 122 additions & 15 deletions test/unit/test_migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,45 +25,152 @@ def open_client(&block)
GroongaClientModel::Client.open(&block)
end

def migrate(direction)
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
output = StringIO.new

up_output = StringIO.new
open_client do |client|
migration = migration_class.new(client)
migration.output = output
migration.output = up_output
migration.up
end
normalize_report(output.string)
end
assert_equal(expected_up_report, normalize_report(up_output.string))

def normalize_report(report)
report.gsub(/[0-9]+\.[0-9]+s/, "0.0s")
end
assert_equal(expected_dump, dump)

def dump
down_output = StringIO.new
open_client do |client|
client.dump.body
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
test("default") do
report = migrate(:up) do |migration|
expected_up_report = <<-REPORT
-- create_table(:posts, {:type=>"TABLE_NO_KEY", :key_type=>nil})
-> 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
assert_equal(<<-REPORT, report)
end

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", :key_type=>nil})
-> 0.0s
REPORT
assert_equal(<<-DUMP.chomp, dump)
-- add_column(:posts, :title, {:flags=>["COLUMN_SCALAR"], :value_type=>"ShortText"})
-> 0.0s
REPORT
expected_down_report = <<-REPORT
-- remove_table(:posts)
-> 0.0s
REPORT
expected_dump = <<-DUMP.chomp
table_create posts TABLE_NO_KEY
DUMP
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
end
end
end
end

sub_test_case("#text") do
test("default") do
expected_up_report = <<-REPORT
-- create_table(:posts, {:type=>"TABLE_NO_KEY", :key_type=>nil})
-> 0.0s
-- add_column(:posts, :content, {:flags=>["COLUMN_SCALAR"], :value_type=>"Text"})
-> 0.0s
REPORT
expected_down_report = <<-REPORT
-- remove_table(:posts)
-> 0.0s
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
end
end
end
end

sub_test_case("#long_text") do
test("default") do
expected_up_report = <<-REPORT
-- create_table(:posts, {:type=>"TABLE_NO_KEY", :key_type=>nil})
-> 0.0s
-- add_column(:posts, :content, {:flags=>["COLUMN_SCALAR"], :value_type=>"LongText"})
-> 0.0s
REPORT
expected_down_report = <<-REPORT
-- remove_table(:posts)
-> 0.0s
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
end
end
end
end
end
end
end

0 comments on commit dd0597d

Please sign in to comment.