Skip to content

Commit 86df702

Browse files
authored
Improve test coverage of "adding rows to table collections" (#143):
* Add generic metadata fixture. Bugs fixed: * Add missing Iterator implementations for IndividualTableIterator and IndividualTableRefIterator. * TableCollection::add_migration_* now return Result<MigrationId, TskitError>. * Fix bug where MigrationTable::node and MigrationTable::source were accessing the wrong C arrays.
1 parent 0366800 commit 86df702

File tree

5 files changed

+576
-13
lines changed

5 files changed

+576
-13
lines changed

src/individual_table.rs

+20
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,26 @@ pub type IndividualTableRefIterator<'a> =
6767
crate::table_iterator::TableIterator<&'a IndividualTable<'a>>;
6868
pub type IndividualTableIterator<'a> = crate::table_iterator::TableIterator<IndividualTable<'a>>;
6969

70+
impl<'a> Iterator for IndividualTableRefIterator<'a> {
71+
type Item = IndividualTableRow;
72+
73+
fn next(&mut self) -> Option<Self::Item> {
74+
let rv = make_individual_table_row(self.table, self.pos);
75+
self.pos += 1;
76+
rv
77+
}
78+
}
79+
80+
impl<'a> Iterator for IndividualTableIterator<'a> {
81+
type Item = IndividualTableRow;
82+
83+
fn next(&mut self) -> Option<Self::Item> {
84+
let rv = make_individual_table_row(&self.table, self.pos);
85+
self.pos += 1;
86+
rv
87+
}
88+
}
89+
7090
impl<'a> IndividualTable<'a> {
7191
pub(crate) fn new_from_table(individuals: &'a ll_bindings::tsk_individual_table_t) -> Self {
7292
IndividualTable {

src/migration_table.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ impl<'a> MigrationTable<'a> {
112112
///
113113
/// * [`TskitError::IndexError`] if `row` is out of range.
114114
pub fn node<M: Into<MigrationId> + Copy>(&'a self, row: M) -> Result<NodeId, TskitError> {
115-
unsafe_tsk_column_access!(row.into().0, 0, self.num_rows(), self.table_.source, NodeId)
115+
unsafe_tsk_column_access!(row.into().0, 0, self.num_rows(), self.table_.node, NodeId)
116116
}
117117

118118
/// Return the source population for a given row.
@@ -128,7 +128,7 @@ impl<'a> MigrationTable<'a> {
128128
row.into().0,
129129
0,
130130
self.num_rows(),
131-
self.table_.node,
131+
self.table_.source,
132132
PopulationId
133133
)
134134
}

src/provenance.rs

+45
Original file line numberDiff line numberDiff line change
@@ -275,3 +275,48 @@ impl<'a> ProvenanceTable<'a> {
275275
crate::table_iterator::make_table_iterator::<&ProvenanceTable<'a>>(&self)
276276
}
277277
}
278+
279+
#[cfg(test)]
280+
mod test_trigger_provenance_errors {
281+
use super::*;
282+
use crate::test_fixtures::make_empty_table_collection;
283+
use Provenance;
284+
285+
#[test]
286+
#[should_panic]
287+
fn test_empty_record_string() {
288+
//TODO: decide if we like this behavior:
289+
//See GitHub issue 150 -- this behavior should change.
290+
let mut tables = make_empty_table_collection(1.0);
291+
let row_id = tables.add_provenance(&String::from("")).unwrap();
292+
let _ = tables.provenances().row(row_id).unwrap();
293+
}
294+
}
295+
296+
#[cfg(test)]
297+
mod test_provenance_tables {
298+
use super::*;
299+
use crate::test_fixtures::make_empty_table_collection;
300+
use Provenance;
301+
302+
#[test]
303+
fn test_add_rows() {
304+
let records = vec!["banana".to_string(), "split".to_string()];
305+
let mut tables = make_empty_table_collection(1.);
306+
for (i, r) in records.iter().enumerate() {
307+
let row_id = tables.add_provenance(&r).unwrap();
308+
assert!(row_id == ProvenanceId(i as crate::tsk_id_t));
309+
assert_eq!(tables.provenances().record(row_id).unwrap(), *r);
310+
}
311+
assert_eq!(tables.provenances().num_rows() as usize, records.len());
312+
for (i, row) in tables.provenances_iter().enumerate() {
313+
assert_eq!(records[i], row.record);
314+
}
315+
for (i, row) in tables.provenances().iter().enumerate() {
316+
assert_eq!(records[i], row.record);
317+
}
318+
319+
assert!(tables.provenances().row(0).unwrap() == tables.provenances().row(0).unwrap());
320+
assert!(tables.provenances().row(0).unwrap() != tables.provenances().row(1).unwrap());
321+
}
322+
}

0 commit comments

Comments
 (0)