sample_spec.rb 4.59 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
10
11
    it 'is possible to create a valid sample' do
      expect(sample.valid?).to be(true)
    end
  end

12
13
14
15
16
17
18
19
20
  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
21
      #CollectionsSample.create!(sample: sample, collection: collection)
22
23
24
25
26
27
28
      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
29
30
    #todo check that the associations are not destroyed
    it 'does not destroy associations for reaction' do
31
      expect(collection.collections_samples).to eq []
PiTrem's avatar
PiTrem committed
32
33
34
35
36
      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 []
37
    end
38
39
40
41

    it 'only soft deletes sample' do
      expect(Sample.with_deleted).to eq [sample]
    end
42
43
  end

44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
  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

71
72


Marco Sehrer's avatar
Marco Sehrer committed
73
  context 'updating molfile' do
PiTrem's avatar
PiTrem committed
74

Marco Sehrer's avatar
Marco Sehrer committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89

    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
90
    let(:sample) { build(:sample, molfile: molfile) }
PiTrem's avatar
PiTrem committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
    let(:mol_attributes){
      {
        "boiling_point" => nil,
              "density" => nil,
             "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
107
108
    before do
      sample.collections << FactoryGirl.build(:collection)
PiTrem's avatar
PiTrem committed
109
      sample.creator = FactoryGirl.build(:person)
PiTrem's avatar
PiTrem committed
110
    end
PiTrem's avatar
PiTrem committed
111

Marco Sehrer's avatar
Marco Sehrer committed
112
    it 'should create a molecule' do
113
      sample.save!
Marco Sehrer's avatar
Marco Sehrer committed
114
115
116
117
      molecule = sample.molecule
      expect(molecule).to be_present
    end

PiTrem's avatar
PiTrem committed
118
    it 'should retrieve molecule information' do
Marco Sehrer's avatar
Marco Sehrer committed
119
120
      sample.save
      molecule = sample.molecule
PiTrem's avatar
PiTrem committed
121
122
123
124
      mol_attributes.each do |k, v|
        expect(molecule.attributes[k]).to eq(v)
      end

PiTrem's avatar
PiTrem committed
125

Marco Sehrer's avatar
Marco Sehrer committed
126
127
    end

PiTrem's avatar
PiTrem committed
128
129
130
131
132
    ##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
133
134
135

  end

Marco Sehrer's avatar
Marco Sehrer committed
136
  context 'count samples created by user' do
PiTrem's avatar
PiTrem committed
137
    let(:user) { create(:person)}
Marco Sehrer's avatar
Marco Sehrer committed
138
139
140
141
142
143
144
145
146
147
148
149
150
151

    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
152
      expect(user.counters['samples'].to_i).to eq(3)
Marco Sehrer's avatar
Marco Sehrer committed
153
154
    end
  end
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169


  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
170
end