Production Level Knowledge & Tips. Updated at every month 1st Monday 7:00 AM JST.

  1. プログラミング
  2. 110 view

[Ruby][Rspec]ファイル出力のテスト

やりたいこと

Rspecでファイル出力するバッチのテストを書きたい。

プログラム

バッチ

mongoDBからデータをcsvファイルに出力する。
require 'mongo'
require 'active_support/all'
require 'yaml'
require 'logger'

class GetUserActionLogs
	def self.setup
		@@config = YAML::load(File.open(File.expand_path('../../../config/common.yml', __FILE__)))

		# Logger Setting
		@@app_logger = Logger.new(File.open(@@config['get_user_action_logs']['app_log_path'] + 'get_user_action_logs.log', 'a'))

		# Connection with mongoDB
		@conn = Mongo::Connection.new
		@userdb = @conn.db('action_log')
		@collection = @userdb.collection('current_00')
	end

	def self.process
		self.setup

		@@app_logger.info "#{Time.now.to_s} get_user_action_logs.rb start."
		self.output_action_log_csv
		@@app_logger.info "#{Time.now.to_s} get_user_action_logs.rb end."

		return true
	end

	def self.output_action_log_csv
		filename = File.join(@@config['get_user_action_logs']['output_dir'], "user_action_log" + Time.now.strftime("%Y%m%d") + ".csv")
		writer = File.open(filename, "w")

		0.upto(@@config['action_log_db']['num_collections']-1).each{ |i|
			records_count = 0
			@collection = @userdb.collection('current_' + sprintf("%02d", i))
			@collection.find.each{ |records|
				records_count += 1
				writer.write records.values_at("user_id", "action_id", "action_type", "timeslot").join(",")
			}
			@@app_logger.info "#{Time.now.to_s} current_#{sprintf("%02d", i)} : #{records_count} records"
		}
		writer.close()

	end
end

GetUserActionLogs.process

Rspec

require_relative '../../spec_helper'
require_relative '../../../batches/get_user_action_logs/get_user_action_logs'

describe GetUserActionLogs do

	describe '.process' do
		it 'should get true' do
			GetUserActionLogs.process().should == true
		end		

		it 'should exist file user_action_logYYYYMMDD.csv' do
			filename=GetUserActionLogs.class_eval("@@config")['get_user_action_logs']['output_dir']+"user_action_log"+Date.today.strftime("%Y%m%d")+".csv"
			File.exist?(filename).should == true
		end
	end
end

プログラミングの最近記事

  1. How to bring columns not GROUP BY key from ne…

  2. HiveでGROUP BYを伴うサブクエリのネストからGROUP BYのキー以外のカラムを…

  3. How to drop Hive’s External Table

  4. HiveでExternal Tableを削除する方法メモ

  5. [触ってみた]Microsoft Quantum Development Kit

関連記事

PAGE TOP