From 0cd21c6da85a9d88e981f85ba6daabd6002d5509 Mon Sep 17 00:00:00 2001 From: Andrii Yurchuk Date: Tue, 1 Oct 2024 17:07:51 +0200 Subject: [PATCH] Add support for spatialite database backend (#439) --- .github/workflows/ci.yml | 6 ++-- .../db/backends/spatialite/__init__.py | 0 .../db/backends/spatialite/base.py | 14 ++++++++ .../tests/end2end/testapp/settings.py | 4 +++ .../tests/end2end/testapp/test_db.py | 32 +++++++++++++++++++ pyproject.toml | 3 +- 6 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 django_prometheus/db/backends/spatialite/__init__.py create mode 100644 django_prometheus/db/backends/spatialite/base.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a091a150..dbc31ba3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -15,8 +15,8 @@ jobs: strategy: fail-fast: false matrix: - python-version: ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12"] - os: [ubuntu-22.04] + python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"] + os: [ubuntu-24.04] runs-on: ${{ matrix.os }} name: "${{ matrix.os }} Python: ${{ matrix.python-version }}" services: @@ -44,7 +44,7 @@ jobs: - name: Install OS Packages run: | sudo apt-get update - sudo apt-get install binutils libproj-dev gdal-bin libmemcached-dev + sudo apt-get install binutils libproj-dev gdal-bin libmemcached-dev libsqlite3-mod-spatialite - uses: actions/checkout@v3 with: fetch-depth: 0 diff --git a/django_prometheus/db/backends/spatialite/__init__.py b/django_prometheus/db/backends/spatialite/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/django_prometheus/db/backends/spatialite/base.py b/django_prometheus/db/backends/spatialite/base.py new file mode 100644 index 00000000..c995781f --- /dev/null +++ b/django_prometheus/db/backends/spatialite/base.py @@ -0,0 +1,14 @@ +from django.contrib.gis.db.backends.spatialite import base, features +from django.db.backends.sqlite3 import base as sqlite_base + +from django_prometheus.db.common import DatabaseWrapperMixin + + +class DatabaseFeatures(features.DatabaseFeatures): + """Our database has the exact same features as the base one.""" + + pass + + +class DatabaseWrapper(DatabaseWrapperMixin, base.DatabaseWrapper): + CURSOR_CLASS = sqlite_base.SQLiteCursorWrapper diff --git a/django_prometheus/tests/end2end/testapp/settings.py b/django_prometheus/tests/end2end/testapp/settings.py index cb4c4dde..abad986f 100644 --- a/django_prometheus/tests/end2end/testapp/settings.py +++ b/django_prometheus/tests/end2end/testapp/settings.py @@ -81,6 +81,10 @@ "HOST": "127.0.0.1", "PORT": "3306", }, + "spatialite": { + "ENGINE": "django_prometheus.db.backends.spatialite", + "NAME": "db_spatialite.sqlite3", + }, # The following databases are used by test_db.py only "test_db_1": { "ENGINE": "django_prometheus.db.backends.sqlite3", diff --git a/django_prometheus/tests/end2end/testapp/test_db.py b/django_prometheus/tests/end2end/testapp/test_db.py index a0e91421..4faa708d 100644 --- a/django_prometheus/tests/end2end/testapp/test_db.py +++ b/django_prometheus/tests/end2end/testapp/test_db.py @@ -178,3 +178,35 @@ def test_counters(self): alias="postgis", vendor="postgresql", ) + + +@pytest.mark.skipif("spatialite" not in connections, reason="Skipped unless spatialite database is enabled") +class TestSpatialiteDbMetrics(BaseDBTest): + """Test django_prometheus.db metrics for spatialite backend. + + Note regarding the values of metrics: many tests interact with the + database, and the test runner itself does. As such, tests that + require that a metric has a specific value are at best very + fragile. Consider asserting that the value exceeds a certain + threshold, or check by how much it increased during the test. + """ + + def test_counters(self): + r = save_registry() + connection = connections["spatialite"] + + # Make sure the extension is loaded and geospatial tables are created + connection.prepare_database() + + cursor = connection.cursor() + + for _ in range(20): + cursor.execute("SELECT 1") + + assert_metric_compare( + r, + lambda a, b: a + 20 <= b < a + 25, + "django_db_execute_total", + alias="spatialite", + vendor="sqlite", + ) diff --git a/pyproject.toml b/pyproject.toml index 4fa7aab0..4b6a47c4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -23,14 +23,13 @@ legacy_tox_ini = """ [tox] min_version = 4.4 envlist = - {py37,py38,py39,py310,py311}-django{320}-{end2end,unittests} + {py38,py39,py310,py311}-django{320}-{end2end,unittests} {py38,py39,py310,py311,py312}-django{400,410,420}-{end2end,unittests} {py310,py311,py312}-django{500,510}-{end2end,unittests} py39-lint [gh-actions] python = - 3.7: py37 3.8: py38 3.9: py39, py39-lint 3.10: py310