Skip to content

Commit

Permalink
add spec of ActiveRecord API
Browse files Browse the repository at this point in the history
  • Loading branch information
i2bskn committed May 9, 2015
1 parent c5fb67c commit 35cbc8d
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
70 changes: 70 additions & 0 deletions spec/findable/base_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,75 @@
require "spec_helper"

describe Findable::Base do
include_context "TemporaryModel"
include_context "ReadModel"

describe ".primary_key" do
it { expect(model.primary_key).to eq("id") }
end

describe ".column_names" do
it { expect(model.column_names).to eq([:id, :name]) }
end

describe ".all" do
it { expect(read_model.all).to be_kind_of(Array) }
it { expect(read_model.all.size).to eq(1) }
end

describe ".find" do
it { expect(read_model.find(id)).to be_kind_of(read_model) }
it { expect(read_model.find([id])).to be_kind_of(Array) }
it {
expect {
read_model.find(invalid_id)
}.to raise_error(Findable::RecordNotFound)
}
end

describe ".find_by" do
it { expect(read_model.find_by(id: id)).to be_kind_of(read_model) }
it { expect(read_model.find_by(id: invalid_id)).to be_nil }
it { expect(read_model.find_by(id: id, name: name)).to be_kind_of(read_model) }
it { expect(read_model.find_by(id: id, name: invalid_name)).to be_nil }
it { expect(read_model.find_by(name: name)).to be_kind_of(read_model) }
it { expect(read_model.find_by(name: invalid_name)).to be_nil }
it { expect(read_model.find_by(id)).to be_kind_of(read_model) }
it { expect(read_model.find_by(invalid_id)).to be_nil }
end

describe ".find_by!" do
it {
expect {
read_model.find_by!(id: id)
}.not_to raise_error
}
it {
expect {
read_model.find_by!(id: invalid_id)
}.to raise_error(Findable::RecordNotFound)
}
end

describe ".where" do
it { expect(read_model.where(id: id)).to be_kind_of(Array) }
it { expect(read_model.where(id: id).first).to be_kind_of(read_model) }
it { expect(read_model.where(id: invalid_id)).to be_empty }
it { expect(read_model.where(id: id, name: name)).to be_kind_of(Array) }
it { expect(read_model.where(id: id, name: name).first).to be_kind_of(read_model) }
it { expect(read_model.where(id: invalid_id, name: name)).to be_empty }
it { expect(read_model.where(name: name)).to be_kind_of(Array) }
it { expect(read_model.where(name: name).first).to be_kind_of(read_model) }
it { expect(read_model.where(name: invalid_name)).to be_empty }
end

describe ".create" do
it {
expect {
model.create(name: "example")
}.to change { model.count }.by(1)
}
it { expect(model).to respond_to(:create!) }
end
end

8 changes: 8 additions & 0 deletions spec/support/shard_contexts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,11 @@
let(:model) { Size }
end

shared_context "ReadModel" do
let(:read_model) { Group }
let(:id) { 1 }
let(:invalid_id) { 2 }
let(:name) { "group1" }
let(:invalid_name) { "invalid" }
end

0 comments on commit 35cbc8d

Please sign in to comment.