Skip to content

Commit

Permalink
basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nstoik committed Mar 15, 2021
1 parent 8e3e1c7 commit d507f8a
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
4 changes: 3 additions & 1 deletion fd_device/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class ProdConfig(Config):
"""Production configuration."""

DEBUG = False
LOG_LEVEL = logging.ERROR
LOG_LEVEL = logging.INFO


class TestConfig(Config):
Expand All @@ -62,6 +62,8 @@ class TestConfig(Config):
DEBUG = True
TESTING = True

# If encountering problems with session access, use a tmp file db instead
# SQLALCHEMY_DATABASE_URI = "sqlite:////tmp/fd_device_test_db.sqlite"
SQLALCHEMY_DATABASE_URI = "sqlite:///:memory:"


Expand Down
46 changes: 46 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# -*- coding: utf-8 -*-
"""Defines fixtures available to all tests."""
# pylint: disable=redefined-outer-name
import pytest
from _pytest.monkeypatch import MonkeyPatch

from fd_device.database.base import create_all_tables, drop_all_tables, get_session


@pytest.fixture(scope="session")
# pylint: disable=unused-argument
def monkeysession(request):
"""Create a MonkeyPatch object that can be scoped to a session.
https://github.com/pytest-dev/pytest/issues/363#issuecomment-289830794
"""
mpatch = MonkeyPatch()
yield mpatch
mpatch.undo()


@pytest.fixture(scope="session", autouse=True)
def set_testing_env(monkeysession):
"""Set the environment variable for testing.
This executes once for the entire session of testing.
The environment variables are set back to the default after.
"""
monkeysession.setenv("FD_DEVICE_CONFIG", "test")
yield
monkeysession.setenv("FD_DEVICE_CONFIG", "dev")


@pytest.fixture(scope="session")
def dbsession():
"""Returns an sqlalchemy session."""
yield get_session()


@pytest.fixture()
def tables(dbsession):
"""Create all tables for testing. Delete when done."""
create_all_tables()
yield
dbsession.close()
drop_all_tables()
10 changes: 10 additions & 0 deletions tests/test_functional.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"""Test functional aspects of the app."""

from fd_device.settings import TestConfig, get_config


def test_main_env():
"""Test that the main environment variable is set for testing."""

config = get_config()
assert config == TestConfig
16 changes: 16 additions & 0 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# pylint: disable=unused-argument
"""Test settings."""
from fd_device.settings import DevConfig, ProdConfig, TestConfig, get_config


def test_retrieving_settings():
"""Test that the correct settings are retrieved."""
test_config = get_config(override_default="test")
dev_config = get_config(override_default="dev")
prod_config = get_config(override_default="prod")
wrong_config = get_config(override_default="wrong")

assert test_config == TestConfig
assert dev_config == DevConfig
assert prod_config == ProdConfig
assert wrong_config == DevConfig

0 comments on commit d507f8a

Please sign in to comment.