Skip to content

Commit

Permalink
Update file format tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromekelleher committed Nov 28, 2021
1 parent 1633ea3 commit f20cede
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 3 deletions.
56 changes: 56 additions & 0 deletions c/tests/test_file_format.c
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,47 @@ test_malformed_indexes(void)
free(ts);
}

static void
test_missing_reference_sequence(void)
{
int ret;
tsk_treeseq_t *ts = caterpillar_tree(5, 3, 3);
tsk_table_collection_t t1, t2;
const char *cols[] = { "reference_sequence/data", "reference_sequence/url",
"reference_sequence/metadata_schema", "reference_sequence/metadata" };

CU_ASSERT_TRUE(tsk_treeseq_has_reference_sequence(ts, 0));

ret = tsk_treeseq_copy_tables(ts, &t1, 0);
CU_ASSERT_EQUAL_FATAL(ret, 0);

copy_store_drop_columns(ts, 1, cols, _tmp_file_name);
ret = tsk_table_collection_load(&t2, _tmp_file_name, 0);
CU_ASSERT_TRUE(tsk_table_collection_has_reference_sequence(&t2, 0));
tsk_table_collection_free(&t2);

copy_store_drop_columns(ts, 2, cols, _tmp_file_name);
ret = tsk_table_collection_load(&t2, _tmp_file_name, 0);
CU_ASSERT_TRUE(tsk_table_collection_has_reference_sequence(&t2, 0));
tsk_table_collection_free(&t2);

copy_store_drop_columns(ts, 3, cols, _tmp_file_name);
ret = tsk_table_collection_load(&t2, _tmp_file_name, 0);
CU_ASSERT_TRUE(tsk_table_collection_has_reference_sequence(&t2, 0));
tsk_table_collection_free(&t2);

/* Dropping all the columns gives us a NULL reference_sequence, though */
copy_store_drop_columns(ts, 4, cols, _tmp_file_name);
ret = tsk_table_collection_load(&t2, _tmp_file_name, 0);
CU_ASSERT_EQUAL_FATAL(ret, 0);
CU_ASSERT_FALSE(tsk_table_collection_has_reference_sequence(&t2, 0));
tsk_table_collection_free(&t2);

tsk_table_collection_free(&t1);
tsk_treeseq_free(ts);
free(ts);
}

static void
test_bad_column_types(void)
{
Expand Down Expand Up @@ -699,6 +740,18 @@ test_bad_column_types(void)
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_BAD_COLUMN_TYPE);
tsk_table_collection_free(&tables);

cols[0] = "reference_sequence/metadata";
copy_store_drop_columns(ts, 1, cols, _tmp_file_name);
ret = kastore_open(&store, _tmp_file_name, "a", 0);
CU_ASSERT_EQUAL_FATAL(ret, 0);
ret = kastore_puts(&store, cols[0], NULL, 0, KAS_FLOAT32, 0);
CU_ASSERT_EQUAL_FATAL(ret, 0);
ret = kastore_close(&store);
CU_ASSERT_EQUAL_FATAL(ret, 0);
ret = tsk_table_collection_load(&tables, _tmp_file_name, 0);
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_BAD_COLUMN_TYPE);
tsk_table_collection_free(&tables);

free(col_memory);
tsk_treeseq_free(ts);
free(ts);
Expand Down Expand Up @@ -760,6 +813,8 @@ test_metadata_schemas_optional(void)
const char *cols[] = {
"metadata",
"metadata_schema",
"reference_sequence/metadata",
"reference_sequence/metadata_schema",
"individuals/metadata_schema",
"populations/metadata_schema",
"nodes/metadata_schema",
Expand Down Expand Up @@ -1290,6 +1345,7 @@ main(int argc, char **argv)
{ "test_format_data_load_errors", test_format_data_load_errors },
{ "test_missing_indexes", test_missing_indexes },
{ "test_malformed_indexes", test_malformed_indexes },
{ "test_missing_reference_sequence", test_missing_reference_sequence },
{ "test_bad_column_types", test_bad_column_types },
{ "test_missing_required_columns", test_missing_required_columns },
{ "test_missing_optional_column_pairs", test_missing_optional_column_pairs },
Expand Down
2 changes: 2 additions & 0 deletions c/tests/test_tables.c
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,8 @@ test_table_collection_reference_sequence(void)
ret = tsk_table_collection_copy(&tc1, &tc2, TSK_NO_INIT);
CU_ASSERT_EQUAL_FATAL(ret, 0);
CU_ASSERT_TRUE(tsk_table_collection_equals(&tc1, &tc2, 0));
tsk_table_collection_free(&tc1);
tsk_table_collection_free(&tc2);

// Test dump and load
ret = tsk_table_collection_init(&tc1, 0);
Expand Down
7 changes: 7 additions & 0 deletions c/tests/testlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,13 @@ caterpillar_tree(tsk_size_t n, tsk_size_t num_sites, tsk_size_t num_mutations)
tsk_table_collection_set_metadata(&tables, ts_metadata, strlen(ts_metadata));
tsk_table_collection_set_metadata_schema(
&tables, ts_metadata_schema, strlen(ts_metadata_schema));
tsk_reference_sequence_set_metadata_schema(
&tables.reference_sequence, ts_metadata_schema, strlen(ts_metadata_schema));
tsk_reference_sequence_set_metadata(
&tables.reference_sequence, ts_metadata, strlen(ts_metadata));
tsk_reference_sequence_set_data(&tables.reference_sequence, "A", 1);
tsk_reference_sequence_set_url(&tables.reference_sequence, "B", 1);

tsk_population_table_set_metadata_schema(
&tables.populations, metadata_schema, strlen(metadata_schema));
tsk_individual_table_set_metadata_schema(
Expand Down
6 changes: 3 additions & 3 deletions c/tskit/tables.c
Original file line number Diff line number Diff line change
Expand Up @@ -10241,11 +10241,13 @@ tsk_table_collection_read_format_data(tsk_table_collection_t *self, kastore_t *s
uint32_t *version;
int8_t *format_name, *uuid;
double *L;

char *time_units = NULL;
char *metadata = NULL;
char *metadata_schema = NULL;
size_t time_units_length, metadata_length, metadata_schema_length;
/* TODO we could simplify this function quite a bit if we use the
* read_table_properties infrastructure. We would need to add the
* ability to have non-optional columns to that though. */

ret = kastore_gets_int8(store, "format/name", &format_name, &len);
if (ret != 0) {
Expand Down Expand Up @@ -10472,8 +10474,6 @@ tsk_table_collection_load_reference_sequence(
}
ret = tsk_reference_sequence_init(&self->reference_sequence);
tsk_bug_assert(ret == 0);
/* TODO we should decide here whether there are any requirements about
* setting url and/or data. Are they mutually exclusive?? */
if (data != NULL) {
ret = tsk_reference_sequence_set_data(
&self->reference_sequence, data, (tsk_size_t) data_length);
Expand Down

0 comments on commit f20cede

Please sign in to comment.