public void Can_round_if_wrong_precision_used(Precision precision)
        {
            var writer = new LineProtocolWriter(precision);

            writer.Measurement("foo").Field("bar", true).Timestamp(TimeSpan.FromTicks(1), PrecisionResolutionStrategy.Round);
            writer.Measurement("foo").Field("bar", true).Timestamp(TimeSpan.FromTicks(((long)precision / 100) - 1), PrecisionResolutionStrategy.Round);

            Assert.Equal("foo bar=t 0\nfoo bar=t 1", writer.ToString());
        }
        public void Can_define_resolution_strategy_when_creating_the_writer()
        {
            var writer = new LineProtocolWriter(Precision.Seconds, PrecisionResolutionStrategy.Round);

            writer.Measurement("foo").Field("bar", true).Timestamp(TimeSpan.FromMilliseconds(499));
            writer.Measurement("foo").Field("bar", true).Timestamp(TimeSpan.FromMilliseconds(500));

            Assert.Equal("foo bar=t 0\nfoo bar=t 1", writer.ToString());
        }
        public void Can_override_resolution_strategy_when_writing_point()
        {
            var writer = new LineProtocolWriter(Precision.Seconds, PrecisionResolutionStrategy.Round);

            writer.Measurement("foo").Field("bar", true).Timestamp(TimeSpan.FromMilliseconds(700));
            writer.Measurement("foo").Field("bar", true).Timestamp(TimeSpan.FromMilliseconds(700), PrecisionResolutionStrategy.Floor);

            Assert.Equal("foo bar=t 1\nfoo bar=t 0", writer.ToString());
        }
        public void Cannot_write_new_measurment_when_field_not_written()
        {
            var writer = new LineProtocolWriter();

            writer.Measurement("my_measurement");

            var ex = Assert.Throws <InvalidOperationException>(() => writer.Measurement("my_measurement"));

            Assert.Equal("Cannot write new measurement \"my_measurement\" as no field written for current line.", ex.Message);
            Assert.Equal("MeasurementWritten", ex.Data["Position"]);

            writer.Tag("foo", "bar");

            ex = Assert.Throws <InvalidOperationException>(() => writer.Measurement("my_measurement"));
            Assert.Equal("TagWritten", ex.Data["Position"]);
        }
        public void Can_ceiling_if_wrong_precision_used(Precision precision)
        {
            var writer = new LineProtocolWriter(precision);

            var timestamp = TimeSpan.FromTicks(1);

            writer.Measurement("foo").Field("bar", 1f).Timestamp(timestamp, PrecisionResolutionStrategy.Ceiling);

            Assert.Equal("foo bar=1 1", writer.ToString());
        }
        public void Will_throw_if_wrong_precision_used(Precision precision)
        {
            var writer = new LineProtocolWriter(precision);

            var timestamp = TimeSpan.FromTicks(1);

            writer.Measurement("foo").Field("bar", 1f);

            Assert.Throws <ArgumentOutOfRangeException>(() => writer.Timestamp(timestamp));
        }
        public void Will_write_timestamps_using_precision_of_writer(Precision precision, long nanoseconds, string expectedTimestamp)
        {
            var unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

            var timestamp = unixEpoch.AddTicks(nanoseconds / 100); // .net tick is 100 nanoseconds.

            var writer = new LineProtocolWriter(precision);

            writer.Measurement("foo").Field("bar", 1f).Timestamp(timestamp);

            Assert.Equal($"foo bar=1 {expectedTimestamp}", writer.ToString());
        }
        public void Cannot_write_tag_after_field_written()
        {
            var writer = new LineProtocolWriter();

            writer.Measurement("my_measurement").Field("value", 1);

            var ex = Assert.Throws <InvalidOperationException>(() => writer.Tag("foo", "bar"));

            Assert.Equal("Cannot write tag \"foo\" as field(s) already written for current line.", ex.Message);
            Assert.Equal("FieldWritten", ex.Data["Position"]);

            writer.Timestamp(123456);

            ex = Assert.Throws <InvalidOperationException>(() => writer.Tag("foo", "bar"));
            Assert.Equal("TimestampWritten", ex.Data["Position"]);
        }
        public void Cannot_write_timestamp_when_no_field_written()
        {
            var writer = new LineProtocolWriter();

            var ex = Assert.Throws <InvalidOperationException>(() => writer.Timestamp(123456));

            Assert.Equal("Cannot write timestamp as no measurement name written.", ex.Message);
            Assert.Equal("NothingWritten", ex.Data["Position"]);

            writer.Measurement("my_measurement");

            ex = Assert.Throws <InvalidOperationException>(() => writer.Timestamp(123456));
            Assert.Equal("Cannot write timestamp as no field written for current measurement.", ex.Message);
            Assert.Equal("MeasurementWritten", ex.Data["Position"]);

            writer.Tag("foo", "bar");

            ex = Assert.Throws <InvalidOperationException>(() => writer.Timestamp(123456));
            Assert.Equal("TagWritten", ex.Data["Position"]);

            Assert.Throws <InvalidOperationException>(() => writer.Timestamp(TimeSpan.FromDays(3045)));
            Assert.Throws <InvalidOperationException>(() => writer.Timestamp(DateTime.UtcNow));
            Assert.Throws <InvalidOperationException>(() => writer.Timestamp(DateTimeOffset.Now));
        }