]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - test/test_journal.rb
chucknorris: fix loading
[user/henk/code/ruby/rbot.git] / test / test_journal.rb
index b9f5c61219009f28d4ee4643f20263d99fa7b90a..73dbcf42e4ecd80b61933e693e3a537bbb82dbc5 100644 (file)
@@ -1,10 +1,17 @@
 $:.unshift File.join(File.dirname(__FILE__), '../lib')
 
+module Irc
+class Bot
+  module Config
+    @@datadir = File.expand_path(File.dirname($0) + '/../data/rbot')
+    @@coredir = File.expand_path(File.dirname($0) + '/../lib/rbot/core') 
+  end
+end
+end
+
 require 'test/unit'
 require 'rbot/ircbot'
 require 'rbot/journal'
-require 'rbot/journal/postgres.rb'
-require 'rbot/journal/mongo.rb'
 
 require 'benchmark'
 
@@ -22,7 +29,9 @@ class JournalMessageTest < Test::Unit::TestCase
     end
     assert_nil(m.get('nope', nil))
     assert_nil(m.get('baz'))
-    assert_equal(23, m.get('qux.quxx'))
+    assert_equal(23, m['qux.quxx'])
+    assert_equal(nil, m['qux.nope'])
+    assert_raise(ArgumentError) { m.get('qux.nope') }
   end
 
 end
@@ -163,8 +172,8 @@ class JournalBrokerTest < Test::Unit::TestCase
     received = []
     journal = JournalBroker.new
 
-    # subscribe to messages:
-    sub = journal.subscribe(Query.define { topic 'foo' }) do |message|
+    # subscribe to messages for topic foo:
+    sub = journal.subscribe('foo') do |message|
       received << message
     end
 
@@ -236,6 +245,31 @@ module JournalStorageTestMixin
     assert_equal(m, @storage.find.first)
   end
 
+  def test_find
+    # tests limit/offset and block parameters of find()
+    @storage.insert(JournalMessage.create('irclogs', {message: 'foo'}))
+    @storage.insert(JournalMessage.create('irclogs', {message: 'bar'}))
+    @storage.insert(JournalMessage.create('irclogs', {message: 'baz'}))
+    @storage.insert(JournalMessage.create('irclogs', {message: 'qux'}))
+
+    msgs = []
+    @storage.find(Query.define({topic: 'irclogs'}), 2, 1) do |m|
+      msgs << m
+    end
+    assert_equal(2, msgs.length)
+    assert_equal('bar', msgs.first['message'])
+    assert_equal('baz', msgs.last['message'])
+
+    msgs = []
+    @storage.find(Query.define({topic: 'irclogs'})) do |m|
+      msgs << m
+    end
+    assert_equal(4, msgs.length)
+    assert_equal('foo', msgs.first['message'])
+    assert_equal('qux', msgs.last['message'])
+
+  end
+
   def test_operations_multiple
     # test operations on multiple messages
     # insert a bunch:
@@ -269,15 +303,29 @@ module JournalStorageTestMixin
     assert_equal(0, @storage.count)
   end
 
-  def test_journal
-    # this journal persists messages in the test storage:
-    journal = JournalBroker.new(storage: @storage)
-    journal.publish 'log.irc', action: 'message'
+  def test_broker_interface
+    journal = JournalBroker.new(storage: @storage) 
+
+    journal.publish 'irclogs', message: 'foo'
+    journal.publish 'irclogs', message: 'bar'
+    journal.publish 'irclogs', message: 'baz'
+    journal.publish 'irclogs', message: 'qux'
+
+    # wait for messages to be consumed:
     sleep 0.1
-    assert_equal(1, journal.count)
+
+    msgs = []
+    journal.find({topic: 'irclogs'}, 2, 1) do |m|
+      msgs << m
+    end
+    assert_equal(2, msgs.length)
+    assert_equal('bar', msgs.first['message'])
+    assert_equal('baz', msgs.last['message'])
+
+    journal.ensure_payload_index('foo.bar.baz')
   end
 
-  NUM=150_000
+  NUM=100 # 1_000_000
   def test_benchmark
     puts
 
@@ -323,50 +371,63 @@ module JournalStorageTestMixin
 
 end
 
-class JournalStoragePostgresTest < Test::Unit::TestCase
+begin
+  require 'rbot/journal/postgres.rb'
+  if ENV['PG_URI']
+  class JournalStoragePostgresTest < Test::Unit::TestCase
 
-  include JournalStorageTestMixin
+    include JournalStorageTestMixin
 
-  def setup
-    @storage = Storage::PostgresStorage.new(
-      uri: ENV['DB_URI'] || 'postgresql://localhost/rbot_journal',
-      drop: true)
-  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'
+    def setup
+      @storage = Storage::PostgresStorage.new(
+        uri: ENV['PG_URI'] || 'postgresql://localhost/rbot_journal',
+        drop: true)
     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'
+
+    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
-    assert_equal('(topic ILIKE $1)', @storage.query_to_sql(q)[0])
-    assert_equal(['foo.%.bar'], @storage.query_to_sql(q)[1])
-  end
 
-end
+  end
+  else
+    puts 'NOTE: Set PG_URI environment variable to test postgresql storage.'
+  end
+rescue Exception; end
 
-class JournalStorageMongoTest < Test::Unit::TestCase
+begin
+  require 'rbot/journal/mongo.rb'
+  if ENV['MONGO_URI']
+  class JournalStorageMongoTest < Test::Unit::TestCase
 
-  include JournalStorageTestMixin
+    include JournalStorageTestMixin
 
-  def setup
-    @storage = Storage::MongoStorage.new(
-      drop: true)
+    def setup
+      @storage = Storage::MongoStorage.new(
+        uri: ENV['MONGO_URI'] || 'mongodb://127.0.0.1:27017/rbot',
+        drop: true)
+    end
   end
-
-end
-
+  else
+    puts 'NOTE: Set MONGO_URI environment variable to test postgresql storage.'
+  end
+rescue Exception; end