X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=test%2Ftest_journal.rb;h=f1653c164783e35eb3830228aac58531403f337e;hb=c4e7896a87d97988926d3b57f62599384c5c7189;hp=b9f5c61219009f28d4ee4643f20263d99fa7b90a;hpb=257ba78e8564964b78f839cce45350b824c2f410;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/test/test_journal.rb b/test/test_journal.rb index b9f5c612..f1653c16 100644 --- a/test/test_journal.rb +++ b/test/test_journal.rb @@ -22,7 +22,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 +165,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 +238,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 +296,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,13 +364,14 @@ module JournalStorageTestMixin end +if ENV['PG_URI'] class JournalStoragePostgresTest < Test::Unit::TestCase include JournalStorageTestMixin def setup @storage = Storage::PostgresStorage.new( - uri: ENV['DB_URI'] || 'postgresql://localhost/rbot_journal', + uri: ENV['PG_URI'] || 'postgresql://localhost/rbot_journal', drop: true) end @@ -358,15 +400,22 @@ class JournalStoragePostgresTest < Test::Unit::TestCase end end +else + puts 'NOTE: Set PG_URI environment variable to test postgresql storage.' +end +if ENV['MONGO_URI'] class JournalStorageMongoTest < Test::Unit::TestCase include JournalStorageTestMixin def setup @storage = Storage::MongoStorage.new( + uri: ENV['MONGO_URI'] || 'mongodb://127.0.0.1:27017/rbot', drop: true) end - +end +else + puts 'NOTE: Set MONGO_URI environment variable to test postgresql storage.' end