Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use default org #48

Merged
merged 1 commit into from
Jan 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
### API
1. [#47](https://github.com/influxdata/influxdb-client-python/pull/47): Updated swagger to latest version

### Bugs
1. [#48](https://github.com/influxdata/influxdb-client-python/pull/48): InfluxDBClient default org is used by WriteAPI

## 1.2.0 [2019-12-06]

### Features
Expand Down
8 changes: 5 additions & 3 deletions influxdb_client/client/write_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# coding: utf-8
import logging
import threading
from datetime import timedelta
from enum import Enum
from random import random
Expand Down Expand Up @@ -134,9 +133,9 @@ def __init__(self, influxdb_client, write_options: WriteOptions = WriteOptions()
self._subject = None
self._disposable = None

def write(self, bucket: str, org: str,
def write(self, bucket: str, org: str = None,
record: Union[
str, List['str'], Point, List['Point'], dict, List['dict'], bytes, List['bytes'], Observable],
str, List['str'], Point, List['Point'], dict, List['dict'], bytes, List['bytes'], Observable] = None,
write_precision: WritePrecision = DEFAULT_WRITE_PRECISION) -> None:
"""
Writes time-series data into influxdb.
Expand All @@ -148,6 +147,9 @@ def write(self, bucket: str, org: str,

"""

if org is None:
org = self._influxdb_client.org

if self._write_options.write_type is WriteType.batching:
return self._write_batching(bucket, org, record, write_precision)

Expand Down
39 changes: 31 additions & 8 deletions tests/test_WriteApi.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ def test_write_line_protocol(self):

record = "h2o_feet,location=coyote_creek level\\ water_level=1.0 1"
self.write_client.write(bucket.name, self.org, record)
self.write_client.flush()

result = self.query_api.query(
"from(bucket:\"" + bucket.name + "\") |> range(start: 1970-01-01T00:00:00.000000001Z) |> last()", self.org)
Expand All @@ -42,8 +41,6 @@ def test_write_line_protocol(self):

self.delete_test_bucket(bucket)

#####################################

def test_write_precision(self):
bucket = self.create_test_bucket()

Expand All @@ -67,8 +64,6 @@ def test_write_records_list(self):

self.write_client.write(bucket.name, self.org, record_list)

self.write_client.flush()

query = 'from(bucket:"' + bucket.name + '") |> range(start: 1970-01-01T00:00:00.000000001Z)'
print(query)

Expand Down Expand Up @@ -104,7 +99,6 @@ def test_write_points_unicode(self):
record_list = [p]

self.write_client.write(bucket.name, self.org, record_list)
self.write_client.flush()

query = 'from(bucket:"' + bucket.name + '") |> range(start: 1970-01-01T00:00:00.000000001Z)'
flux_result = self.client.query_api().query(query)
Expand Down Expand Up @@ -142,7 +136,6 @@ def test_write_dictionary(self):
"time": "2009-11-10T23:00:00Z", "fields": {"water_level": 1.0}}

self.write_client.write(_bucket.name, self.org, _point)
self.write_client.flush()

result = self.query_api.query(
"from(bucket:\"" + _bucket.name + "\") |> range(start: 1970-01-01T00:00:00.000000001Z) |> last()", self.org)
Expand All @@ -162,7 +155,6 @@ def test_write_bytes(self):
_bytes = "h2o_feet,location=coyote_creek level\\ water_level=1.0 1".encode("utf-8")

self.write_client.write(_bucket.name, self.org, _bytes)
self.write_client.flush()

result = self.query_api.query(
"from(bucket:\"" + _bucket.name + "\") |> range(start: 1970-01-01T00:00:00.000000001Z) |> last()", self.org)
Expand All @@ -177,6 +169,37 @@ def test_write_bytes(self):

self.delete_test_bucket(_bucket)

def test_use_default_org(self):
bucket = self.create_test_bucket()

record = "h2o_feet,location=coyote_creek level\\ water_level=1.0 1"
self.write_client.write(bucket.name, record=record)

result = self.query_api.query(
"from(bucket:\"" + bucket.name + "\") |> range(start: 1970-01-01T00:00:00.000000001Z) |> last()", self.org)

self.assertEqual(len(result), 1)
self.assertEqual(result[0].records[0].get_measurement(), "h2o_feet")
self.assertEqual(result[0].records[0].get_value(), 1.0)
self.assertEqual(result[0].records[0].get_field(), "level water_level")
self.assertEqual(result[0].records[0].get_time(),
datetime.datetime(1970, 1, 1, 0, 0, tzinfo=datetime.timezone.utc))

def test_write_empty_data(self):
bucket = self.create_test_bucket()

with self.assertRaises(ApiException) as cm:
self.write_client.write(bucket.name)
exception = cm.exception

self.assertEqual(400, exception.status)
self.assertEqual("Bad Request", exception.reason)

result = self.query_api.query(
"from(bucket:\"" + bucket.name + "\") |> range(start: 1970-01-01T00:00:00.000000001Z) |> last()", self.org)

self.assertEqual(len(result), 0)


class AsynchronousWriteTest(BaseTest):

Expand Down