Tech Tips

  1. Uncategorized
  2. 198 view

[Ruby][Rspec]File Output Test

Target

I want to write test code for file output batch with Rspec.

Program

Batch

Output csv file which has data from mongoDB.
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

Uncategorized recent post

  1. Run Amazon FreeRTOS on M5Stack Core2 for AWS …

  2. Udacity Self-Driving Car Engineer Nanodegree …

  3. Install sbt 1.0.0 and run sample template

  4. Visualization of Neural Network and its Train…

  5. [Machine Learning]Created docker image includ…

関連記事

PAGE TOP