-
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.
Merge pull request #3 from betacraft/specs_with_temping_gem
Use Temping gem to test against ActiveRecord models
- Loading branch information
Showing
11 changed files
with
279 additions
and
11 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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
env: | ||
RUBY_VERSION: 3.2.2 | ||
|
||
name: RichEnums CI | ||
on: | ||
pull_request: | ||
paths-ignore: | ||
- .gitignore | ||
- CHANGELOG.md | ||
- CONTRIBUTING.md | ||
- README.md | ||
push: | ||
paths-ignore: | ||
- .gitignore | ||
- CHANGELOG.md | ||
- CONTRIBUTING.md | ||
- README.md | ||
|
||
jobs: | ||
rspec-test: | ||
name: Run tests (ruby ${{ matrix.ruby }}, rails ${{ matrix.rails }}) | ||
runs-on: ubuntu-22.04 | ||
strategy: | ||
matrix: | ||
include: | ||
- ruby: '3.2' | ||
rails: '7.1' | ||
- ruby: '3.2' | ||
rails: '7.0' | ||
- ruby: '3.2' | ||
rails: '6.1' | ||
- ruby: '3.1' | ||
rails: '6.1' | ||
env: | ||
BUNDLE_GEMFILE: gemfiles/activerecord_${{ matrix.rails }}.gemfile | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v4 | ||
- name: Install Ruby | ||
uses: ruby/setup-ruby@v1 | ||
with: | ||
ruby-version: ${{ matrix.ruby }} | ||
- name: Gem cache | ||
uses: actions/cache@v4 | ||
with: | ||
path: vendor/bundle | ||
key: ${{ runner.os }}-gem-use-ruby-${{ hashFiles('**/Gemfile.lock') }} | ||
- name: Install dependencies | ||
run: | | ||
gem install bundler --version 2.3.25 --no-document | ||
bundle config path vendor/bundle | ||
bundle install --jobs 4 --retry 3 | ||
- name: Run tests | ||
run: bundle exec rake spec |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
3.1.1 |
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
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
source "https://rubygems.org" | ||
|
||
gem "activerecord", "~> 6.1" | ||
gem "rake", "~> 12.0" | ||
gem "rspec", "~> 3.0" | ||
|
||
# Specify your gem's dependencies in rich_enums.gemspec | ||
gemspec path: "../" |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
source "https://rubygems.org" | ||
|
||
gem "activerecord", "~> 7.0" | ||
gem "rake", "~> 12.0" | ||
gem "rspec", "~> 3.0" | ||
|
||
# Specify your gem's dependencies in rich_enums.gemspec | ||
gemspec path: "../" |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
source "https://rubygems.org" | ||
|
||
gem "activerecord", "~> 7.1" | ||
gem "rake", "~> 12.0" | ||
gem "rspec", "~> 3.0" | ||
|
||
# Specify your gem's dependencies in rich_enums.gemspec | ||
gemspec path: "../" |
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
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,3 +1,3 @@ | ||
module RichEnums | ||
VERSION = '0.1.3' | ||
VERSION = '0.1.4' | ||
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
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