Skip to content

Commit

Permalink
test: 💍 ExportToDistCsv::*.Service のテストを追加した(テストに合わせてリファクタも) (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikukyugamer committed Dec 7, 2023
1 parent 45ea7bc commit aaa9a3c
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 12 deletions.
4 changes: 1 addition & 3 deletions app/services/export_to_dist_csv/albums_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ module ExportToDistCsv
class AlbumsService
def initialize; end # rubocop:disable Style/RedundantInitialize

def execute
exported_file_path = 'db/dist_csv_files/albums.csv'

def execute(exported_file_path = 'db/dist_csv_files/albums.csv')
CSV.open(exported_file_path, 'w', force_quotes: true) do |csv|
csv << headers

Expand Down
4 changes: 1 addition & 3 deletions app/services/export_to_dist_csv/artists_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ module ExportToDistCsv
class ArtistsService
def initialize; end # rubocop:disable Style/RedundantInitialize

def execute
exported_file_path = 'db/dist_csv_files/artists.csv'

def execute(exported_file_path = 'db/dist_csv_files/artists.csv')
CSV.open(exported_file_path, 'w', force_quotes: true) do |csv|
csv << headers

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

module ExportToDistCsv
class FeaturesInAllAlbumsService
EXPORTED_FILE_PATH = Rails.root.join('db/dist_csv_files/features_all.csv')

def initialize; end # rubocop:disable Style/RedundantInitialize

def execute
CSV.open(EXPORTED_FILE_PATH, 'w', force_quotes: true) do |csv|
def execute(exported_file_path = Rails.root.join('db/dist_csv_files/features_all.csv'))
CSV.open(exported_file_path, 'w', force_quotes: true) do |csv|
csv << headers

rows.each do |row|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ def initialize(album_name)
@album_name = album_name
end

def execute
def execute(exported_files_dir = Rails.root.join('db/dist_csv_files'))
raise 'アルバム名の指定が誤っています' unless @album_name.in?(album_names)

exported_file_name = album_name_to_exported_file_name[@album_name]
exported_file_path = Rails.root.join("db/dist_csv_files/#{exported_file_name}")
exported_file_path = "#{exported_files_dir}/#{exported_file_name}"

CSV.open(exported_file_path, 'w', force_quotes: true) do |csv|
csv << headers
Expand Down
11 changes: 11 additions & 0 deletions spec/services/export_to_dist_csv/albums_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'rails_helper'

RSpec.describe 'ExportToDistCsv::AlbumsService' do
it 'エクスポータが正しく実行できること' do
csv_filepath = 'tmp/dist_csv_albums.csv'

ExportToDistCsv::AlbumsService.new.execute(csv_filepath)

expect(File.exist?(csv_filepath)).to eq true
end
end
11 changes: 11 additions & 0 deletions spec/services/export_to_dist_csv/artists_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'rails_helper'

RSpec.describe 'ExportToDistCsv::ArtistsService' do
it 'エクスポータが正しく実行できること' do
csv_filepath = 'tmp/dist_csv_artists.csv'

ExportToDistCsv::ArtistsService.new.execute(csv_filepath)

expect(File.exist?(csv_filepath)).to eq true
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'rails_helper'

RSpec.describe 'ExportToDistCsv::FeaturesInAllAlbumsService' do
it 'エクスポータが正しく実行できること' do
csv_filepath = 'tmp/dist_csv_features_all.csv'

ExportToDistCsv::FeaturesInAllAlbumsService.new.execute(csv_filepath)

expect(File.exist?(csv_filepath)).to eq true
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'rails_helper'

RSpec.describe 'ExportToDistCsv::FeaturesInEachAlbumService' do
it 'エクスポータが正しく実行できること' do
exported_files_dir = 'tmp'

# TODO: DRY じゃないので直したい
album_name_to_exported_file_name = {
'幻想水滸伝 ORIGINAL GAME SOUNDTRACK' => 'features_s1.csv',
'幻想水滸伝II ORIGINAL GAME SOUNDTRACK Vol.1' => 'features_s2_1.csv',
'幻想水滸伝II ORIGINAL GAME SOUNDTRACK Vol.2' => 'features_s2_2.csv',
'幻想水滸伝III ORIGINAL SOUNDTRACK SELECTION' => 'features_s3.csv',
'幻想水滸伝IV ORIGINAL SOUNDTRACK SELECTION' => 'features_s4.csv',
'幻想水滸伝V ORIGINAL SOUNDTRACK SELECTION' => 'features_s5.csv',
'幻想水滸伝ティアクライス ORIGINAL SOUNDTRACK' => 'features_tk.csv',
'幻想水滸伝 紡がれし百年の時 オリジナルサウンドトラック' => 'features_tsumutoki.csv'
}

album_name_to_exported_file_name.each do |album_name, exported_file_name|
service = ExportToDistCsv::FeaturesInEachAlbumService.new(album_name)
service.execute(exported_files_dir)

expect(File.exist?("#{exported_files_dir}/#{exported_file_name}")).to be_truthy
end
end
end

0 comments on commit aaa9a3c

Please sign in to comment.