Skip to content

Commit a9b6aa6

Browse files
committed
Break out precision timestamp test into its own separate test function
Signed-off-by: Eric Herot <[email protected]>
1 parent f67a848 commit a9b6aa6

File tree

3 files changed

+123
-3
lines changed

3 files changed

+123
-3
lines changed

test/fixtures/schema.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
t.string "message"
2020
t.datetime "created_at", null: false
2121
t.datetime "updated_at", null: false
22+
end
23+
24+
create_table "messages_custom_time", force: :cascade do |t|
25+
t.string "message"
26+
t.datetime "created_at", null: false
27+
t.datetime "updated_at", null: false
2228
t.string "custom_time"
2329
end
2430
end

test/plugin/test_in_sql.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ def test_message
8282
[d.events[2][1], "message 3"],
8383
]
8484
actual = [
85-
[Time.parse(d.events[0][2]["updated_at"]).to_i, d.events[0][2]["message"]],
86-
[Time.parse(d.events[1][2]["updated_at"]).to_i, d.events[1][2]["message"]],
87-
[Time.parse(d.events[2][2]["updated_at"]).to_i, d.events[2][2]["message"]],
85+
[Fluent::EventTime.parse(d.events[0][2]["updated_at"]), d.events[0][2]["message"]],
86+
[Fluent::EventTime.parse(d.events[1][2]["updated_at"]), d.events[1][2]["message"]],
87+
[Fluent::EventTime.parse(d.events[2][2]["updated_at"]), d.events[2][2]["message"]],
8888
]
8989
assert_equal(expected, actual)
9090
end
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
require "helper"
2+
require "fluent/test/driver/input"
3+
4+
class SqlInputCustomTimeTest < Test::Unit::TestCase
5+
def setup
6+
Fluent::Test.setup
7+
end
8+
9+
def teardown
10+
end
11+
12+
CONFIG = %[
13+
adapter postgresql
14+
host localhost
15+
port 5432
16+
database fluentd_test
17+
18+
username fluentd
19+
password fluentd
20+
21+
schema_search_path public
22+
23+
tag_prefix db
24+
25+
<table>
26+
table messages_custom_time
27+
tag logs
28+
update_column updated_at
29+
time_column custom_time
30+
</table>
31+
]
32+
33+
def create_driver(conf = CONFIG)
34+
Fluent::Test::Driver::Input.new(Fluent::Plugin::SQLInput).configure(conf)
35+
end
36+
37+
def test_configure
38+
d = create_driver
39+
expected = {
40+
host: "localhost",
41+
port: 5432,
42+
adapter: "postgresql",
43+
database: "fluentd_test",
44+
username: "fluentd",
45+
password: "fluentd",
46+
schema_search_path: "public",
47+
tag_prefix: "db"
48+
}
49+
actual = {
50+
host: d.instance.host,
51+
port: d.instance.port,
52+
adapter: d.instance.adapter,
53+
database: d.instance.database,
54+
username: d.instance.username,
55+
password: d.instance.password,
56+
schema_search_path: d.instance.schema_search_path,
57+
tag_prefix: d.instance.tag_prefix
58+
}
59+
assert_equal(expected, actual)
60+
tables = d.instance.instance_variable_get(:@tables)
61+
assert_equal(1, tables.size)
62+
messages_custom_time = tables.first
63+
assert_equal("messages_custom_time", messages_custom_time.table)
64+
assert_equal("logs", messages_custom_time.tag)
65+
end
66+
67+
def test_message
68+
d = create_driver(CONFIG + "select_interval 1")
69+
70+
start_time = Fluent::EventTime.now
71+
72+
# Create one message with a valid timestamp containing milliseconds and a time zone
73+
Message.create!(message: "message 1", custom_time: '2020-08-27 15:00:16.100758000 -0400')
74+
75+
# Create one message without a timestamp so that we can test auto-creation
76+
Message.create!(message: "message 2 (no timestamp)", custom_time: nil)
77+
78+
# Create one message with an unparseable timestamp so that we can check that a valid
79+
# one is auto-generated.
80+
Message.create!(message: "message 3 (bad timestamp)", custom_time: 'foo')
81+
82+
d.end_if do
83+
d.record_count >= 3
84+
end
85+
d.run(timeout: 5)
86+
87+
assert_equal("db.logs", d.events[0][0])
88+
expected = [
89+
[d.events[0][1], "message 1"],
90+
[d.events[1][1], "message 2 (no timestamp)"],
91+
[d.events[2][1], "message 3 (bad timestamp)"],
92+
]
93+
94+
actual = [
95+
[Fluent::EventTime.parse(d.events[0][2]["custom_time"]), d.events[0][2]["message"]],
96+
d.events[1][2]["message"],
97+
d.events[2][2]["message"],
98+
]
99+
100+
assert_equal(expected[0], actual[0])
101+
102+
# Messages 2 and 3 should have the same messages but (usually) a slightly later
103+
# timestamps because they are generated by the input plugin instead of the test
104+
# code
105+
[1,2].each do |i|
106+
assert_equal(expected[i][1], actual[i])
107+
assert_operator(expected[i][0], :>=, start_time)
108+
end
109+
end
110+
111+
class Message < ActiveRecord::Base
112+
self.table_name = "messages_custom_time"
113+
end
114+
end

0 commit comments

Comments
 (0)