Skip to content

How to test DB connection failures mid execution #1338

Closed Answered by andre-qumulo
andre-qumulo asked this question in Q&A
Discussion options

You must be logged in to vote

I ended up changing my testing strategy. Our code creates a postgresql server in python so we passed a reference up to the test and stopped the server.


class PostgresClient:
    port: int
    server: PostgresServer

    def __init__(self, port: int, server: PostgresServer) -> None:
        self.port = port
        self.server = server

    def get_url(self) -> str:
        return f'postgresql://localhost:{self.port}/{DEFAULT_DB}'

    def connect(self, **kwargs: Any) -> psycopg2.extensions.connection:
        return psycopg2.connect(host='localhost', port=self.port, **kwargs)

    def create_db(self, dbname: str) -> None:
        conn = self.connect(dbname=DEFAULT_DB)
        conn.autoco…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by andre-qumulo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant