2013-03-19 23:03:26 +04:00
|
|
|
import pytest
|
2017-06-09 20:28:30 +03:00
|
|
|
import responses
|
2015-08-10 01:02:37 +03:00
|
|
|
from celery import current_app
|
2013-03-26 20:24:38 +04:00
|
|
|
from django.core.cache import cache
|
2015-11-26 00:35:47 +03:00
|
|
|
from django.core.management import call_command
|
2013-03-19 23:03:26 +04:00
|
|
|
|
2018-01-17 21:09:44 +03:00
|
|
|
from treeherder.config.utils import get_tls_redis_url
|
2017-06-09 20:28:30 +03:00
|
|
|
from treeherder.etl.common import fetch_text
|
|
|
|
|
2013-03-19 23:03:26 +04:00
|
|
|
|
2017-06-09 20:28:30 +03:00
|
|
|
def test_block_unmocked_requests():
|
|
|
|
"""Ensure the `block_unmocked_requests` fixture prevents requests from hitting the network."""
|
|
|
|
url = 'https://example.com'
|
|
|
|
|
|
|
|
with pytest.raises(RuntimeError, message='Tests must mock all HTTP requests!'):
|
|
|
|
fetch_text(url)
|
|
|
|
|
|
|
|
with responses.RequestsMock() as rsps:
|
|
|
|
rsps.add(responses.GET, url, body='Mocked requests still work')
|
|
|
|
text = fetch_text(url)
|
|
|
|
assert text == 'Mocked requests still work'
|
|
|
|
|
|
|
|
|
2017-01-04 02:00:09 +03:00
|
|
|
@pytest.mark.django_db
|
2015-11-26 00:35:47 +03:00
|
|
|
def test_no_missing_migrations():
|
|
|
|
"""Check no model changes have been made since the last `./manage.py makemigrations`."""
|
2017-01-05 03:03:59 +03:00
|
|
|
call_command('makemigrations', interactive=False, dry_run=True, check_changes=True)
|
2015-11-26 00:35:47 +03:00
|
|
|
|
|
|
|
|
2018-01-11 20:42:22 +03:00
|
|
|
def test_django_cache():
|
|
|
|
"""Test the Django cache backend & associated server are properly set up."""
|
2013-03-28 04:40:42 +04:00
|
|
|
k, v = 'my_key', 'my_value'
|
2018-01-11 20:42:22 +03:00
|
|
|
cache.set(k, v, 10)
|
2013-03-28 04:40:42 +04:00
|
|
|
assert cache.get(k) == v
|
2013-05-13 21:19:19 +04:00
|
|
|
|
|
|
|
|
2018-01-17 21:09:44 +03:00
|
|
|
def test_get_tls_redis_url():
|
|
|
|
"""
|
|
|
|
Test conversion from REDIS_URL to the stunnel TLS URL described here:
|
|
|
|
https://devcenter.heroku.com/articles/securing-heroku-redis#connecting-directly-to-stunnel
|
|
|
|
"""
|
|
|
|
REDIS_URL = 'redis://h:abc8069@ec2-12-34-56-78.compute-1.amazonaws.com:8069'
|
|
|
|
TLS_REDIS_URL = 'rediss://h:abc8069@ec2-12-34-56-78.compute-1.amazonaws.com:8070'
|
|
|
|
assert get_tls_redis_url(REDIS_URL) == TLS_REDIS_URL
|
|
|
|
|
|
|
|
|
2013-05-13 21:19:19 +04:00
|
|
|
@current_app.task
|
|
|
|
def add(x, y):
|
|
|
|
return x + y
|
|
|
|
|
|
|
|
|
|
|
|
def test_celery_setup():
|
|
|
|
"Test celery executes a task properly"
|
|
|
|
|
|
|
|
result = add.delay(7, 3)
|
|
|
|
assert result.wait() == 10
|
2016-07-08 18:15:57 +03:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.django_db(transaction=True)
|
|
|
|
def test_load_initial_data():
|
|
|
|
"Test load_initial_data executes properly"
|
|
|
|
|
|
|
|
call_command('load_initial_data')
|