Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
10 changes: 10 additions & 0 deletions lib/seek/isa_templates/template_attributes_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,16 @@
"type": "null"
}
]
},
"unit": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
]
}
},
"additionalProperties": false
Expand Down
10 changes: 10 additions & 0 deletions lib/seek/isa_templates/template_attributes_schema_test.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,16 @@
"type": "null"
}
]
},
"unit": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
]
}
},
"additionalProperties": false
Expand Down
10 changes: 10 additions & 0 deletions lib/seek/isa_templates/template_extractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def self.extract_templates(user)
description: attribute['description'],
sample_controlled_vocab_id: scv&.id,
pid: attribute['pid'],
unit_id: get_unit_id(attribute['unit']),
sample_attribute_type_id: get_sample_attribute_type(attribute['dataType']),
allow_cv_free_text: allow_cv_free_text,
title: attribute['name'])
Expand Down Expand Up @@ -192,6 +193,15 @@ def self.get_isa_tag_id(title)
it&.id
end

def self.get_unit_id(symbol)
return nil if symbol.blank?

unit = Unit.find_by(symbol: symbol)
@errors.append "<li>Could not find a Unit with symbol '#{symbol}'</li>" if unit.nil?

unit&.id
end
Comment thread
Copilot marked this conversation as resolved.

def self.seed_isa_tags
Rake::Task['db:seed:015_isa_tags'].invoke if ISATag.all.blank?
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"data": [
{
"metadata": {
"name": "Invalid Unit Test Template",
"group": "ISA Test",
"group_order": 1,
"temporary_name": "1_invalid_unit_test_template",
"version": "1.0.0",
"isa_config": null,
"isa_measurement_type": null,
"isa_technology_type": null,
"isa_protocol_type": null,
"repo_schema_id": null,
"organism": "any",
"level": "study source"
},
"data": [
{
"iri": null,
"name": "Source Name",
"description": "Sources are considered as the starting biological material used in a study.",
"dataType": "String",
"title": true,
"required": true,
"isaTag": "source"
},
{
"iri": null,
"name": "Weight",
"description": "Weight with an unrecognised unit symbol.",
"dataType": "Integer",
"required": false,
"ontology": null,
"CVList": null,
"isaTag": "source_characteristic",
"unit": "INVALID_UNIT_XYZ"
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"data": [
{
"metadata": {
"name": "Unit Test Template",
"group": "ISA Test",
"group_order": 1,
"temporary_name": "1_unit_test_template",
"version": "1.0.0",
"isa_config": null,
"isa_measurement_type": null,
"isa_technology_type": null,
"isa_protocol_type": null,
"repo_schema_id": null,
"organism": "any",
"level": "study source"
},
"data": [
{
"iri": null,
"name": "Source Name",
"description": "Sources are considered as the starting biological material used in a study.",
"dataType": "String",
"title": true,
"required": true,
"isaTag": "source"
},
{
"iri": null,
"name": "Weight",
"description": "Weight of the source material in grams.",
"dataType": "Integer",
"required": false,
"ontology": null,
"CVList": null,
"isaTag": "source_characteristic",
"unit": "g"
}
]
}
]
}
75 changes: 75 additions & 0 deletions test/functional/templates_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,81 @@ class TemplatesControllerTest < ActionController::TestCase
assert_select 'script#project-selector-possibilities-json', text: /#{options}/, count: 1
end

test 'should create template attribute with unit' do
unit = Unit.find_or_create_by(symbol: 'g')

assert_difference('Template.count') do
post :create, params: { template: { title: 'Template with unit',
project_ids: @project_ids,
level: 'study source',
organism: 'any',
version: '1.0.0',
template_attributes_attributes: {
'0' => {
pos: '1',
title: 'Weight',
required: '1',
description: 'weight in grams',
sample_attribute_type_id: @int_type.id,
unit_id: unit.id,
_destroy: '0',
isa_tag_id: @default_isa_tag
}
}
}
}
end

template = assigns(:template)
assert_redirected_to template_path(template)
weight = template.template_attributes.find_by(title: 'Weight')
assert_not_nil weight
assert_equal unit, weight.unit
end

test 'should update template attribute to set unit' do
unit = Unit.find_or_create_by(symbol: 'g')
template = FactoryBot.create(:min_template, project_ids: @project_ids, contributor: @person, title: 'unit_update_template')
attribute = template.template_attributes.first

fields = {
'0' => {
id: attribute.id,
pos: attribute.pos,
title: attribute.title,
required: (attribute.required ? '1' : '0'),
sample_attribute_type_id: attribute.sample_attribute_type_id,
unit_id: unit.id,
_destroy: '0',
isa_tag_id: attribute.isa_tag_id
}
}

put :update, params: { id: template, template: { title: template.title, template_attributes_attributes: fields } }

assert_redirected_to template_path(assigns(:template))
assert_equal unit, assigns(:template).template_attributes.first.unit
end

test 'should populate template with unit from json upload' do
login_as(@admin)
unit = Unit.find_or_create_by(symbol: 'g')

template_json = fixture_file_upload('upload_json_sample_type_template/test_unit_template.json', 'application/json')

assert_enqueued_jobs 1, only: PopulateTemplatesJob do
post :populate_template, params: { template_json_file: template_json }
end

assert_difference('Template.count', 1) do
perform_enqueued_jobs(only: PopulateTemplatesJob)
end

weight_attribute = Template.last.template_attributes.find_by(title: 'Weight')
assert_not_nil weight_attribute
assert_equal unit, weight_attribute.unit
end

test 'should reuse existing controlled vocabularies' do
apples_cv = FactoryBot.create(:apples_sample_controlled_vocab, title: "apples controlled vocab for template", )
physics_ontology = FactoryBot.create(:sample_controlled_vocab, title: "physics ontology", source_ontology: "edam", ols_root_term_uris: "http://edamontology.org/topic_3318")
Expand Down
43 changes: 43 additions & 0 deletions test/unit/jobs/populate_templates_job_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ def setup
# Create the SampleAttributeTypes
# The title MUST be set manually!
FactoryBot.create(:string_sample_attribute_type, title: 'String') if SampleAttributeType.find_by(title: 'String').nil?
FactoryBot.create(:integer_sample_attribute_type, title: 'Integer') if SampleAttributeType.find_by(title: 'Integer').nil?
FactoryBot.create(:sample_multi_sample_attribute_type, title: 'Registered Sample List') if SampleAttributeType.find_by(title: 'Registered Sample List').nil?

# Create the ISA Tags
Expand Down Expand Up @@ -64,4 +65,46 @@ def teardown
end
end

test 'perform sets unit on template attribute from json' do
unit = Unit.find_or_create_by(symbol: 'g')

src = Rails.root.join('test', 'fixtures', 'files', 'upload_json_sample_type_template', 'test_unit_template.json')
dest = Seek::Config.append_filestore_path('source_types')
FileUtils.cp(src, dest)

assert_difference('Template.count', 1) do
PopulateTemplatesJob.perform_now(@admin)
end

weight_attribute = Template.last.template_attributes.find_by(title: 'Weight')
assert_not_nil weight_attribute
assert_equal unit, weight_attribute.unit
end

test 'perform with null unit leaves unit blank on template attribute' do
Comment thread
Copilot marked this conversation as resolved.
Outdated
src = Rails.root.join('test', 'fixtures', 'files', 'upload_json_sample_type_template', 'test_templates.json')
dest = Seek::Config.append_filestore_path('source_types')
FileUtils.cp(src, dest)

assert_difference('Template.count', 4) do
PopulateTemplatesJob.perform_now(@admin)
end

Template.last.template_attributes.each do |ta|
assert_nil ta.unit
end
end

test 'perform with json containing invalid unit symbol raises error' do
src = Rails.root.join('test', 'fixtures', 'files', 'upload_json_sample_type_template', 'invalid_unit_template.json')
dest = Seek::Config.append_filestore_path('source_types')
FileUtils.cp(src, dest)

assert_no_difference('Template.count') do
assert_raises(RuntimeError) do
PopulateTemplatesJob.perform_now(@admin)
end
end
end

end
Loading