sample_spec.rb 5.15 KB
Newer Older
1
2
3
4
5
require 'rails_helper'

RSpec.describe Sample, type: :model do
  describe 'creation' do
    let(:sample) { create(:sample) }
6

7
8
9
    it 'is possible to create a valid sample' do
      expect(sample.valid?).to be(true)
    end
10
11
12
13

    it 'has molecule_name_id' do
      expect(sample.molecule_name_id).not_to be_blank
    end
14
15
  end

16
17
18
19
  describe 'after creation' do
    let(:sample)  { create(:sample) }
    let(:samples) { create_list(:sample, 500) }

PiTrem's avatar
PiTrem committed
20
21
22
23
24
    it 'has a CodeLog' do
      expect(sample.code_log.value).to match(/\d{40}/)
      expect(sample.code_log.id).to match(
      /^[0-9A-F]{8}-[0-9A-F]{4}-[4][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
      )
25
26
27
    end
  end

28
29
30
31
32
33
34
35
36
  describe 'deletion' do
    let(:sample)     { create(:sample) }
    let(:reaction_1) { create(:reaction) }
    let(:reaction_2) { create(:reaction) }
    let(:wellplate)  { create(:wellplate) }
    let(:well)       { create(:well, sample: sample, wellplate: wellplate) }
    let(:collection) { create(:collection) }

    before do
PiTrem's avatar
PiTrem committed
37
      #CollectionsSample.create!(sample: sample, collection: collection)
38
39
40
41
42
43
44
      ReactionsStartingMaterialSample.create!(sample: sample, reaction: reaction_1)
      ReactionsReactantSample.create!(sample: sample, reaction: reaction_1)
      ReactionsProductSample.create!(sample: sample, reaction: reaction_2)
      sample.destroy
      wellplate.reload
    end

PiTrem's avatar
PiTrem committed
45
46
    #todo check that the associations are not destroyed
    it 'does not destroy associations for reaction' do
47
      expect(collection.collections_samples).to eq []
PiTrem's avatar
PiTrem committed
48
49
50
51
52
      expect(sample.reactions_starting_material_samples).to eq []
      #expect(reaction_1.reactions_starting_material_samples).to eq sample.reactions_starting_material_samples
      #expect(reaction_1.reactions_reactant_samples).to eq sample
      #expect(reaction_2.reactions_product_samples).to eq sample
      #expect(wellplate.wells).to eq []
53
    end
54
55
56
57

    it 'only soft deletes sample' do
      expect(Sample.with_deleted).to eq [sample]
    end
Florian Hübsch's avatar
Florian Hübsch committed
58
59

    it 'also destroys corresponding CodeLog' do
PiTrem's avatar
PiTrem committed
60
      expect(CodeLog.where(source: "sample",source_id: sample.id)).to be_empty
Florian Hübsch's avatar
Florian Hübsch committed
61
    end
62
63
  end

64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
  describe 'for_ui_state scope' do
    let(:c1) { create(:collection) }
    let(:c2) { create(:collection) }
    let(:s1) { create(:sample) }
    let(:s2) { create(:sample) }
    let(:s3) { create(:sample) }

    let(:ui_state) {
      {
        all: true,
        included_ids: [],
        excluded_ids: [],
        collection_id: c1.id
      }
    }

    before do
      CollectionsSample.create!(collection: c1, sample: s1)
      CollectionsSample.create!(collection: c1, sample: s2)
      CollectionsSample.create!(collection: c2, sample: s3)
    end

    it 'returns samples according to ui_state' do
      expect(Sample.for_ui_state(ui_state)).to match_array([s1, s2])
    end
  end

91
92


Marco Sehrer's avatar
Marco Sehrer committed
93
  context 'updating molfile' do
PiTrem's avatar
PiTrem committed
94

Marco Sehrer's avatar
Marco Sehrer committed
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109

    let(:molfile) {
      <<-MOLFILE
H2O Water 7732185
##CCCBDB 8251509:58
Geometry Optimized at HF/STO-3G
  3  2  0  0  0  0  0  0  0  0    V2000
    0.0000    0.0000    0.1271 O  0000000000000000000
    0.0000    0.7580   -0.5085 H  0000000000000000000
    0.0000   -0.7580   -0.5085 H  0000000000000000000
  1  2  1  0     0  0
  1  3  1  0     0  0
M  END
MOLFILE
    }
PiTrem's avatar
PiTrem committed
110
    let(:sample) { build(:sample, molfile: molfile) }
PiTrem's avatar
PiTrem committed
111
112
113
    let(:mol_attributes){
      {
        "boiling_point" => nil,
jasonych99's avatar
jasonych99 committed
114
              "density" => 0.0,
PiTrem's avatar
PiTrem committed
115
116
117
118
119
120
121
122
123
124
125
126
             "inchikey" => "XLYOFNOQVPJJNP-UHFFFAOYSA-N",
        "inchistring" => "InChI=1S/H2O/h1H2",
          "iupac_name" => "oxidane",
       "melting_point" => nil,
   "molecular_weight" => 18.01528,
  #  "molecule_svg_file" => "XLYOFNOQVPJJNP-UHFFFAOYSA-N.svg", #todo
            "molfile" => molfile.rstrip,
              "names" => ["water", "oxidane"],
       "sum_formular" => "H2O"
     }
    }

PiTrem's avatar
PiTrem committed
127
128
    before do
      sample.collections << FactoryGirl.build(:collection)
PiTrem's avatar
PiTrem committed
129
      sample.creator = FactoryGirl.build(:person)
PiTrem's avatar
PiTrem committed
130
    end
PiTrem's avatar
PiTrem committed
131

Marco Sehrer's avatar
Marco Sehrer committed
132
    it 'should create a molecule' do
133
      sample.save!
Marco Sehrer's avatar
Marco Sehrer committed
134
135
136
137
      molecule = sample.molecule
      expect(molecule).to be_present
    end

PiTrem's avatar
PiTrem committed
138
    it 'should retrieve molecule information' do
An Nguyen's avatar
An Nguyen committed
139
      sample.save!
Marco Sehrer's avatar
Marco Sehrer committed
140
      molecule = sample.molecule
PiTrem's avatar
PiTrem committed
141
142
143
144
      mol_attributes.each do |k, v|
        expect(molecule.attributes[k]).to eq(v)
      end

PiTrem's avatar
PiTrem committed
145

Marco Sehrer's avatar
Marco Sehrer committed
146
147
    end

PiTrem's avatar
PiTrem committed
148
149
150
151
152
    ##Fixme : now file are anonymised
    #it 'should create the molecule svg file' do
    #  expect(File).to receive(:new).with('public/images/molecules/XLYOFNOQVPJJNP-UHFFFAOYSA-N.svg','w+').and_call_original
    #  sample.save
    #end
Marco Sehrer's avatar
Marco Sehrer committed
153
154
155

  end

Marco Sehrer's avatar
Marco Sehrer committed
156
  context 'count samples created by user' do
PiTrem's avatar
PiTrem committed
157
    let(:user) { create(:person)}
Marco Sehrer's avatar
Marco Sehrer committed
158
159
160
161
162
163
164
165
166
167
168
169
170
171

    before do
      3.times do
        create(:sample, creator: user)
      end
    end

    it 'should associate the samples with its creator' do
      expect(Sample.last.creator).to eq(user)
      expect(user.samples_created.count).to eq(3)
    end

    it 'should count samples created by user' do
      user.reload
172
      expect(user.counters['samples'].to_i).to eq(3)
Marco Sehrer's avatar
Marco Sehrer committed
173
174
    end
  end
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189


  context 'count subsamples created per sample' do
    let(:sample) { create(:sample)}

    before do
      3.times do
        create(:sample, parent: sample)
      end
    end

    it 'should associate the subsamples with its parent' do
      expect(sample.children.count).to eq(3)
    end
  end
190
end