From 97ccbd5883dfcf398e2799975ee0c534c4c97a30 Mon Sep 17 00:00:00 2001 From: Lujeni Date: Mon, 30 Dec 2024 15:21:54 +0100 Subject: [PATCH] test: initial support --- .github/workflows/tests.yml | 29 +++++++++++++++++++++++++ pyproject.toml | 12 +++++++++++ requirements-dev.txt | 5 +++++ tests/__init__.py | 0 tests/conftest.py | 0 tests/test_typesense_exporter.py | 37 ++++++++++++++++++++++++++++++++ 6 files changed, 83 insertions(+) create mode 100644 .github/workflows/tests.yml create mode 100644 requirements-dev.txt create mode 100644 tests/__init__.py create mode 100644 tests/conftest.py create mode 100644 tests/test_typesense_exporter.py diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..0196a5f --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,29 @@ +name: Python package + +on: [push] + +jobs: + build: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"] + + steps: + - uses: actions/checkout@v4 + + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v5 + with: + python-version: ${{ matrix.python-version }} + + - name: Display Python version + run: python -c "import sys; print(sys.version)" + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt -r requirements-dev.txt + + - name: Tests + run: pytest diff --git a/pyproject.toml b/pyproject.toml index 09bbbeb..9a1d8b4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -25,6 +25,18 @@ pytest = "^7.0" mypy = "^1.14" types-requests = "^2.32" +[tool.pytest.ini_options] +addopts = "-v --cov=typesense_exporter --cov-report=term-missing --cov-report=xml --cov-report=html" +testpaths = ["tests"] +asyncio_mode = "auto" + +[tool.coverage.run] +branch = true +source = ["typesense_exporter"] + +[tool.coverage.html] +directory = "coverage_html" + [build-system] requires = ["setuptools", "wheel"] build-backend = "setuptools.build_meta" diff --git a/requirements-dev.txt b/requirements-dev.txt new file mode 100644 index 0000000..a6b8462 --- /dev/null +++ b/requirements-dev.txt @@ -0,0 +1,5 @@ +mypy>=1.14 +pytest-asyncio>=0.23.5 +pytest-cov>=4.1 +pytest>=7.0 +types-requests>=2.32 diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_typesense_exporter.py b/tests/test_typesense_exporter.py new file mode 100644 index 0000000..afd390c --- /dev/null +++ b/tests/test_typesense_exporter.py @@ -0,0 +1,37 @@ +import pytest +from typesense_exporter import parse_nodes_from_str + + +@pytest.mark.parametrize("test_input,expected,protocol,description", [ + ( + "localhost:8108", + [{"host": "localhost", "port": "8108", "protocol": "https"}], + "https", + "single node with port" + ), + ( + "host1:8108,host2:8108", + [ + {"host": "host1", "port": "8108", "protocol": "https"}, + {"host": "host2", "port": "8108", "protocol": "https"} + ], + "https", + "multiple nodes" + ), + ( + "localhost", + [{"host": "localhost", "port": "8108", "protocol": "https"}], + "https", + "default port" + ), + ( + "localhost:8108", + [{"host": "localhost", "port": "8108", "protocol": "http"}], + "http", + "custom protocol" + ), +]) +def test_parse_nodes_from_str(test_input, expected, protocol, description): + nodes = parse_nodes_from_str(test_input, default_protocol=protocol) + assert len(nodes) == len(expected) + assert nodes == expected