/
test-analyzer.rb
106 lines (91 loc) · 3.41 KB
/
test-analyzer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# -*- coding: utf-8 -*-
#
# Copyright (C) 2014 Kouhei Sutou <kou@clear-code.com>
# Copyright (C) 2012 Haruka Yoshihara <yoshihara@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 "tempfile"
require "pathname"
require "groonga/query-log/analyzer"
class AnalyzerTest < Test::Unit::TestCase
setup
def setup_fixtures
@fixtures_path = File.join(File.dirname(__FILE__), "fixtures")
@query_log_path = File.join(@fixtures_path, "query.log")
end
def setup
@analyzer = Groonga::QueryLog::Analyzer.new
end
class TestInputFile < self
def test_multi
other_query_log_path = File.join(@fixtures_path, "other-query.log")
actual_result = run_analyzer(@query_log_path, other_query_log_path)
expected_result_path = File.join(@fixtures_path, "multi.expected")
assert_equal(File.read(expected_result_path), actual_result)
end
def test_no_specified
assert_raise(Groonga::QueryLog::Analyzer::NoInputError) do
run_analyzer
end
end
end
data("console" => "console",
"HTML" => "html",
"JSON" => "json",
"JSON stream" => "json-stream")
def test_reporter(reporter)
actual_result = run_analyzer("--reporter", reporter, @query_log_path)
case reporter
when "json", "json-stream"
actual_result = normalize_json(actual_result)
end
expected_result = expected_analyzed_query("reporter/#{reporter}.expected")
assert_equal(expected_result, actual_result)
end
def test_n_entries
actual_result = run_analyzer("--n-entries=1", @query_log_path)
expected_result = expected_analyzed_query("n_entries.expected")
assert_equal(expected_result, actual_result)
end
data(:asc_elapsed => "elapsed",
:asc_start_time => "start-time",
:desc_elapsed => "-elapsed",
:desc_start_time => "-start-time")
def test_order(order)
actual_result = run_analyzer("--order=#{order}", @query_log_path)
expected_result = expected_analyzed_query("order/#{order}.expected")
assert_equal(expected_result, actual_result)
end
def test_no_report_summary
actual_result = run_analyzer("--no-report-summary", @query_log_path)
expected_result = expected_analyzed_query("no-report-summary.expected")
assert_equal(expected_result, actual_result)
end
private
def run_analyzer(*arguments)
Tempfile.open("output.actual") do |output|
arguments << "--output" << output.path
@analyzer.run(arguments)
File.read(output.path)
end
end
def normalize_json(json)
json = json.gsub(/(\"start_time\"):\d+/, "\\1:START_TIME")
json.gsub(/(\"last_time\"):\d+/, "\\1:LAST_TIME")
end
def expected_analyzed_query(file_name)
File.read(File.join(@fixtures_path, file_name))
end
end