]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - test/test_journal.rb
journal: more postgres tests
[user/henk/code/ruby/rbot.git] / test / test_journal.rb
index 1e55f54b280fc7f36d8958102954b9322912694d..3f1ce7667a5020538793277f612e52f68c32126b 100644 (file)
@@ -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,107 @@ 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_operations
+    # insertion
+    m = JournalMessage.create('log.core', {foo: {bar: 'baz'}})
+    @storage.insert(m)
+
+    # query by id
+    res = @storage.find(Query.define { id m.id })
+    assert_equal(1, res.length)
+    assert_equal(m, res.first)
+
+    # check timestamp was returned correctly:
+    assert_equal(m.timestamp.strftime('%Y-%m-%d %H:%M:%S%z'),
+                 res.first.timestamp.strftime('%Y-%m-%d %H:%M:%S%z'))
+
+    # check if payload was returned correctly:
+    assert_equal({'foo' => {'bar' => 'baz'}}, res.first.payload)
+
+    # query by topic
+    assert_equal(m, @storage.find(Query.define { topic('log.core') }).first)
+    assert_equal(m, @storage.find(Query.define { topic('log.*') }).first)
+    assert_equal(m, @storage.find(Query.define { topic('*.*') }).first)
+
+    # query by timestamp range
+    assert_equal(1, @storage.find(Query.define {
+      timestamp(from: Time.now-DAY, to: Time.now+DAY) }).length)
+    assert_equal(0, @storage.find(Query.define {
+      timestamp(from: Time.now-DAY*2, to: Time.now-DAY) }).length)
+
+    # query by payload
+    res = @storage.find(Query.define { payload('foo.bar' => 'baz') })
+    assert_equal(m, res.first)
+    res = @storage.find(Query.define { payload('foo.bar' => 'x') })
+    assert_true(res.empty?)
+
+    # without arguments: find and count
+    assert_equal(1, @storage.count)
+    assert_equal(m, @storage.find.first)
+  end
+
+  def test_operations_multiple
+    # test operations on multiple messages
+    # insert a bunch:
+    @storage.insert(JournalMessage.create('test.topic', {name: 'one'}))
+    @storage.insert(JournalMessage.create('test.topic', {name: 'two'}))
+    @storage.insert(JournalMessage.create('test.topic', {name: 'three'}))
+    @storage.insert(JournalMessage.create('archived.topic', {name: 'four'},
+                                          timestamp: Time.now - DAY*100))
+    @storage.insert(JournalMessage.create('complex', {name: 'five', country: {
+      name: 'Italy'
+    }}))
+    @storage.insert(JournalMessage.create('complex', {name: 'six', country: {
+      name: 'Austria'
+    }}))
+
+
+  end
+
+  def test_journal
+    received = []
+    # this journal persists messages in the test storage:
+    journal = JournalBroker.new(storage: @storage)
+
+
   end
 
 end