Skip to content

Commit d89f4e9

Browse files
author
Enric Sala
committed
Port the point builder tests to the series builder
The series builder now implements all the functionalities of the point builder
1 parent b6b8c96 commit d89f4e9

File tree

2 files changed

+93
-23
lines changed

2 files changed

+93
-23
lines changed

influxdb-client/Series.m

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@
2525

2626
% Add a field value
2727
function obj = field(obj, key, value)
28-
if isempty(value)
29-
error('field:emptyValue', 'value of field "%s" is empty', key);
28+
if ischar(value)
29+
field = struct('key', key, 'value', {{value}});
30+
obj.Fields{end + 1} = field;
31+
elseif isempty(value)
32+
% ignore field with empty value
3033
elseif isnumeric(value) || islogical(value)
3134
field = struct('key', key, 'value', value);
3235
obj.Fields{end + 1} = field;
@@ -72,20 +75,30 @@
7275
time_length = length(obj.Time);
7376
field_lengths = unique(cellfun(@(x) length(x.value), obj.Fields));
7477

78+
% Check if the series name is valid
79+
assert(~isempty(obj.Name), ...
80+
'toLine:emptyName', 'series name cannot be empty');
81+
82+
% Return empty if there are no fields
83+
if isempty(field_lengths)
84+
lines = '';
85+
return;
86+
end
87+
7588
% Make sure the dimensions match
76-
assert(~isempty(obj.Time), ...
77-
'toLine:emptyTime', 'the time vector cannot be empty');
78-
assert(~isempty(field_lengths), ...
79-
'toLine:emptyFields', 'must define at least one field');
8089
assert(length(field_lengths) == 1, ...
8190
'toLine:sizeMismatch', 'all fields must have the same length');
8291
assert(time_length == field_lengths || time_length == 0, ...
8392
'toLine:sizeMismatch', 'time and fields must have the same length');
93+
assert(~isempty(obj.Time) || field_lengths == 1, ...
94+
'toLine:emptyTime', 'the time vector cannot be empty');
8495

8596
% Obtain the time precision scale
86-
if nargin < 2, precision = 'ms'; end
87-
scale = obj.timeScale(precision);
88-
timestamp = int64(scale * posixtime(obj.Time));
97+
if time_length > 0
98+
if nargin < 2, precision = 'ms'; end
99+
scale = obj.timeScale(precision);
100+
timestamp = int64(scale * posixtime(obj.Time));
101+
end
89102

90103
% Create a line for each sample
91104
prefix = [strjoin([{obj.Name}, obj.Tags], ','), ' '];
@@ -107,11 +120,15 @@
107120
end
108121
if ~isempty(values)
109122
values = values(1:end-1);
110-
time = sprintf(' %i', timestamp(i));
111-
builder = [builder, prefix, values, time, newline];
123+
if time_length > 0
124+
time = sprintf(' %i', timestamp(i));
125+
builder = [builder, prefix, values, time, newline];
126+
else
127+
builder = [builder, prefix, values, newline];
128+
end
112129
end
113130
end
114-
lines = builder(1:end-1);
131+
lines = iif(isempty(builder), '', builder(1:end-1));
115132
end
116133
end
117134

@@ -151,7 +168,8 @@
151168
case 'h'
152169
scale = 1 / 3600;
153170
otherwise
154-
error('precision:unknown', '"%s" is not a valid precision', precision);
171+
error('precision:unknown', ...
172+
'"%s" is not a valid precision', precision);
155173
end
156174
end
157175
end

tests/SeriesTest.m

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
classdef SeriesTest < matlab.unittest.TestCase
22

33
properties(Access = private)
4-
Time, Temperature, Humidity, WindDirection, RainDrops, Raining;
4+
Time, Temperature, Humidity, WindDirection, RainDrops, Raining
55
end
66

77
methods(TestMethodSetup)
@@ -17,25 +17,52 @@ function setup(test)
1717
end
1818

1919
methods(Test)
20-
function fails_when_time_is_not_set(test)
20+
function fails_when_empty_name(test)
21+
f = @() Series('').fields('temperature', 24.3).toLine();
22+
test.verifyError(f, 'toLine:emptyName');
23+
end
24+
25+
function fails_when_empty_time(test)
2126
s = Series('weather') ...
2227
.fields('temperature', test.Temperature);
2328
test.verifyError(@() s.toLine(), 'toLine:emptyTime');
2429
end
2530

26-
function fails_when_fields_are_not_set(test)
31+
function allow_empty_time_for_single_samples(test)
2732
s = Series('weather') ...
28-
.time(test.Time);
29-
test.verifyError(@() s.toLine(), 'toLine:emptyFields');
33+
.fields('temperature', 24.3);
34+
exp = 'weather temperature=24.3';
35+
test.verifyEqual(s.toLine(), exp);
3036
end
3137

32-
function field_with_empty_value_fails(test)
33-
f = @() Series('weather') ...
34-
.fields('temperature', []);
35-
test.verifyError(f, 'field:emptyValue');
38+
function when_empty_fields_return_empty(test)
39+
s = Series('weather');
40+
test.verifyEqual(s.toLine(), '');
41+
end
42+
43+
function empty_fields_are_ignored(test)
44+
p = Series('weather') ...
45+
.fields('temperature', [], 'humidity', 60.7);
46+
exp = 'weather humidity=60.7';
47+
test.verifyEqual(p.toLine(), exp);
48+
end
49+
50+
function empty_char_fields_are_not_ignored(test)
51+
p = Series('weather') ...
52+
.fields('temperature', 24.3, 'wind_dir', '');
53+
exp = 'weather temperature=24.3,wind_dir=""';
54+
test.verifyEqual(p.toLine(), exp);
3655
end
3756

3857
function single_field(test)
58+
p = Series('weather') ...
59+
.fields('temperature', 24.3) ...
60+
.time(test.Time(1));
61+
exp = 'weather temperature=24.3 1529933525520';
62+
test.verifyEqual(p.toLine(), exp);
63+
end
64+
65+
function single_field_array(test)
3966
s = Series('weather') ...
4067
.time(test.Time) ...
4168
.fields('temperature', test.Temperature);
@@ -55,7 +82,15 @@ function supports_fields_with_integer_values(test)
5582
test.verifyEqual(p.toLine(), exp);
5683
end
5784

58-
function supports_fields_with_string_values(test)
85+
function supports_fields_with_single_string_value(test)
86+
p = Series('weather') ...
87+
.fields('wind_direction', 'north-west') ...
88+
.time(test.Time(1));
89+
exp = 'weather wind_direction="north-west" 1529933525520';
90+
test.verifyEqual(p.toLine(), exp);
91+
end
92+
93+
function supports_fields_with_cell_string_values(test)
5994
p = Series('weather') ...
6095
.time(test.Time) ...
6196
.fields('wind_direction', test.WindDirection);
@@ -171,6 +206,15 @@ function fails_when_field_sizes_do_not_match(test)
171206
end
172207

173208
function every_property_is_used(test)
209+
s = Series('weather') ...
210+
.time(test.Time(1)) ...
211+
.tags('city', 'barcelona') ...
212+
.fields('temperature', 24.3);
213+
exp = 'weather,city=barcelona temperature=24.3 1529933525520';
214+
test.verifyEqual(s.toLine(), exp);
215+
end
216+
217+
function every_property_is_used_with_multiple_samples(test)
174218
s = Series('weather') ...
175219
.time(test.Time) ...
176220
.tags('city', 'barcelona') ...
@@ -201,6 +245,14 @@ function skips_nonfinite_samples(test)
201245
test.verifyEqual(s.toLine(), exp);
202246
end
203247

248+
function returns_empty_when_all_values_are_nonfinite(test)
249+
s = Series('weather') ...
250+
.time(test.Time) ...
251+
.fields('temperature', [NaN; Inf]) ...
252+
.fields('humidity', [-Inf; NaN]);
253+
test.verifyEqual(s.toLine(), '');
254+
end
255+
204256
function time_is_added_in_millis_by_default(test)
205257
millis = 1529933525520;
206258
time = datetime(millis / 1000, 'ConvertFrom', 'posixtime');

0 commit comments

Comments
 (0)