forked from collerek/ormar
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use lifespan function instead of event
- Loading branch information
Showing
129 changed files
with
1,596 additions
and
3,470 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 |
---|---|---|
|
@@ -39,7 +39,7 @@ jobs: | |
POSTGRES_DB: testsuite | ||
ports: | ||
- 5432:5432 | ||
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5 | ||
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5 --name postgres | ||
|
||
steps: | ||
- name: Checkout | ||
|
@@ -71,6 +71,30 @@ jobs: | |
DATABASE_URL: "mysql://username:[email protected]:3306/testsuite" | ||
run: bash scripts/test.sh | ||
|
||
- name: Install postgresql-client | ||
run: | | ||
sudo apt-get update | ||
sudo apt-get install --yes postgresql-client | ||
- name: Connect to PostgreSQL with CLI | ||
run: env PGPASSWORD=password psql -h localhost -U username -c 'SELECT VERSION();' testsuite | ||
|
||
- name: Show max connections | ||
run: env PGPASSWORD=password psql -h localhost -U username -c 'SHOW max_connections;' testsuite | ||
|
||
- name: Alter max connections | ||
run: | | ||
docker exec -i postgres bash << EOF | ||
sed -i -e 's/max_connections = 100/max_connections = 1000/' /var/lib/postgresql/data/postgresql.conf | ||
sed -i -e 's/shared_buffers = 128MB/shared_buffers = 512MB/' /var/lib/postgresql/data/postgresql.conf | ||
EOF | ||
docker restart --time 0 postgres | ||
sleep 5 | ||
- name: Show max connections | ||
run: env PGPASSWORD=password psql -h localhost -U username -c 'SHOW max_connections;' testsuite | ||
|
||
- name: Run postgres | ||
env: | ||
DATABASE_URL: "postgresql://username:password@localhost:5432/testsuite" | ||
|
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,33 @@ | ||
import pytest | ||
import sqlalchemy | ||
|
||
from contextlib import asynccontextmanager | ||
from fastapi import FastAPI | ||
from typing import AsyncIterator | ||
|
||
|
||
def lifespan(config): | ||
@asynccontextmanager | ||
async def do_lifespan(_: FastAPI) -> AsyncIterator[None]: | ||
if not config.database.is_connected: | ||
await config.database.connect() | ||
|
||
yield | ||
|
||
if config.database.is_connected: | ||
await config.database.disconnect() | ||
|
||
return do_lifespan | ||
|
||
|
||
def init_tests(config, scope="module"): | ||
@pytest.fixture(autouse=True, scope=scope) | ||
def create_database(): | ||
config.engine = sqlalchemy.create_engine(config.database.url._url) | ||
config.metadata.create_all(config.engine) | ||
|
||
yield | ||
|
||
config.metadata.drop_all(config.engine) | ||
|
||
return create_database |
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,23 @@ | ||
import os | ||
|
||
import databases | ||
import ormar | ||
import sqlalchemy | ||
|
||
DATABASE_URL = os.getenv("DATABASE_URL", "sqlite:///test.db") | ||
database_url = databases.DatabaseURL(DATABASE_URL) | ||
if database_url.scheme == "postgresql+aiopg": # pragma no cover | ||
DATABASE_URL = str(database_url.replace(driver=None)) | ||
print("USED DB:", DATABASE_URL) | ||
|
||
|
||
def create_config(**args): | ||
database_ = databases.Database(DATABASE_URL, **args) | ||
metadata_ = sqlalchemy.MetaData() | ||
engine_ = sqlalchemy.create_engine(DATABASE_URL) | ||
|
||
return ormar.OrmarConfig( | ||
metadata=metadata_, | ||
database=database_, | ||
engine=engine_, | ||
) |
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
Oops, something went wrong.