Skip to content

Commit

Permalink
correcting 'io.Reader.run' with 'group_objects_by_model'=False to mat…
Browse files Browse the repository at this point in the history
…ch behavior of 'obj_tables.io.Reader' with the same option (i.e. return list of objects rather than dictionary)
  • Loading branch information
jonrkarr committed Sep 25, 2019
1 parent 392de22 commit 0f66b69
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
18 changes: 18 additions & 0 deletions tests/test_io.py
Expand Up @@ -379,6 +379,7 @@ class TestModel(obj_tables.Model):
finally:
core.KnowledgeBase.Meta.related_attributes.pop('test')

@unittest.skip('Assertions need to be uncommented')
def test_seq_path_consistency(self):
pass

Expand All @@ -387,3 +388,20 @@ def test_seq_path_consistency(self):
#kb = io.Reader().run(core_path, seq_path=self.seq_path)[core.KnowledgeBase][0]
#core_path = os.path.join(self.dir, 'core2.xlsx')
#seq_path = os.path.join(self.dir, 'seq2.fna')

def test_read_flat_list_of_objects(self):
core_path = os.path.join(self.dir, 'core.xlsx')

writer = io.Writer()
writer.run(core_path, self.kb, data_repo_metadata=False)

reader = io.Reader()

objs = reader.run(core_path, seq_path=self.seq_path)
self.assertIsInstance(objs, dict)

objs = reader.run(core_path, seq_path=self.seq_path,
group_objects_by_model=False)
self.assertIsInstance(objs, list)
kb = next(obj for obj in objs if isinstance(obj, core.KnowledgeBase))
self.assertTrue(kb.is_equal(self.kb))
9 changes: 8 additions & 1 deletion wc_kb/io.py
Expand Up @@ -322,7 +322,7 @@ def run(self, core_path,
ignore_missing_attributes=ignore_missing_attributes,
ignore_extra_attributes=ignore_extra_attributes,
ignore_attribute_order=ignore_attribute_order,
group_objects_by_model=group_objects_by_model,
group_objects_by_model=True,
validate=False)

# Check if sequence pathes are consistent
Expand Down Expand Up @@ -381,6 +381,13 @@ def run(self, core_path,
raise ValueError(
indent_forest(['The knowledge base cannot be loaded because it fails to validate:', [errors]]))

# if `group_objects_by_model` is False, flatten objects into list
if not group_objects_by_model:
flat_objects = []
for model_objs in objects.values():
flat_objects.extend(list(model_objs))
objects = flat_objects

return objects


Expand Down

0 comments on commit 0f66b69

Please sign in to comment.