testcontainers-python-influxdb


Nametestcontainers-python-influxdb JSON
Version 0.2.2 PyPI version JSON
download
home_page
SummaryBrings InfluxDB in testcontainers-python (until PR #413 is merged)
upload_time2024-01-26 10:53:06
maintainer
docs_urlNone
authorLuc Sorel-Giffo
requires_python>=3.8,<4.0
licenseApache-2.0
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            Brings InfluxDB in testcontainers-python (until [PR #413](https://github.com/testcontainers/testcontainers-python/pull/413) is merged).
This project is hosted at https://github.com/Purecontrol/testcontainers-python-influxdb.
Thanks to my employer - [Purecontrol](https://www.purecontrol.com/) - for sponsoring the development of this testing utility 🙏

# Installation in your project

This package supports versions 1.x and 2.x of InfluxDB.
Specify the version you want to use during the installation so that only the needed Python client library will be installed.

- if you use `pip`:

```sh
# for InfluxDB 1.x versions
pip install "testcontainers-python-influxdb[influxdb1]"

# for InfluxDB 2.x versions
pip install "testcontainers-python-influxdb[influxdb2]"

# for both InfluxDB 1.x and 2.x versions (unlikely, but who knows?)
pip install "testcontainers-python-influxdb[influxdb1,influxdb2]"
```

- if you use `poetry`:

```sh
# for InfluxDB 1.x versions
poetry add "testcontainers-python-influxdb[influxdb1]"

# for InfluxDB 2.x versions
poetry add "testcontainers-python-influxdb[influxdb2]"

# for both InfluxDB 1.x and 2.x versions (unlikely, but who knows?)
poetry add "testcontainers-python-influxdb[influxdb1,influxdb2]"
```

# Use cases

## InfluxDB v1

```python
from influxdb.resultset import ResultSet
from testcontainers_python_influxdb.influxdb1 import InfluxDb1Container

def test_create_and_retrieve_datapoints():
    with InfluxDb1Container("influxdb:1.8") as influxdb1_container:
        influxdb1_client = influxdb1_container.get_client()
        databases = influxdb1_client.get_list_database()
        assert len(databases) == 0, "the InfluxDB container starts with no database at all"

        # creates a database and inserts some datapoints
        influxdb1_client.create_database("testcontainers")
        databases = influxdb1_client.get_list_database()
        assert len(databases) == 1, "the InfluxDB container now contains one database"
        assert databases[0] == {"name": "testcontainers"}

        influxdb1_client.write_points(
            [
                {"measurement": "influxdbcontainer", "time": "1978-11-30T09:30:00Z", "fields": {"ratio": 0.42}},
                {"measurement": "influxdbcontainer", "time": "1978-12-25T10:30:00Z", "fields": {"ratio": 0.55}},
            ],
            database="testcontainers",
        )

        # retrieves the inserted datapoints
        datapoints_set: ResultSet = influxdb1_client.query(
            "select ratio from influxdbcontainer;", database="testcontainers"
        )
        datapoints = list(datapoints_set.get_points())
        assert len(datapoints) == 2, "2 datapoints are retrieved"

        datapoint = datapoints[0]
        assert datapoint["time"] == "1978-11-30T09:30:00Z"
        assert datapoint["ratio"] == 0.42

        datapoint = datapoints[1]
        assert datapoint["time"] == "1978-12-25T10:30:00Z"
        assert datapoint["ratio"] == 0.55
```

## InfluxDB v2

```python
from datetime import datetime
from influxdb_client import Bucket
from influxdb_client.client.write_api import SYNCHRONOUS
from testcontainers_python_influxdb.influxdb2 import InfluxDb2Container

def test_create_and_retrieve_datapoints():
    with InfluxDb2Container(
        "influxdb:2.7",
        init_mode="setup",
        username="root",
        password="secret-password",
        org_name="testcontainers-org",
        bucket="my-init-bucket",
        admin_token="secret-token",
    ) as influxdb2_container:
        influxdb2_client, test_org = influxdb2_container.get_client(token="secret-token", org_name="testcontainers-org")
        assert influxdb2_client.ping(), "the client can connect to the InfluxDB instance"

        # ensures that the bucket does not exist yet
        buckets_api = influxdb2_client.buckets_api()
        bucket: Bucket = buckets_api.find_bucket_by_name("testcontainers")
        assert bucket is None, "the test bucket does not exist yet"

        # creates a test bucket and insert a point
        buckets_api.create_bucket(bucket_name="testcontainers", org=test_org)
        bucket: Bucket = buckets_api.find_bucket_by_name("testcontainers")
        assert bucket.name == "testcontainers", "the test bucket now exists"

        write_api = influxdb2_client.write_api(write_options=SYNCHRONOUS)
        write_api.write(
            "testcontainers",
            "testcontainers-org",
            [
                {"measurement": "influxdbcontainer", "time": "1978-11-30T09:30:00Z", "fields": {"ratio": 0.42}},
                {"measurement": "influxdbcontainer", "time": "1978-12-25T10:30:00Z", "fields": {"ratio": 0.55}},
            ],
        )

        # retrieves the inserted datapoints
        query_api = influxdb2_client.query_api()
        tables = query_api.query('from(bucket: "testcontainers") |> range(start: 1978-11-01T22:00:00Z)', org=test_org)
        results = tables.to_values(["_measurement", "_field", "_time", "_value"])

        assert len(results) == 2, "2 datapoints were retrieved"
        assert results[0] == ["influxdbcontainer", "ratio", datetime.fromisoformat("1978-11-30T09:30:00+00:00"), 0.42]
        assert results[1] == ["influxdbcontainer", "ratio", datetime.fromisoformat("1978-12-25T10:30:00+00:00"), 0.55]
```

# Development

## Tests

- install the libraries for 1.x and 2.x clients:

```sh
poetry install --all-extras
```

- run the automated tests:

```sh
# directly with poetry
poetry run pytest -v
```

Code coverage (with [missed branch statements](https://pytest-cov.readthedocs.io/en/latest/config.html?highlight=--cov-branch)):

```sh
poetry run pytest -v --cov=testcontainers_python_influxdb --cov-branch --cov-report term-missing --cov-fail-under 94
```

## Code conventions

The code conventions are described and enforced by [pre-commit hooks](https://pre-commit.com/hooks.html) to maintain style and quality consistency across the code base.
The hooks are declared in the [.pre-commit-config.yaml](.pre-commit-config.yaml) file.

When you contribute, set the git hooks (pre-commit and commit-msg types) on your development environment:

```sh
poetry run pre-commit install --hook-type pre-commit --hook-type commit-msg
```

Before committing, you can check your changes manually with:

```sh
# put all your changes in the git staging area (or add the changes manually and skip this)
git add -A

# run all hooks
poetry run pre-commit run --all-files

# run a specific hook
poetry run pre-commit run ruff --all-files
```

# Licence

Unless stated otherwise, all works are licensed under the [Apache 2.0](https://spdx.org/licenses/Apache-2.0.html), a copy of which is included [here](LICENSE).

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "testcontainers-python-influxdb",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8,<4.0",
    "maintainer_email": "",
    "keywords": "",
    "author": "Luc Sorel-Giffo",
    "author_email": "",
    "download_url": "https://files.pythonhosted.org/packages/01/cb/f55182097c4265a28b0a8c22cb3e9a8d442712f5897462bf7b984c771752/testcontainers_python_influxdb-0.2.2.tar.gz",
    "platform": null,
    "description": "Brings InfluxDB in testcontainers-python (until [PR #413](https://github.com/testcontainers/testcontainers-python/pull/413) is merged).\nThis project is hosted at https://github.com/Purecontrol/testcontainers-python-influxdb.\nThanks to my employer - [Purecontrol](https://www.purecontrol.com/) - for sponsoring the development of this testing utility \ud83d\ude4f\n\n# Installation in your project\n\nThis package supports versions 1.x and 2.x of InfluxDB.\nSpecify the version you want to use during the installation so that only the needed Python client library will be installed.\n\n- if you use `pip`:\n\n```sh\n# for InfluxDB 1.x versions\npip install \"testcontainers-python-influxdb[influxdb1]\"\n\n# for InfluxDB 2.x versions\npip install \"testcontainers-python-influxdb[influxdb2]\"\n\n# for both InfluxDB 1.x and 2.x versions (unlikely, but who knows?)\npip install \"testcontainers-python-influxdb[influxdb1,influxdb2]\"\n```\n\n- if you use `poetry`:\n\n```sh\n# for InfluxDB 1.x versions\npoetry add \"testcontainers-python-influxdb[influxdb1]\"\n\n# for InfluxDB 2.x versions\npoetry add \"testcontainers-python-influxdb[influxdb2]\"\n\n# for both InfluxDB 1.x and 2.x versions (unlikely, but who knows?)\npoetry add \"testcontainers-python-influxdb[influxdb1,influxdb2]\"\n```\n\n# Use cases\n\n## InfluxDB v1\n\n```python\nfrom influxdb.resultset import ResultSet\nfrom testcontainers_python_influxdb.influxdb1 import InfluxDb1Container\n\ndef test_create_and_retrieve_datapoints():\n    with InfluxDb1Container(\"influxdb:1.8\") as influxdb1_container:\n        influxdb1_client = influxdb1_container.get_client()\n        databases = influxdb1_client.get_list_database()\n        assert len(databases) == 0, \"the InfluxDB container starts with no database at all\"\n\n        # creates a database and inserts some datapoints\n        influxdb1_client.create_database(\"testcontainers\")\n        databases = influxdb1_client.get_list_database()\n        assert len(databases) == 1, \"the InfluxDB container now contains one database\"\n        assert databases[0] == {\"name\": \"testcontainers\"}\n\n        influxdb1_client.write_points(\n            [\n                {\"measurement\": \"influxdbcontainer\", \"time\": \"1978-11-30T09:30:00Z\", \"fields\": {\"ratio\": 0.42}},\n                {\"measurement\": \"influxdbcontainer\", \"time\": \"1978-12-25T10:30:00Z\", \"fields\": {\"ratio\": 0.55}},\n            ],\n            database=\"testcontainers\",\n        )\n\n        # retrieves the inserted datapoints\n        datapoints_set: ResultSet = influxdb1_client.query(\n            \"select ratio from influxdbcontainer;\", database=\"testcontainers\"\n        )\n        datapoints = list(datapoints_set.get_points())\n        assert len(datapoints) == 2, \"2 datapoints are retrieved\"\n\n        datapoint = datapoints[0]\n        assert datapoint[\"time\"] == \"1978-11-30T09:30:00Z\"\n        assert datapoint[\"ratio\"] == 0.42\n\n        datapoint = datapoints[1]\n        assert datapoint[\"time\"] == \"1978-12-25T10:30:00Z\"\n        assert datapoint[\"ratio\"] == 0.55\n```\n\n## InfluxDB v2\n\n```python\nfrom datetime import datetime\nfrom influxdb_client import Bucket\nfrom influxdb_client.client.write_api import SYNCHRONOUS\nfrom testcontainers_python_influxdb.influxdb2 import InfluxDb2Container\n\ndef test_create_and_retrieve_datapoints():\n    with InfluxDb2Container(\n        \"influxdb:2.7\",\n        init_mode=\"setup\",\n        username=\"root\",\n        password=\"secret-password\",\n        org_name=\"testcontainers-org\",\n        bucket=\"my-init-bucket\",\n        admin_token=\"secret-token\",\n    ) as influxdb2_container:\n        influxdb2_client, test_org = influxdb2_container.get_client(token=\"secret-token\", org_name=\"testcontainers-org\")\n        assert influxdb2_client.ping(), \"the client can connect to the InfluxDB instance\"\n\n        # ensures that the bucket does not exist yet\n        buckets_api = influxdb2_client.buckets_api()\n        bucket: Bucket = buckets_api.find_bucket_by_name(\"testcontainers\")\n        assert bucket is None, \"the test bucket does not exist yet\"\n\n        # creates a test bucket and insert a point\n        buckets_api.create_bucket(bucket_name=\"testcontainers\", org=test_org)\n        bucket: Bucket = buckets_api.find_bucket_by_name(\"testcontainers\")\n        assert bucket.name == \"testcontainers\", \"the test bucket now exists\"\n\n        write_api = influxdb2_client.write_api(write_options=SYNCHRONOUS)\n        write_api.write(\n            \"testcontainers\",\n            \"testcontainers-org\",\n            [\n                {\"measurement\": \"influxdbcontainer\", \"time\": \"1978-11-30T09:30:00Z\", \"fields\": {\"ratio\": 0.42}},\n                {\"measurement\": \"influxdbcontainer\", \"time\": \"1978-12-25T10:30:00Z\", \"fields\": {\"ratio\": 0.55}},\n            ],\n        )\n\n        # retrieves the inserted datapoints\n        query_api = influxdb2_client.query_api()\n        tables = query_api.query('from(bucket: \"testcontainers\") |> range(start: 1978-11-01T22:00:00Z)', org=test_org)\n        results = tables.to_values([\"_measurement\", \"_field\", \"_time\", \"_value\"])\n\n        assert len(results) == 2, \"2 datapoints were retrieved\"\n        assert results[0] == [\"influxdbcontainer\", \"ratio\", datetime.fromisoformat(\"1978-11-30T09:30:00+00:00\"), 0.42]\n        assert results[1] == [\"influxdbcontainer\", \"ratio\", datetime.fromisoformat(\"1978-12-25T10:30:00+00:00\"), 0.55]\n```\n\n# Development\n\n## Tests\n\n- install the libraries for 1.x and 2.x clients:\n\n```sh\npoetry install --all-extras\n```\n\n- run the automated tests:\n\n```sh\n# directly with poetry\npoetry run pytest -v\n```\n\nCode coverage (with [missed branch statements](https://pytest-cov.readthedocs.io/en/latest/config.html?highlight=--cov-branch)):\n\n```sh\npoetry run pytest -v --cov=testcontainers_python_influxdb --cov-branch --cov-report term-missing --cov-fail-under 94\n```\n\n## Code conventions\n\nThe code conventions are described and enforced by [pre-commit hooks](https://pre-commit.com/hooks.html) to maintain style and quality consistency across the code base.\nThe hooks are declared in the [.pre-commit-config.yaml](.pre-commit-config.yaml) file.\n\nWhen you contribute, set the git hooks (pre-commit and commit-msg types) on your development environment:\n\n```sh\npoetry run pre-commit install --hook-type pre-commit --hook-type commit-msg\n```\n\nBefore committing, you can check your changes manually with:\n\n```sh\n# put all your changes in the git staging area (or add the changes manually and skip this)\ngit add -A\n\n# run all hooks\npoetry run pre-commit run --all-files\n\n# run a specific hook\npoetry run pre-commit run ruff --all-files\n```\n\n# Licence\n\nUnless stated otherwise, all works are licensed under the [Apache 2.0](https://spdx.org/licenses/Apache-2.0.html), a copy of which is included [here](LICENSE).\n",
    "bugtrack_url": null,
    "license": "Apache-2.0",
    "summary": "Brings InfluxDB in testcontainers-python (until PR #413 is merged)",
    "version": "0.2.2",
    "project_urls": null,
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "76912277ade0ca8c3dcdd0e0f188ebdeb6bec156c0c984c257fa635e752ab3b5",
                "md5": "9b11ec3239319b88b69e842d9dd26893",
                "sha256": "0d3890206af31fb1300e6f66ab05ad9865f4a662c6cf5f16efa13028dafb1dcc"
            },
            "downloads": -1,
            "filename": "testcontainers_python_influxdb-0.2.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "9b11ec3239319b88b69e842d9dd26893",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8,<4.0",
            "size": 12403,
            "upload_time": "2024-01-26T10:53:04",
            "upload_time_iso_8601": "2024-01-26T10:53:04.769463Z",
            "url": "https://files.pythonhosted.org/packages/76/91/2277ade0ca8c3dcdd0e0f188ebdeb6bec156c0c984c257fa635e752ab3b5/testcontainers_python_influxdb-0.2.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "01cbf55182097c4265a28b0a8c22cb3e9a8d442712f5897462bf7b984c771752",
                "md5": "16d7ec8e6c12f702433fc78ed670cdaa",
                "sha256": "1a2a234d4c48effe5a62bd7ab7aa0b80950892b73f6b40a942e354e07baf4c42"
            },
            "downloads": -1,
            "filename": "testcontainers_python_influxdb-0.2.2.tar.gz",
            "has_sig": false,
            "md5_digest": "16d7ec8e6c12f702433fc78ed670cdaa",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8,<4.0",
            "size": 9925,
            "upload_time": "2024-01-26T10:53:06",
            "upload_time_iso_8601": "2024-01-26T10:53:06.582680Z",
            "url": "https://files.pythonhosted.org/packages/01/cb/f55182097c4265a28b0a8c22cb3e9a8d442712f5897462bf7b984c771752/testcontainers_python_influxdb-0.2.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-01-26 10:53:06",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "testcontainers-python-influxdb"
}
        
Elapsed time: 1.49367s