From 4c4bc03e46c2a0ddfb2141e2949977a4297aaab8 Mon Sep 17 00:00:00 2001 From: Dewey Dunnington Date: Fri, 31 Jan 2025 17:19:57 -0600 Subject: [PATCH] first go --- tests/conftest.py | 60 +++++++++++++++++++++++++++++++++++++ tests/test_basic_connect.py | 10 +++++++ 2 files changed, 70 insertions(+) create mode 100644 tests/conftest.py create mode 100644 tests/test_basic_connect.py diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..f25743f --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,60 @@ +import os + +import pytest +from typing import Union + +from wherobots.db import connect, Connection, InterfaceError +from wherobots.db.region import Region +from wherobots.db.runtime import Runtime + + +class TestConnection: + """Lazily resolved connection suitable for use as a fixture + + This is to avoid connecting if at all possible, and for skipping tests + such that `pytest tests/` can run without a live connection for + tests that don't require it (without the test writer having to make up a + skip message). + """ + def __init__(self, api_key, **kwargs): + self._api_key = api_key + self._kwargs = kwargs + self._connect_error: Union[str, None] = None + self._conn: Union[Connection, None] = None + + def conn_or_skip(self) -> Connection: + if self._conn is not None: + return self._conn + + if self._connect_error is not None: + pytest.skip(self._connect_error) + + if not self._api_key: + pytest.skip("WHEROBOTS_API_KEY environment variable was not wet") + + try: + self._conn = connect(api_key=self._api_key, **self._kwargs) + return self._conn + except InterfaceError as e: + self._connect_error = f"{type(e).__name__}: {str(e)}" + return self.conn_or_skip() + + +@pytest.fixture() +def wbc() -> TestConnection: + if "WHEROBOTS_API_KEY" in os.environ: + api_key = os.environ["WHEROBOTS_API_KEY"] or None + else: + api_key = None + + if "WHEROBOTS_HOST" in os.environ: + host = os.environ["WHEROBOTS_HOST"] or None + else: + host = None + + if api_key is None: + return None + + return TestConnection( + api_key=api_key, host=host, region=Region.AWS_US_WEST_2, runtime=Runtime.TINY + ) diff --git a/tests/test_basic_connect.py b/tests/test_basic_connect.py new file mode 100644 index 0000000..851eb99 --- /dev/null +++ b/tests/test_basic_connect.py @@ -0,0 +1,10 @@ +from wherobots.db import Connection + + +def test_basic_connect(wbc): + conn: Connection = wbc.conn_or_skip() + + with conn.cursor() as curr: + curr.execute("SHOW SCHEMAS IN wherobots_open_data") + all_schemas = curr.fetchall() + assert len(all_schemas) > 0