1 $:.unshift File.join(File.dirname(__FILE__), '../lib')
7 class JournalMessageTest < Test::Unit::TestCase
9 include Irc::Bot::Journal
12 m = JournalMessage.create('foo', {'bar': 42, 'baz': nil, 'qux': {'quxx': 23}})
13 assert_equal(42, m.get('bar'))
14 assert_raise ArgumentError do
17 assert_nil(m.get('nope', nil))
18 assert_nil(m.get('baz'))
19 assert_equal(23, m.get('qux.quxx'))
24 class QueryTest < Test::Unit::TestCase
26 include Irc::Bot::Journal
34 topic 'log.core', 'baz'
35 timestamp from: Time.now, to: Time.now + 60 * 10
36 payload 'action': :privmsg, 'alice': 'bob'
37 payload 'channel': '#rbot'
38 payload 'foo.bar': 'baz'
40 assert_equal(['foo', 'bar', 'baz'], q.id)
41 assert_equal(['log.irc.*', 'log.core', 'baz'], q.topic)
42 assert_equal([:from, :to], q.timestamp.keys)
43 assert_equal(Time, q.timestamp[:to].class)
44 assert_equal(Time, q.timestamp[:from].class)
46 'action': :privmsg, 'alice': 'bob',
53 def test_topic_matches
57 assert_true(q.topic_matches?('foo'))
58 assert_false(q.topic_matches?('bar'))
59 assert_false(q.topic_matches?('foo.bar'))
64 assert_false(q.topic_matches?('foo'))
65 assert_false(q.topic_matches?('bar'))
66 assert_true(q.topic_matches?('foo.bar'))
71 assert_false(q.topic_matches?('foo'))
72 assert_false(q.topic_matches?('bar'))
73 assert_true(q.topic_matches?('foo.bar'))
74 assert_true(q.topic_matches?('foo.baz'))
79 assert_false(q.topic_matches?('foo'))
80 assert_false(q.topic_matches?('bar'))
81 assert_true(q.topic_matches?('foo.bar'))
82 assert_true(q.topic_matches?('bar.bar'))
83 assert_false(q.topic_matches?('foo.foo'))
88 assert_false(q.topic_matches?('foo'))
89 assert_true(q.topic_matches?('foo.bar'))
94 topic 'baz.alice.bob.*.foo'
96 assert_true(q.topic_matches?('foo'))
97 assert_true(q.topic_matches?('bar'))
98 assert_true(q.topic_matches?('baz.alice.bob.asdf.foo'))
99 assert_false(q.topic_matches?('baz.alice.bob..foo'))
107 topic 'log.irc.*', 'log.core'
108 timestamp from: Time.now - DAY, to: Time.now + DAY
109 payload 'action': 'privmsg', 'foo.bar': 'baz'
111 assert_true(q.matches? JournalMessage.create('log.irc.raw', {'action' => 'privmsg'}))
112 assert_false(q.matches? JournalMessage.create('baz', {}))
113 assert_true(q.matches? JournalMessage.create('log.core', {foo: {bar: 'baz'}}))
115 # tests timestamp from/to:
116 assert_true(q.matches? JournalMessage.new(
120 payload: {action: 'privmsg'}))
121 assert_false(q.matches? JournalMessage.new(
124 timestamp: Time.now - DAY*3,
125 payload: {action: 'privmsg'}))
126 assert_false(q.matches? JournalMessage.new(
129 timestamp: Time.now + DAY*3,
130 payload: {action: 'privmsg'}))
135 class JournalBrokerTest < Test::Unit::TestCase
137 include Irc::Bot::Journal
141 journal = JournalBroker.new(consumer: Proc.new { |message|
145 # publish some messages:
146 journal.publish 'log.irc',
147 source: 'alice', message: '<3 pg'
148 journal.publish 'log.irc',
149 source: 'bob', message: 'mysql > pg'
150 journal.publish 'log.irc',
151 source: 'alice', target: 'bob', action: :kick
153 # wait for messages to be consumed:
155 assert_equal(3, received.length)
160 journal = JournalBroker.new
162 # subscribe to messages:
163 journal.subscribe(Query.define { topic 'foo' }) do |message|
167 # publish some messages:
168 journal.publish 'foo', {}
169 journal.publish 'bar', {}
170 journal.publish 'foo', {}
172 # wait for messages to be consumed:
174 assert_equal(2, received.length)