-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add flag to @service decorator to create and initialize the service o…
…n application startup (#41)
- Loading branch information
1 parent
8124fb2
commit 1c1dda6
Showing
7 changed files
with
91 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import pytest | ||
|
||
from selva.di import service, Container | ||
|
||
|
||
@pytest.fixture | ||
def service_class(): | ||
class ServiceClass: | ||
startup_called = False | ||
|
||
def initialize(self): | ||
ServiceClass.startup_called = not ServiceClass.startup_called | ||
|
||
return ServiceClass | ||
|
||
|
||
@pytest.fixture | ||
def service_factory(service_class): | ||
def service_factory() -> service_class: | ||
service_class.startup_called = not service_class.startup_called | ||
return service_class() | ||
|
||
return service_factory | ||
|
||
|
||
async def test_startup_class(ioc: Container, service_class): | ||
ioc.register(service_class, startup=True) | ||
await ioc._run_startup() | ||
assert service_class.startup_called | ||
|
||
|
||
async def test_startup_factory(ioc: Container, service_class, service_factory): | ||
ioc.register(service_factory, startup=True) | ||
await ioc._run_startup() | ||
assert service_class.startup_called |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from selva.configuration.defaults import default_settings | ||
from selva.configuration.settings import Settings | ||
from selva.di import service | ||
from selva.web.application import Selva | ||
|
||
|
||
async def test_application(): | ||
@service(startup=True) | ||
class Service: | ||
startup_called = False | ||
|
||
def initialize(self): | ||
Service.startup_called = True | ||
|
||
settings = Settings( | ||
default_settings | { | ||
"application": f"{__package__}.application", | ||
} | ||
) | ||
|
||
app = Selva(settings) | ||
app.di.service(Service) | ||
|
||
await app._lifespan_startup() | ||
|
||
assert Service.startup_called |