require_relative 'test_helper' # Unit Test for SemanticLogger::Appender::MongoDB # class AppenderMongoDBTest < Minitest::Test context SemanticLogger::Appender::MongoDB do setup do @db = Mongo::Connection.new['test'] @appender = SemanticLogger::Appender::MongoDB.new( db: @db, collection_size: 10*1024**2, # 10MB host_name: 'test', application: 'test_application', level: :trace ) @hash = { :tracking_number => 12345, :session_id => 'HSSKLEU@JDK767'} Thread.current.name = 'thread' end teardown do @appender.purge_all if @appender end context "format logs into documents" do should "handle nil name, message and hash" do @appender.debug document = @appender.collection.find_one assert_equal :debug, document['level'] assert_equal nil, document['message'] assert_equal 'thread', document['thread_name'] assert document['time'].is_a?(Time) assert_equal nil, document['payload'] assert_equal $$, document['pid'] assert_equal 'test', document['host_name'] assert_equal 'test_application', document['application'] end should "handle nil message and payload" do @appender.debug(@hash) document = @appender.collection.find_one assert_equal :debug, document['level'] assert_equal @hash.inspect, document['message'] assert_equal 'thread', document['thread_name'] assert document['time'].is_a?(Time) assert_nil document['payload'] assert_equal $$, document['pid'] assert_equal 'test', document['host_name'] assert_equal 'test_application', document['application'] end should "handle message and payload" do @appender.debug('hello world', @hash) document = @appender.collection.find_one assert_equal :debug, document['level'] assert_equal 'hello world', document['message'] assert_equal 'thread', document['thread_name'] assert document['time'].is_a?(Time) assert_equal({ "tracking_number" => 12345, "session_id" => 'HSSKLEU@JDK767'}, document['payload']) assert_equal $$, document['pid'] assert_equal 'test', document['host_name'] assert_equal 'test_application', document['application'] end should "handle message without payload" do log = SemanticLogger::Base::Log.new(:debug) @appender.debug('hello world') document = @appender.collection.find_one assert_equal :debug, document['level'] assert_equal 'hello world', document['message'] assert_equal 'thread', document['thread_name'] assert document['time'].is_a?(Time) assert_equal nil, document['payload'] assert_equal $$, document['pid'] assert_equal 'test', document['host_name'] assert_equal 'test_application', document['application'] end end context "for each log level" do # Ensure that any log level can be logged SemanticLogger::LEVELS.each do |level| should "log #{level} information" do @appender.send(level, 'hello world -- Calculations', @hash) document = @appender.collection.find_one assert_equal level, document['level'] assert_equal 'hello world -- Calculations', document['message'] assert_equal 'thread', document['thread_name'] assert document['time'].is_a?(Time) assert_equal({ "tracking_number" => 12345, "session_id" => 'HSSKLEU@JDK767'}, document['payload']) assert_equal $$, document['pid'] assert_equal 'test', document['host_name'] assert_equal 'test_application', document['application'] end end end end end