Package: testcontainers-python-influxdb
Brings InfluxDB in testcontainers-python (untilPR #413is merged).
This project is hosted athttps://github.com/Purecontrol/testcontainers-python-influxdb.
Thanks to my employer -Purecontrol- for sponsoring the development of this testing utility 🙏Installation in your projectThis 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 usepip:# for InfluxDB 1.x versionspipinstall"testcontainers-python-influxdb[influxdb1]"# for InfluxDB 2.x versionspipinstall"testcontainers-python-influxdb[influxdb2]"# for both InfluxDB 1.x and 2.x versions (unlikely, but who knows?)pipinstall"testcontainers-python-influxdb[influxdb1,influxdb2]"if you usepoetry:# for InfluxDB 1.x versionspoetryadd"testcontainers-python-influxdb[influxdb1]"# for InfluxDB 2.x versionspoetryadd"testcontainers-python-influxdb[influxdb2]"# for both InfluxDB 1.x and 2.x versions (unlikely, but who knows?)poetryadd"testcontainers-python-influxdb[influxdb1,influxdb2]"Use casesInfluxDB v1frominfluxdb.resultsetimportResultSetfromtestcontainers_python_influxdb.influxdb1importInfluxDb1Containerdeftest_create_and_retrieve_datapoints():withInfluxDb1Container("influxdb:1.8")asinfluxdb1_container:influxdb1_client=influxdb1_container.get_client()databases=influxdb1_client.get_list_database()assertlen(databases)==0,"the InfluxDB container starts with no database at all"# creates a database and inserts some datapointsinfluxdb1_client.create_database("testcontainers")databases=influxdb1_client.get_list_database()assertlen(databases)==1,"the InfluxDB container now contains one database"assertdatabases[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 datapointsdatapoints_set:ResultSet=influxdb1_client.query("select ratio from influxdbcontainer;",database="testcontainers")datapoints=list(datapoints_set.get_points())assertlen(datapoints)==2,"2 datapoints are retrieved"datapoint=datapoints[0]assertdatapoint["time"]=="1978-11-30T09:30:00Z"assertdatapoint["ratio"]==0.42datapoint=datapoints[1]assertdatapoint["time"]=="1978-12-25T10:30:00Z"assertdatapoint["ratio"]==0.55InfluxDB v2fromdatetimeimportdatetimefrominfluxdb_clientimportBucketfrominfluxdb_client.client.write_apiimportSYNCHRONOUSfromtestcontainers_python_influxdb.influxdb2importInfluxDb2Containerdeftest_create_and_retrieve_datapoints():withInfluxDb2Container("influxdb:2.7",init_mode="setup",username="root",password="secret-password",org_name="testcontainers-org",bucket="my-init-bucket",admin_token="secret-token",)asinfluxdb2_container:influxdb2_client,test_org=influxdb2_container.get_client(token="secret-token",org_name="testcontainers-org")assertinfluxdb2_client.ping(),"the client can connect to the InfluxDB instance"# ensures that the bucket does not exist yetbuckets_api=influxdb2_client.buckets_api()bucket:Bucket=buckets_api.find_bucket_by_name("testcontainers")assertbucketisNone,"the test bucket does not exist yet"# creates a test bucket and insert a pointbuckets_api.create_bucket(bucket_name="testcontainers",org=test_org)bucket:Bucket=buckets_api.find_bucket_by_name("testcontainers")assertbucket.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 datapointsquery_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"])assertlen(results)==2,"2 datapoints were retrieved"assertresults[0]==["influxdbcontainer","ratio",datetime.fromisoformat("1978-11-30T09:30:00+00:00"),0.42]assertresults[1]==["influxdbcontainer","ratio",datetime.fromisoformat("1978-12-25T10:30:00+00:00"),0.55]DevelopmentTestsinstall the libraries for 1.x and 2.x clients:poetryinstall--all-extrasrun the automated tests:# directly with poetrypoetryrunpytest-vCode coverage (withmissed branch statements):poetryrunpytest-v--cov=testcontainers_python_influxdb--cov-branch--cov-reportterm-missing--cov-fail-under94Code conventionsThe code conventions are described and enforced bypre-commit hooksto maintain style and quality consistency across the code base.
The hooks are declared in the.pre-commit-config.yamlfile.When you contribute, set the git hooks (pre-commit and commit-msg types) on your development environment:poetryrunpre-commitinstall--hook-typepre-commit--hook-typecommit-msgBefore committing, you can check your changes manually with:# put all your changes in the git staging area (or add the changes manually and skip this)gitadd-A# run all hookspoetryrunpre-commitrun--all-files# run a specific hookpoetryrunpre-commitrunruff--all-filesLicenceUnless stated otherwise, all works are licensed under theApache 2.0, a copy of which is includedhere.