Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thought-tobi committed Apr 2, 2024
1 parent 9d80793 commit 913dc1c
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 38 deletions.
28 changes: 24 additions & 4 deletions test/integration/conftest.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
from unittest.mock import patch

import mongomock
import pymongo
import pytest
from kink import di
from pymongo import MongoClient

from src.app import MoodTrackerApplication
from src.config import ConfigurationProvider
from src.repository import record_repository
from src.repository.user_repository import UserRepository


@pytest.fixture(autouse=True)
def patch_user_collection():
mock_client = mongomock.MongoClient()
di['mongo_client'] = mock_client
def mock_client():
return mongomock.MongoClient()


@pytest.fixture(autouse=True)
def user_repository(mock_client):
user_repository = UserRepository(mock_client)
user_repository.register()
return user_repository


@pytest.fixture(autouse=True)
Expand All @@ -23,3 +29,17 @@ def patch_records_collection():
record_repository, "records", new=mongomock.MongoClient().db.collection
):
yield


@pytest.fixture(autouse=True)
def configuration():
ConfigurationProvider("test/resources/config.test.yaml").get_configuration().register()


@pytest.fixture(autouse=True)
def application():
# initializes application, registers notifier implicitly
application = MoodTrackerApplication("some-token")
di[MoodTrackerApplication] = application
return application

14 changes: 9 additions & 5 deletions test/integration/test_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ async def test_record_registration(button_update, update):
Does not cover the state transition from Metric N to Finished.
"""
# when user calls /record
await create_user(update, None)
await command_handlers.record_handler(update, None)

# bot responds with first metric user prompt
# omit this in further tests
assert update.effective_user.get_bot().send_message.call_count == 1
assert command_handlers.prompt_user_for_metric.call_count == 1

# when user response is received
Expand All @@ -108,13 +108,15 @@ async def test_record_registration(button_update, update):


@pytest.mark.asyncio
async def test_finish_record_creation(update, button_update, mocker, user, metrics):
async def test_finish_record_creation(update, button_update, user, metrics):
"""
Tests state transition from recording Metric N to Finished.
"""
# given only one metric is defined
# mocker.patch("src.repository.user_repository.find_user", return_value=user)
user.metrics = [metrics[0]]
# given a user with only one metric is registered
configuration = ConfigurationProvider("test/resources/config.test.yaml").get_configuration()
configuration.metrics = configuration.metrics[:1]
configuration.register()
await create_user(update, None)

# when user calls /record
await command_handlers.record_handler(update, None)
Expand All @@ -140,6 +142,7 @@ async def test_double_answer_works_as_intended(update, button_update):
Extends `test_record_registration`
:return:
"""
await create_user(update, None)
# when user calls /record
await command_handlers.record_handler(update, None)

Expand All @@ -162,6 +165,7 @@ async def test_double_answer_works_as_intended(update, button_update):

@pytest.mark.asyncio
async def test_record_with_offset(update):
await create_user(update, None)
# when user calls /record
await command_handlers.record_handler(update, None)

Expand Down
19 changes: 2 additions & 17 deletions test/integration/test_user_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import pytest

import src.repository.user_repository as user_repository
from src.app import MoodTrackerApplication
from kink import di

Expand All @@ -19,26 +18,12 @@ def update():
return update


@pytest.fixture(autouse=True)
def mock_notifier():
application = MoodTrackerApplication("some-token")
di[MoodTrackerApplication] = application
assert di[Notifier.get_fully_qualified_name()] is not None


@pytest.fixture(autouse=True)
def configuration():
ConfigurationProvider(
"test/resources/config.test.yaml"
).get_configuration().register()


def test_querying_nonexistent_user_returns_none():
def test_querying_nonexistent_user_returns_none(user_repository):
assert user_repository.find_user(1) is None


@pytest.mark.asyncio
async def test_registration(update):
async def test_registration(update, user_repository):
# user does not exist
assert user_repository.find_user(1) is None

Expand Down
56 changes: 44 additions & 12 deletions test/unit/test_autowiring.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import kink
import pytest

from src.autowiring.inject import autowire
from src.autowiring.inject import autowire, ParameterNotInCacheError, ParameterNotInSignatureError
from src.autowiring.injectable import Injectable


Expand All @@ -12,22 +13,53 @@ def __init__(self, field: str = "some-value"):
self.field = field


@pytest.fixture(autouse=True)
def setup():
def test_trivial_autowiring():
SomeClass().register()


def test_trivial_autowiring():
@autowire("test_class")
def test_func(test_class: SomeClass):
return test_class.field
@autowire("some_class")
def test_func(some_class: SomeClass):
return some_class.field

assert test_func() == "some-value"


def test_autowiring_with_args():
@autowire("test_class")
def test_func(string: str, test_class: SomeClass):
return test_class.field + string
SomeClass().register()

@autowire("some_class")
def test_func(string: str, some_class: SomeClass):
return some_class.field + string

assert test_func("arg") == "some-valuearg"


def test_exception_thrown_when_di_cache_is_empty():
# the fact that there is no way of unregistering things may be problematic
kink.di._services = {}

@autowire("some_class")
def test_func(some_class: SomeClass):
return some_class.field

with pytest.raises(ParameterNotInCacheError):
test_func()


def test_cache_object_can_be_overwritten():
@autowire("some_class")
def test_func(some_class: SomeClass):
return some_class.field

kink.di[SomeClass.get_fully_qualified_name()] = SomeClass("new-value")
assert test_func() == "new-value"


def test_exception_thrown_on_argument_mismatch():
SomeClass().register()

@autowire("another_class")
def test_func(some_class: SomeClass):
return some_class.field

assert test_func(string="arg") == "some-valuearg"
with pytest.raises(ParameterNotInSignatureError):
test_func()

0 comments on commit 913dc1c

Please sign in to comment.