detail_reaction_spec.rb 8.82 KB
Newer Older
1
2
require 'rails_helper'

3
describe 'Reporter::Docx::DetailReaction instance' do
4
  let(:tit)   { 'According to General Procedure N' }
5
6
7
8
9
10
  let(:sta)   { 'Planned' }
  let(:sol)   { 'correct solvent' }
  let(:pur)   { '{TLC, Distillation}' }
  let(:rf)    { 'correct tlc_rf' }
  let(:t_sol) { 'correct tlc_solvents' }
  let(:t_des) { 'correct tlc_description' }
11
12
  let!(:correct_obsv) { "correct observation" }
  let(:obs)   { { "ops" => [{"insert" => "#{correct_obsv}\n" }] } }
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  let(:des)   { { "ops" => [{"insert" => "correct description" }] } }
  let(:prev_index) { 5 }
  let(:equiv) { 0.88 }
  let(:d1)    { "Damage to environment" }
  let(:d2)    { "Explosive (Class 1)" }
  let(:dangerous) { "{\"#{d1}\", \"#{d2}\"}" }
  let!(:user) { create(:user) }
  let!(:gp)   { create(:group, users: [user]) }
  let!(:c1)   { create(:collection,
                        label: 'C1',
                        user: user,
                        permission_level: 10,
                        sample_detail_level: 10,
                        reaction_detail_level: 10,
                        is_shared: false) }
  let!(:r1)   { create(:reaction, name: tit,
29
30
31
32
33
34
35
                                  status: sta,
                                  solvent: sol,
                                  description: des,
                                  purification: pur,
                                  rf_value: rf,
                                  tlc_solvents: t_sol,
                                  tlc_description: t_des,
36
37
38
39
40
                                  observation: obs,
                                  dangerous_products: dangerous) }
  let!(:s1) { create(:sample, name: 'Sample 1') }
  let!(:s2) { create(:sample, name: 'Sample 2') }
  let!(:s3) { create(:sample, name: 'Sample 3') }
jasonych99's avatar
jasonych99 committed
41
42
  let!(:s4) { create(:sample, name: 'Solvent') }
  let!(:correct_content) { "analysis contents (true for report)" }
43
  let!(:non_breaking_space) { " " }
jasonych99's avatar
jasonych99 committed
44
  let!(:inverse) { "{\"attributes\":{\"color\":\"black\",\"script\":\"super\"},\"insert\":\"-1\"}" }
45
46
47
48
49
50
51
52
53
54
55
56
57
58
  let!(:r1_serialized) do
    CollectionsReaction.create!(reaction: r1, collection: c1)
    CollectionsSample.create!(sample: s1, collection: c1)
    CollectionsSample.create!(sample: s2, collection: c1)
    CollectionsSample.create!(sample: s3, collection: c1)
    ReactionsStartingMaterialSample.create!(
      reaction: r1, sample: s1, equivalent: equiv
    )
    ReactionsProductSample.create!(
      reaction: r1, sample: s2, equivalent: equiv
    )
    ReactionsProductSample.create!(
      reaction: r1, sample: s3, equivalent: equiv
    )
jasonych99's avatar
jasonych99 committed
59
60
61
62
63
    ReactionsSolventSample.create!(
      reaction: r1, sample: s4, equivalent: equiv
    )
    con = r1.products[0].container.children[0].children[0]
    con.extended_metadata["report"] = "true"
jasonych99's avatar
jasonych99 committed
64
65
66
67
68
69
70
71
    con.extended_metadata["content"] = "{\"ops\":
      [
        {\"insert\": \"  \\n\"},
        #{inverse},
        {\"insert\": \"#{correct_content} #{non_breaking_space} \"},
        {\"insert\": \" #{non_breaking_space} ;#{non_breaking_space} \\n\"}
      ]
    }"
jasonych99's avatar
jasonych99 committed
72
73
    con.save!

74
75
    ElementReportPermissionProxy.new(user, r1, [user.id]).serialized
  end
jasonych99's avatar
jasonych99 committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
  let!(:serial) { '1a' }
  let!(:mol_serials) do
    mol = s2.molecule
    [
      {
        'mol' => {
          'id' => mol.id,
          'svgPath' => mol.molecule_svg_file,
          'sumFormula' => mol.sum_formular,
          'iupacName' => mol.iupac_name
        },
        'value' => serial
      }
    ]
  end
91
92
  let!(:target) do
    Reporter::Docx::DetailReaction.new(reaction: OpenStruct.new(r1_serialized),
jasonych99's avatar
jasonych99 committed
93
                                        mol_serials: mol_serials,
94
95
                                        index: prev_index)
  end
96
97

  context '.content' do
98
    let(:content) { target.content }
99
100
101
102
103

    it "returns a Hash" do
      expect(content.class).to eq(Hash)
    end

jasonych99's avatar
jasonych99 committed
104
    it "has a png image & a bin file" do
105
106
107
      expect(content[:equation_reaction].class).to eq(Sablon::Chem::Definition)
      expect(content[:equation_reaction].img.name.split('.').last).to eq('png')
      expect(content[:equation_reaction].ole.name.split('.').last).to eq('bin')
108
109
110
111
112
113
114
115
116
    end

    it "has a correct status" do
      expect(content[:status].name).to include('png')
      expect(content[:status].name).to include(sta.downcase)
    end

    it "has correct content" do
      expect(content[:title]).to eq(tit)
jasonych99's avatar
jasonych99 committed
117
      expect(content[:solvents]).to eq("#{s4.preferred_label} (0.000ml)")
118
119
120
      expect(content[:description]).to eq(
        Sablon.content(:html, Reporter::Delta.new(des).getHTML())
      )
121
122
123
      expect(content[:observation]).to eq(
        Sablon.content(:html, Reporter::Delta.new(obs).getHTML())
      )
124
125
126
      expect(content[:tlc_rf]).to eq(rf)
      expect(content[:tlc_solvent]).to eq(t_sol)
      expect(content[:tlc_description]).to eq(t_des)
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
      expect(content[:short_label]).to eq(r1.short_label)
      expect(content[:products_html].class).to eq(Sablon::Content::HTML)
      expect(content[:synthesis_html].class).to eq(Sablon::Content::HTML)

      pur.tr('{}', '').split(',').each do |p|
        expect(content[:purification]).to include(p)
      end
    end
  end

  context 'private methods' do
    it "has correct data" do
      expect(target.send(:title)).to eq(tit)
      expect(target.send(:gp_title_delta)).to eq(
        [
          {"insert"=>"[3.#{prev_index + 1}] "},
          {"insert"=>"#{tit} "},
          {"insert"=>"(#{r1.short_label})"}
        ]
      )
      expect(target.send(:synthesis_title_delta)).to eq(
        [
          {"insert"=>"[4.#{prev_index + 1}] "},
150
          {"insert"=>"#{s2.molecule_name_hash[:label]}"},
jasonych99's avatar
jasonych99 committed
151
          {"insert"=>" ("},
jasonych99's avatar
jasonych99 committed
152
          {"insert"=>serial, "attributes"=>{"bold"=>"true"}},
jasonych99's avatar
jasonych99 committed
153
154
          {"insert"=>")"},
          {"insert"=>", "},
155
          {"insert"=>"#{s3.molecule_name_hash[:label]}"},
156
          {"insert"=>" ("},
jasonych99's avatar
jasonych99 committed
157
          {"insert"=>serial, "attributes"=>{"bold"=>"true"}},
jasonych99's avatar
jasonych99 committed
158
          {"insert"=>")"},
159
160
161
162
163
        ]
      )
      expect(target.send(:products_delta)).to eq(
        [
          {"insert"=>"Name: "},
164
          {"insert"=>"#{s2.molecule_name_hash[:label]}"},
165
166
167
168
169
170
171
172
173
174
175
          {"insert"=>"; "},
          {"insert"=>"Formula: "},
          {"insert"=>"H"},
          {"attributes"=>{"script"=>"sub"}, "insert"=>"2"},
          {"insert"=>"O"},
          {"insert"=>"; "},
          {"insert"=>"CAS: - ; " +
                      "Smiles: #{s2.molecule.cano_smiles}; " +
                      "InCHI: #{s2.molecule.inchikey}; " +
                      "Molecular Mass: 18.0153; Exact Mass: 18.0106; "},
          {"insert"=>"EA: "},
176
          {"insert"=>"H, 11.19; O, 88.81"},
177
178
179
          {"insert"=>"."},
          {"insert"=>"\n"},
          {"insert"=>"Name: "},
180
          {"insert"=>"#{s3.molecule_name_hash[:label]}"},
181
182
183
184
185
186
187
188
189
190
191
192
          {"insert"=>"; "},
          {"insert"=>"Formula: "},
          {"insert"=>"H"},
          {"attributes"=>{"script"=>"sub"}, "insert"=>"2"},
          {"insert"=>"O"},
          {"insert"=>"; "},
          {"insert"=>"CAS: - ; " +
                      "Smiles: #{s3.molecule.cano_smiles}; " +
                      "InCHI: #{s3.molecule.inchikey}; " +
                      "Molecular Mass: 18.0153; " +
                      "Exact Mass: 18.0106; "},
          {"insert"=>"EA: "},
193
          {"insert"=>"H, 11.19; O, 88.81"},
194
195
196
197
198
199
200
          {"insert"=>"."},
          {"insert"=>"\n"}
        ]
      )
      expect(target.send(:synthesis_delta)).to eq(
        [
          {"insert"=>"#{tit}: "},
201
          {"insert"=>"{A|"},
jasonych99's avatar
jasonych99 committed
202
          {"insert"=>serial, "attributes"=>{"bold"=>"true"}},
203
          {"insert"=>"} "},
204
          {"insert"=>"#{s2.molecule_name_hash[:label]}"},
jasonych99's avatar
jasonych99 committed
205
          {"insert"=>" (1.000 g, 55.508 mmol, 0.88 equiv.); "},
206
          {"insert"=>"{B"},
jasonych99's avatar
jasonych99 committed
207
208
209
          {"insert"=>"} "},
          {"insert"=>s4.preferred_label},
          {"insert"=>" (0.00 mL); "},
210
          {"insert"=>"Yield "},
211
          {"insert"=>"{C|"},
jasonych99's avatar
jasonych99 committed
212
          {"insert"=>serial, "attributes"=>{"bold"=>"true"}},
jasonych99's avatar
jasonych99 committed
213
          {"insert"=>"} = #{(equiv * 100).to_i}% (0.000 g, 0.000 mmol)"},
214
          {"insert"=>"; "},
215
          {"insert"=>"{D|"},
jasonych99's avatar
jasonych99 committed
216
          {"insert"=>serial, "attributes"=>{"bold"=>"true"}},
jasonych99's avatar
jasonych99 committed
217
          {"insert"=>"} = #{(equiv * 100).to_i}% (0.000 g, 0.000 mmol)"},
218
219
          {"insert"=>"."},
          {"insert"=>"\n"},
220
221
          {"insert"=>"#{correct_obsv}"},
          {"insert"=>"."},
jasonych99's avatar
jasonych99 committed
222
          {"insert"=>" "},
jasonych99's avatar
jasonych99 committed
223
          {"attributes"=>{"italic"=>true}, "insert"=>"R"},
224
225
          {"attributes"=>{"italic"=>true, "script"=>"sub"}, "insert"=>"f"},
          {"insert"=>" = #{rf} (#{t_sol})."}, {"insert"=>"\n"},
jasonych99's avatar
jasonych99 committed
226
          {"attributes"=>{"color"=>"black", "script"=>"super"}, "insert"=>"-1"},
jasonych99's avatar
jasonych99 committed
227
          {"insert"=>correct_content},
228
          {"insert"=>"."},
229
          {"insert"=>"\n"},
jasonych99's avatar
jasonych99 committed
230
          {"insert"=>"\n"},
jasonych99's avatar
jasonych99 committed
231
          {"attributes"=>{"bold"=>"true"}, "insert"=>"Attention! "},
232
233
234
235
          {"insert"=>"The reaction includes the use of dangerous chemicals, " +
                      "which have the following classification: #{d1}, #{d2}."}
        ]
      )
236
237
238
    end
  end
end