tk0miya / testing.postgresql

Apache License 2.0
290 stars 46 forks source link

pytest fixture has database refusing connection #30

Open dtracers opened 5 years ago

dtracers commented 5 years ago

I am using python 3.7 and postgresql

@pytest.fixture(autouse=True, scope="session")
def postgres_factory():
    """
    Creates an initial fake database for use in unit tests.
    """
    postgres_factory = postgresql.PostgresqlFactory(cache_initialized_db=True, on_initialized=create_initial_data)
    return postgres_factory

@pytest.fixture(autouse=True, scope="class")
def postgres_instance(postgres_factory):
    fake_db = postgres_factory()
    return fake_db

I have these as pytest fixtures and when it tries to recreate them the first query throws a connection refused error.

qemtek commented 4 years ago

Im getting the same problem.. Whenever I try to create a connection to the fake DB when imported as a fixture I get the error

petroslamb commented 11 months ago

This was perplexing for me too, as this works on and off on slight modifications, without good reason. Although I was using the Postgresql class not the factory, a fellow coworker pointed out to try to pass the port that the error refers to:

psycopg2.OperationalError: connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused

And what solved the problem for me (although it worked at times):

pg = testing.postgresql.Postgresql(
    port=5432,
    postgres_args="-h 127.0.0.1 -F -c logging_collector=off -c max_locks_per_transaction=200",
)

No idea why.