-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmodels.rs
More file actions
973 lines (840 loc) · 32.9 KB
/
models.rs
File metadata and controls
973 lines (840 loc) · 32.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
/*!
* Trait implementations for [`crate::report::models`] types for use with
* `rusqlite`.
*
* `ToSql`/`FromSql` allow enums to be used as model fields.
* `TryFrom<&rusqlite::Row>` allows models to be automatically constructed
* from query results (provided the query's column names are
* named appropriately). [`Insertable`] takes care of the boilerplate to
* insert a model into the database when provided a few constants for each
* model.
*/
use rusqlite::types::{FromSql, FromSqlResult, ToSql, ToSqlOutput, ValueRef};
use super::super::models::*;
use crate::{error::Result, parsers::json::JsonVal};
/// Takes care of the boilerplate to insert a model into the database.
/// Implementers must provide three things:
/// - `const TABLE_NAME`: The name of the table
/// - `const FIELDS`: The names of all the fields
/// - `fn extend_params`: A function that fills in the field values. The number
/// and order of params has to match those in `FIELDS`.
///
/// # Examples
///
/// ```
/// # use codecov_rs::report::sqlite::Insertable;
/// struct File {
/// id: i64,
/// path: String,
/// }
///
/// impl Insertable for File {
/// const TABLE_NAME: &'static str = "file";
/// const FIELDS: &'static [&'static str] = &["id", "path"];
///
/// fn extend_params<'a>(&'a self, params: &mut Vec<&'a dyn rusqlite::ToSql>) {
/// params.extend(&[
/// &self.id as &dyn rusqlite::ToSql,
/// &self.path as &dyn rusqlite::ToSql,
/// ])
/// }
/// }
/// ```
///
/// IDs are not assigned automatically; assign your own to models before you
/// insert them.
pub trait Insertable {
/// The name of the table.
const TABLE_NAME: &'static str;
/// The field names to be inserted.
const FIELDS: &'static [&'static str];
/// This method is supposed to extend the input `params` with the parameters
/// matching the `FIELDS`.
fn extend_params<'a>(&'a self, params: &mut Vec<&'a dyn rusqlite::ToSql>);
/// Determines the maximum chunk size depending on the number of fields and
/// placeholder limit.
fn maximum_chunk_size(conn: &rusqlite::Connection) -> usize {
let var_limit = conn.limit(rusqlite::limits::Limit::SQLITE_LIMIT_VARIABLE_NUMBER) as usize;
// If each model takes up `FIELDS` variables, we can fit `var_limit /
// FIELDS` complete models in each "page" of our query
var_limit / Self::FIELDS.len()
}
/// Dynamically builds an `INSERT` query suitable for the given number of
/// `rows`.
fn build_query(rows: usize) -> String {
let mut query = format!("INSERT INTO {} (", Self::TABLE_NAME);
let mut placeholder = String::from('(');
for (i, field) in Self::FIELDS.iter().enumerate() {
if i > 0 {
placeholder.push_str(", ");
query.push_str(", ");
}
placeholder.push('?');
query.push_str(field);
}
placeholder.push(')');
query.push_str(") VALUES ");
for i in 0..rows {
if i > 0 {
query.push_str(", ");
}
query.push_str(&placeholder);
}
query.push(';');
query
}
fn insert(&self, conn: &rusqlite::Connection) -> Result<()> {
let mut stmt = conn.prepare_cached(&Self::build_query(1))?;
let mut params = vec![];
self.extend_params(&mut params);
stmt.execute(params.as_slice())?;
Ok(())
}
fn multi_insert<'a>(
mut models: impl ExactSizeIterator<Item = &'a Self>,
conn: &rusqlite::Connection,
) -> Result<()>
where
Self: 'a,
{
let chunk_size = Self::maximum_chunk_size(conn);
let mut params = Vec::with_capacity(Self::FIELDS.len() * (models.len().min(chunk_size)));
// first: insert huge chunks using a single prepared (cached) query
if models.len() >= chunk_size {
let mut chunked_stmt = conn.prepare_cached(&Self::build_query(chunk_size))?;
while models.len() >= chunk_size {
for row in models.by_ref().take(chunk_size) {
row.extend_params(&mut params);
}
chunked_stmt.execute(params.as_slice())?;
params.clear();
}
}
// then: insert the remainder
if models.len() > 0 {
// this statement is not cached, as the number of models / params can be
// different for every call
let mut remainder_stmt = conn.prepare(&Self::build_query(models.len()))?;
for row in models {
row.extend_params(&mut params);
}
remainder_stmt.execute(params.as_slice())?;
params.clear();
}
Ok(())
}
}
/// Can't implement foreign traits (`ToSql`/`FromSql`) on foreign types
/// (`serde_json::Value`) so this helper function fills in.
pub fn json_value_from_sql(s: String, col: usize) -> rusqlite::Result<JsonVal> {
serde_json::from_str(s.as_str()).map_err(|e| {
rusqlite::Error::FromSqlConversionFailure(col, rusqlite::types::Type::Text, Box::new(e))
})
}
impl ToSql for CoverageType {
fn to_sql(&self) -> rusqlite::Result<ToSqlOutput<'_>> {
match self {
CoverageType::Line => Ok("l".into()),
CoverageType::Branch => Ok("b".into()),
CoverageType::Method => Ok("m".into()),
}
}
}
impl FromSql for CoverageType {
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
let variant = match value.as_str()? {
"l" => CoverageType::Line,
"b" => CoverageType::Branch,
"m" => CoverageType::Method,
_ => panic!("Uh oh"),
};
Ok(variant)
}
}
impl ToSql for BranchFormat {
fn to_sql(&self) -> rusqlite::Result<ToSqlOutput<'_>> {
match self {
BranchFormat::Line => Ok("l".into()),
BranchFormat::Condition => Ok("c".into()),
BranchFormat::BlockAndBranch => Ok("bb".into()),
}
}
}
impl FromSql for BranchFormat {
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
let variant = match value.as_str()? {
"l" => BranchFormat::Line,
"c" => BranchFormat::Condition,
"bb" => BranchFormat::BlockAndBranch,
_ => panic!("Uh oh"),
};
Ok(variant)
}
}
impl<'a> std::convert::TryFrom<&'a rusqlite::Row<'a>> for SourceFile {
type Error = rusqlite::Error;
fn try_from(row: &'a ::rusqlite::Row) -> Result<Self, Self::Error> {
Ok(Self {
id: row.get(row.as_ref().column_index("id")?)?,
path: row.get(row.as_ref().column_index("path")?)?,
})
}
}
impl Insertable for SourceFile {
const TABLE_NAME: &'static str = "source_file";
const FIELDS: &'static [&'static str] = &["id", "path"];
fn extend_params<'a>(&'a self, params: &mut Vec<&'a dyn rusqlite::ToSql>) {
params.extend(&[
&self.id as &dyn rusqlite::ToSql,
&self.path as &dyn rusqlite::ToSql,
])
}
}
impl<'a> std::convert::TryFrom<&'a rusqlite::Row<'a>> for CoverageSample {
type Error = rusqlite::Error;
fn try_from(row: &'a ::rusqlite::Row) -> Result<Self, Self::Error> {
Ok(Self {
raw_upload_id: row.get(row.as_ref().column_index("raw_upload_id")?)?,
local_sample_id: row.get(row.as_ref().column_index("local_sample_id")?)?,
source_file_id: row.get(row.as_ref().column_index("source_file_id")?)?,
line_no: row.get(row.as_ref().column_index("line_no")?)?,
coverage_type: row.get(row.as_ref().column_index("coverage_type")?)?,
hits: row.get(row.as_ref().column_index("hits")?)?,
hit_branches: row.get(row.as_ref().column_index("hit_branches")?)?,
total_branches: row.get(row.as_ref().column_index("total_branches")?)?,
})
}
}
impl Insertable for CoverageSample {
const TABLE_NAME: &'static str = "coverage_sample";
const FIELDS: &'static [&'static str] = &[
"raw_upload_id",
"local_sample_id",
"source_file_id",
"line_no",
"coverage_type",
"hits",
"hit_branches",
"total_branches",
];
fn extend_params<'a>(&'a self, params: &mut Vec<&'a dyn rusqlite::ToSql>) {
params.extend(&[
&self.raw_upload_id as &dyn rusqlite::ToSql,
&self.local_sample_id as &dyn rusqlite::ToSql,
&self.source_file_id as &dyn rusqlite::ToSql,
&self.line_no as &dyn rusqlite::ToSql,
&self.coverage_type as &dyn rusqlite::ToSql,
&self.hits as &dyn rusqlite::ToSql,
&self.hit_branches as &dyn rusqlite::ToSql,
&self.total_branches as &dyn rusqlite::ToSql,
])
}
}
impl<'a> std::convert::TryFrom<&'a rusqlite::Row<'a>> for BranchesData {
type Error = rusqlite::Error;
fn try_from(row: &'a ::rusqlite::Row) -> Result<Self, Self::Error> {
Ok(Self {
raw_upload_id: row.get(row.as_ref().column_index("raw_upload_id")?)?,
local_branch_id: row.get(row.as_ref().column_index("local_branch_id")?)?,
source_file_id: row.get(row.as_ref().column_index("source_file_id")?)?,
local_sample_id: row.get(row.as_ref().column_index("local_sample_id")?)?,
hits: row.get(row.as_ref().column_index("hits")?)?,
branch_format: row.get(row.as_ref().column_index("branch_format")?)?,
branch: row.get(row.as_ref().column_index("branch")?)?,
})
}
}
impl Insertable for BranchesData {
const TABLE_NAME: &'static str = "branches_data";
const FIELDS: &'static [&'static str] = &[
"raw_upload_id",
"local_branch_id",
"source_file_id",
"local_sample_id",
"hits",
"branch_format",
"branch",
];
fn extend_params<'a>(&'a self, params: &mut Vec<&'a dyn rusqlite::ToSql>) {
params.extend(&[
&self.raw_upload_id as &dyn rusqlite::ToSql,
&self.local_branch_id as &dyn rusqlite::ToSql,
&self.source_file_id as &dyn rusqlite::ToSql,
&self.local_sample_id as &dyn rusqlite::ToSql,
&self.hits as &dyn rusqlite::ToSql,
&self.branch_format as &dyn rusqlite::ToSql,
&self.branch as &dyn rusqlite::ToSql,
])
}
}
impl<'a> std::convert::TryFrom<&'a rusqlite::Row<'a>> for MethodData {
type Error = rusqlite::Error;
fn try_from(row: &'a ::rusqlite::Row) -> Result<Self, Self::Error> {
Ok(Self {
raw_upload_id: row.get(row.as_ref().column_index("raw_upload_id")?)?,
local_method_id: row.get(row.as_ref().column_index("local_method_id")?)?,
source_file_id: row.get(row.as_ref().column_index("source_file_id")?)?,
local_sample_id: row.get(row.as_ref().column_index("local_sample_id")?)?,
line_no: row.get(row.as_ref().column_index("line_no")?)?,
hit_branches: row.get(row.as_ref().column_index("hit_branches")?)?,
total_branches: row.get(row.as_ref().column_index("total_branches")?)?,
hit_complexity_paths: row.get(row.as_ref().column_index("hit_complexity_paths")?)?,
total_complexity: row.get(row.as_ref().column_index("total_complexity")?)?,
})
}
}
impl Insertable for MethodData {
const TABLE_NAME: &'static str = "method_data";
const FIELDS: &'static [&'static str] = &[
"raw_upload_id",
"local_method_id",
"source_file_id",
"local_sample_id",
"line_no",
"hit_branches",
"total_branches",
"hit_complexity_paths",
"total_complexity",
];
fn extend_params<'a>(&'a self, params: &mut Vec<&'a dyn rusqlite::ToSql>) {
params.extend(&[
&self.raw_upload_id as &dyn rusqlite::ToSql,
&self.local_method_id as &dyn rusqlite::ToSql,
&self.source_file_id as &dyn rusqlite::ToSql,
&self.local_sample_id as &dyn rusqlite::ToSql,
&self.line_no as &dyn rusqlite::ToSql,
&self.hit_branches as &dyn rusqlite::ToSql,
&self.total_branches as &dyn rusqlite::ToSql,
&self.hit_complexity_paths as &dyn rusqlite::ToSql,
&self.total_complexity as &dyn rusqlite::ToSql,
])
}
}
impl<'a> std::convert::TryFrom<&'a rusqlite::Row<'a>> for SpanData {
type Error = rusqlite::Error;
fn try_from(row: &'a ::rusqlite::Row) -> Result<Self, Self::Error> {
Ok(Self {
raw_upload_id: row.get(row.as_ref().column_index("raw_upload_id")?)?,
local_span_id: row.get(row.as_ref().column_index("local_span_id")?)?,
source_file_id: row.get(row.as_ref().column_index("source_file_id")?)?,
local_sample_id: row.get(row.as_ref().column_index("local_sample_id")?)?,
hits: row.get(row.as_ref().column_index("hits")?)?,
start_line: row.get(row.as_ref().column_index("start_line")?)?,
start_col: row.get(row.as_ref().column_index("start_col")?)?,
end_line: row.get(row.as_ref().column_index("end_line")?)?,
end_col: row.get(row.as_ref().column_index("end_col")?)?,
})
}
}
impl Insertable for SpanData {
const TABLE_NAME: &'static str = "span_data";
const FIELDS: &'static [&'static str] = &[
"raw_upload_id",
"local_span_id",
"source_file_id",
"local_sample_id",
"hits",
"start_line",
"start_col",
"end_line",
"end_col",
];
fn extend_params<'a>(&'a self, params: &mut Vec<&'a dyn rusqlite::ToSql>) {
params.extend(&[
&self.raw_upload_id as &dyn rusqlite::ToSql,
&self.local_span_id as &dyn rusqlite::ToSql,
&self.source_file_id as &dyn rusqlite::ToSql,
&self.local_sample_id as &dyn rusqlite::ToSql,
&self.hits as &dyn rusqlite::ToSql,
&self.start_line as &dyn rusqlite::ToSql,
&self.start_col as &dyn rusqlite::ToSql,
&self.end_line as &dyn rusqlite::ToSql,
&self.end_col as &dyn rusqlite::ToSql,
])
}
}
impl<'a> std::convert::TryFrom<&'a rusqlite::Row<'a>> for ContextAssoc {
type Error = rusqlite::Error;
fn try_from(row: &'a ::rusqlite::Row) -> Result<Self, Self::Error> {
Ok(Self {
context_id: row.get(row.as_ref().column_index("context_id")?)?,
raw_upload_id: row.get(row.as_ref().column_index("raw_upload_id")?)?,
local_sample_id: row.get(row.as_ref().column_index("local_sample_id")?)?,
local_span_id: row.get(row.as_ref().column_index("local_span_id")?)?,
})
}
}
impl Insertable for ContextAssoc {
const TABLE_NAME: &'static str = "context_assoc";
const FIELDS: &'static [&'static str] = &[
"context_id",
"raw_upload_id",
"local_sample_id",
"local_span_id",
];
fn extend_params<'a>(&'a self, params: &mut Vec<&'a dyn rusqlite::ToSql>) {
params.extend(&[
&self.context_id as &dyn rusqlite::ToSql,
&self.raw_upload_id as &dyn rusqlite::ToSql,
&self.local_sample_id as &dyn rusqlite::ToSql,
&self.local_span_id as &dyn rusqlite::ToSql,
])
}
}
impl<'a> std::convert::TryFrom<&'a rusqlite::Row<'a>> for Context {
type Error = rusqlite::Error;
fn try_from(row: &'a ::rusqlite::Row) -> Result<Self, Self::Error> {
Ok(Self {
id: row.get(row.as_ref().column_index("id")?)?,
name: row.get(row.as_ref().column_index("name")?)?,
})
}
}
impl Insertable for Context {
const TABLE_NAME: &'static str = "context";
const FIELDS: &'static [&'static str] = &["id", "name"];
fn extend_params<'a>(&'a self, params: &mut Vec<&'a dyn rusqlite::ToSql>) {
params.extend(&[
&self.id as &dyn rusqlite::ToSql,
&self.name as &dyn rusqlite::ToSql,
])
}
}
impl Insertable for RawUpload {
const TABLE_NAME: &'static str = "raw_upload";
const FIELDS: &'static [&'static str] = &[
"id",
"timestamp",
"raw_upload_url",
"flags",
"provider",
"build",
"name",
"job_name",
"ci_run_url",
"state",
"env",
"session_type",
"session_extras",
];
fn extend_params<'a>(&'a self, params: &mut Vec<&'a dyn rusqlite::ToSql>) {
params.extend(&[
&self.id as &dyn rusqlite::ToSql,
&self.timestamp as &dyn rusqlite::ToSql,
&self.raw_upload_url as &dyn rusqlite::ToSql,
&self.flags as &dyn rusqlite::ToSql,
&self.provider as &dyn rusqlite::ToSql,
&self.build as &dyn rusqlite::ToSql,
&self.name as &dyn rusqlite::ToSql,
&self.job_name as &dyn rusqlite::ToSql,
&self.ci_run_url as &dyn rusqlite::ToSql,
&self.state as &dyn rusqlite::ToSql,
&self.env as &dyn rusqlite::ToSql,
&self.session_type as &dyn rusqlite::ToSql,
&self.session_extras as &dyn rusqlite::ToSql,
])
}
}
impl<'a> std::convert::TryFrom<&'a rusqlite::Row<'a>> for RawUpload {
type Error = rusqlite::Error;
fn try_from(row: &'a ::rusqlite::Row) -> Result<Self, Self::Error> {
let flags_index = row.as_ref().column_index("flags")?;
let flags = if let Some(flags) = row.get(flags_index)? {
Some(json_value_from_sql(flags, flags_index)?)
} else {
None
};
let session_extras_index = row.as_ref().column_index("session_extras")?;
let session_extras = if let Some(session_extras) = row.get(session_extras_index)? {
Some(json_value_from_sql(session_extras, session_extras_index)?)
} else {
None
};
Ok(Self {
id: row.get(row.as_ref().column_index("id")?)?,
timestamp: row.get(row.as_ref().column_index("timestamp")?)?,
raw_upload_url: row.get(row.as_ref().column_index("raw_upload_url")?)?,
flags,
provider: row.get(row.as_ref().column_index("provider")?)?,
build: row.get(row.as_ref().column_index("build")?)?,
name: row.get(row.as_ref().column_index("name")?)?,
job_name: row.get(row.as_ref().column_index("job_name")?)?,
ci_run_url: row.get(row.as_ref().column_index("ci_run_url")?)?,
state: row.get(row.as_ref().column_index("state")?)?,
env: row.get(row.as_ref().column_index("env")?)?,
session_type: row.get(row.as_ref().column_index("session_type")?)?,
session_extras,
})
}
}
impl<'a> std::convert::TryFrom<&'a rusqlite::Row<'a>> for CoverageTotals {
type Error = rusqlite::Error;
fn try_from(row: &'a ::rusqlite::Row) -> Result<Self, Self::Error> {
Ok(Self {
hit_lines: row.get(row.as_ref().column_index("hit_lines")?)?,
total_lines: row.get(row.as_ref().column_index("total_lines")?)?,
hit_branches: row.get(row.as_ref().column_index("hit_branches")?)?,
total_branches: row.get(row.as_ref().column_index("total_branches")?)?,
total_branch_roots: row.get(row.as_ref().column_index("total_branch_roots")?)?,
hit_methods: row.get(row.as_ref().column_index("hit_methods")?)?,
total_methods: row.get(row.as_ref().column_index("total_methods")?)?,
hit_complexity_paths: row.get(row.as_ref().column_index("hit_complexity_paths")?)?,
total_complexity: row.get(row.as_ref().column_index("total_complexity")?)?,
})
}
}
impl<'a> std::convert::TryFrom<&'a rusqlite::Row<'a>> for ReportTotals {
type Error = rusqlite::Error;
fn try_from(row: &'a ::rusqlite::Row) -> Result<Self, Self::Error> {
Ok(Self {
files: row.get(row.as_ref().column_index("file_count")?)?,
uploads: row.get(row.as_ref().column_index("upload_count")?)?,
test_cases: row.get(row.as_ref().column_index("test_case_count")?)?,
coverage: row.try_into()?,
})
}
}
#[cfg(test)]
mod tests {
use serde_json::json;
use tempfile::TempDir;
use super::{
super::{
super::{Report, ReportBuilder},
SqliteReport, SqliteReportBuilder,
},
*,
};
#[derive(PartialEq, Debug)]
struct TestModel {
id: i64,
data: String,
}
impl Insertable for TestModel {
const TABLE_NAME: &'static str = "test";
const FIELDS: &'static [&'static str] = &["id", "data"];
fn extend_params<'a>(&'a self, params: &mut Vec<&'a dyn rusqlite::ToSql>) {
params.extend(&[
&self.id as &dyn rusqlite::ToSql,
&self.data as &dyn rusqlite::ToSql,
])
}
}
#[test]
fn query_builder() {
let query = TestModel::build_query(1);
assert_eq!(query, "INSERT INTO test (id, data) VALUES (?, ?);");
let query = TestModel::build_query(3);
assert_eq!(
query,
"INSERT INTO test (id, data) VALUES (?, ?), (?, ?), (?, ?);"
);
}
impl<'a> std::convert::TryFrom<&'a rusqlite::Row<'a>> for TestModel {
type Error = rusqlite::Error;
fn try_from(row: &'a ::rusqlite::Row) -> Result<Self, Self::Error> {
Ok(Self {
id: row.get(row.as_ref().column_index("id")?)?,
data: row.get(row.as_ref().column_index("data")?)?,
})
}
}
struct Ctx {
temp_dir: TempDir,
report: SqliteReport,
}
fn setup() -> Ctx {
let temp_dir = TempDir::new().ok().unwrap();
let db_file = temp_dir.path().join("db.sqlite");
let report = SqliteReport::open(db_file).unwrap();
report
.conn
.execute(
"CREATE TABLE test (id INTEGER PRIMARY KEY, data VARCHAR)",
[],
)
.unwrap();
Ctx { temp_dir, report }
}
fn list_test_models(report: &SqliteReport) -> Vec<TestModel> {
let mut stmt = report
.conn
.prepare_cached("SELECT id, data FROM test ORDER BY id ASC")
.unwrap();
let models = stmt
.query_map([], |row| row.try_into())
.unwrap()
.collect::<rusqlite::Result<Vec<TestModel>>>()
.unwrap();
models
}
#[test]
fn test_test_model_single_insert() {
let ctx = setup();
let model = TestModel {
id: 5,
data: "foo".to_string(),
};
model.insert(&ctx.report.conn).unwrap();
let duplicate_result = model.insert(&ctx.report.conn);
let test_models = list_test_models(&ctx.report);
assert_eq!(test_models, vec![model]);
let error = duplicate_result.unwrap_err();
assert_eq!(
error.to_string(),
"sqlite failure: 'UNIQUE constraint failed: test.id'"
);
}
#[test]
fn test_test_model_multi_insert() {
let ctx = setup();
// Our chunk-size is set to 50, so inserting more than twice that will use
// multiple chunks, as well as using single inserts for the remainder.
let models_to_insert: Vec<_> = (0..111)
.map(|id| TestModel {
id,
data: format!("Test {id}"),
})
.collect();
TestModel::multi_insert(models_to_insert.iter(), &ctx.report.conn).unwrap();
let test_models = list_test_models(&ctx.report);
assert_eq!(test_models, models_to_insert);
}
#[test]
fn test_source_file_single_insert() {
let ctx = setup();
let model = SourceFile {
id: 0,
path: "src/report/report.rs".to_string(),
};
model.insert(&ctx.report.conn).unwrap();
let duplicate_result = model.insert(&ctx.report.conn);
let files = ctx.report.list_files().unwrap();
assert_eq!(files, vec![model]);
let error = duplicate_result.unwrap_err();
assert_eq!(
error.to_string(),
"sqlite failure: 'UNIQUE constraint failed: source_file.id'"
);
}
#[test]
fn test_context_single_insert() {
let ctx = setup();
let model = Context {
id: 0,
name: "test_upload".to_string(),
};
model.insert(&ctx.report.conn).unwrap();
let duplicate_result = model.insert(&ctx.report.conn);
let contexts = ctx.report.list_contexts().unwrap();
assert_eq!(contexts, vec![model]);
let error = duplicate_result.unwrap_err();
assert_eq!(
error.to_string(),
"sqlite failure: 'UNIQUE constraint failed: context.id'"
);
}
#[test]
fn test_context_assoc_single_insert() {
let ctx = setup();
let db_file = ctx.temp_dir.path().join("db.sqlite");
let mut report_builder = SqliteReportBuilder::open(db_file).unwrap();
let raw_upload = report_builder
.insert_raw_upload(Default::default())
.unwrap();
let context = report_builder.insert_context("foo").unwrap();
let report = report_builder.build().unwrap();
let model = ContextAssoc {
context_id: context.id,
raw_upload_id: raw_upload.id,
local_sample_id: Some(rand::random()),
local_span_id: None,
};
model.insert(&report.conn).unwrap();
let assoc: ContextAssoc = report
.conn
.query_row(
"SELECT context_id, raw_upload_id, local_sample_id, local_span_id FROM context_assoc",
[],
|row| row.try_into(),
)
.unwrap();
assert_eq!(assoc, model);
/* TODO: Figure out how to re-enable this part of the test
let duplicate_result = <ContextAssoc as Insertable<4>>::insert(&model, &ctx.report.conn);
let error = duplicate_result.unwrap_err();
assert_eq!(
error.to_string(),
"sqlite failure: 'UNIQUE constraint failed: context_assoc.context_id, context_assoc.raw_upload_id, context_assoc.local_sample_id'"
);
*/
}
#[test]
fn test_coverage_sample_single_insert() {
let ctx = setup();
let db_file = ctx.temp_dir.path().join("db.sqlite");
let mut report_builder = SqliteReportBuilder::open(db_file).unwrap();
let source_file = report_builder.insert_file("foo.rs").unwrap();
let raw_upload = report_builder
.insert_raw_upload(Default::default())
.unwrap();
let report = report_builder.build().unwrap();
let model = CoverageSample {
raw_upload_id: raw_upload.id,
local_sample_id: rand::random(),
source_file_id: source_file.id,
..Default::default()
};
model.insert(&report.conn).unwrap();
let duplicate_result = model.insert(&report.conn);
let samples = report.list_coverage_samples().unwrap();
assert_eq!(samples, vec![model]);
let error = duplicate_result.unwrap_err();
assert_eq!(
error.to_string(),
"sqlite failure: 'UNIQUE constraint failed: coverage_sample.raw_upload_id, coverage_sample.local_sample_id'"
);
}
#[test]
fn test_branches_data_single_insert() {
let ctx = setup();
let db_file = ctx.temp_dir.path().join("db.sqlite");
let mut report_builder = SqliteReportBuilder::open(db_file).unwrap();
let source_file = report_builder.insert_file("path").unwrap();
let raw_upload = report_builder
.insert_raw_upload(Default::default())
.unwrap();
let report = report_builder.build().unwrap();
let local_sample_id = rand::random();
CoverageSample {
raw_upload_id: raw_upload.id,
local_sample_id,
source_file_id: source_file.id,
..Default::default()
}
.insert(&report.conn)
.unwrap();
let model = BranchesData {
raw_upload_id: raw_upload.id,
local_branch_id: rand::random(),
local_sample_id,
source_file_id: source_file.id,
..Default::default()
};
model.insert(&report.conn).unwrap();
let duplicate_result = model.insert(&report.conn);
let branch: BranchesData = report
.conn
.query_row(
"SELECT local_branch_id, source_file_id, local_sample_id, raw_upload_id, hits, branch_format, branch FROM branches_data",
[],
|row| row.try_into(),
).unwrap();
assert_eq!(branch, model);
let error = duplicate_result.unwrap_err();
assert_eq!(
error.to_string(),
"sqlite failure: 'UNIQUE constraint failed: branches_data.raw_upload_id, branches_data.local_branch_id'"
);
}
#[test]
fn test_method_data_single_insert() {
let ctx = setup();
let db_file = ctx.temp_dir.path().join("db.sqlite");
let mut report_builder = SqliteReportBuilder::open(db_file).unwrap();
let source_file = report_builder.insert_file("foo.rs").unwrap();
let raw_upload = report_builder
.insert_raw_upload(Default::default())
.unwrap();
let coverage_sample = report_builder
.insert_coverage_sample(CoverageSample {
raw_upload_id: raw_upload.id,
source_file_id: source_file.id,
..Default::default()
})
.unwrap();
let report = report_builder.build().unwrap();
let model = MethodData {
raw_upload_id: raw_upload.id,
local_method_id: rand::random(),
local_sample_id: coverage_sample.local_sample_id,
source_file_id: source_file.id,
..Default::default()
};
model.insert(&report.conn).unwrap();
let duplicate_result = model.insert(&report.conn);
let method: MethodData = report
.conn
.query_row(
"SELECT raw_upload_id, local_method_id, source_file_id, local_sample_id, line_no, hit_branches, total_branches, hit_complexity_paths, total_complexity FROM method_data",
[],
|row| row.try_into(),
).unwrap();
assert_eq!(method, model);
let error = duplicate_result.unwrap_err();
assert_eq!(
error.to_string(),
"sqlite failure: 'UNIQUE constraint failed: method_data.raw_upload_id, method_data.local_method_id'"
);
}
#[test]
fn test_span_data_single_insert() {
let ctx = setup();
let db_file = ctx.temp_dir.path().join("db.sqlite");
let mut report_builder = SqliteReportBuilder::open(db_file).unwrap();
let source_file = report_builder.insert_file("foo.rs").unwrap();
let raw_upload = report_builder
.insert_raw_upload(Default::default())
.unwrap();
let report = report_builder.build().unwrap();
let model = SpanData {
raw_upload_id: raw_upload.id,
local_span_id: rand::random(),
source_file_id: source_file.id,
..Default::default()
};
model.insert(&report.conn).unwrap();
let duplicate_result = model.insert(&report.conn);
let branch: SpanData = report
.conn
.query_row(
"SELECT raw_upload_id, local_span_id, source_file_id, local_sample_id, hits, start_line, start_col, end_line, end_col FROM span_data",
[],
|row| row.try_into(),
).unwrap();
assert_eq!(branch, model);
let error = duplicate_result.unwrap_err();
assert_eq!(
error.to_string(),
"sqlite failure: 'UNIQUE constraint failed: span_data.raw_upload_id, span_data.local_span_id'"
);
}
#[test]
fn test_raw_upload_single_insert() {
let ctx = setup();
let model = RawUpload {
id: 5,
timestamp: Some(123),
raw_upload_url: Some("https://example.com".to_string()),
flags: Some(json!(["abc".to_string(), "def".to_string()])),
provider: Some("provider".to_string()),
build: Some("build".to_string()),
name: Some("name".to_string()),
job_name: Some("job name".to_string()),
ci_run_url: Some("https://example.com".to_string()),
state: Some("state".to_string()),
env: Some("env".to_string()),
session_type: Some("uploaded".to_string()),
session_extras: Some(json!({})),
};
model.insert(&ctx.report.conn).unwrap();
let duplicate_result = model.insert(&ctx.report.conn);
let uploads = ctx.report.list_raw_uploads().unwrap();
assert_eq!(uploads, vec![model]);
let error = duplicate_result.unwrap_err();
assert_eq!(
error.to_string(),
"sqlite failure: 'UNIQUE constraint failed: raw_upload.id'"
);
}
}