mirror of
https://github.com/galaxyproject/galaxy.git
synced 2026-09-24 16:30:27 +08:00
Dependency injection is cool - it should however not be confined to the top-layer of the application. Inversion of control can help every layer and is a great way to de-couple various parts of the app.
This code brings in a framework (Lagom) to build a dependency injection framework and then adds a small function (``depends``) that adapts it to FastAPI's dependency injection mechanism ("Depends").
The advantages to this approach are numerous.
We don't need to write these little adapters for each component of the backend to adapt it FastAPI. For example look at the change to the roles API controller:
```diff
-def get_role_manager(app: UniverseApplication = Depends(get_app)) -> RoleManager:
- return app.role_manager
-
-
@cbv(router)
class FastAPIRoles:
- role_manager: RoleManager = Depends(get_role_manager)
+ role_manager: RoleManager = depends(RoleManager)
```
This is much less boilerplate. We don't need to implement & type that function get_role_manager and we don't need to bring in the import on UniverseApplication.
Additionally, we've got a clean abstraction that shields us from ``fastapi`` imports in all of our controllers. It should make it more possible to switch to new frameworks and such as the Python ecosystem matures.
Also, the same DI that is used to inject ``RoleManager`` into this contoller is used to inject app into RoleManager when it is constructed during application initialization. Any component being managed by UniverseApplication can now rely on its constructor arguments to be injected if it wants. It is easy to see the cool examples on FastAPI and think it is just a technology for controllers, but it totally is not.
I don't think there is really a way to use FastAPI's dependency injection outside the context of that framework, but even if one could Lagom is superior. All the auto-wiring is by type and requires zero framework and zero configuration (https://github.com/meadsteve/lagom#auto-wiring-with-zero-configuraton).
Having a web framework provide these framework-bound extension points for injecting stuff into controllers was the state of the art of Java like 15 years ago. Skipping that whole learning process and using the type system and auto-wiring that isn't dependent on framework annotation really jumps out to Java circa 8 years ago!
Why Lagom is an interesting question. When researching DI frameworks, I couldn't find a clear winner but Lagom focus on type annotations versus annotating by name makes it clearly more modern than a lot frameworks by much bigger names (https://github.com/meadsteve/lagom/blob/master/docs/comparison.md). The other type-centric framework that had even a nice interface that I found was punq (https://punq.readthedocs.io/en/latest/). The development just doesn't seem as active on punq. While I didn't land up using the integration Lagom seems to have async frameworks in mind (https://github.com/meadsteve/lagom/blob/master/lagom/integrations/fast_api.py), so that is another plus. Ultimately though I think I can swap between these two with like 10 lines of code switch, they do cool things with very simple interfaces and neither requires a bunch of investment in annotation on your components.
124 lines
5.7 KiB
Python
124 lines
5.7 KiB
Python
#!/usr/bin/env python
|
|
"""
|
|
"""
|
|
import unittest
|
|
|
|
from galaxy import model
|
|
from galaxy.managers.collections import DatasetCollectionManager
|
|
from galaxy.managers.datasets import DatasetManager
|
|
from galaxy.managers.hdas import HDAManager
|
|
from galaxy.managers.histories import HistoryManager
|
|
from .base import BaseTestCase, CreatesCollectionsMixin
|
|
|
|
# =============================================================================
|
|
default_password = '123456'
|
|
user2_data = dict(email='user2@user2.user2', username='user2', password=default_password)
|
|
user3_data = dict(email='user3@user3.user3', username='user3', password=default_password)
|
|
|
|
|
|
# =============================================================================
|
|
class DatasetCollectionManagerTestCase(BaseTestCase, CreatesCollectionsMixin):
|
|
|
|
def set_up_managers(self):
|
|
super().set_up_managers()
|
|
self.dataset_manager = self.app[DatasetManager]
|
|
self.hda_manager = self.app[HDAManager]
|
|
self.history_manager = self.app[HistoryManager]
|
|
self.collection_manager = self.app[DatasetCollectionManager]
|
|
|
|
def test_create_simple_list(self):
|
|
owner = self.user_manager.create(**user2_data)
|
|
|
|
history = self.history_manager.create(name='history1', user=owner)
|
|
|
|
hda1 = self.hda_manager.create(name='one',
|
|
history=history, dataset=self.dataset_manager.create())
|
|
hda2 = self.hda_manager.create(name='two',
|
|
history=history, dataset=self.dataset_manager.create())
|
|
hda3 = self.hda_manager.create(name='three',
|
|
history=history, dataset=self.dataset_manager.create())
|
|
|
|
self.log("should be able to create a new Collection via ids")
|
|
element_identifiers = self.build_element_identifiers([hda1, hda2, hda3])
|
|
hdca = self.collection_manager.create(self.trans, history, 'test collection', 'list',
|
|
element_identifiers=element_identifiers)
|
|
self.assertIsInstance(hdca, model.HistoryDatasetCollectionAssociation)
|
|
self.assertEqual(hdca.name, 'test collection')
|
|
self.assertEqual(hdca.hid, 4)
|
|
self.assertFalse(hdca.deleted)
|
|
self.assertTrue(hdca.visible)
|
|
|
|
self.log("should contain an underlying, well-formed DatasetCollection")
|
|
self.assertIsInstance(hdca.collection, model.DatasetCollection)
|
|
collection = hdca.collection
|
|
self.assertEqual(collection.collection_type, 'list')
|
|
self.assertEqual(collection.state, 'ok')
|
|
self.assertEqual(len(collection.dataset_instances), 3)
|
|
self.assertEqual(len(collection.elements), 3)
|
|
|
|
self.log("and that collection should have three well-formed Elements")
|
|
self.assertIsInstance(collection.elements[0], model.DatasetCollectionElement)
|
|
self.assertEqual(collection.elements[0].element_identifier, 'one')
|
|
self.assertEqual(collection.elements[0].element_index, 0)
|
|
self.assertEqual(collection.elements[0].element_type, 'hda')
|
|
self.assertEqual(collection.elements[0].element_object, hda1)
|
|
|
|
self.assertIsInstance(collection.elements[1], model.DatasetCollectionElement)
|
|
self.assertEqual(collection.elements[1].element_identifier, 'two')
|
|
self.assertEqual(collection.elements[1].element_index, 1)
|
|
self.assertEqual(collection.elements[1].element_type, 'hda')
|
|
self.assertEqual(collection.elements[1].element_object, hda2)
|
|
|
|
self.assertIsInstance(collection.elements[2], model.DatasetCollectionElement)
|
|
self.assertEqual(collection.elements[2].element_identifier, 'three')
|
|
self.assertEqual(collection.elements[2].element_index, 2)
|
|
self.assertEqual(collection.elements[2].element_type, 'hda')
|
|
self.assertEqual(collection.elements[2].element_object, hda3)
|
|
|
|
self.log("should be able to create a new Collection via objects")
|
|
elements = dict(one=hda1, two=hda2, three=hda3)
|
|
hdca2 = self.collection_manager.create(self.trans, history, 'test collection 2', 'list', elements=elements)
|
|
self.assertIsInstance(hdca2, model.HistoryDatasetCollectionAssociation)
|
|
|
|
def test_update_from_dict(self):
|
|
owner = self.user_manager.create(**user2_data)
|
|
|
|
history = self.history_manager.create(name='history1', user=owner)
|
|
|
|
hda1 = self.hda_manager.create(name='one',
|
|
history=history, dataset=self.dataset_manager.create())
|
|
hda2 = self.hda_manager.create(name='two',
|
|
history=history, dataset=self.dataset_manager.create())
|
|
hda3 = self.hda_manager.create(name='three',
|
|
history=history, dataset=self.dataset_manager.create())
|
|
|
|
elements = dict(one=hda1, two=hda2, three=hda3)
|
|
hdca = self.collection_manager.create(self.trans, history, 'test collection', 'list', elements=elements)
|
|
|
|
self.log("should be set from a dictionary")
|
|
self.collection_manager._set_from_dict(self.trans, hdca, {
|
|
'deleted': True,
|
|
'visible': False,
|
|
'name': 'New Name',
|
|
# TODO: doesn't work
|
|
# 'tags' : [ 'one', 'two', 'three' ]
|
|
# 'annotations' : [?]
|
|
})
|
|
self.assertEqual(hdca.name, 'New Name')
|
|
self.assertTrue(hdca.deleted)
|
|
self.assertFalse(hdca.visible)
|
|
# self.assertEqual( hdca.tags, [ 'one', 'two', 'three' ] )
|
|
# self.assertEqual( hdca.annotations, [ 'one', 'two', 'three' ] )
|
|
|
|
# def test_validation( self ):
|
|
# self.log( "should be able to change the name" )
|
|
# self.log( "should be able to set deleted" )
|
|
# self.log( "should be able to set visible" )
|
|
# self.log( "should be able to set tags" )
|
|
|
|
|
|
# =============================================================================
|
|
if __name__ == '__main__':
|
|
# or more generally, nosetests test_resourcemanagers.py -s -v
|
|
unittest.main()
|