def test_descriptor_schema(self): collection = Collection(id=1, resolwe=MagicMock()) collection._descriptor_schema = 1 # test getting descriptor schema attribute self.assertEqual(collection.descriptor_schema, 1) # descriptor schema is not set collection._descriptor_schema = None self.assertEqual(collection.descriptor_schema, None) # hidrated descriptor schema descriptor_schema = { "slug": "test-schema", "name": "Test schema", "version": "1.0.0", "schema": [{ "default": "56G", "type": "basic:string:", "name": "description", "label": "Object description", }], "id": 1, } collection = Collection(id=1, descriptor_schema=descriptor_schema, resolwe=MagicMock()) self.assertTrue( isinstance(collection.descriptor_schema, DescriptorSchema)) self.assertEqual(collection.descriptor_schema.slug, "test-schema")
def test_descriptor_schema(self): collection = Collection(id=1, resolwe=MagicMock()) collection._descriptor_schema = 1 # test getting descriptor schema attribute collection.resolwe.descriptor_schema.get = MagicMock( return_value='descriptor_schema') self.assertEqual(collection.descriptor_schema, 'descriptor_schema') _, get_kwargs = collection.resolwe.descriptor_schema.get.call_args self.assertEqual(get_kwargs['id'], 1) # descriptor schema is not set collection._descriptor_schema = None self.assertEqual(collection.descriptor_schema, None) # cache is cleared at update collection._hydrated_descriptor_schema = 'descriptor_schema' collection.update() self.assertEqual(collection._hydrated_descriptor_schema, None) # new collection collection = Collection(resolwe=MagicMock()) collection.descriptor_schema = 'my-schema' self.assertEqual(collection._descriptor_schema, 'my-schema') collection.resolwe.descriptor_schema.get = MagicMock( return_value='descriptor_schema') self.assertEqual(collection.descriptor_schema, 'descriptor_schema') _, get_kwargs = collection.resolwe.descriptor_schema.get.call_args self.assertEqual(get_kwargs['slug'], 'my-schema') # hidrated descriptor schema descriptor_schema = { "slug": "test-schema", "name": "Test schema", "version": "1.0.0", "schema": [{ "default": "56G", "type": "basic:string:", "name": "description", "label": "Object description" }], "id": 1, } collection = Collection(id=1, descriptor_schema=descriptor_schema, resolwe=MagicMock()) self.assertTrue( isinstance(collection.descriptor_schema, DescriptorSchema)) # pylint: disable=no-member self.assertEqual(collection.descriptor_schema.slug, 'test-schema')
def test_collections(self): data = Data(id=1, resolwe=MagicMock()) # test getting collections attribute data.api(data.id).get = MagicMock(return_value={'collections': [5]}) data.resolwe.collection.filter = MagicMock(return_value=[ Collection(data.resolwe, **{'id': 5, 'name': 'XYZ'}) ]) self.assertEqual(len(data.collections), 1) self.assertEqual(data.collections[0].id, 5) self.assertEqual(data.collections[0].name, 'XYZ') # test caching collections attribute self.assertEqual(len(data.collections), 1) self.assertEqual(data.resolwe.collection.filter.call_count, 1) # cache is cleared at update data.update() self.assertEqual(data._collections, None) # raising error if data object is not saved data.id = None with self.assertRaises(ValueError): _ = data.collections
def test_create_group(self): collection = Collection(id=1, resolwe=MagicMock()) collection.id = 1 # this is overriden when initialized # only samples collection.create_group_relation(category='replicates', samples=[1, 2, 3]) collection.resolwe.relation.create.assert_called_with( collection=1, type='group', category='replicates', partitions=[ { 'entity': 1 }, { 'entity': 2 }, { 'entity': 3 }, ], ) collection.resolwe.relation.create.reset_mock() # samples with labels collection.create_group_relation(category='replicates', samples=[1, 2, 3], labels=['first', 'second', 'third']) collection.resolwe.relation.create.assert_called_with( collection=1, type='group', category='replicates', partitions=[ { 'label': 'first', 'entity': 1 }, { 'label': 'second', 'entity': 2 }, { 'label': 'third', 'entity': 3 }, ], ) collection.resolwe.relation.create.reset_mock() # samples with labels - length mismatch with self.assertRaises(ValueError): collection.create_group_relation(category='replicates', samples=[1, 2, 3], labels=['first']) self.assertEqual(collection.resolwe.relation.create.call_count, 0) collection.resolwe.relation.create.reset_mock()
def test_collection(self): relation = Relation(id=1, resolwe=MagicMock()) collection = Collection(id=3, resolwe=MagicMock()) collection.id = 3 # this is overriden when initialized # get collection relation.resolwe.collection.get = MagicMock(return_value=collection) relation._collection = 3 self.assertEqual(relation.collection, collection) # test caching collection attribute self.assertEqual(relation.collection, collection) self.assertEqual(relation.resolwe.collection.get.call_count, 1) # set collection relation.collection = 2 self.assertEqual(relation._collection, 2) self.assertEqual(relation._hydrated_collection, None) relation.collection = collection self.assertEqual(relation._collection, 3) self.assertEqual(relation._hydrated_collection, collection) # cache is cleared at update relation._collection = 2 relation._hydrated_collection = collection relation.update() self.assertEqual(relation._hydrated_collection, None)
def test_create_series(self): collection = Collection(id=1, resolwe=MagicMock()) collection.id = 1 # this is overriden when initialized # only samples collection.create_series_relation(category="time-series", samples=[1, 2, 3]) collection.resolwe.relation.create.assert_called_with( collection=1, type="series", category="time-series", partitions=[ {"entity": 1}, {"entity": 2}, {"entity": 3}, ], ) collection.resolwe.relation.create.reset_mock() # samples with labels collection.create_series_relation( category="time-series", samples=[1, 2, 3], labels=["0Hr", "2Hr", "4Hr"] ) collection.resolwe.relation.create.assert_called_with( collection=1, type="series", category="time-series", partitions=[ {"label": "0Hr", "entity": 1}, {"label": "2Hr", "entity": 2}, {"label": "4Hr", "entity": 3}, ], ) # samples with positions collection.create_series_relation( category="time-series", samples=[1, 2, 3], positions=[10, 20, 30] ) collection.resolwe.relation.create.assert_called_with( collection=1, type="series", category="time-series", partitions=[ {"position": 10, "entity": 1}, {"position": 20, "entity": 2}, {"position": 30, "entity": 3}, ], ) collection.resolwe.relation.create.reset_mock() # samples with labels - length mismatch with self.assertRaises(ValueError): collection.create_series_relation( category="time-series", samples=[1, 2], labels=["0Hr"] ) self.assertEqual(collection.resolwe.relation.create.call_count, 0) collection.resolwe.relation.create.reset_mock()
def test_collection(self, copy_mock): relation = Relation(id=1, resolwe=MagicMock()) collection = Collection(id=3, resolwe=MagicMock()) collection.id = 3 # this is overriden when initialized # get collection relation.resolwe.collection.get = MagicMock(return_value=collection) relation._collection = collection self.assertEqual(relation.collection, collection)
def test_data(self): resolwe_mock = MagicMock(**{'data.filter.return_value': ['data_1', 'data_2', 'data_3']}) collection = Collection(id=1, resolwe=resolwe_mock) self.assertEqual(collection.data, ['data_1', 'data_2', 'data_3']) # cache is cleared at update collection.data = MagicMock() collection.update() self.assertEqual(collection.data.clear_cache.call_count, 1)
def test_samples(self): resolwe_mock = MagicMock(**{'sample.filter.return_value': ['sample1', 'sample2']}) collection = Collection(id=1, resolwe=resolwe_mock) self.assertEqual(collection.samples, ['sample1', 'sample2']) # cache is cleared at update collection.samples = MagicMock() collection.update() self.assertEqual(collection.samples.clear_cache.call_count, 1)
def test_create_compare(self): collection = Collection(id=1, resolwe=MagicMock()) collection.id = 1 # this is overriden when initialized # only samples collection.create_compare_relation(category='case-control', samples=[1, 2]) collection.resolwe.relation.create.assert_called_with( collection=1, type='compare', category='case-control', partitions=[ { 'entity': 1 }, { 'entity': 2 }, ], ) collection.resolwe.relation.create.reset_mock() # samples with labels collection.create_compare_relation(category='case-control', samples=[1, 2], labels=['case', 'control']) collection.resolwe.relation.create.assert_called_with( collection=1, type='compare', category='case-control', partitions=[ { 'label': 'case', 'entity': 1 }, { 'label': 'control', 'entity': 2 }, ], ) collection.resolwe.relation.create.reset_mock() # samples with labels - length mismatch with self.assertRaises(ValueError): collection.create_compare_relation(category='case-control', samples=[1, 2], labels=['case']) self.assertEqual(collection.resolwe.relation.create.call_count, 0) collection.resolwe.relation.create.reset_mock()
def test_data_types(self): resolwe = MagicMock() data1 = Data(resolwe=resolwe, id=1) data1._process = Process(resolwe=resolwe, type="data:reads:fastq:single:") collection = Collection(resolwe=resolwe, id=1) collection._data = [data1] types = collection.data_types() self.assertEqual(types, ["data:reads:fastq:single:"])
def test_create_series(self): collection = Collection(id=1, resolwe=MagicMock()) collection.id = 1 # this is overriden when initialized # only samples collection.create_series_relation(samples=[1, 2, 3]) collection.resolwe.relation.create.assert_called_with( collection=1, type='series', entities=[ {'entity': 1}, {'entity': 2}, {'entity': 3}, ], ) collection.resolwe.relation.create.reset_mock() # samples with positions collection.create_series_relation(samples=[1, 2, 3], positions=['0Hr', '2Hr', '4Hr']) collection.resolwe.relation.create.assert_called_with( collection=1, type='series', entities=[ {'position': '0Hr', 'entity': 1}, {'position': '2Hr', 'entity': 2}, {'position': '4Hr', 'entity': 3}, ], ) collection.resolwe.relation.create.reset_mock() # samples with positions - length mismatch with self.assertRaises(ValueError): collection.create_series_relation(samples=[1, 2], positions=['0Hr']) self.assertEqual(collection.resolwe.relation.create.call_count, 0) collection.resolwe.relation.create.reset_mock() # with label collection.create_series_relation(samples=[1, 2], label='time-series') collection.resolwe.relation.create.assert_called_with( collection=1, type='series', label='time-series', entities=[ {'entity': 1}, {'entity': 2}, ], )
def test_create_background(self): collection = Collection(id=1, resolwe=MagicMock()) collection.id = 1 # this is overriden when initialized # only samples collection.create_background_relation("background 1", "sample_1", ["sample_2"]) collection.resolwe.relation.create.assert_called_with( collection=1, type="background", category="background 1", partitions=[ {"label": "background", "entity": "sample_1"}, {"label": "case", "entity": "sample_2"}, ], )
def test_create_background(self): collection = Collection(id=1, resolwe=MagicMock()) collection.id = 1 # this is overriden when initialized # only samples collection.create_background_relation('sample_1', 'sample_2') collection.resolwe.relation.create.assert_called_with( collection=1, type='compare', label='background', entities=[ {'position': 'sample', 'entity': 'sample_1'}, {'position': 'background', 'entity': 'sample_2'}, ], )
def test_relations(self): collection = Collection(id=1, resolwe=MagicMock()) # test getting relations attribute collection.resolwe.relation.filter = MagicMock(return_value=['relation1', 'relation2']) self.assertEqual(collection.relations, ['relation1', 'relation2']) # cache is cleared at update collection._relations = ['relation'] collection.update() self.assertEqual(collection._relations, None) # raising error if data collection is not saved collection.id = None with self.assertRaises(ValueError): _ = collection.relations
def test_samples(self): collection = Collection(id=1, resolwe=MagicMock()) # test getting samples attribute collection.resolwe.sample.filter = MagicMock( return_value=["sample1", "sample2"]) self.assertEqual(collection.samples, ["sample1", "sample2"]) # cache is cleared at update collection._samples = ["sample"] collection.update() self.assertEqual(collection._samples, None) # raising error if data collection is not saved collection.id = None with self.assertRaises(ValueError): _ = collection.samples
def test_create_group(self): collection = Collection(id=1, resolwe=MagicMock()) collection.id = 1 # this is overriden when initialized # only samples collection.create_group_relation(category="replicates", samples=[1, 2, 3]) collection.resolwe.relation.create.assert_called_with( collection=1, type="group", category="replicates", partitions=[ {"entity": 1}, {"entity": 2}, {"entity": 3}, ], ) collection.resolwe.relation.create.reset_mock() # samples with labels collection.create_group_relation( category="replicates", samples=[1, 2, 3], labels=["first", "second", "third"], ) collection.resolwe.relation.create.assert_called_with( collection=1, type="group", category="replicates", partitions=[ {"label": "first", "entity": 1}, {"label": "second", "entity": 2}, {"label": "third", "entity": 3}, ], ) collection.resolwe.relation.create.reset_mock() # samples with labels - length mismatch with self.assertRaises(ValueError): collection.create_group_relation( category="replicates", samples=[1, 2, 3], labels=["first"] ) self.assertEqual(collection.resolwe.relation.create.call_count, 0) collection.resolwe.relation.create.reset_mock()
def test_data(self): collection = Collection(id=1, resolwe=MagicMock()) # test getting data attribute collection.resolwe.data.filter = MagicMock(return_value=['data_1', 'data_2', 'data_3']) self.assertEqual(collection.data, ['data_1', 'data_2', 'data_3']) # test caching data attribute self.assertEqual(collection.data, ['data_1', 'data_2', 'data_3']) self.assertEqual(collection.resolwe.data.filter.call_count, 1) # cache is cleared at update collection._data = ['data'] collection.update() self.assertEqual(collection._data, None) # raising error if collection is not saved collection.id = None with self.assertRaises(ValueError): _ = collection.data
def test_create_background(self): collection = Collection(id=1, resolwe=MagicMock()) collection.id = 1 # this is overriden when initialized # only samples collection.create_background_relation('background 1', 'sample_1', ['sample_2']) collection.resolwe.relation.create.assert_called_with( collection=1, type='background', category='background 1', partitions=[ { 'label': 'background', 'entity': 'sample_1' }, { 'label': 'case', 'entity': 'sample_2' }, ], )
def test_bad_field_name(self): collection = Collection(resolwe=MagicMock(), id=1) with self.assertRaisesRegex(ValueError, "Invalid argument value `field_name`."): collection.download(field_name=123)
def test_files(self): collection = Collection(resolwe=MagicMock(), id=1) collection._data = [DATA1, DATA2] files = collection.files() self.assertCountEqual(files, ["arch.gz", "reads.fq", "outfile.exp"])
def test_collection_print_ann(self, collection_mock): with self.assertRaises(NotImplementedError): Collection.print_annotation(collection_mock)
def test_collection_init(self, collection_mock): collection_mock.configure_mock(endpoint="fake_endpoint") Collection.__init__(collection_mock, id=1, resolwe=MagicMock())