diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/test_journal.rb | 80 |
1 files changed, 73 insertions, 7 deletions
diff --git a/test/test_journal.rb b/test/test_journal.rb index 1e55f54b..2a522aa7 100644 --- a/test/test_journal.rb +++ b/test/test_journal.rb @@ -5,6 +5,8 @@ require 'rbot/ircbot' require 'rbot/journal' require 'rbot/journal/postgres.rb' +DAY=60*60*24 + class JournalMessageTest < Test::Unit::TestCase include Irc::Bot::Journal @@ -100,8 +102,6 @@ class QueryTest < Test::Unit::TestCase assert_false(q.topic_matches?('baz.alice.bob..foo')) end - - DAY=60*60*24 def test_matches q = Query.define do #id 'foo', 'bar' @@ -186,14 +186,80 @@ class JournalBrokerTest < Test::Unit::TestCase end -class JournalStorageTest < Test::Unit::TestCase +class JournalStoragePostgresTest < Test::Unit::TestCase include Irc::Bot::Journal - def test_storage - s = Storage::PostgresStorage.new( - uri: 'postgresql://apoc:seed42@localhost/rbot_journal') - assert_equal(true, true) + def setup + @storage = Storage::PostgresStorage.new( + uri: ENV['DB_URI'] || 'postgresql://localhost/rbot_journal', + drop: true) + end + + def teardown + @storage.drop + end + + def test_query_to_sql + q = Query.define do + id 'foo' + id 'bar', 'baz' + topic 'log.irc.*' + topic 'log.core', 'baz' + timestamp from: Time.now, to: Time.now + 60 * 10 + payload 'action': :privmsg, 'alice': 'bob' + payload 'channel': '#rbot' + payload 'foo.bar': 'baz' + end + sql = @storage.query_to_sql(q) + assert_equal("(id = $1 OR id = $2 OR id = $3) AND (topic ILIKE $4 OR topic ILIKE $5 OR topic ILIKE $6) AND (timestamp >= $7 AND timestamp <= $8) AND (payload->>'action' = $9 OR payload->>'alice' = $10 OR payload->>'channel' = $11 OR payload->'foo'->>'bar' = $12)", sql[0]) + q = Query.define do + id 'foo' + end + assert_equal('(id = $1)', @storage.query_to_sql(q)[0]) + q = Query.define do + topic 'foo.*.bar' + end + assert_equal('(topic ILIKE $1)', @storage.query_to_sql(q)[0]) + assert_equal(['foo.%.bar'], @storage.query_to_sql(q)[1]) + end + + def test_insert + # the test message to persist + m = JournalMessage.create('log.core', {foo: {bar: 'baz'}}) + # insert the test message: + @storage.insert(m) + + # find the test message by query: + q = Query.define do + topic 'log.core' + end + res = @storage.find(q) + _m = res.first + assert_equal(m, _m) # this only checks id + assert_equal(m.timestamp.strftime('%Y-%m-%d %H:%M:%S%z'), + _m.timestamp.strftime('%Y-%m-%d %H:%M:%S%z')) + assert_equal('log.core', _m.topic) + assert_equal({'foo' => {'bar' => 'baz'}}, _m.payload) + assert_equal(1, @storage.count(q)) + end + + def test_query_range + timestamp = Time.now - DAY*7 + m = JournalMessage.create('log.core', {foo: {bar: 'baz'}}, + timestamp: timestamp) + assert_equal(timestamp, m.timestamp) + + @storage.insert(m) + @storage.insert(JournalMessage.create('a.foo', {})) + @storage.insert(JournalMessage.create('b.bar', {})) + @storage.insert(JournalMessage.create('b.baz', {})) + + r = @storage.find(Query.define { timestamp(from: timestamp-DAY, to: timestamp+DAY) }) + + assert_equal(1, r.length) + assert_equal(m, r.first) + end end |