Merge pull request #5191 from solgenomics/topic/quality_control
[sgn.git] / t / unit_mech / AJAX / LabelDesign.t
blob7853dc47ecec1e70d4ce8b88b76d487720b0c33a
2 use strict;
3 use warnings;
5 use lib 't/lib';
6 use SGN::Test::Fixture;
7 use Test::More;
8 use Test::WWW::Mechanize;
9 use Data::Dumper;
10 use CAM::PDF;
11 use File::Slurp;
12 use JSON;
14 my $f = SGN::Test::Fixture->new();
15 my $schema = $f->bcs_schema;
17 my $mech = Test::WWW::Mechanize->new;
19 $mech->get_ok("http://localhost:3010//tools/label_designer/retrieve_longest_fields?data_type=Field%20Trials&source_id=139&data_level=plots");
20 my $response = decode_json $mech->content;
21 print STDERR Dumper $response;
23 my $expected_response = {
24           'num_units' => 692,
25           'fields' => {
26                         'plot_id' => 39295,
27                         'trial_name' => 'Kasese solgs trial',
28                         'block_number' => '10',
29                         'tier' => '/',
30                         'location_name' => 'test_location',
31                         'rep_number' => '1',
32                         'year' => '2014',
33                         'dry matter content percentage|CO_334:0000092' => '21.2',
34                         'accession_name' => 'UG120054',
35                         'fresh shoot weight measurement in kg|CO_334:0000016' => '13.5',
36                         'pedigree' => 'NA/NA',
37                         'plot_number' => 35667,
38                         'fresh root weight|CO_334:0000012' => '15.37',
39                         'accession_id' => 38926,
40                         'plot_name' => 'KASESE_TP2013_1000'
41                       },
42           'reps' => {
43                       '1' => 370,
44                       '2' => 322
45                     }
46         };
49 is_deeply($response, $expected_response, 'retrieve longest fields test');
51 my $download_type = 'pdf';
52 my $data_type = 'Field Trials';
53 my $source_id = 139;
54 my $data_level = 'plots';
55 my $design_json = encode_json {
56    "page_format" => "US Letter PDF",
57    "page_width" => 611,
58    "page_height" => 790.7,
59    "left_margin" => 13.68,
60    "top_margin" => 36.7,
61    "horizontal_gap" => 10,
62    "vertical_gap" => 0,
63    "number_of_columns" => 3,
64    "number_of_rows" => 10,
65    "plot_filter" => 'all',
66    "copies_per_plot" => "1",
67    "sort_order_1" => "plot_number",
68    "label_format" => "1\" x 2 5/8\"",
69    "label_width" => 189,
70    "label_height" => 72,
71    "label_elements" => [
72       {
73          "x" => 113.5,
74          "y" => 105.5,
75          "height" => 125,
76          "width" => 125,
77          "value" => "{plot_name}",
78          "type" => "QRCode",
79          "font" => "Courier",
80          "size" => "5"
81       },
82       {
83          "x" => 343,
84          "y" => 111,
85          "height" => 56.140625,
86          "width" => 241.21875,
87          "value" => "{accession_name}",
88          "type" => "PDFText",
89          "font" => "Courier-Bold",
90          "size" => "50"
91      },
92      {
93          "x" => 323,
94          "y" => 167,
95          "height" => 38,
96          "width" => 242,
97          "value" => "{trial_name}",
98          "type" => "Code128",
99          "font" => "Courier",
100          "size" => "1"
101       },
102       {
103          "x" => 323,
104          "y" => 90,
105          "height" => 26.640625,
106          "width" => 146.53125,
107          "value" => "Plot: {plot_number}",
108          "type" => "PDFText",
109          "font" => "Courier",
110          "size" => "23"
111       },
112       {
113          "x" => 318,
114          "y" => 121,
115          "height" => 26.640625,
116          "width" => 159.84375,
117          "value" => "NIR: 17-{Number:0001:1}",
118          "type" => "PDFText",
119          "font" => "Courier",
120          "size" => "23"
121       }
122    ]
125 # print STDERR Dumper $design_json;
127 $mech->post_ok('http://localhost:3010/tools/label_designer/download', [ 'download_type' => $download_type, 'data_type' => $data_type, 'source_id'=> $source_id, 'data_level' => $data_level, 'design_json' => $design_json ]);
128 $response = decode_json $mech->content;
129 print STDERR Dumper $response;
131 my $file = $response->{'filepath'};
132 my $pdf = CAM::PDF->new($file);
133 print STDERR "Page 1 text: " . Dumper $pdf->getPageText(1);
135 my $expected_text = "UG120054 Plot: 35667 NIR: 17-0001 UG130026 Plot: 35668 NIR: 17-0002 UG130106 Plot: 35669 NIR: 17-0003 UG130034 Plot: 35670 NIR: 17-0004 UG120062 Plot: 35671 NIR: 17-0005 UG130071 Plot: 35672 NIR: 17-0006 UG120251 Plot: 35673 NIR: 17-0007 UG130088 Plot: 35674 NIR: 17-0008 UG120022 Plot: 35675 NIR: 17-0009 UG120141 Plot: 35676 NIR: 17-0010 UG120161 Plot: 35677 NIR: 17-0011 UG130046 Plot: 35678 NIR: 17-0012 UG120017 Plot: 35679 NIR: 17-0013 UG120053 Plot: 35680 NIR: 17-0014 UG120239 Plot: 35681 NIR: 17-0015 UG130111 Plot: 35682 NIR: 17-0016 UG130011 Plot: 35683 NIR: 17-0017 UG130096 Plot: 35684 NIR: 17-0018 UG130105 Plot: 35685 NIR: 17-0019 UG130131 Plot: 35686 NIR: 17-0020 UG120163 Plot: 35687 NIR: 17-0021 UG120257 Plot: 35688 NIR: 17-0022 UG120027 Plot: 35689 NIR: 17-0023 UG120195 Plot: 35690 NIR: 17-0024 UG120240 Plot: 35691 NIR: 17-0025 UG130007 Plot: 35692 NIR: 17-0026 UG120300 Plot: 35693 NIR: 17-0027 UG120291 Plot: 35694 NIR: 17-0028 UG120196 Plot: 35695 NIR: 17-0029 UG130098 Plot: 35696 NIR: 17-0030\n";
136 is($pdf->getPageText(1), $expected_text, 'download pdf test');
139 $download_type = 'zpl';
140 $design_json = encode_json {
141    "page_format" => "Zebra printer file",
142    "page_width" => "",
143    "page_height" => "",
144    "copies_per_plot" => "1",
145    "sort_order_1" => "plot_number",
146    "label_format" => "1 1/4\" x 2\"",
147    "label_width" => 144,
148    "label_height" => 90,
149    "label_elements" => [
150       {
151          "x" => 197,
152          "y" => 48,
153          "height" => 67.734375,
154          "width" => 286.5625,
155          "value" => "{accession_name}",
156          "type" => "ZebraText",
157          "font" => "Courier",
158          "size" => "54"
159       },
160       {
161          "x" => 90,
162          "y" => 130,
163          "height" => 100,
164          "width" => 100,
165          "value" => "{plot_name}",
166          "type" => "QRCode",
167          "font" => "Courier",
168          "size" => "4"
169      },
170      {
171          "x" => 202,
172          "y" => 217,
173          "height" => 38,
174          "width" => 242,
175          "value" => "{trial_name}",
176          "type" => "Code128",
177          "font" => "Courier",
178          "size" => "1"
179       },
180       {
181          "x" => 323,
182          "y" => 90,
183          "height" => 26.640625,
184          "width" => 146.53125,
185          "value" => "Plot: {plot_number}",
186          "type" => "PDFText",
187          "font" => "Courier",
188          "size" => "23"
189       },
190       {
191          "x" => 266,
192          "y" => 128,
193          "height" => 34.25,
194          "width" => 197,
195          "value" => "Plot: {plot_number}",
196          "type" => "ZebraText",
197          "font" => "Courier",
198          "size" => "27"
199       }
200    ]
203 $mech->post_ok('http://localhost:3010/tools/label_designer/download', [ 'download_type' => $download_type, 'data_type' => $data_type, 'source_id'=> $source_id, 'data_level' => $data_level, 'design_json' => $design_json ]);
204 $response = decode_json $mech->content;
206 $file = $response->{'filepath'};
207 my $file_content = read_file($file);
208 #print STDERR "File Content is:\n$file_content";
209 $file_content = substr $file_content, 0, 399;
210 #print STDERR "Substring File Content is:\n$file_content";
211 my $expected_content = '^XA
212 ^LL254.7^PW407.52
213 ^FO53.71875,14.1328125^AA,54^FDUG120054^FS
214 ^FO40,70^BQ,,4^FDMA,KASESE_TP2013_666^FS
215 ^FO81,198^BCN,25,N,N,N^FD   Kasese solgs trial^FS
216 ^FO167.5,110.875^AA,27^FDPlot: 35667^FS
219 ^LL254.7^PW407.52
220 ^FO53.71875,14.1328125^AA,54^FDUG130026^FS
221 ^FO40,70^BQ,,4^FDMA,KASESE_TP2013_667^FS
222 ^FO81,198^BCN,25,N,N,N^FD   Kasese solgs trial^FS
223 ^FO167.5,110.875^AA,27^FDPlot: 35668^FS
224 ^XZ';
225 is($file_content, $expected_content, 'download zpl test');
228 done_testing;