Skip to content

Commit

Permalink
Merge pull request #4082 from daipom/use-driver-for-formatter-tests
Browse files Browse the repository at this point in the history
Use driver for formatter tests
  • Loading branch information
ashie authored Mar 7, 2023
2 parents c0125d8 + f35ab99 commit 248160f
Showing 1 changed file with 46 additions and 47 deletions.
93 changes: 46 additions & 47 deletions test/test_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ def record
{'message' => 'awesome', 'greeting' => 'hello'}
end

def config_element_in_params(params)
config_element('test', '', params)
end

class BaseFormatterTest < ::Test::Unit::TestCase
include FormatterTest

Expand Down Expand Up @@ -134,12 +130,12 @@ class MessagePackFormatterTest < ::Test::Unit::TestCase
include FormatterTest

def setup
@formatter = TextFormatter::MessagePackFormatter.new
@formatter = Fluent::Test::FormatterTestDriver.new(TextFormatter::MessagePackFormatter)
@time = Engine.now
end

def test_format
@formatter.configure(config_element())
@formatter.configure({})
formatted = @formatter.format(tag, @time, record)

assert_equal(record.to_msgpack, formatted)
Expand All @@ -150,7 +146,7 @@ class LabeledTSVFormatterTest < ::Test::Unit::TestCase
include FormatterTest

def setup
@formatter = TextFormatter::LabeledTSVFormatter.new
@formatter = Fluent::Test::FormatterTestDriver.new(TextFormatter::LabeledTSVFormatter)
@time = Engine.now
@newline = if Fluent.windows?
"\r\n"
Expand All @@ -160,30 +156,30 @@ def setup
end

def test_config_params
assert_equal "\t", @formatter.delimiter
assert_equal ":", @formatter.label_delimiter
assert_equal "\t", @formatter.instance.delimiter
assert_equal ":", @formatter.instance.label_delimiter

@formatter.configure(config_element_in_params(
@formatter.configure(
'delimiter' => ',',
'label_delimiter' => '=',
))
)

assert_equal ",", @formatter.delimiter
assert_equal "=", @formatter.label_delimiter
assert_equal ",", @formatter.instance.delimiter
assert_equal "=", @formatter.instance.label_delimiter
end

def test_format
@formatter.configure(config_element())
@formatter.configure({})
formatted = @formatter.format(tag, @time, record)

assert_equal("message:awesome\tgreeting:hello#{@newline}", formatted)
end

def test_format_with_customized_delimiters
@formatter.configure(config_element_in_params(
@formatter.configure(
'delimiter' => ',',
'label_delimiter' => '=',
))
)
formatted = @formatter.format(tag, @time, record)

assert_equal("message=awesome,greeting=hello#{@newline}", formatted)
Expand All @@ -194,26 +190,26 @@ def record_with_tab
end

def test_format_suppresses_tab
@formatter.configure(config_element())
@formatter.configure({})
formatted = @formatter.format(tag, @time, record_with_tab)

assert_equal("message:awe some\tgreeting:hello #{@newline}", formatted)
end

def test_format_suppresses_tab_custom_replacement
@formatter.configure(config_element_in_params(
@formatter.configure(
'replacement' => 'X',
))
)
formatted = @formatter.format(tag, @time, record_with_tab)

assert_equal("message:aweXsome\tgreeting:helloX#{@newline}", formatted)
end

def test_format_suppresses_custom_delimiter
@formatter.configure(config_element_in_params(
@formatter.configure(
'delimiter' => 'w',
'label_delimiter' => '=',
))
)
formatted = @formatter.format(tag, @time, record)

assert_equal("message=a esomewgreeting=hello#{@newline}", formatted)
Expand All @@ -224,14 +220,14 @@ class CsvFormatterTest < ::Test::Unit::TestCase
include FormatterTest

def setup
@formatter = TextFormatter::CsvFormatter.new
@formatter = Fluent::Test::FormatterTestDriver.new(TextFormatter::CsvFormatter)
@time = Engine.now
end

def test_config_params
assert_equal ',', @formatter.delimiter
assert_equal true, @formatter.force_quotes
assert_nil @formatter.fields
assert_equal ',', @formatter.instance.delimiter
assert_equal true, @formatter.instance.force_quotes
assert_nil @formatter.instance.fields
end

data(
Expand All @@ -240,13 +236,13 @@ def test_config_params
'pipe' => ['|', '|'])
def test_config_params_with_customized_delimiters(data)
expected, target = data
@formatter.configure(config_element_in_params('delimiter' => target, 'fields' => 'a,b,c'))
assert_equal expected, @formatter.delimiter
assert_equal ['a', 'b', 'c'], @formatter.fields
@formatter.configure('delimiter' => target, 'fields' => 'a,b,c')
assert_equal expected, @formatter.instance.delimiter
assert_equal ['a', 'b', 'c'], @formatter.instance.fields
end

def test_format
@formatter.configure(config_element_in_params('fields' => 'message,message2'))
@formatter.configure('fields' => 'message,message2')
formatted = @formatter.format(tag, @time, {
'message' => 'awesome',
'message2' => 'awesome2'
Expand All @@ -255,10 +251,10 @@ def test_format
end

def test_format_with_customized_delimiters
@formatter.configure(config_element_in_params(
@formatter.configure(
'fields' => 'message,message2',
'delimiter' => '\t'
))
)
formatted = @formatter.format(tag, @time, {
'message' => 'awesome',
'message2' => 'awesome2'
Expand All @@ -267,10 +263,10 @@ def test_format_with_customized_delimiters
end

def test_format_with_non_quote
@formatter.configure(config_element_in_params(
@formatter.configure(
'fields' => 'message,message2',
'force_quotes' => 'false'
))
)
formatted = @formatter.format(tag, @time, {
'message' => 'awesome',
'message2' => 'awesome2'
Expand All @@ -290,9 +286,9 @@ def test_format_with_non_quote
'message3' => 'awesome3'
})
def test_format_with_empty_fields(data)
@formatter.configure(config_element_in_params(
@formatter.configure(
'fields' => 'message,message2,message3'
))
)
formatted = @formatter.format(tag, @time, data)
assert_equal("\"awesome\",\"\",\"awesome3\"\n", formatted)
end
Expand All @@ -302,8 +298,8 @@ def test_format_with_empty_fields(data)
'white_space' => 'one , two , three',
'blank' => 'one,,two,three')
def test_config_params_with_fields(data)
@formatter.configure(config_element_in_params('fields' => data))
assert_equal %w(one two three), @formatter.fields
@formatter.configure('fields' => data)
assert_equal %w(one two three), @formatter.instance.fields
end
end

Expand All @@ -317,32 +313,35 @@ def setup
end
end

def create_driver(klass_or_str)
Fluent::Test::FormatterTestDriver.new(klass_or_str)
end

def test_config_params
formatter = TextFormatter::SingleValueFormatter.new
assert_equal "message", formatter.message_key
formatter = create_driver(TextFormatter::SingleValueFormatter)
assert_equal "message", formatter.instance.message_key

formatter.configure(config_element_in_params('message_key' => 'foobar'))
assert_equal "foobar", formatter.message_key
formatter.configure('message_key' => 'foobar')
assert_equal "foobar", formatter.instance.message_key
end

def test_format
formatter = Fluent::Plugin.new_formatter('single_value')
formatter.configure(config_element())
formatter = create_driver('single_value')
formatter.configure({})
formatted = formatter.format('tag', Engine.now, {'message' => 'awesome'})
assert_equal("awesome#{@newline}", formatted)
end

def test_format_without_newline
formatter = Fluent::Plugin.new_formatter('single_value')
formatter.configure(config_element_in_params('add_newline' => 'false'))
formatter = create_driver('single_value')
formatter.configure('add_newline' => 'false')
formatted = formatter.format('tag', Engine.now, {'message' => 'awesome'})
assert_equal("awesome", formatted)
end

def test_format_with_message_key
formatter = TextFormatter::SingleValueFormatter.new
formatter.configure(config_element_in_params('message_key' => 'foobar'))
formatter = create_driver(TextFormatter::SingleValueFormatter)
formatter.configure('message_key' => 'foobar')
formatted = formatter.format('tag', Engine.now, {'foobar' => 'foo'})

assert_equal("foo#{@newline}", formatted)
Expand Down

0 comments on commit 248160f

Please sign in to comment.