Skip to content

Commit 3b54052

Browse files
authored
Merge pull request #90 from aaxelb/10239-djelme-tests
[wip][ENG-10239] djelme tests
2 parents 23cb26c + f3e9dc0 commit 3b54052

56 files changed

Lines changed: 1599 additions & 585 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.containerignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.git
2+
.tox
3+
.venv
4+
garbage
5+
**/*.pyc
6+
testbox.Containerfile
7+
docker-compose.yml

.github/workflows/test_djelme.yml

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,43 +12,42 @@ jobs:
1212
lint:
1313
runs-on: ubuntu-latest
1414
steps:
15-
- uses: actions/checkout@v3
16-
- uses: actions/setup-python@v4
15+
- uses: actions/checkout@v4
16+
- uses: actions/setup-python@v5
1717
id: setup-py
1818
with:
19-
python-version: '3.7'
19+
python-version: '3.11'
2020
- run: pip install -U tox
2121
- name: alias pythonX.Y for tox
2222
run: alias python${{ steps.setup-py.outputs.python-version }}=${{ steps.setup-py.outputs.python-path }}
2323
- run: TOXENV=lint tox
2424

2525

2626
test:
27+
runs-on: ubuntu-latest
2728
strategy:
2829
matrix:
29-
python: ['3.6', '3.7', '3.8', '3.9', '3.10']
30-
django: ['1.11', '2.0', '2.1', '2.2', '3.0', '3.1', '3.2', '4.0', '4.1']
30+
python: ['3.10', '3.11', '3.12', '3.13', '3.14']
31+
django: ['4.2', '5.1', '5.2']
3132
# TODO: elasticsearch: ['6', '7', '8', '9']
32-
exclude:
33-
- {python: '3.6', django: '4.0'}
34-
- {python: '3.6', django: '4.1'}
35-
- {python: '3.7', django: '4.0'}
36-
- {python: '3.7', django: '4.1'}
37-
- {python: '3.10', django: '1.11'}
38-
- {python: '3.10', django: '2.0'}
39-
runs-on: ubuntu-latest
4033
services:
41-
elasticsearch:
34+
elasticsearch6:
4235
image: elasticsearch:6.8.23
4336
ports:
44-
- 9201:9200
37+
- 9206:9200
4538
steps:
46-
- uses: actions/checkout@v3
47-
- uses: actions/setup-python@v4
39+
- uses: actions/checkout@v4
40+
- uses: actions/setup-python@v5
4841
id: setup-py
4942
with:
5043
python-version: ${{ matrix.python }}
51-
- run: pip install -U tox
52-
- run: alias python${{ steps.setup-py.outputs.python-version }}=${{ steps.setup-py.outputs.python-path }}
53-
- run: TOXENV=`echo 'py${{ matrix.python }}-django${{matrix.django}}' | sed 's/\.//g'` tox
5444

45+
- name: alias python
46+
run: alias python${{ steps.setup-py.outputs.python-version }}=${{ steps.setup-py.outputs.python-path }}
47+
48+
- name: install despondencies
49+
run: pip install -U poetry && poetry install --with=dev --extras=elastic6
50+
51+
- run: TOXENV=`echo 'py${{ matrix.python }}-django${{matrix.django}}' | sed 's/\.//g'` poetry run tox
52+
env:
53+
ELASTICSEARCH6_HOST: localhost:9206

.konchrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ konch.config(
3939

4040
def setup():
4141
# Set up django and add the default elasticsearch-py client and Metric to the context
42-
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tests.settings")
42+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "elasticsearch_metrics.tests.settings")
4343
django.setup()
4444
from elasticsearch_metrics.metrics import Metric
4545

.pre-commit-config.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
repos:
22
- repo: https://github.com/psf/black
3-
rev: 22.8.0
3+
rev: 26.1.0
44
hooks:
55
- id: black
6-
language_version: python3.7
6+
language_version: python3.11
77
- repo: https://github.com/PyCQA/flake8
8-
rev: 5.0.4
8+
rev: 7.3.0
99
hooks:
1010
- id: flake8
11-
additional_dependencies: ["flake8-bugbear==22.8.23"]
11+
additional_dependencies: ["flake8-bugbear==25.11.29"]
1212
- repo: https://github.com/asottile/blacken-docs
1313
rev: v1.12.1
1414
hooks:
1515
- id: blacken-docs
16-
additional_dependencies: [black==22.8.0]
16+
additional_dependencies: [black==26.1.0]

CONTRIBUTING.md

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,18 @@
11
# Contributing
2+
(TODO: update)
23

34
## Setting up for development
45

56
* Create and activate a new virtual environment
67
* `pip install -e '.[dev]'`
78
* Install django: `pip install django`
89
* (Optional but recommended) Run elasticsearch: `docker-compose up`
9-
* (Optional but recommended) If you're using a Python 3.6 virtual
10-
environment, install the pre-commit hooks, which will
11-
format and lint your git staged files:
12-
13-
14-
```
15-
# The pre-commit CLI was installed above
16-
pre-commit install
17-
```
1810

1911
* Run tests using the following commands:
2012

2113
```
2214
# Run all tests (requires elasticsearch to be running)
23-
pytest
24-
25-
# Skip tests that require elasticsearch to be running
26-
pytest -m "not es"
15+
python3 -m elasticsearch_metrics.tests
2716
2817
# Check syntax
2918
flake8

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ Django app for storing time-series metrics in Elasticsearch.
77

88
## Pre-requisites
99

10-
* Python 2.7 or >=3.6
11-
* Django 1.11 or 2.0
10+
* Python >=3.10
11+
* Django 4.2, 5.1, or 5.2
1212
* Elasticsearch 6
1313

1414
## Install
@@ -43,10 +43,10 @@ A `Metric` is a subclass of [`elasticsearch_dsl.Document`](https://elasticsearch
4343
```python
4444
# myapp/metrics.py
4545

46-
from elasticsearch_metrics import metrics
46+
from elasticsearch_metrics.imps import elastic8
4747

4848

49-
class PageView(metrics.Metric):
49+
class PageView(elastic8.EventLog):
5050
user_id = metrics.Integer(index=True, doc_values=True)
5151
```
5252

docker-compose.yml

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,30 @@
11
version: '3'
22
services:
3-
elasticsearch:
3+
elasticsearch6:
44
image: docker.elastic.co/elasticsearch/elasticsearch:6.3.1
5+
healthcheck:
6+
interval: 30s
7+
retries: 10
8+
test: curl -s http://localhost:9200/_cluster/health | grep -vq '"status":"red"'
9+
networks:
10+
- djelme
511
testbox:
612
build:
713
context: .
8-
dockerfile: testbox.Dockerfile
14+
dockerfile: testbox.Containerfile
915
depends_on:
10-
- elasticsearch
16+
elasticsearch6:
17+
condition: service_healthy
1118
environment:
12-
TOXENV: py39-django40
13-
ELASTICSEARCH_HOST: elasticsearch:9200
14-
volumes:
15-
- ./:/code:cached
19+
# TOXENV: py310-django42
20+
ELASTICSEARCH6_HOST: elasticsearch6:9200
21+
networks:
22+
- djelme
23+
# volumes:
24+
# - ./:/code:cached
25+
26+
27+
networks:
28+
djelme:
29+
driver: bridge
30+

elasticsearch_metrics/apps.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
from django.apps import AppConfig
2-
from django.conf import settings
3-
from elasticsearch_dsl.connections import connections
42
from django.utils.module_loading import autodiscover_modules
53

4+
from elasticsearch_metrics.djelmetrics_imps import each_djelmetrics_imp
5+
66

77
class ElasticsearchMetricsConfig(AppConfig):
88
name = "elasticsearch_metrics"
99

1010
def ready(self):
11-
connections.configure(**settings.ELASTICSEARCH_DSL)
11+
for _imp in each_djelmetrics_imp():
12+
_imp.configure()
1213
autodiscover_modules("metrics")
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from __future__ import annotations
2+
from collections.abc import Iterator
3+
import importlib
4+
5+
from django.conf import settings
6+
7+
from elasticsearch_metrics.protocols import (
8+
ProtoDjelmetricsImp,
9+
ProtoDjelmetricsImpModule,
10+
)
11+
12+
__all__ = (
13+
"each_djelmetrics_imp",
14+
"get_djelmetrics_imp",
15+
"ProtoDjelmetricsImp",
16+
)
17+
18+
19+
def each_djelmetrics_imp() -> Iterator[ProtoDjelmetricsImp]:
20+
for _imp_name in getattr(settings, "DJELMETRICS_IMPS", ()):
21+
yield get_djelmetrics_imp(_imp_name)
22+
23+
24+
def get_djelmetrics_imp(imp_name: str) -> ProtoDjelmetricsImp:
25+
try:
26+
_raw_imp_config = settings.DJELMETRICS_IMPS[imp_name]
27+
except KeyError as _error:
28+
raise ValueError(
29+
f"{imp_name!r} not found in `settings.DJELMETRICS_IMPS`"
30+
) from _error
31+
try:
32+
_imp_path = _raw_imp_config["DJELMETRICS_IMP"]
33+
except KeyError as _error:
34+
raise ValueError(
35+
f"missing 'DJELMETRICS_IMP' from {imp_name!r} in `settings.DJELMETRICS_IMPS`"
36+
" -- must be a path to a djelme imp module like those under"
37+
"`elasticsearch_metrics.imps...`"
38+
) from _error
39+
try:
40+
_imp_module = importlib.import_module(_imp_path)
41+
except ImportError as _error:
42+
raise ValueError(
43+
f"could not import {_imp_path!r} for `settings.DJELMETRICS_IMPS[{imp_name!r}]`"
44+
) from _error
45+
assert isinstance(_imp_module, ProtoDjelmetricsImpModule)
46+
_imp_config = {
47+
_k: _v for _k, _v in _raw_imp_config.items() if _k != "DJELMETRICS_IMP"
48+
}
49+
return _imp_module.djelme_imp_from_config(imp_name, _imp_config)

elasticsearch_metrics/exceptions.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ class ElasticsearchMetricsError(Exception):
55
class IndexTemplateNotFoundError(ElasticsearchMetricsError):
66
def __init__(self, message, client_error):
77
self.client_error = client_error
8-
super(IndexTemplateNotFoundError, self).__init__(message)
8+
super(IndexTemplateNotFoundError, self).__init__(message, client_error)
99

1010

1111
class IndexTemplateOutOfSyncError(ElasticsearchMetricsError):
1212
def __init__(self, message, mappings_in_sync, patterns_in_sync, settings_in_sync):
1313
self.mappings_in_sync = mappings_in_sync
1414
self.patterns_in_sync = patterns_in_sync
1515
self.settings_in_sync = settings_in_sync
16-
super(IndexTemplateOutOfSyncError, self).__init__(message)
16+
super(IndexTemplateOutOfSyncError, self).__init__(
17+
message, mappings_in_sync, patterns_in_sync, settings_in_sync
18+
)

0 commit comments

Comments
 (0)