rspecでredshiftのデータを使ったテストをするときに、テストデータの作成方法をメモしておきます。
より良い方法があれば、教えて下さい!
今回はredshiftにデータがすでにあったので、それを参考に作る
redshift
unload ('select * from table_name) to 's3://xxx-spec/table_name/' credentials 'aws_access_key_id=xxxxx;aws_secret_access_key=xxxx';
テストデータとして使えそうなデータをピックアップ&追記
bash
aws s3 cp s3://xxx-spec/table_name/ . --recursive vi 0000_part_00 aws s3 cp 0000_part_00 s3://xxx-spec/table_name/
redshift.rb
require 'rails_helper'
class Redshift < ActiveRecord::Base
config_redshift = YAML.load(ERB.new(File.read('config/redshift.yml')).result)[Rails.env]
establish_connection(config_redshift)
def self.query(sql)
self.connection.execute(sql)
end
end
hoge_rspec.rb
require 'rails_helper'
RSpec.describe Hoge do
describe "#test" do
before :all do
@user = FactoryGirl.create(:user)
@user.create_redshift # schema作成
Redshift.query("copy test_table_name from 's3://xxx-spec/table_name/' credentials 'aws_access_key_id=#{ENV[AWS_ACCESS_KEY_ID]};aws_secret_access_key=#{ENV[AWS_SECRET_ACCESS_KEY]}'") # データ投入
end
end
end
現在のプロジェクトではredshiftのテーブル毎にModelを作ってないが、Modelを作ったらFactoryGirlとか使えるかもと思っています。