Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into hb2-72
Browse files Browse the repository at this point in the history
# Conflicts:
#	homebytwo/conftest.py
  • Loading branch information
Cedric Hofstetter committed Oct 7, 2020
2 parents f30353c + 656d508 commit 33ca89a
Show file tree
Hide file tree
Showing 33 changed files with 913 additions and 640 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -25,4 +25,4 @@ before_script:
- psql -c "CREATE EXTENSION postgis_topology;" -U postgres

script:
- tox
- tox -e travis
3 changes: 3 additions & 0 deletions CHANGELOG.md
@@ -1,4 +1,7 @@
# Change Log
## [0.10.3] - 20200-10-07 Report usage data to coda.io
- Report athlete, routes and activities count on a daily basis

## [0.10.2] - 20200-10-02 Do not import Strava activities on every login
- New flag to only import all athlete Strava activities once

Expand Down
17 changes: 12 additions & 5 deletions config/settings/base.py
Expand Up @@ -46,7 +46,6 @@
"homebytwo.routes",
"homebytwo.importers",
"homebytwo.landingpage",
"homebytwo.celery.CeleryConfig",
]

MIDDLEWARE = [
Expand Down Expand Up @@ -143,10 +142,10 @@
# Celery #
##############

CELERY_BROKER_URL = get_env_variable("CELERY_BROKER_URL", "amqp://localhost")
CELERY_ACCEPT_CONTENT = ["application/json"]
CELERY_RESULT_SERIALIZER = "json"
CELERY_TASK_SERIALIZER = "json"
celery_broker_url = get_env_variable("celery_broker_url", "amqp://localhost")
celery_accept_content = ["application/json"]
celery_result_serializer = "json"
celery_task_serializer = "json"

#############
# Mailchimp #
Expand All @@ -161,6 +160,14 @@

GTM_CONTAINER_ID = get_env_variable("GTM_CONTAINER_ID", "")

###########
# Coda.io #
###########

CODA_API_KEY = get_env_variable("CODA_API_KEY", "")
CODA_DOC_ID = get_env_variable("CODA_DOC_ID", "")
CODA_TABLE_ID = get_env_variable("CODA_TABLE_ID", "")

##########
# Mapbox #
##########
Expand Down
2 changes: 1 addition & 1 deletion homebytwo/__init__.py
@@ -1,3 +1,3 @@
from .celery import app as celery_app

__all__ = ['celery_app']
__all__ = ("celery_app",)
19 changes: 4 additions & 15 deletions homebytwo/celery.py
@@ -1,25 +1,14 @@
from os import environ
from pathlib import Path

from django.apps import AppConfig, apps
from django.conf import settings

from celery import Celery

from config import get_project_root_path, import_env_vars

if not settings.configured:
import_env_vars(Path(get_project_root_path(), "envdir"))
environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.dev")
import_env_vars(Path(get_project_root_path(), "envdir"))
environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.dev")

app = Celery("homebytwo")


class CeleryConfig(AppConfig):
name = "homebytwo.celery"
verbose_name = "Celery config for homebytwo"

def ready(self):
app.config_from_object("django.conf:settings", namespace="CELERY")
installed_apps = [app_config.name for app_config in apps.get_app_configs()]
app.autodiscover_tasks(lambda: installed_apps, force=True)
app.config_from_object("django.conf:settings", namespace="celery")
app.autodiscover_tasks()
142 changes: 89 additions & 53 deletions homebytwo/conftest.py
Expand Up @@ -2,87 +2,105 @@
from pathlib import Path

from django.core.files.uploadedfile import SimpleUploadedFile
import httpretty

import responses
from pytest import fixture
from requests.exceptions import ConnectionError

from .utils.factories import AthleteFactory
from .utils.tests import open_data, raise_connection_error
from .utils.tests import open_data

METHODS = {
"get": responses.GET,
"post": responses.POST,
"delete": responses.DELETE,
}


@fixture(autouse=True)
def media_storage(settings, tmpdir):
settings.MEDIA_ROOT = tmpdir.strpath


@fixture()
def celery(settings):
settings.CELERY_TASK_ALWAYS_EAGER = True
settings.CELERY_TASK_EAGER_PROPAGATES = True


@fixture()
@fixture
def athlete(db, client):
athlete = AthleteFactory(user__password="test_password")
client.login(username=athlete.user.username, password="test_password")
return athlete


@fixture()
def test_dir_path(request):
def _test_dir_path():
@fixture
def data_dir_path(request):
def _data_dir_path():
return Path(request.module.__file__).parent.resolve()

return _test_dir_path
return _data_dir_path


@fixture()
def open_file(test_dir_path):
@fixture
def open_file(data_dir_path):
def _open_file(file, binary=False):
return open_data(file, test_dir_path(), binary)
return open_data(file, data_dir_path(), binary)

return _open_file


@fixture()
@fixture
def read_file(open_file):
def _read_file(file, binary=False):
return open_file(file, binary=binary).read()

return _read_file


@fixture()
def enable_httpretty():
def _httpretty(
@fixture
def celery(settings):
settings.celery_task_always_eager = True
settings.celery_task_eager_propagates = True


@fixture
def coda(settings):
settings.CODA_API_KEY = "coda_key"
settings.CODA_DOC_ID = "doc_id"
settings.CODA_TABLE_ID = "grid-table_id"
api_url = "https://coda.io/apis/v1"
doc_url = api_url + f"/docs/{settings.CODA_DOC_ID}/"
table_url = doc_url + f"tables/{settings.CODA_TABLE_ID}"
yield {"doc_url": doc_url, "table_url": table_url}


@fixture
def mocked_responses():
with responses.RequestsMock() as response:
yield response


@fixture
def mock_call_response(mocked_responses):
def _mock_call_response(
call,
uri,
url,
method="get",
body=None,
content_type="application/json",
status=200,
*args,
**kwargs
**kwargs,
):
with httpretty.enabled(allow_net_connect=False):
method_map = {
"get": httpretty.GET,
"post": httpretty.POST,
"delete": httpretty.DELETE,
}
httpretty.register_uri(
method=method_map[method],
uri=uri,
body=body,
content_type=content_type,
status=status,
)
return call(*args, **kwargs)
mocked_responses.add(
method=METHODS[method],
url=url,
body=body,
content_type=content_type,
status=status,
)
return call(*args, **kwargs)

return _httpretty
return _mock_call_response


@fixture()
@fixture
def uploaded_file(read_file):
def _uploaded_file(file):
return SimpleUploadedFile(
Expand All @@ -93,32 +111,50 @@ def _uploaded_file(file):
return _uploaded_file


@fixture()
def intercept(read_file, enable_httpretty):
def _intercept(call, url, response_json, method="get", status=200, *args, **kwargs):
return enable_httpretty(
@fixture
def mock_call_json_response(read_file, mock_call_response):
def _mock_call_json_response(
call, url, response_json, method="get", status=200, *args, **kwargs
):
return mock_call_response(
call,
url,
body=read_file(response_json),
method=method,
status=status,
*args,
**kwargs
**kwargs,
)

return _intercept
return _mock_call_json_response


@fixture
def mock_call_json_responses(read_file, mocked_responses):
def _mock_call_json_responses(call, response_mocks, *args, **kwargs):
for response in response_mocks:
mocked_responses.add(
METHODS.get(response.get("method")) or responses.GET,
response["url"],
body=read_file(response["response_json"]),
status=response.get("status") or 200,
content_type="application/json",
)
return call(*args, **kwargs)

return _mock_call_json_responses


@fixture()
def connection_error(enable_httpretty):
return partial(enable_httpretty, body=raise_connection_error)
@fixture
def connection_error(mock_call_response):
return partial(mock_call_response, body=ConnectionError("Connection error."))


@fixture()
def server_error(intercept):
return partial(intercept, status=500)
@fixture
def server_error(mock_call_json_response):
return partial(mock_call_json_response, status=500)


@fixture()
def not_found(intercept):
return partial(intercept, status=404)
@fixture
def not_found(mock_call_json_response):
return partial(mock_call_json_response, status=404)

0 comments on commit 33ca89a

Please sign in to comment.