Skip to content

Commit

Permalink
Merge pull request #8 from PsychoinformaticsLab/enh/test
Browse files Browse the repository at this point in the history
Move to Python 3.8, add tests
  • Loading branch information
adelavega authored Sep 1, 2020
2 parents 8996e89 + cc496aa commit 9b828d5
Show file tree
Hide file tree
Showing 19 changed files with 128 additions and 44 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ matrix:
include:
- language: python
python:
- "3.6"
- "3.8"
install:
- pip install -U setuptools==45
- pip install -r neurostuff/requirements.txt
- pip install pytest-cov
script:
- python -m pytest neurostuff/ --cov=./
env:
- APP_SETTINGS=neurostuff.config.app.TravisConfig PYTHONHASHSEED=0
- APP_SETTINGS=neurostuff.config.TravisConfig PYTHONHASHSEED=0
addons:
postgresql: "9.4"
before_script:
Expand Down
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# neurostuff

[Add badges]
[![Build Status](https://travis-ci.com/PsychoinformaticsLab/neurostuff.svg?branch=master)](https://travis-ci.com/PsychoinformaticsLab/neurostuff)

Requirements: Docker and docker-compose.

Expand Down Expand Up @@ -47,3 +47,13 @@ If you need to upgrade the db after changing any models:

docker-compose exec neurostuff python manage.py db migrate
docker-compose exec neurostuff python manage.py db upgrade


## Running tests
To run tests, after starting services, create a test database:

docker-compose exec postgres psql -h postgres -U postgres -c "create database test_db"

and execute:

docker-compose run -e "APP_SETTINGS=neurostuff.config.DockerTestConfig" --rm -w /neurostuff neurostuff python -m pytest neurostuff/tests
2 changes: 1 addition & 1 deletion neurostuff/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3.6-stretch
FROM python:3.8-buster
ARG DEBIAN_FRONTEND=noninteractive

RUN mkdir -p /usr/src/app
Expand Down
File renamed without changes.
7 changes: 5 additions & 2 deletions neurostuff/example_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Config(object):
SQLALCHEMY_TRACK_MODIFICATIONS = False
WTF_CSRF_ENABLED = False

POSTGRES_PASSWORD = os.environ['POSTGRES_PASSWORD']
POSTGRES_PASSWORD = os.environ.get('POSTGRES_PASSWORD', '')
DB_NAME = 'neurostuff'
SQLALCHEMY_DATABASE_URI = "postgres://postgres:" \
f"{POSTGRES_PASSWORD}@postgres:5432/{DB_NAME}"
Expand All @@ -21,6 +21,9 @@ class Config(object):
GITHUB_CLIENT_SECRET = "github-secret"
DANCE_SECRET_KEY = "temporary"

SECURITY_PASSWORD_HASH = 'pbkdf2_sha512'
SECURITY_PASSWORD_SALT = 'A_SECRET'


class ProductionConfig(Config):
ENV = 'production'
Expand All @@ -37,7 +40,7 @@ class TestingConfig(Config):


class DockerTestConfig(TestingConfig):
SQLALCHEMY_DATABASE_URI = 'postgres://postgres@postgres:5432/scout_test'
SQLALCHEMY_DATABASE_URI = 'postgres://postgres@postgres:5432/test_db'


class TravisConfig(TestingConfig):
Expand Down
2 changes: 1 addition & 1 deletion neurostuff/ingest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def ingest_neurosynth(max_rows=None):
# tf.write(response.raw.read())
# tf.seek(0)

path = Path(__file__).parent / '..' / 'data' / 'data_0.7.July_2018.tar.gz'
path = Path(__file__).parent.parent / 'data' / 'data_0.7.July_2018.tar.gz'
with open(path, 'rb') as tf:
tar = tarfile.open(fileobj=tf)
f = tar.extractfile('database.txt')
Expand Down
1 change: 1 addition & 0 deletions neurostuff/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ psycopg2==2.8.3
PyLD==2.0.3
python-dateutil==2.8.1
pytz==2020.1
pytest==6.0.1
requests==2.24.0
requests-oauthlib==1.3.0
Rx==1.6.1
Expand Down
30 changes: 30 additions & 0 deletions neurostuff/tests/api/test_analyses.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from ..request_utils import decode_json


def test_get_analyses(auth_client, ingest_neurosynth):
# List of analyses
resp = auth_client.get('/api/analyses/')
assert resp.status_code == 200
analysis_list = decode_json(resp)
assert type(analysis_list) == list

assert len(analysis_list) == 1

# Check analysis keys
analysis = analysis_list[0]
keys = ['@context', '@id', '@type', 'condition', 'created_at', 'image',
'name', 'point', 'study', 'weight']
for k in keys:
assert k in analysis

assert analysis['@context'] == {'@vocab': 'http://neurostuff.org/nimads/'}
assert analysis['@type'] == 'Analysis'
a_id = analysis['@id'].split('/')[-1]

# Query specify analysis ID
resp = auth_client.get(f"/api/analyses/{a_id}")
assert resp.status_code == 200
assert decode_json(resp) == analysis

assert decode_json(resp)['@id'] == \
f'http://neurostuff.org/api/analyses/{a_id}'
4 changes: 4 additions & 0 deletions neurostuff/tests/api/test_conditions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

def test_get_conditions(auth_client, ingest_neurosynth):
# Currently no conditions in db
assert 1
9 changes: 0 additions & 9 deletions neurostuff/tests/api/test_dataset.py

This file was deleted.

5 changes: 5 additions & 0 deletions neurostuff/tests/api/test_datasets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@


def test_get_datasets(auth_client, ingest_neurosynth):
# Currently no datasets in db
assert 1
14 changes: 14 additions & 0 deletions neurostuff/tests/api/test_images.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from ..request_utils import decode_json


def test_get_images(auth_client, ingest_neurosynth):
# List of datasets
resp = auth_client.get('/api/images/')
assert resp.status_code == 200
images_list = decode_json(resp)

assert type(images_list) == list

assert len(images_list) == 0

# Add more tests for images with ingest_neurovault
19 changes: 19 additions & 0 deletions neurostuff/tests/api/test_points.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from ..request_utils import decode_json


def test_get_points(auth_client, ingest_neurosynth):
# Get an analysis
resp = auth_client.get('/api/analyses/')
analysis = decode_json(resp)[0]

point_id = analysis['point'][0].split('/')[-1]

# Get a point
resp = auth_client.get(f'/api/points/{point_id}')
point = decode_json(resp)

# Test a few fields
assert point['@id'] == f'http://neurostuff.org/api/points/{point_id}'
assert point['coordinates'] == [-34.0, -68.0, -15.0]
assert point['space'] == 'TAL'
assert point['@type'] == 'Point'
33 changes: 33 additions & 0 deletions neurostuff/tests/api/test_studies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from ..request_utils import decode_json


def test_get_studies(auth_client, ingest_neurosynth):
# List of studies
resp = auth_client.get('/api/studies/')
assert resp.status_code == 200
studies_list = decode_json(resp)

assert type(studies_list) == list

assert len(studies_list) == 1

# Check study keys
study = studies_list[0]

assert study['@context'] == {'@vocab': 'http://neurostuff.org/nimads/'}
assert study['@type'] == 'Study'
s_id = study['@id'].split('/')[-1]

# Query specify analysis ID
resp = auth_client.get(f"/api/studies/{s_id}")
assert resp.status_code == 200
full_study = decode_json(resp)

# Check extra keys
for k in ['analysis', 'created_at', 'doi', 'name']:
assert k in full_study

assert full_study['doi'] == '10.1016/S0896-6273(00)80456-0'

assert full_study['@id'] == \
f'http://neurostuff.org/api/studies/{s_id}'
14 changes: 0 additions & 14 deletions neurostuff/tests/api/test_user.py

This file was deleted.

5 changes: 2 additions & 3 deletions neurostuff/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def auth_client(add_users):
""" Return authorized client wrapper """
from .request_utils import Client

_, ((email, password), _) = add_users
_, email, password = add_users
client = Client(email=email, password=password)
return client

Expand All @@ -96,7 +96,6 @@ def add_users(app, db, session):
pass1 = 'test1'

user_datastore.create_user(email=user1, password=encrypt_password(pass1),
user_name='testuser',
confirmed_at=datetime.datetime.now())
session.commit()
id_1 = user_datastore.find_user(email=user1).id
Expand All @@ -107,4 +106,4 @@ def add_users(app, db, session):
@pytest.fixture(scope="function")
def ingest_neurosynth(session):
""" Add a dataset with two subjects """
return ingest.ingest_neurosynth()
return ingest.ingest_neurosynth(800)
8 changes: 0 additions & 8 deletions neurostuff/tests/test_views.py

This file was deleted.

3 changes: 0 additions & 3 deletions nginx/nginx.dev.conf
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ http {
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

server {
server_name _;
root /neurostuff/neurostuff/frontend/build;

location /static/ {
}

Expand Down
Empty file modified postgres/migrations/migrations/alembic.ini
100644 → 100755
Empty file.

0 comments on commit 9b828d5

Please sign in to comment.