Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specs for DataAttribute batch_update #3755

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions spec/models/data_attribute_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,71 @@
end
end

context '.batch_update' do
let(:p) { FactoryBot.create(:valid_controlled_vocabulary_term_predicate) }
let!(:o0) { FactoryBot.create(:valid_otu) }

specify 'creating new attributes' do
o1 = FactoryBot.create(:valid_otu)
expect(o1.data_attributes.count).to eq(0)

params = {
data_attribute: { controlled_vocabulary_term_id: p, value: 'foo' },
data_attribute_query: { attribute_subject_id: [o1.id] },
}

response = DataAttribute.batch_update(params).to_json

expect(response[:updated]).to include(o1.id)
expect(response[:not_updated]).to eq([])
expect(o1.reload.data_attributes.count).to eq(1)
expect(o1.data_attributes.first.value).to eq('foo')
end

specify 'updating existing attribute' do
o1 = FactoryBot.create(:valid_otu)
attribute = FactoryBot.create(:valid_data_attribute_internal_attribute, attribute_subject: o1, predicate: p, value: 'bar')
expect(o1.data_attributes.count).to eq(1)
expect(o1.data_attributes.first.value).to eq('bar')

params = {
data_attribute: { controlled_vocabulary_term_id: p, value: 'foo' },
data_attribute_query: { attribute_subject_id: [o1.id] },
}

response = DataAttribute.batch_update(params).to_json

expect(response[:updated]).to include(o1.id)
expect(response[:not_updated]).to eq([])

expect(o1.reload.data_attributes.count).to eq(1)
expect(o1.data_attributes.first.value).to eq('foo')
expect(o1.data_attributes.first.id).to eq(attribute.id)
end

specify 'updating existing and creating new attributes' do
o1 = FactoryBot.create(:valid_otu)
o2 = FactoryBot.create(:valid_otu)
attribute = FactoryBot.create(:valid_data_attribute_internal_attribute, attribute_subject: o1, predicate: p, value: 'bar')

params = {
data_attribute: { controlled_vocabulary_term_id: p, value: 'foo' },
data_attribute_query: { attribute_subject_id: [o1.id, o2.id] },
}

response = DataAttribute.batch_update(params).to_json
expect(response[:updated]).to include(o1.id, o2.id)
expect(response[:not_updated]).to eq([])

expect(o1.reload.data_attributes.count).to eq(1)
expect(o2.reload.data_attributes.count).to eq(1)
expect(o1.data_attributes.first.value).to eq('foo')
expect(o2.data_attributes.first.value).to eq('foo')
expect(o1.data_attributes.first.id).to eq(attribute.id)
expect(o1.data_attributes.first.id).to_not eq(attribute.id)
end
end

context 'uniqueness' do
let(:o) {FactoryBot.create(:valid_otu) }

Expand Down