Skip to content

feat: add mutable Builder for PointData (#178) #180

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

Merged
merged 4 commits into from
Mar 19, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### Features
1. [#171](https://github.com/influxdata/influxdb-client-csharp/pull/171): Added `QueryApiSync` for synchronous querying
1. [#171](https://github.com/influxdata/influxdb-client-csharp/pull/171): Added `IDomainObjectMapper` for custom mapping DomainObject from/to InfluxDB
1. [#180](https://github.com/influxdata/influxdb-client-csharp/pull/180): Added a mutable `PointData.Builder` to optimize building of immutable `PointData`

### API
1. [#174](https://github.com/influxdata/influxdb-client-csharp/pull/174): Added possibility to use `CancellationToken` in REST API
Expand Down
166 changes: 166 additions & 0 deletions Client.Test/PointDataBuilderTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
using System;
using InfluxDB.Client.Api.Domain;
using InfluxDB.Client.Writes;
using NUnit.Framework;

namespace InfluxDB.Client.Test
{
[TestFixture]
public class PointDataBuilderTest
{
[Test]
public void BuilderValuesToPoint()
{
var builder = PointData.Builder.Measurement("h2o")
.Tag("location", "europe")
.Tag("log", "some_log")
.Field("level", 2);

var point = builder.ToPointData();
Assert.AreEqual("h2o,location=europe,log=some_log level=2i", point.ToLineProtocol());
}

[Test]
public void TagEmptyRemovesTagValue()
{
var builder = PointData.Builder.Measurement("h2o")
.Tag("location", "europe")
.Tag("log", "to_delete")
.Tag("log", "")
.Field("level", 2);

var point = builder.ToPointData();
Assert.AreEqual("h2o,location=europe level=2i", point.ToLineProtocol());
}

[Test]
public void ReplaceTagValue()
{
var builder = PointData.Builder.Measurement("h2o")
.Tag("location", "europe")
.Tag("log", "old_log")
.Tag("log", "new_log")
.Field("level", 2);

var point = builder.ToPointData();
Assert.AreEqual("h2o,location=europe,log=new_log level=2i", point.ToLineProtocol());
}

[Test]
public void ReplaceTagValueInNewPoint()
{
var builder = PointData.Builder.Measurement("h2o")
.Tag("location", "europe")
.Tag("log", "old_log")
.Field("level", 2);

var point = builder.ToPointData();

builder.Tag("log", "new_log");

var anotherPoint = builder.ToPointData();

Assert.AreEqual("h2o,location=europe,log=old_log level=2i", point.ToLineProtocol());
Assert.AreEqual("h2o,location=europe,log=new_log level=2i", anotherPoint.ToLineProtocol());
}

[Test]
public void TagEmptyKey()
{
var builder = PointData.Builder.Measurement("h2o")
.Tag("location", "europe")
.Tag("", "warn")
.Field("level", 2);

var point = builder.ToPointData();

Assert.AreEqual("h2o,location=europe level=2i", point.ToLineProtocol());
}

[Test]
public void TagEmptyValue()
{
var builder = PointData.Builder.Measurement("h2o")
.Tag("location", "europe")
.Tag("log", "")
.Field("level", 2);

var point = builder.ToPointData();

Assert.AreEqual("h2o,location=europe level=2i", point.ToLineProtocol());
}

[Test]
public void ReplaceFieldValue()
{
var builder = PointData.Builder.Measurement("h2o")
.Tag("location", "europe2")
.Field("level", 2)
.Field("level", 3);

var point = builder.ToPointData();

Assert.AreEqual("h2o,location=europe2 level=3i", point.ToLineProtocol());
}

[Test]
public void MultipleFields()
{
var builder = PointData.Builder.Measurement("h2o")
.Tag("location", "europe2")
.Field("levelA", 2)
.Field("levelB", 3);

var point = builder.ToPointData();

Assert.AreEqual("h2o,location=europe2 levelA=2i,levelB=3i", point.ToLineProtocol());
}

[Test]
public void FieldNullValue()
{
var builder = PointData.Builder.Measurement("h2o")
.Tag("location", "europe")
.Field("level", 2)
.Field("warning", null);

var point = builder.ToPointData();

Assert.AreEqual("h2o,location=europe level=2i", point.ToLineProtocol());
}

[Test]
public void Time()
{
var builder = PointData.Builder.Measurement("h2o")
.Tag("location", "europe")
.Field("level", 2)
.Timestamp(123L, WritePrecision.S);

var point = builder.ToPointData();

Assert.AreEqual("h2o,location=europe level=2i 123", point.ToLineProtocol());
}

[Test]
public void DateTimeMustBeUtc()
{
var dateTime = new DateTime(2015, 10, 15, 8, 20, 15);

var builder = PointData.Builder.Measurement("h2o")
.Tag("location", "europe")
.Field("level", 2);

Assert.Throws<ArgumentException>(() => builder.Timestamp(dateTime, WritePrecision.Ms));
}

[Test]
public void HasFields()
{
Assert.IsFalse(PointData.Builder.Measurement("h2o").HasFields());
Assert.IsFalse(PointData.Builder.Measurement("h2o").Tag("location", "europe").HasFields());
Assert.IsTrue(PointData.Builder.Measurement("h2o").Field("level", "2").HasFields());
Assert.IsTrue(PointData.Builder.Measurement("h2o").Tag("location", "europe").Field("level", "2").HasFields());
}
}
}
Loading