-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(spec) - add specs using temping gem to simulate model classes
- Loading branch information
1 parent
005d11e
commit 003f793
Showing
2 changed files
with
156 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,149 @@ | ||
require "spec_helper" | ||
|
||
RSpec.describe RichEnums do | ||
it "has a version number" do | ||
expect(RichEnums::VERSION).not_to be nil | ||
describe "error scenarios" do | ||
it "raises an error if the argument to rich_enum is not a Hash" do | ||
expect do | ||
Temping.create(:course_class) do | ||
include RichEnums | ||
rich_enum([]) | ||
end | ||
end.to raise_error(RichEnums::Error) | ||
end | ||
|
||
it "raises an error if an empty Hash is passed to rich_enum" do | ||
expect do | ||
Temping.create(:course_class) do | ||
include RichEnums | ||
rich_enum({}) | ||
end | ||
end.to raise_error(RichEnums::Error) | ||
end | ||
|
||
it "raises an error if the enum definition uses the Array form" do | ||
# This is not supported yet | ||
expect do | ||
Temping.create(:course_class) do | ||
include RichEnums | ||
rich_enum({status: [:active, :inactive]}) | ||
end | ||
end.to raise_error(RichEnums::Error) | ||
end | ||
end | ||
|
||
it "does something useful" do | ||
# TODO write tests | ||
expect(false).to eq(true) | ||
describe "rich_enum usage" do | ||
context "it calls the ActiveRecord::Enum.enum method" do | ||
let(:course_class) do | ||
Temping.create(:course_class) do | ||
with_columns do |t| | ||
t.integer :status | ||
t.string :category | ||
end | ||
|
||
include RichEnums | ||
end | ||
end | ||
|
||
it "invokes the enum method with the correct arguments" do | ||
allow(course_class).to receive(:enum).and_call_original | ||
|
||
course_class.rich_enum status: { active: 0, inactive: 1 }, alt: :name | ||
# it passes only the necessary arguments to the enum method, stripping out the alt: option | ||
expect(course_class).to have_received(:enum).with(status: { active: 0, inactive: 1 }) | ||
end | ||
|
||
it "invokes the enum method with the correct arguments" do | ||
allow(course_class).to receive(:enum).and_call_original | ||
|
||
course_class.rich_enum status: { active: [0, 'LIVE'], inactive: [1, 'NOT_LIVE'] }, alt: :name | ||
# it passes only the necessary arguments to the enum method, | ||
# stripping out the alternate names LIVE and NOT_LIVE and the alt: option | ||
expect(course_class).to have_received(:enum).with(status: { active: 0, inactive: 1 }) | ||
end | ||
|
||
it "invokes the enum method with the correct arguments" do | ||
allow(course_class).to receive(:enum).and_call_original | ||
course_class.rich_enum status: { active: [0, 'LIVE'], inactive: [1, 'NOT_LIVE'] }, _prefix: true, alt: 'state' | ||
# it passes only the necessary arguments to the enum method, | ||
# stripping out the alternate names LIVE and NOT_LIVE and the alt: option | ||
expect(course_class).to have_received(:enum).with(status: { active: 0, inactive: 1 }, _prefix: true) | ||
end | ||
end | ||
|
||
context "with only an alternate name specified without additional mapping" do | ||
let(:course_class) do | ||
Temping.create(:course_class) do | ||
with_columns do |t| | ||
t.integer :status | ||
t.string :category | ||
end | ||
|
||
include RichEnums | ||
rich_enum status: { active: 0, inactive: 1 }, alt: :name | ||
end | ||
end | ||
let(:test_instance) { course_class.new } | ||
|
||
it "defines a class method for each enum" do | ||
expect(course_class).to respond_to(:status_names) | ||
end | ||
|
||
it "returns a hash of enum values and names" do | ||
expect(course_class.status_names).to eq({"active"=>"active", "inactive"=>"inactive"}) | ||
end | ||
|
||
it "defines an instance method for each enum" do | ||
expect(test_instance).to respond_to(:status_name) | ||
end | ||
|
||
it "returns the value in response to alternate name" do | ||
test_instance.status = 0 | ||
expect(test_instance.status_name).to eq(test_instance.status) | ||
test_instance.status = 1 | ||
expect(test_instance.status_name).to eq(test_instance.status) | ||
test_instance.status = :active | ||
expect(test_instance.status_name).to eq(test_instance.status) | ||
test_instance.status = 'inactive' | ||
expect(test_instance.status_name).to eq(test_instance.status) | ||
end | ||
end | ||
|
||
context "with an alternate name and mapping specified" do | ||
let(:course_class) do | ||
Temping.create(:course_class) do | ||
with_columns do |t| | ||
t.string :status | ||
end | ||
|
||
include RichEnums | ||
rich_enum status: { active: [0, 'LIVE'], inactive: [1, 'NOT_LIVE'] }, alt: :name | ||
end | ||
end | ||
|
||
let(:test_instance) { course_class.new } | ||
|
||
it "defines a class method for each enum" do | ||
expect(course_class).to respond_to(:status_names) | ||
end | ||
|
||
it "returns a hash of enum values and names" do | ||
expect(course_class.status_names).to eq({"active"=>"LIVE", "inactive"=>"NOT_LIVE"}) | ||
end | ||
|
||
it "defines an instance method for each enum" do | ||
expect(test_instance).to respond_to(:status_name) | ||
end | ||
|
||
it "returns the alternate name of the enum value" do | ||
test_instance.status = 0 | ||
expect(test_instance.status_name).to eq("LIVE") | ||
test_instance.status = 1 | ||
expect(test_instance.status_name).to eq("NOT_LIVE") | ||
test_instance.status = :active | ||
expect(test_instance.status_name).to eq("LIVE") | ||
test_instance.status = 'inactive' | ||
expect(test_instance.status_name).to eq("NOT_LIVE") | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters