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_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_descriptor_schema(self): data = Data(id=1, resolwe=MagicMock()) data._descriptor_schema = 1 # test getting descriptor schema attribute data.resolwe.descriptor_schema.get = MagicMock( return_value='descriptor_schema') self.assertEqual(data.descriptor_schema, 'descriptor_schema') _, get_kwargs = data.resolwe.descriptor_schema.get.call_args self.assertEqual(get_kwargs['id'], 1) # descriptor schema is not set data._descriptor_schema = None self.assertEqual(data.descriptor_schema, None) # cache is cleared at update data._hydrated_descriptor_schema = 'descriptor_schema' data.update() self.assertEqual(data._hydrated_descriptor_schema, None) # new data object data = Data(resolwe=MagicMock()) data.descriptor_schema = 'my-schema' self.assertEqual(data._descriptor_schema, 'my-schema') data.resolwe.descriptor_schema.get = MagicMock( return_value='descriptor_schema') self.assertEqual(data.descriptor_schema, 'descriptor_schema') _, get_kwargs = data.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, } data = Data(id=1, descriptor_schema=descriptor_schema, resolwe=MagicMock()) self.assertTrue(isinstance(data.descriptor_schema, DescriptorSchema)) # pylint: disable=no-member self.assertEqual(data.descriptor_schema.slug, 'test-schema')
def test_collections(self): resolwe_mock = MagicMock(**{ 'collection.filter.return_value': ['collection_1', 'collection_2'] }) data = Data(id=1, resolwe=resolwe_mock) self.assertEqual(data.collections, ['collection_1', 'collection_2']) # cache is cleared at update data.collections = MagicMock() data.update() self.assertEqual(data.collections.clear_cache.call_count, 1)
def test_children(self): # Data with no id should fail. data = Data(id=None, resolwe=MagicMock()) with self.assertRaisesRegex(ValueError, 'Instance must be saved *'): data.children # pylint: disable=pointless-statement # Core functionality should be checked with e2e tests. # Check that cache is cleared at update. data = Data(id=42, resolwe=MagicMock()) data._children = 'foo' data.update() self.assertEqual(data._children, None)
def test_children(self): # Data with no id should fail. data = Data(id=None, resolwe=MagicMock()) with self.assertRaisesRegex(ValueError, 'Instance must be saved *'): data.children # pylint: disable=pointless-statement # Core functionality should be checked with e2e tests. # Check that cache is cleared at update. data = Data(id=42, resolwe=MagicMock()) data._children = 'foo' data.update() self.assertEqual(data._children, None)
def test_children(self): # Data with no id should fail. data = Data(id=None, resolwe=MagicMock()) with self.assertRaisesRegex(ValueError, "Instance must be saved *"): data.children # Core functionality should be checked with e2e tests. # Check that cache is cleared at update. data = Data(id=42, resolwe=MagicMock()) data._children = "foo" data.update() self.assertEqual(data._children, None)
def test_descriptor_schema(self): data = Data(id=1, resolwe=MagicMock()) data._descriptor_schema = 1 # test getting descriptor schema attribute data.resolwe.descriptor_schema.get = MagicMock(return_value='descriptor_schema') self.assertEqual(data.descriptor_schema, 'descriptor_schema') _, get_kwargs = data.resolwe.descriptor_schema.get.call_args self.assertEqual(get_kwargs['id'], 1) # descriptor schema is not set data._descriptor_schema = None self.assertEqual(data.descriptor_schema, None) # cache is cleared at update data._hydrated_descriptor_schema = 'descriptor_schema' data.update() self.assertEqual(data._hydrated_descriptor_schema, None) # new data object data = Data(resolwe=MagicMock()) data.descriptor_schema = 'my-schema' self.assertEqual(data._descriptor_schema, 'my-schema') data.resolwe.descriptor_schema.get = MagicMock(return_value='descriptor_schema') self.assertEqual(data.descriptor_schema, 'descriptor_schema') _, get_kwargs = data.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, } data = Data(id=1, descriptor_schema=descriptor_schema, resolwe=MagicMock()) self.assertTrue(isinstance(data.descriptor_schema, DescriptorSchema)) # pylint: disable=no-member self.assertEqual(data.descriptor_schema.slug, 'test-schema')
def test_sample(self): data = Data(id=1, resolwe=MagicMock()) data.resolwe.sample.filter = MagicMock(return_value=[]) self.assertEqual(data.sample, None) data.resolwe.sample.filter = MagicMock(return_value=['sample']) self.assertEqual(data.sample, 'sample') # test caching self.assertEqual(data.sample, 'sample') self.assertEqual(data.resolwe.sample.filter.call_count, 1) # cache is cleared at update data._sample = 'sample' data.update() self.assertEqual(data._sample, None)
def test_collections(self): data = Data(id=1, resolwe=MagicMock()) # test getting collections attribute data.resolwe.collection.filter = MagicMock(return_value=['collection']) self.assertEqual(data.collections, ['collection']) # test caching collections attribute self.assertEqual(data.collections, ['collection']) self.assertEqual(data.resolwe.collection.filter.call_count, 1) # cache is cleared at update data._collections = ['collection'] 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_sample(self): data = Data(id=1, resolwe=MagicMock()) data.api(data.id).get = MagicMock(return_value={'entities': [5]}) data.resolwe.sample.get = MagicMock( return_value=Sample(data.resolwe, **{'id': 5, 'name': 'XYZ'})) self.assertEqual(data.sample.id, 5) self.assertEqual(data.sample.name, 'XYZ') # test caching self.assertEqual(data.sample.id, 5) self.assertEqual(data.resolwe.sample.get.call_count, 1) # cache is cleared at update data.update() self.assertEqual(data._sample, None) # raising error if data object is not saved data.id = None with self.assertRaises(ValueError): _ = data.sample
def test_sample(self): data = Data(id=1, resolwe=MagicMock()) data.api(data.id).get = MagicMock(return_value={'entities': [5]}) data.resolwe.sample.get = MagicMock( return_value=Sample(data.resolwe, **{'id': 5, 'name': 'XYZ'})) self.assertEqual(data.sample.id, 5) self.assertEqual(data.sample.name, 'XYZ') # test caching self.assertEqual(data.sample.id, 5) self.assertEqual(data.resolwe.sample.get.call_count, 1) # cache is cleared at update data.update() self.assertEqual(data._sample, None) # raising error if data object is not saved data.id = None with self.assertRaises(ValueError): _ = data.sample
def test_sample(self): data = Data(id=1, resolwe=MagicMock()) data.resolwe.sample.filter = MagicMock(return_value=[]) self.assertEqual(data.sample, None) data.resolwe.sample.filter = MagicMock(return_value=['sample']) self.assertEqual(data.sample, 'sample') # test caching self.assertEqual(data.sample, 'sample') self.assertEqual(data.resolwe.sample.filter.call_count, 1) # cache is cleared at update data._sample = 'sample' data.update() self.assertEqual(data._sample, None) # raising error if data object is not saved data.id = None with self.assertRaises(ValueError): _ = data.sample
def test_presample(self): data = Data(id=1, resolwe=MagicMock()) data.resolwe.presample.filter = MagicMock(return_value=[]) self.assertEqual(data.presample, None) data.resolwe.presample.filter = MagicMock(return_value=['presample']) self.assertEqual(data.presample, 'presample') # don't overwrite existing presample data.resolwe.presample.filter = MagicMock(return_value=['presample 2']) self.assertEqual(data.presample, 'presample') # remove presample when gone data.resolwe.presample.filter = MagicMock(return_value=[]) self.assertEqual(data.presample, None) # cache is cleared at update data._presample = 'presample' data.update() self.assertEqual(data._presample, None)