Skip to content

Commit

Permalink
Initial tests, coverage and code-quality configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
dmonroy committed Oct 15, 2015
1 parent ce31040 commit 413bbc0
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 13 deletions.
6 changes: 6 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[run]
omit =
tests/*
[report]
fail_under = 100
show_missing = True
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
sudo: false
language: python
python:
- "2.6"
- "2.7"
- "3.3"
- "3.4"
- "3.5"
install: pip install tox-travis
script: tox
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include .coveragerc
29 changes: 16 additions & 13 deletions schema_migrations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
import os

from psycopg2._psycopg import ProgrammingError
from urllib.parse import urlsplit
try:
from urllib.parse import urlsplit
except ImportError: # pragma: no cover
from urlparse import urlsplit


DEFAULT_GROUP = {'default': './migrations/'}
STATUS_PENDING = -1
Expand All @@ -11,6 +15,15 @@

SELECT_SQL = 'select group_name, name from migration_history'
INSERT_SQL = 'insert into migration_history values (%s, %s)'
CREATE_TABLE_DDL = '''
BEGIN;
CREATE TABLE migration_history (
group_name TEXT NOT NULL,
name TEXT NOT NULL,
PRIMARY KEY (group_name, name)
);
END;
'''


class MigrationController(object):
Expand Down Expand Up @@ -40,18 +53,8 @@ def list_completed_in_db(self, db):
completed[group].append(name)
except ProgrammingError:
# Must rollback previous transaction
cur.execute(
'''
ROLLBACK;
BEGIN;
CREATE TABLE migration_history (
group_name TEXT NOT NULL,
name TEXT NOT NULL,
PRIMARY KEY (group_name, name)
);
END;
'''
)
cur.execute('ROLLBACK;')
cur.execute(CREATE_TABLE_DDL)
cur.close()

return completed
Expand Down
5 changes: 5 additions & 0 deletions tests/migrations/0001-initial/forward.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

CREATE TABLE friend(
id SERIAL NOT NULL PRIMARY KEY,
name VARCHAR(100)
);
2 changes: 2 additions & 0 deletions tests/migrations/0002-add-gender/forward.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

alter table friend add column gender VARCHAR(1);
76 changes: 76 additions & 0 deletions tests/test_schema_migrations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import psycopg2
import pytest
import sys
from schema_migrations import MigrationController, STATUS_OK, STATUS_PENDING


@pytest.fixture(scope='session')
def databases(request):
db_base = 'schema_migrations_{0}{1}{2}'.format(*sys.version_info)
database_names = [
'{0}_{1}'.format(db_base, i) for i in range(3)
]
def create():
conn = psycopg2.connect(
user='postgres'
)
conn.set_isolation_level(0)
cur = conn.cursor()
for db in database_names:
cur.execute('CREATE DATABASE {name}'.format(name=db))
conn.close()

def drop():
conn = psycopg2.connect(
user='postgres'
)
conn.set_isolation_level(0)
cur = conn.cursor()
for db in database_names:
cur.execute('DROP DATABASE IF EXISTS {name}'.format(name=db))
conn.close()

drop()
create()

request.addfinalizer(drop)

return {
name:'postgresql://postgres@localhost:5432/{name}'.format(name=name)
for name in database_names
}


class TestSchemaMigrations:
def test_migrations(self, databases):
groups = dict(
test='./tests/migrations/'
)
mc = MigrationController(databases=databases, groups=groups)

migrations = mc.list()['test'] # test group
assert len(migrations) == 2
for m in migrations:
assert m['status']['all'] == STATUS_PENDING
dbs = m['status']['databases']
assert len(dbs.keys()) == 3

for db_status in dbs.values():
assert db_status == False

mc.migrate()
mc.close()

mc2 = MigrationController(databases=databases, groups=groups)

migrations = mc2.list()['test'] # test group
assert len(migrations) == 2
for m in migrations:
assert m['status']['all'] == STATUS_OK
dbs = m['status']['databases']
assert len(dbs.keys()) == 3

for db_status in dbs.values():
assert db_status == True

mc2.close()
26 changes: 26 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[tox]
envlist = py27, py33, py34, py35, flake8, isort
[testenv]
deps=
pytest
commands=py.test
[testenv:py35]
deps=
pytest
coverage
pytest-cov
commands=py.test --cov {envsitepackagesdir}/schema_migrations --cov-config .coveragerc
[testenv:flake8]
deps =
flake8
mccabe
commands = flake8 --max-complexity 10 schema_migrations
[testenv:isort]
basepython = python2.7
deps = isort
commands = isort -rc -c schema_migrations
[tox:travis]
2.7 = py27
3.3 = py33
3.4 = py34
3.5 = py35, flake8, isort

0 comments on commit 413bbc0

Please sign in to comment.