Skip to content

Commit

Permalink
Add RSpect tests for validations and enums, with shoulda matchers for…
Browse files Browse the repository at this point in the history
… associations
  • Loading branch information
SaravShah committed Sep 6, 2018
1 parent b7277da commit 7358209
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
67 changes: 66 additions & 1 deletion spec/models/bundle_context_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,70 @@
require 'rails_helper'

RSpec.describe BundleContext, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
context "validation" do
let(:user) { User.new(sunet_id: "Jdoe")}
subject(:bc) { BundleContext.new(project_name: "SmokeTest",
content_structure: 1,
bundle_dir: "spec/test_data/bundle_input_g",
staging_style_symlink: false,
content_metadata_creation: 1,
user: user) }

it "is not valid unless it has all required attributes" do
expect(BundleContext.new).not_to be_valid
expect(bc).to be_valid
end

context "defines enum with expected values" do
it "content_structure enum" do
is_expected.to define_enum_for(:content_structure).with(
"simple_image_structure" => 0,
"simple_book_structure" => 1,
"book_as_iamge_structure" => 2,
"file_structure" => 3,
"smpl_structure" => 4
)
end

it "content_metadata_creation enum" do
is_expected.to define_enum_for(:content_metadata_creation).with(
"default_style" => 0,
"filename_style" => 1,
"smpl_style" => 2
)
end
end

describe "#content_structure=" do
it "validation rejects a value if it does not match the enum" do
expect { described_class.new(content_structure: 654) }
.to raise_error(ArgumentError, "'654' is not a valid content_structure")
expect { described_class.new(content_structure: 'book_as_pdf') }
.to raise_error(ArgumentError, "'book_as_pdf' is not a valid content_structure")
end

it "will accept a symbol, but will always return a string" do
expect(described_class.new(content_structure: :smpl_structure).content_structure).to eq 'smpl_structure'
end
end

describe "#content_metadata_creation=" do
it "validation rejects a value if it does not match the enum" do
expect { described_class.new(content_metadata_creation: 654) }
.to raise_error(ArgumentError, "'654' is not a valid content_metadata_creation")
expect { described_class.new(content_metadata_creation: 'dpg') }
.to raise_error(ArgumentError, "'dpg' is not a valid content_metadata_creation")
end

it "will accept a symbol, but will always return a string" do
expect(described_class.new(content_metadata_creation: :smpl_style).content_metadata_creation).to eq 'smpl_style'
end
end

it { is_expected.to validate_presence_of(:project_name) }
it { is_expected.to validate_presence_of(:content_structure) }
it { is_expected.to validate_presence_of(:bundle_dir) }
it { is_expected.to validate_presence_of(:content_metadata_creation) }
it { is_expected.to belong_to(:user) }
end
end
1 change: 1 addition & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

it { is_expected.to validate_uniqueness_of(:sunet_id) }
it { is_expected.to validate_presence_of(:sunet_id) }
it { is_expected.to have_many(:bundle_contexts) }

describe 'enforces unique constraint on sunet_id' do
let(:required_attributes) do
Expand Down

0 comments on commit 7358209

Please sign in to comment.