Skip to content

Commit

Permalink
Extracted primary_key_attributes spec
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexParamonov committed Mar 30, 2014
1 parent aab696c commit 6b9cf24
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 26 deletions.
38 changes: 38 additions & 0 deletions spec/undo/serializer/active_model/connector_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require "spec_helper"

describe Undo::Serializer::ActiveModel::Connector do
let(:object) { double :object }
let(:serializer) { subject }

describe "#primary_key_attributes" do
let(:attributes) { { uuid: "identifier", foo: :bar, "hello" => "world" } }

it "extracts object primary key attributes" do
expect(object).to receive(:primary_key) { "uuid" }

expect(serializer.primary_key_attributes object, attributes).to eq uuid: "identifier"
end

it "redefines primary key attribute names by primary_key_fetcher" do
serializer = described_class.new primary_key_fetcher: -> object { %w[hello uuid] }

expect(serializer.primary_key_attributes object, attributes).to eq ({
hello: "world",
uuid: "identifier"
})
end

it "use nil value when no primary_key attributes available" do
allow(object).to receive(:primary_key) { "unknown" }

expect(serializer.primary_key_attributes object, attributes).to eq unknown: nil
end

it "accepts string and symbol as uuid key" do
allow(object).to receive(:primary_key) { "uuid" }
expect(serializer.primary_key_attributes object, attributes).to eq uuid: "identifier"
allow(object).to receive(:primary_key) { :uuid }
expect(serializer.primary_key_attributes object, attributes).to eq uuid: "identifier"
end
end
end
26 changes: 0 additions & 26 deletions spec/undo/serializer/active_model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,6 @@ def attributes; marshal_dump end
let(:object) { FooBarTestObject.new }

describe "options" do
describe "primary key" do
it "uses object primary key" do
object.uuid = "identifier"
allow(object).to receive(:primary_key) { "uuid" }

expect(object_class).to receive(:new).with(uuid: "identifier") { double.as_null_object }
serializer.deserialize serializer.serialize object
end

it "uses primary_key_fetcher" do
object.uuid = "identifier"

serializer = subject.new primary_key_fetcher: -> object { "uuid" }

expect(object_class).to receive(:new).with(uuid: "identifier") { double.as_null_object }
serializer.deserialize serializer.serialize object
end

it "use nil value when no primary_key attributes available" do
allow(object).to receive(:primary_key) { "uuid" }

expect(object_class).to receive(:new).with(uuid: nil) { double.as_null_object }
serializer.deserialize serializer.serialize object
end
end

describe "attribute_serializer" do
let(:attribute_serializer) { double :attribute_serializer }
let(:serializer) { subject.new attribute_serializer: attribute_serializer }
Expand Down

0 comments on commit 6b9cf24

Please sign in to comment.