Skip to content

Commit

Permalink
Merge pull request #44 from sanger/next_release
Browse files Browse the repository at this point in the history
Release 1.7.0
  • Loading branch information
JamesGlover authored Apr 18, 2017
2 parents 595796f + 359334d commit bfde29a
Show file tree
Hide file tree
Showing 6 changed files with 280 additions and 2 deletions.
24 changes: 24 additions & 0 deletions app/models/stock_resource.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class StockResource < ActiveRecord::Base
include ResourceTools
include CompositeResourceTools

has_associated(:study)
has_associated(:sample)

has_composition_keys(:id_stock_resource_lims,:id_sample_tmp)

json do

translate(
:stock_resource_id => :id_stock_resource_lims,
:uuid => :stock_resource_uuid
)

has_nested_model(:samples) do
end

ignore(:samples)

end

end
35 changes: 35 additions & 0 deletions db/migrate/20160919144230_add_stock_resources_table.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class AddStockResourcesTable < ActiveRecord::Migration

def change
# Using singluar table names to maintain convention with rest of warehouse
create_table :stock_resource, primary_key: :id_stock_resource_tmp do |t|

t.datetime :last_updated, null: false, comment: 'Timestamp of last update'
t.datetime :recorded_at, null: false, comment: 'Timestamp of warehouse update'
t.datetime :created, null: false, comment: 'Timestamp of initial registration of stock in LIMS'
t.datetime :deleted_at, null: true, comment: 'Timestamp of initial registration of deletion in parent LIMS. NULL if not deleted.'

t.column :id_sample_tmp, 'integer unsigned', null: false, comment: 'Sample id, see "sample.id_sample_tmp"'
t.column :id_study_tmp, 'integer unsigned', null: false, comment: 'Sample id, see "study.id_study_tmp"'
t.string :id_lims, limit:10, null:false, comment: 'LIM system identifier'

t.string :id_stock_resource_lims, limit: 20, null: false, comment: 'Lims specific identifier for the stock'
t.string :stock_resource_uuid, limit: 36, comment: 'Uuid identifier for the stock'

t.string :labware_type, null: false, comment: 'The type of labware containing the stock. eg. Well, Tube'

t.string :labware_machine_barcode, null: false, comment: 'The barcode of the containing labware as read by a barcode scanner'
t.string :labware_human_barcode, null: false, comment: 'The barcode of the containing labware in human readable format'
t.string :labware_coordinate, null: true, comment: 'For wells, the coordinate on the containing plate. Null for tubes.'

t.float :current_volume, null: true, comment: 'The current volume of material in microlitres based on measurements and know usage'
t.float :initial_volume, null: true, comment: 'The result of the initial volume measurement in microlitres conducted on the material'
t.float :concentration, null: true, comment: 'The concentration of material recorded in the lab in nanograms per microlitre'

t.string :gel_pass, null: true, comment: 'The recorded result for the qel QC assay.'
t.string :pico_pass, null: true, comment: 'The recorded result for the pico green assay. A pass indicates a successful assay, not sufficient material.'
t.integer :snp_count, null: true, comment: 'The number of markers detected in genotyping assays'
t.string :measured_gender, null: true, comment: 'The gender call base on the genotyping assay'
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class AddForeiginKeyContraintsToStockTable < ActiveRecord::Migration

require './lib/foreign_key_constraint'
include ForeignKeyConstraint

def up
add_constraint :stock_resource, :sample, as: :id_sample_tmp, foreign_key: :id_sample_tmp
add_constraint :stock_resource, :study, as: :id_study_tmp, foreign_key: :id_study_tmp
end

def down
drop_constraint :stock_resource, :sample, as: :id_sample_tmp, foreign_key: :id_sample_tmp
drop_constraint :stock_resource, :study, as: :id_study_tmp, foreign_key: :id_study_tmp
end
end
43 changes: 42 additions & 1 deletion db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,43 @@ CREATE TABLE `schema_migrations` (
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Table structure for table `stock_resource`
--

DROP TABLE IF EXISTS `stock_resource`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `stock_resource` (
`id_stock_resource_tmp` int(11) NOT NULL AUTO_INCREMENT,
`last_updated` datetime NOT NULL COMMENT 'Timestamp of last update',
`recorded_at` datetime NOT NULL COMMENT 'Timestamp of warehouse update',
`created` datetime NOT NULL COMMENT 'Timestamp of initial registration of stock in LIMS',
`deleted_at` datetime DEFAULT NULL COMMENT 'Timestamp of initial registration of deletion in parent LIMS. NULL if not deleted.',
`id_sample_tmp` int(10) unsigned NOT NULL COMMENT 'Sample id, see "sample.id_sample_tmp"',
`id_study_tmp` int(10) unsigned NOT NULL COMMENT 'Sample id, see "study.id_study_tmp"',
`id_lims` varchar(10) COLLATE utf8_unicode_ci NOT NULL COMMENT 'LIM system identifier',
`id_stock_resource_lims` varchar(20) COLLATE utf8_unicode_ci NOT NULL COMMENT 'Lims specific identifier for the stock',
`stock_resource_uuid` varchar(36) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'Uuid identifier for the stock',
`labware_type` varchar(255) COLLATE utf8_unicode_ci NOT NULL COMMENT 'The type of labware containing the stock. eg. Well, Tube',
`labware_machine_barcode` varchar(255) COLLATE utf8_unicode_ci NOT NULL COMMENT 'The barcode of the containing labware as read by a barcode scanner',
`labware_human_barcode` varchar(255) COLLATE utf8_unicode_ci NOT NULL COMMENT 'The barcode of the containing labware in human readable format',
`labware_coordinate` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'For wells, the coordinate on the containing plate. Null for tubes.',
`current_volume` float DEFAULT NULL COMMENT 'The current volume of material in microlitres based on measurements and know usage',
`initial_volume` float DEFAULT NULL COMMENT 'The result of the initial volume measurement in microlitres conducted on the material',
`concentration` float DEFAULT NULL COMMENT 'The concentration of material recorded in the lab in nanograms per microlitre',
`gel_pass` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'The recorded result for the qel QC assay.',
`pico_pass` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'The recorded result for the pico green assay. A pass indicates a successful assay, not sufficient material.',
`snp_count` int(11) DEFAULT NULL COMMENT 'The number of markers detected in genotyping assays',
`measured_gender` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'The gender call base on the genotyping assay',
PRIMARY KEY (`id_stock_resource_tmp`),
KEY `fk_stock_resource_to_sample` (`id_sample_tmp`),
KEY `fk_stock_resource_to_study` (`id_study_tmp`),
CONSTRAINT `fk_stock_resource_to_sample` FOREIGN KEY (`id_sample_tmp`) REFERENCES `sample` (`id_sample_tmp`),
CONSTRAINT `fk_stock_resource_to_study` FOREIGN KEY (`id_study_tmp`) REFERENCES `study` (`id_study_tmp`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Table structure for table `study`
--
Expand Down Expand Up @@ -290,7 +327,7 @@ CREATE TABLE `study_users` (
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2016-08-10 11:13:37
-- Dump completed on 2017-04-12 14:56:42
INSERT INTO schema_migrations (version) VALUES ('20141113110635');

INSERT INTO schema_migrations (version) VALUES ('20141113130813');
Expand Down Expand Up @@ -341,3 +378,7 @@ INSERT INTO schema_migrations (version) VALUES ('20160621125538');

INSERT INTO schema_migrations (version) VALUES ('20160810093024');

INSERT INTO schema_migrations (version) VALUES ('20160919144230');

INSERT INTO schema_migrations (version) VALUES ('20170412135215');

160 changes: 160 additions & 0 deletions spec/models/stock_resource_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
require 'spec_helper'

describe StockResource do

let!(:mock_sample) { create(:sample) }
let!(:mock_study) { create(:study) }

before(:each) do
mock_sample; mock_study
end

shared_examples_for 'a stock resource' do

it_behaves_like 'maps JSON fields', {
}

it_behaves_like 'ignores JSON fields', [
]

it_behaves_like 'belongs to',[
:sample,
:study
], [:samples]

it_behaves_like 'a nested resource'
end

context 'for a well' do
# We have a row for the lane, the sample and the control
let(:expected_entries) { 1 }

it_behaves_like 'a stock resource'

let(:json) do
{
"created_at" => "2012-03-11 10:22:42",
"updated_at" => "2012-03-11 10:22:42",

"samples" => [
"sample_uuid" => "000000-0000-0000-0000-0000000000",
"study_uuid" => "000000-0000-0000-0000-0000000001"
],

"stock_resource_id" => "12345",
"stock_resource_uuid" => "000000-0000-0000-0000-0000000002",

# We don't validate the barcode format, other
# LIMS might use different standards
"labware_machine_barcode" => "1220456987123",
"labware_human_barcode" => "DN456987D",
"labware_coordinate" => "A1",
"labware_type" => "well",

"current_volume" => 1.23,
"initial_volume" => 4.56,
"concentration" => 23.56,

"gel_pass" => "Pass",
"pico_pass" => "Pass",
"snp_count" => 2,
"measured_gender" => "Unknown"

}
end

context 'when update with identical tag indexes' do

let(:example_lims) { 'example' }

let(:updated_json) do
updated_json = json.dup
updated_json['current_volume'] = 1.00
updated_json['updated_at'] = "2012-03-11 10:22:42"
updated_json
end

it 'reuses the existing records' do
described_class.create_or_update_from_json(json, example_lims)
original_ids = described_class.pluck(:id_stock_resource_tmp,:id_sample_tmp)
described_class.create_or_update_from_json(updated_json, example_lims)
new_ids = described_class.pluck(:id_stock_resource_tmp,:id_sample_tmp)
expect(new_ids).to eq(original_ids)
end
end

end

context 'for a well with SS formatted data' do
# We have a row for the lane, the sample and the control
let(:expected_entries) { 1 }

it_behaves_like 'a stock resource'

let(:json) do
{
"created_at" => "2012-03-11T10:22:42+00:00",
"updated_at" => "2012-03-11T10:22:42+00:00",

"samples" => [
"sample_uuid" => "000000-0000-0000-0000-0000000000",
"study_uuid" => "000000-0000-0000-0000-0000000001"
],

"stock_resource_id" => 12345,
"stock_resource_uuid" => "000000-0000-0000-0000-0000000002",

# We don't validate the barcode format, other
# LIMS might use different standards
"labware_machine_barcode" => "1220456987123",
"labware_human_barcode" => "DN456987D",
"labware_coordinate" => "A1",
"labware_type" => "well",

"current_volume" => 1.23,
"initial_volume" => 4.56,
"concentration" => 23.56,

"gel_pass" => "Pass",
"pico_pass" => "Pass",
"snp_count" => 2,
"measured_gender" => "Unknown"

}
end

end


context 'for a tube' do

let(:expected_entries) { 1 }

let(:json) do
{
"created_at" => "2012-03-11 10:22:42",
"updated_at" => "2012-03-11 10:22:42",

"samples" => [
"sample_uuid" => "000000-0000-0000-0000-0000000000",
"study_uuid" => "000000-0000-0000-0000-0000000001"
],

"stock_resource_id" => "12345",
"stock_resource_uuid" => "000000-0000-0000-0000-0000000002",

# We don't validate the barcode format, other
# LIMS might use different standards
"machine_barcode" => "3040456987123",
"human_barcode" => "NT456987D",
"labware_type" => "tube"

# QC data will not always be present, especially for tubes
# So lets make sure its absence doesn't cause issues

}
end

end

end
5 changes: 4 additions & 1 deletion spec/support/it_behaves_like_belongs_to.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
let!(:json_handler) { described_class.send(:json) }

def find_target(json,graph)
return json if graph.nil?
return json[graph.to_s].first if graph.is_a?(Symbol)
return json[graph.first.to_s].first if (graph.count==1 && graph.first.is_a?(Symbol))
k,v = graph.first
find_target(json[k.to_s].first,v)
end

def find_handler(handler,graph)
return handler if graph.nil?
return handler.nested_models[graph] if graph.is_a?(Symbol)
return handler.nested_models[graph.first] if (graph.count==1 && graph.first.is_a?(Symbol))
k,v = graph.first
Expand Down Expand Up @@ -40,6 +42,7 @@ def find_handler(handler,graph)
end

after(:each) do
described_class.send(:create_or_update, subject.detect {|json| json.class == owning_handler })
content = subject.is_a?(Array) ? subject.detect {|json| json.class == owning_handler } : subject
described_class.send(:create_or_update, content)
end
end

0 comments on commit bfde29a

Please sign in to comment.