Commit 6128d0c2 authored by Marco Sehrer's avatar Marco Sehrer
Browse files

Merge pull request #42 from ninjaconcept/37-molecule-model

Creation of the Molecule model
parents 09d70b77 6f98279b
class Molecule < ActiveRecord::Base
has_many :samples
validates_uniqueness_of :inchikey
end
......@@ -9,6 +9,8 @@ class Sample < ActiveRecord::Base
has_many :reactions_as_starting_material, through: :reactions_starting_material_samples, source: :reaction
has_many :reactions_as_reactant, through: :reactions_reactant_samples, source: :reaction
has_many :reactions_as_product, through: :reactions_product_samples, source: :reaction
belongs_to :molecule
composed_of :amount, mapping: %w(amount_value, amount_unit)
end
class CreateMolecules < ActiveRecord::Migration
def change
create_table :molecules do |t|
t.string :inchikey
t.string :inchistring
t.float :density
t.float :molecular_weight
t.binary :molfile
t.float :melting_point
t.float :boiling_point
t.string :sum_formular
t.string :names, array: true, default: []
t.string :iupac_name
t.string :molecule_svg_file
t.timestamps null: false
end
add_index :molecules, :inchikey, unique: true
end
end
class AddMoleculeIdToSamples < ActiveRecord::Migration
def change
add_column :samples, :molecule_id, :integer
end
end
......@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20150817085601) do
ActiveRecord::Schema.define(version: 20150818100730) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
......@@ -49,6 +49,25 @@ ActiveRecord::Schema.define(version: 20150817085601) do
add_index "collections_samples", ["collection_id"], name: "index_collections_samples_on_collection_id", using: :btree
add_index "collections_samples", ["sample_id"], name: "index_collections_samples_on_sample_id", using: :btree
create_table "molecules", force: :cascade do |t|
t.integer "sample_id"
t.string "inchikey"
t.string "inchistring"
t.float "density"
t.float "molecular_weight"
t.binary "molfile"
t.float "melting_point"
t.float "boiling_point"
t.string "sum_formular"
t.string "names", default: [], array: true
t.string "iupac_name"
t.string "molecule_svg_file"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "molecules", ["inchikey"], name: "index_molecules_on_inchikey", unique: true, using: :btree
create_table "reactions", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
......@@ -86,6 +105,7 @@ ActiveRecord::Schema.define(version: 20150817085601) do
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.text "description", default: ""
t.integer "molecule_id"
end
create_table "users", force: :cascade do |t|
......
FactoryGirl.define do
factory :molecule do
sample_id "1"
inchikey "inchikey"
inchistring "inchistring"
density 0.12345
molecular_weight 0.54321
molfile ""
melting_point 150.00
boiling_point 100.00
sum_formular "sum_formular"
names %w(name1 name2 name3)
iupac_name "iupac_name"
molecule_svg_file "molecule_svg_file"
end
end
require 'rails_helper'
require 'digest'
RSpec.describe Molecule, type: :model do
describe 'creation' do
let(:molecule) { create(:molecule) }
it 'is possible to create a valid molecule' do
expect(molecule.valid?).to be(true)
end
it 'should have a unique inchikey' do
molecule.save
invalid_molecule = Molecule.new
invalid_molecule.inchikey = molecule.inchikey
expect {invalid_molecule.save!}.to raise_error
end
end
describe 'persistance' do
let (:molecule) { build(:molecule) }
it 'should persist array of names' do
molecule.names = %w(foo bar quz)
molecule.save
persisted_molecule = Molecule.last
expect(persisted_molecule.names).to match_array(molecule.names)
end
it 'should persist the binary molfile' do
molfile_example = File.open("spec/models/molecule_spec.rb", "rb")
molecule.molfile = molfile_example.read
molfile_example.close
molecule.save
persisted_molecule = Molecule.last
persisted_molfile_SHA =
(Digest::SHA256.new << persisted_molecule.molfile).hexdigest
molfile_SHA =
(Digest::SHA256.new << molecule.molfile).hexdigest
expect(persisted_molfile_SHA).to be === (molfile_SHA)
end
end
end
......@@ -11,4 +11,18 @@ RSpec.describe Sample, type: :model do
context 'with molecule' do
let(:sample) { build(:sample) }
let(:molecule) {create(:molecule)}
it 'it should belong to a sample' do
sample.molecule = molecule
sample.save
persisted_sample = Sample.last
expect(persisted_sample.molecule).to be === (molecule)
end
end
end
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment