Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(tests): use in-memory SQLite databases for testing where possible #2080

Merged
merged 3 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
provinzkraut marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from pathlib import Path
from unittest import TestCase

import pytest
Expand All @@ -24,6 +25,9 @@ def setUp(self):

def tearDown(self):
Task.alter().drop_table().run_sync()
from docs.examples.contrib.piccolo.piccolo_conf import DB

Path(DB.path).unlink()

def test_get_tasks(self):
with TestClient(app=app) as client:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,75 @@
from typing import Any

import pytest
from _pytest.monkeypatch import MonkeyPatch
from sqlalchemy import Engine, StaticPool, create_engine

from litestar.testing import TestClient

pytestmark = pytest.mark.xdist_group("sqla-plugin-examples")


@pytest.fixture
def data() -> list[dict[str, Any]]:
return [{"title": "test", "done": False}]


def test_sqlalchemy_async_plugin_example(data: dict[str, Any]) -> None:
from docs.examples.contrib.sqlalchemy.plugins.sqlalchemy_async_plugin_example import app
@pytest.fixture()
def sqlite_engine() -> Engine:
return create_engine("sqlite://", connect_args={"check_same_thread": False}, poolclass=StaticPool)

with TestClient(app) as client:

def test_sqlalchemy_async_plugin_example(data: dict[str, Any], monkeypatch: MonkeyPatch) -> None:
from docs.examples.contrib.sqlalchemy.plugins import sqlalchemy_async_plugin_example

monkeypatch.setattr(sqlalchemy_async_plugin_example.config, "connection_string", "sqlite+aiosqlite://")

with TestClient(sqlalchemy_async_plugin_example.app) as client:
assert client.post("/", json=data[0]).json() == data


def test_sqlalchemy_sync_plugin_example(data: dict[str, Any]) -> None:
from docs.examples.contrib.sqlalchemy.plugins.sqlalchemy_sync_plugin_example import app
def test_sqlalchemy_sync_plugin_example(data: dict[str, Any], monkeypatch: MonkeyPatch, sqlite_engine: Engine) -> None:
from docs.examples.contrib.sqlalchemy.plugins import sqlalchemy_sync_plugin_example

with TestClient(app) as client:
monkeypatch.setattr(sqlalchemy_sync_plugin_example.config, "engine_instance", sqlite_engine)

with TestClient(sqlalchemy_sync_plugin_example.app) as client:
assert client.post("/", json=data[0]).json() == data


def test_sqlalchemy_async_init_plugin_example(data: dict[str, Any]) -> None:
from docs.examples.contrib.sqlalchemy.plugins.sqlalchemy_async_init_plugin_example import app
def test_sqlalchemy_async_init_plugin_example(data: dict[str, Any], monkeypatch: MonkeyPatch) -> None:
from docs.examples.contrib.sqlalchemy.plugins import sqlalchemy_async_init_plugin_example

with TestClient(app) as client:
monkeypatch.setattr(sqlalchemy_async_init_plugin_example.config, "connection_string", "sqlite+aiosqlite://")

with TestClient(sqlalchemy_async_init_plugin_example.app) as client:
assert client.post("/", json=data[0]).json() == data


def test_sqlalchemy_sync_init_plugin_example(data: dict[str, Any]) -> None:
from docs.examples.contrib.sqlalchemy.plugins.sqlalchemy_sync_init_plugin_example import app
def test_sqlalchemy_sync_init_plugin_example(
data: dict[str, Any], monkeypatch: MonkeyPatch, sqlite_engine: Engine
) -> None:
from docs.examples.contrib.sqlalchemy.plugins import sqlalchemy_sync_init_plugin_example

with TestClient(app) as client:
monkeypatch.setattr(sqlalchemy_sync_init_plugin_example.config, "engine_instance", sqlite_engine)

with TestClient(sqlalchemy_sync_init_plugin_example.app) as client:
assert client.post("/", json=data[0]).json() == data


def test_sqlalchemy_async_init_plugin_dependencies() -> None:
from docs.examples.contrib.sqlalchemy.plugins.sqlalchemy_async_dependencies import app
def test_sqlalchemy_async_init_plugin_dependencies(monkeypatch: MonkeyPatch) -> None:
from docs.examples.contrib.sqlalchemy.plugins import sqlalchemy_async_dependencies

with TestClient(app) as client:
monkeypatch.setattr(sqlalchemy_async_dependencies.config, "connection_string", "sqlite+aiosqlite://")

with TestClient(sqlalchemy_async_dependencies.app) as client:
assert client.post("/").json() == [1, 2]


def test_sqlalchemy_sync_init_plugin_dependencies() -> None:
from docs.examples.contrib.sqlalchemy.plugins.sqlalchemy_sync_dependencies import app
def test_sqlalchemy_sync_init_plugin_dependencies(monkeypatch: MonkeyPatch) -> None:
from docs.examples.contrib.sqlalchemy.plugins import sqlalchemy_sync_dependencies

with TestClient(app) as client:
monkeypatch.setattr(sqlalchemy_sync_dependencies.config, "connection_string", "sqlite://")

with TestClient(sqlalchemy_sync_dependencies.app) as client:
assert client.post("/").json() == [1, 2]


Expand Down
26 changes: 16 additions & 10 deletions tests/examples/test_plugins/test_sqlalchemy_init_plugin.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
from __future__ import annotations

from typing import TYPE_CHECKING

import pytest
from docs.examples.plugins.sqlalchemy_init_plugin.sqlalchemy_async import app as async_sqla_app
from docs.examples.plugins.sqlalchemy_init_plugin.sqlalchemy_sync import app as sync_sqla_app
from _pytest.monkeypatch import MonkeyPatch

from litestar.testing import TestClient

if TYPE_CHECKING:
from litestar import Litestar

def test_sync_app(monkeypatch: MonkeyPatch) -> None:
from docs.examples.plugins.sqlalchemy_init_plugin import sqlalchemy_sync

monkeypatch.setattr(sqlalchemy_sync.sqlalchemy_config, "connection_string", "sqlite://")
with TestClient(app=sqlalchemy_sync.app) as client:
res = client.get("/sqlalchemy-app")
assert res.status_code == 200
assert res.text == "1 2"


def test_async_app(monkeypatch: MonkeyPatch) -> None:
from docs.examples.plugins.sqlalchemy_init_plugin import sqlalchemy_async

monkeypatch.setattr(sqlalchemy_async.sqlalchemy_config, "connection_string", "sqlite+aiosqlite://")

@pytest.mark.parametrize("app", [async_sqla_app, sync_sqla_app])
def test_app(app: Litestar) -> None:
with TestClient(app=app) as client:
with TestClient(app=sqlalchemy_async.app) as client:
res = client.get("/sqlalchemy-app")
assert res.status_code == 200
assert res.text == "1 2"