Skip to content

Commit

Permalink
Fix tests and use Tox/Py.test
Browse files Browse the repository at this point in the history
I also fixed a few issues that appeared in the way.
  • Loading branch information
vitorbaptista committed Jul 14, 2017
1 parent b99aa54 commit 32fd70a
Show file tree
Hide file tree
Showing 11 changed files with 293 additions and 142 deletions.
113 changes: 108 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,109 @@
*.git
*pyc
.idea
*iml
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/

# Editors temporary files
.#*
*~
*.swp
*.swo

# Misc
celerydb.sqlite
gunicorn.log
gunicorn.log
23 changes: 4 additions & 19 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

language: python
python:
- "2.7"
Expand All @@ -16,28 +15,14 @@ env:
global:
- OS_API_ENGINE=postgresql://postgres@/os
- OS_ELASTICSEARCH_ADDRESS=localhost:9200
- TOXENV="py${PYTHON_VERSION//./}"

install:
- python -m pip install --upgrade pip
- python -m pip install pytest flask-testing coverage coveralls
- python -m pip install git+git://github.com/frictionlessdata/jsontableschema-py.git
- python -m pip install git+git://github.com/frictionlessdata/jsontableschema-sql-py.git
- python -m pip install git+git://github.com/okfn/tabulator-py.git
- python -m pip install git+git://github.com/okfn/datapackage-py.git
- python -m pip install git+git://github.com/openspending/babbage.git
- python -m pip install git+git://github.com/openspending/babbage.fiscal-data-package.git
- python -m pip install -e .
- python -m pip install -U git+git://github.com/openspending/babbage.fiscal-data-package.git
- python -m pip install -U git+git://github.com/openspending/babbage.git
- python -m pip install -U git+git://github.com/okfn/datapackage-py.git
- python -m pip install -U git+git://github.com/okfn/tabulator-py.git
- python -m pip install -U git+git://github.com/frictionlessdata/jsontableschema-py.git
- python -m pip install -U git+git://github.com/frictionlessdata/jsontableschema-sql-py.git
- pip install tox coveralls
before_script:
- sleep 10
- psql -c 'create database os;' -U postgres
- python -m pytest --version
script:
- python -m coverage run --source os_api -m pytest
- tox
after_success:
- coveralls
notifications:
Expand Down
25 changes: 17 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
FROM gliderlabs/alpine:3.4

RUN apk add --update python3 git libpq wget ca-certificates python3-dev postgresql-dev build-base \
libxml2-dev libxslt-dev libstdc++
RUN apk --no-cache add \
python3 \
git \
libpq \
wget \
ca-certificates \
python3-dev \
postgresql-dev \
build-base \
libxml2-dev \
libxslt-dev \
libstdc++
RUN update-ca-certificates
RUN wget "https://bootstrap.pypa.io/get-pip.py" -O /dev/stdout | python3
RUN python3 --version
RUN pip3 --version
RUN pip3 install --upgrade pip
RUN git clone http://github.com/openspending/os-api.git app
RUN cd app && pip install -r requirements.txt
RUN pip install -U git+git://github.com/openspending/babbage.fiscal-data-package.git
RUN pip install -U git+git://github.com/openspending/babbage.git@feature/optimize-member-queries#egg=babbage==0.2.0
RUN rm -rf /var/cache/apk/*

WORKDIR /app
ADD . .
RUN pip install -r requirements.txt

ENV OS_API_CACHE=redis
ENV OS_STATSD_HOST=10.7.255.254
Expand All @@ -22,4 +31,4 @@ ADD docker/startup.sh /startup.sh

EXPOSE 8000

CMD /startup.sh
CMD /startup.sh
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,12 @@ An API to explore the OpenSpending database.
## Quick start

Clone the repo, install dependencies from pypi, and run the server. See the [docs](http://docs.openspending.org/en/latest/developers/api/) for more information.

## Testing

Make sure you have a local ElasticSearch instance running on `localhost:9200`,
and run:

```
$ tox
```
5 changes: 4 additions & 1 deletion os_api/cache.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from urllib.parse import urlparse
try:
from urllib.parse import urlparse
except ImportError:
from urlparse import urlparse

from flask import current_app, request, url_for

Expand Down
4 changes: 2 additions & 2 deletions os_api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

from sqlalchemy import create_engine

_connection_string = os.environ.get('OS_API_ENGINE')#,u'postgresql://osuser:1234@localhost/os')
_connection_string = os.environ.get('OS_API_ENGINE', 'sqlite://')
_engine = None


def get_engine():
"""Return engine singleton"""
global _engine
if _engine is None:
_engine = create_engine(_connection_string)
return _engine

27 changes: 13 additions & 14 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
setup(
name='os_api',
version=__version__,
description="API for OpenSpending Next",
long_description="",
description='API for OpenSpending Next',
long_description='',
classifiers=[
"Development Status :: 3 - Alpha",
"Intended Audience :: Developers",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
'Programming Language :: Python :: 2.6',
'Development Status :: 3 - Alpha',
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
'Operating System :: OS Independent',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4'
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
],
keywords='api fiscal datapackage babbage openspending next',
author='OpenSpending',
Expand All @@ -24,15 +24,14 @@
license='MIT',
packages=find_packages(exclude=['ez_setup', 'examples', 'test']),
namespace_packages=[],
package_data={},
include_package_data=True,
package_data={
'': ['*.json'],
},
zip_safe=False,
test_suite='nose.collector',
install_requires=[
# TODO: Add other dependencies once they go into pypi
# We're using requirements.txt
],
tests_require=[
'pytest',
'flask-testing',
'tox',
]
)
Empty file added tests/__init__.py
Empty file.
33 changes: 33 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import os
from elasticsearch import Elasticsearch
import pytest

import babbage_fiscal
from os_api.app import create_app


@pytest.fixture
def app():
app = create_app()
app.config['DEBUG'] = True
app.config['TESTING'] = True
app.config['PRESERVE_CONTEXT_ON_EXCEPTION'] = True
return app


@pytest.fixture(scope='module')
def elasticsearch():
host = os.environ.get('OS_ELASTICSEARCH_ADDRESS', 'localhost:9200')
es = Elasticsearch(hosts=[host])

def _delete_indices():
indices = [
'packages',
]

for index in indices:
es.indices.delete(index=index, ignore=[400, 404])

_delete_indices()
yield es
_delete_indices()
Loading

0 comments on commit 32fd70a

Please sign in to comment.