Commit 2bded11d authored by Bruce Flynn's avatar Bruce Flynn

fix issues with testing and DB config

parent 51955155
Pipeline #2694 passed with stages
in 56 seconds
......@@ -7,6 +7,7 @@ RUN apt-get update && apt-get -y install \
gcc \
git \
libx11-6 \
postgresql-client \
&& rm -rf /var/lib/apt/lists/*
# install Miniconda3
......@@ -27,12 +28,6 @@ WORKDIR /code
COPY requirements.txt /tmp/requirements.txt
RUN pip install -r /tmp/requirements.txt
COPY stations.yaml /code
COPY amrc_aws /code/amrc_aws
COPY setup.py /code/setup.py
# .git is required for setuptools_scm to determine version.
COPY .git /code/.git
COPY . .
RUN pip install -e ".[testing]"
COPY tests /code/tests
import os
from subprocess import call, check_call
from datetime import timedelta
from webtest import TestApp
from subprocess import check_call
import pytest
import sqlalchemy as sa
from pyramid import testing
from sqlalchemy.engine.url import make_url
from sqlalchemy.pool import NullPool
from webtest import TestApp
from amrc_aws import db
from amrc_aws import db, config
from amrc_aws.app import main
pytestmark = pytest.mark.skipif('TEST_DBURL' not in os.environ,
......@@ -32,6 +32,7 @@ def database():
check_call("createdb {}".format(opts), shell=True)
eng = sa.create_engine(dburl, poolclass=NullPool)
eng.execute('CREATE schema %s' % config.DB_SCHEMA)
db.init(eng)
db.create_schema()
yield eng
......@@ -43,21 +44,21 @@ def database():
@pytest.fixture()
def dbdata(database):
sqlpath = os.path.join(os.path.dirname(__file__), 'data.sql')
conn = database.raw_connection()
cur = conn.cursor()
try:
cur.execute(open(sqlpath).read())
conn.commit()
finally:
conn.close()
statements = [l for l in open(sqlpath)
if not l.startswith('-') and l.strip()]
with db.connection() as conn:
for stmt in statements:
conn.execute(stmt)
stamps = database.execute('SELECT min(stamp), max(stamp) FROM data').fetchone()
stations = database.execute('SELECT distinct(station) as station FROM data').fetchall()
with db.connection() as conn:
stamps = conn.execute('SELECT min(stamp), max(stamp) FROM data').fetchone()
stations = conn.execute('SELECT distinct(station) as station FROM data').fetchall()
return (stamps.min, stamps.max + timedelta(seconds=1)), [r.station for r in stations]
@pytest.fixture()
def wsgiapp():
def wsgiapp(database, monkeypatch):
monkeypatch.setattr(config, 'DB_URL', str(database.url))
testing.setUp()
app = main({})
yield TestApp(app)
......
......@@ -3,9 +3,10 @@ version: "2"
services:
app:
build:
context: .
context: ..
dockerfile: tests/Dockerfile
environment:
AWSAPI_DB_URL: postgresql://docker@db/__test
TEST_DBURL: postgresql://docker@db/__test
db:
environment:
......
......@@ -55,4 +55,3 @@ def test_data_csv(wsgiapp, dbdata):
filepath = os.path.join(tmpdir, 'test.csv')
with open(filepath, 'wb') as fptr:
fptr.write(response.body)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment