Skip to content

Commit

Permalink
Consolidate tests in tox.ini and other testing enhancements.
Browse files Browse the repository at this point in the history
 * pyrseas/testutils.py (remove_temp_files): New function to be used
   by test cases.  (DatabaseToMapTestCase.remove_tempfiles): New
   method.  (DbMigrateTestCase.to_map): Use TEST_DIR instead of
   tempfile.gettempdir().  (DbMigrateTestCase.remove_tempfiles):
   Refactor code out to remove_temp_files.
 * tests/dbobject/test_extern_file.py
   (ExternalFilenameMapTestCase.setUp): Remove temporary files before
   running tests.
 * tests/functional/pagila-schema.sql: Remove COMMENT on public schema
   (caused problems with other tests).
 * tox.ini: Run all tests (dbobject, functional and relation).
  • Loading branch information
jmafc committed Mar 27, 2013
1 parent fd88686 commit f6ccbc0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 24 deletions.
38 changes: 23 additions & 15 deletions pyrseas/testutils.py
Expand Up @@ -20,10 +20,27 @@ def fix_indent(stmt):
replace('( ', '(')


def remove_temp_files(tmpdir, prefix=''):
"Remove files in a temporary directory"
for tfile in glob.glob(os.path.join(tmpdir, prefix + '*')):
if os.path.isdir(tfile):
for entry in os.listdir(tfile):
entry = os.path.join(tmpdir, tfile, entry)
if os.path.isdir(entry):
for file in os.listdir(entry):
os.remove(os.path.join(entry, file))
os.rmdir(entry)
else:
os.remove(entry)
os.rmdir(tfile)
else:
os.remove(tfile)


TEST_DBNAME = os.environ.get("PYRSEAS_TEST_DB", 'pyrseas_testdb')
TEST_USER = os.environ.get("PYRSEAS_TEST_USER", getpass.getuser())
TEST_HOST = os.environ.get("PYRSEAS_TEST_HOST", None)
TEST_PORT = os.environ.get("PYRSEAS_TEST_PORT", None)
TEST_PORT = int(os.environ.get("PYRSEAS_TEST_PORT", 5432))
PG_OWNER = 'postgres'
TEST_DIR = os.path.join(tempfile.gettempdir(),
os.environ.get("PYRSEAS_TEST_DIR", 'pyrseas_test'))
Expand Down Expand Up @@ -315,6 +332,9 @@ def yaml_load(self, filename, subdir=None):
inmap = f.read()
return yaml.safe_load(inmap)

def remove_tempfiles(self):
remove_temp_files(TEST_DIR)


class InputMapToSqlTestCase(PyrseasTestCase):
"""Base class for "input map to SQL" test cases"""
Expand Down Expand Up @@ -383,23 +403,11 @@ def setUpClass(cls):
progdir = os.path.abspath(os.path.dirname(__file__))
cls.dbtoyaml = os.path.join(progdir, 'dbtoyaml.py')
cls.yamltodb = os.path.join(progdir, 'yamltodb.py')
cls.tmpdir = tempfile.gettempdir()
cls.tmpdir = TEST_DIR

@classmethod
def remove_tempfiles(cls, prefix):
for tfile in glob.glob(os.path.join(cls.tmpdir, prefix + '*')):
if os.path.isdir(tfile):
for entry in os.listdir(tfile):
entry = os.path.join(cls.tmpdir, tfile, entry)
if os.path.isdir(entry):
for file in os.listdir(entry):
os.remove(os.path.join(entry, file))
os.rmdir(entry)
else:
os.remove(entry)
os.rmdir(tfile)
else:
os.remove(tfile)
remove_temp_files(cls.tmpdir, prefix)

def execute_script(self, path, scriptname):
scriptfile = os.path.join(os.path.abspath(os.path.dirname(path)),
Expand Down
6 changes: 5 additions & 1 deletion tests/dbobject/test_extern_file.py
@@ -1,9 +1,9 @@
# -*- coding: utf-8 -*-
"""Test external files used in --directory option"""
import sys
from pyrseas.testutils import DatabaseToMapTestCase

from pyrseas.testutils import PyrseasTestCase
from pyrseas.testutils import DatabaseToMapTestCase
from pyrseas.dbobject.schema import Schema
from pyrseas.dbobject.function import Function
from pyrseas.dbobject.table import Sequence, Table, View
Expand All @@ -23,6 +23,10 @@

class ExternalFilenameMapTestCase(DatabaseToMapTestCase):

def setUp(self):
super(ExternalFilenameMapTestCase, self).setUp()
self.remove_tempfiles()

def test_map_casts(self):
"Map casts"
self.to_map(["CREATE FUNCTION int2_bool(smallint) RETURNS boolean "
Expand Down
7 changes: 0 additions & 7 deletions tests/functional/pagila-schema.sql
Expand Up @@ -8,13 +8,6 @@ SET check_function_bodies = false;
SET client_min_messages = warning;
SET escape_string_warning = off;

--
-- Name: SCHEMA public; Type: COMMENT; Schema: -; Owner: postgres
--

COMMENT ON SCHEMA public IS 'Standard public schema';


--
-- Name: plpgsql; Type: PROCEDURAL LANGUAGE; Schema: -; Owner: postgres
--
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Expand Up @@ -59,4 +59,4 @@ deps=pytest
setenv =
PYTHONPATH = {toxinidir}
commands =
py.test tests/dbobject
py.test tests

0 comments on commit f6ccbc0

Please sign in to comment.