-
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 option to set application in settings
- Loading branch information
1 parent
e6198ea
commit fa4f1b9
Showing
14 changed files
with
110 additions
and
83 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import os | ||
|
||
from dotenv import load_dotenv | ||
|
||
__all__ = ("init",) | ||
|
||
|
||
def init(): | ||
dotenv_path = os.getenv("SELVA_DOTENV", os.path.join(os.getcwd(), ".env")) | ||
load_dotenv(dotenv_path) |
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 |
---|---|---|
@@ -1,12 +1,7 @@ | ||
import os | ||
|
||
from dotenv import load_dotenv | ||
|
||
from selva._util import dotenv | ||
from selva.configuration.settings import get_settings | ||
from selva.web.application import Selva | ||
|
||
dotenv_path = os.getenv("SELVA_DOTENV", os.path.join(os.getcwd(), ".env")) | ||
load_dotenv(dotenv_path) | ||
|
||
dotenv.init() | ||
settings = get_settings() | ||
app = Selva(settings) |
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 |
---|---|---|
@@ -1,18 +1,20 @@ | ||
import os | ||
from pathlib import Path | ||
|
||
from selva._util import dotenv | ||
|
||
|
||
def test_dotenv(monkeypatch): | ||
monkeypatch.chdir(Path(__file__).parent / "dotenv") | ||
from selva import run | ||
dotenv.init() | ||
|
||
assert "TEST_VARIABLE" in os.environ | ||
assert os.environ["TEST_VARIABLE"] == "value" | ||
|
||
|
||
def test_dotenv_custom_location(monkeypatch): | ||
monkeypatch.setenv("SELVA_DOTENV", str(Path(__file__).parent / "dotenv" / ".env")) | ||
from selva import run | ||
dotenv.init() | ||
|
||
assert "TEST_VARIABLE" in os.environ | ||
assert os.environ["TEST_VARIABLE"] == "value" |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import logging | ||
import os | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
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 |
---|---|---|
@@ -1,9 +1,10 @@ | ||
from asgikit.responses import respond_text | ||
|
||
from selva.web import controller, get | ||
|
||
|
||
@controller | ||
class Controller: | ||
@get | ||
async def index(self, request): | ||
await respond_text(request.response, "Selva") | ||
await respond_text(request.response, "Ok") |
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,38 @@ | ||
from asgikit.requests import Request | ||
from httpx import AsyncClient | ||
|
||
from selva.configuration import Settings | ||
from selva.configuration.defaults import default_settings | ||
from selva.di import service | ||
from selva.web.application import Selva | ||
from selva.web.middleware import Middleware | ||
|
||
|
||
@service | ||
class TestMiddleware(Middleware): | ||
async def __call__(self, call, request): | ||
send = request.asgi.send | ||
|
||
async def new_send(event: dict): | ||
if event["type"] == "http.response.body": | ||
event["body"] = b"Middleware Ok" | ||
await send(event) | ||
|
||
new_request = Request(request.asgi.scope, request.asgi.receive, new_send) | ||
await call(new_request) | ||
|
||
|
||
async def test_middleware(): | ||
settings = Settings( | ||
default_settings | ||
| { | ||
"application": "tests.web.application.application", | ||
"middleware": ["tests.web.application.test_middleware.TestMiddleware"], | ||
} | ||
) | ||
app = Selva(settings) | ||
await app._lifespan_startup() | ||
|
||
client = AsyncClient(app=app) | ||
response = await client.get("http://localhost:8000/") | ||
assert response.text == "Middleware Ok" |