Skip to content

Commit

Permalink
Add hook to centralize schema loads
Browse files Browse the repository at this point in the history
  • Loading branch information
jcpunk committed Sep 10, 2020
1 parent 26c1a09 commit 0f45cb3
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 5 deletions.
12 changes: 12 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@ Sample test
postgresql.commit()
cur.close()
If you want the database fixture to be automatically populated with your schema:

.. code-block:: python
postgresql_my_with_schema = factories.postgresql('postgresql_my_proc', load=['schemafile.sql', 'otherschema.sql'])
.. note::

The database will still be dropped each time.



Connecting to already existing postgresql database
--------------------------------------------------

Expand Down
8 changes: 7 additions & 1 deletion src/pytest_postgresql/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,13 @@ def postgresql_noproc_fixture(request):
return postgresql_noproc_fixture


def postgresql(process_fixture_name, db_name=None):
def postgresql(process_fixture_name, db_name=None, load=None):
"""
Return connection fixture factory for PostgreSQL.
:param str process_fixture_name: name of the process fixture
:param str db_name: database name
:param list load: SQL to automatically load into our test database
:rtype: func
:returns: function which makes a connection to postgresql
"""
Expand Down Expand Up @@ -248,6 +249,11 @@ def postgresql_factory(request):
port=pg_port,
options=pg_options
)
if load:
for filename in load:
with open(filename, 'r') as _fd:
with connection.cursor() as cur:
cur.execute(_fd.read())
yield connection
connection.close()

Expand Down
4 changes: 4 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"""Tests main conftest file."""
import os
from pytest_postgresql import factories


PG_CTL = '/usr/lib/postgresql/{ver}/bin/pg_ctl'
TEST_SQL_DIR = os.path.dirname(os.path.abspath(__file__)) + '/test_sql/'

# pylint:disable=invalid-name
postgresql92 = factories.postgresql_proc(PG_CTL.format(ver='9.2'), port=None)
Expand All @@ -15,6 +17,8 @@

postgresql_proc2 = factories.postgresql_proc(port=9876)
postgresql2 = factories.postgresql('postgresql_proc2', db_name='test-db')
postgresql3 = factories.postgresql('postgresql_proc2', db_name='test-db', load=[TEST_SQL_DIR + 'test.sql',])
postgresql4 = factories.postgresql('postgresql_proc2', db_name='test-db', load=[TEST_SQL_DIR + 'test.sql', TEST_SQL_DIR + 'test2.sql'])

postgresql_rand_proc = factories.postgresql_proc(port=None)
postgresql_rand = factories.postgresql('postgresql_rand_proc')
Expand Down
26 changes: 22 additions & 4 deletions tests/test_postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import pytest


QUERY = "CREATE TABLE test (id serial PRIMARY KEY, num integer, data varchar);"
CREATE_QUERY = "CREATE TABLE test (id serial PRIMARY KEY, num integer, data varchar);"
SELECT_QUERY = "SELECT * FROM test;"


@pytest.mark.skipif(
Expand All @@ -27,23 +28,40 @@ def test_postgresql_proc(request, postgres):
def test_main_postgres(postgresql):
"""Check main postgresql fixture."""
cur = postgresql.cursor()
cur.execute(QUERY)
cur.execute(CREATE_QUERY)
postgresql.commit()
cur.close()


def test_two_postgreses(postgresql, postgresql2):
"""Check two postgresql fixtures on one test."""
cur = postgresql.cursor()
cur.execute(QUERY)
cur.execute(CREATE_QUERY)
postgresql.commit()
cur.close()

cur = postgresql2.cursor()
cur.execute(QUERY)
cur.execute(CREATE_QUERY)
postgresql2.commit()
cur.close()

def test_postgres_load_one_file(postgresql3):
"""Check postgresql fixture can load one file."""
cur = postgresql3.cursor()
cur.execute(SELECT_QUERY)
results = cur.fetchall()
assert len(results) is 1
cur.close()

def test_postgres_load_two_files(postgresql4):
"""Check postgresql fixture can load two files."""
cur = postgresql4.cursor()
cur.execute(SELECT_QUERY)
results = cur.fetchall()
assert len(results) is 2
cur.close()



def test_rand_postgres_port(postgresql_rand):
"""Check if postgres fixture can be started on random port."""
Expand Down
2 changes: 2 additions & 0 deletions tests/test_sql/test.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CREATE TABLE test (id serial PRIMARY KEY, num integer, data varchar);
INSERT INTO test VALUES(1, 2, 'c');
1 change: 1 addition & 0 deletions tests/test_sql/test2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INSERT INTO test VALUES(2, 1, 'z');

0 comments on commit 0f45cb3

Please sign in to comment.