6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

rspecでredshiftのテストデータを作る

6
Last updated at Posted at 2016-03-01

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とか使えるかもと思っています。

6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?