Skip to content

Commit 2ffa97a

Browse files
committed
add a unique constraint
1 parent a976e36 commit 2ffa97a

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# frozen_string_literal: true
2+
3+
class UniqueConstraint < ActiveRecord::Migration[5.2]
4+
def change
5+
add_index :workflow_steps,
6+
%i[druid version datastream process],
7+
unique: true,
8+
name: 'uk_workflow_steps'
9+
end
10+
end

db/schema.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 2019_01_10_221945) do
13+
ActiveRecord::Schema.define(version: 2019_01_10_224307) do
1414

1515
create_table "workflow_steps", force: :cascade do |t|
1616
t.string "druid", null: false
@@ -29,6 +29,7 @@
2929
t.string "lane_id", default: "default", null: false
3030
t.datetime "created_at", null: false
3131
t.datetime "updated_at", null: false
32+
t.index ["druid", "version", "datastream", "process"], name: "uk_workflow_steps", unique: true
3233
t.index ["druid", "version"], name: "index_workflow_steps_on_druid_and_version"
3334
t.index ["druid"], name: "index_workflow_steps_on_druid"
3435
end

spec/controllers/workflows_controller_spec.rb

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,41 @@
6767
let(:workflow) { 'accessionWF' }
6868
let(:repository) { 'dor' }
6969
let(:request_data) { workflow_create }
70-
let(:client) { double(current_version: '1') }
70+
let(:client) { double(current_version: current_version) }
71+
let(:expected_count) { Nokogiri::XML(workflow_create).xpath('//process').count }
7172

7273
before do
7374
allow(Dor::Services::Client).to receive(:object).with(druid).and_return(client)
7475
end
75-
it 'creates new workflows' do
76-
expect do
77-
put :create, body: request_data, params: { repo: repository, druid: druid, workflow: workflow, format: :xml }
78-
end.to change(WorkflowStep, :count)
79-
.by(Nokogiri::XML(workflow_create).xpath('//process').count)
76+
77+
context 'when no workflows exist' do
78+
let(:current_version) { '1' }
79+
80+
it 'creates new workflows' do
81+
expect do
82+
put :create, body: request_data, params: { repo: repository, druid: druid, workflow: workflow, format: :xml }
83+
end.to change(WorkflowStep, :count)
84+
.by(expected_count)
85+
end
86+
end
87+
88+
context 'when some workflows exist' do
89+
let(:current_version) { '2' }
90+
before do
91+
WorkflowParser.new(
92+
workflow_create,
93+
druid: druid,
94+
repository: repository,
95+
version: '1'
96+
).create_workflow_steps
97+
end
98+
99+
it 'creates new workflows' do
100+
expect do
101+
put :create, body: request_data, params: { repo: repository, druid: druid, workflow: workflow, format: :xml }
102+
end.to change(WorkflowStep, :count)
103+
.by(expected_count)
104+
end
80105
end
81106
end
82107
end

0 commit comments

Comments
 (0)