Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Consolidate fixtures into conftest.py
I didn't see this file when I first added the new fixtures.
  • Loading branch information
james-gray committed Apr 2, 2020
commit afca2a934130652d0d8152595387bfd2a4fdbbc5
91 changes: 90 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@

Global conftest file for shared pytest fixtures
"""
import datetime
import os
from contextlib import asynccontextmanager
from unittest import mock

import pytest
from fastapi.testclient import TestClient
from async_asgi_testclient import TestClient as AsyncTestClient
from fastapi.testclient import TestClient

from app.main import APP
from app.utils import httputils


@pytest.fixture
Expand All @@ -25,3 +31,86 @@ async def async_api_client():
Returns an async_asgi_testclient.TestClient.
"""
return AsyncTestClient(APP)


class DateTimeStrpTime:
"""Returns instance of `DateTimeStrpTime`
when calling `app.services.location.jhu.datetime.trptime(date, '%m/%d/%y').isoformat()`.
"""

def __init__(self, date, strformat):
self.date = date
self.strformat = strformat

def isoformat(self):
return datetime.datetime.strptime(self.date, self.strformat).isoformat()


class FakeRequestsGetResponse:
"""Fake instance of a response from `aiohttp.ClientSession.get`.
"""

def __init__(self, url, filename, state):
self.url = url
self.filename = filename
self.state = state

async def text(self):
return self.read_file(self.state)

def read_file(self, state):
"""
Mock HTTP GET-method and return text from file
"""
state = state.lower()

# Determine filepath.
filepath = os.path.join(os.path.dirname(__file__), "example_data/{}.csv".format(state))

# Return fake response.
print("Try to read {}".format(filepath))
with open(filepath, "r") as file:
return file.read()


@asynccontextmanager
async def mock_client_session():
"""Context manager that replaces the global client_session with an AsyncMock instance.

:Example:

>>> async with mock_client_session() as mocked_client_session:
>>> mocked_client_session.get = mocked_session_get
>>> # test code...

"""

httputils.client_session = mocked_client_session = mock.AsyncMock()
try:
yield mocked_client_session
finally:
del httputils.client_session


@asynccontextmanager
async def mocked_session_get(*args, **kwargs):
"""Mock response from client_session.get.
"""

url = args[0]
filename = url.split("/")[-1]

# clean up for id token (e.g. Deaths)
state = filename.split("-")[-1].replace(".csv", "").lower().capitalize()

yield FakeRequestsGetResponse(url, filename, state)


def mocked_strptime_isoformat(*args, **kwargs):
"""Mock return value from datetime.strptime().isoformat().
"""

date = args[0]
strformat = args[1]

return DateTimeStrpTime(date, strformat)
89 changes: 0 additions & 89 deletions tests/fixtures.py

This file was deleted.

4 changes: 2 additions & 2 deletions tests/test_csbs.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import pytest

from app.services.location import csbs
from tests.fixtures import mock_client_session
from tests.fixtures import mocked_session_get
from tests.conftest import mock_client_session
from tests.conftest import mocked_session_get


def mocked_csbs_requests_get(*args, **kwargs):
Expand Down
6 changes: 3 additions & 3 deletions tests/test_jhu.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

from app import location
from app.services.location import jhu
from tests.fixtures import mock_client_session
from tests.fixtures import mocked_session_get
from tests.fixtures import mocked_strptime_isoformat
from tests.conftest import mock_client_session
from tests.conftest import mocked_session_get
from tests.conftest import mocked_strptime_isoformat

DATETIME_STRING = "2020-03-17T10:23:22.505550"

Expand Down
6 changes: 3 additions & 3 deletions tests/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import pytest
from async_asgi_testclient import TestClient

from .fixtures import mock_client_session
from .fixtures import mocked_session_get
from .fixtures import mocked_strptime_isoformat
from .conftest import mock_client_session
from .conftest import mocked_session_get
from .conftest import mocked_strptime_isoformat
from .test_jhu import DATETIME_STRING
from app.main import APP

Expand Down