Sha256: a6d54d2b7a7267c04da3d77eebd4c47db9dda8577efd30a6ea1b1de94d077c00

Contents?: true

Size: 1.52 KB

Versions: 2

Compression:

Stored size: 1.52 KB

Contents

require 'spec_helper'

describe Encore::Persister do
  let(:persister) { Encore::Persister::Instance }
  let(:persist!) { persister.new(model, params).persist! }

  let(:run_migrations!) do
    run_migration do
      create_table(:users, force: true) do |t|
        t.string :name, default: nil
        t.string :phone, default: nil
      end
    end
  end

  let(:spawn_objects!) do
    spawn_model('User')
    spawn_serializer('UserSerializer') do
      attributes :snake_case_key

      def snake_case_key
        :name
      end

      def key_mappings
        { snake_case_key: :name }
      end
    end
  end

  let(:model) { User }

  before do
    run_migrations!
    spawn_objects!
  end

  context 'single create' do
    let(:params) do
      [{
        snake_case_key: 'Allan',
        phone: '555-2525'
      }]
    end

    it { expect { persist! }.to change { model.count }.by(1) }
    it { expect { persist! }.to change { model.first.try(:name) }.to('Allan') }
    it { expect { persist! }.to change { model.first.try(:phone) }.to('555-2525') }
  end

  context 'many create' do
    let(:params) do
      [{
        snake_case_key: 'Allan',
        phone: '555-2525'
      }, {
        snake_case_key: 'Bob'
      }]
    end

    it { expect { persist! }.to change { model.count }.by(2) }
    it { expect { persist! }.to change { model.first.try(:name) }.to('Allan') }
    it { expect { persist! }.to change { model.first.try(:phone) }.to('555-2525') }
    it { expect { persist! }.to change { model.last.try(:name) }.to('Bob') }
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
encore-0.2 spec/encore/persister/key_mappings_spec.rb
encore-0.1 spec/encore/persister/key_mappings_spec.rb