Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lunaticusgreen committed Jan 8, 2025
1 parent 07f0fd5 commit 7ddf223
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 65 deletions.
12 changes: 6 additions & 6 deletions src/spec/datasource.jest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ describe('clickhouse sql series:', () => {
});

it('should get three datapoints', () => {
expect(size(timeSeries[0].datapoints)).toBe(3);
expect(size(timeSeries[1].datapoints)).toBe(3);
expect(size(timeSeries[0].fields[0].values)).toBe(3);
expect(size(timeSeries[0].fields[0].values)).toBe(3);
});
});

Expand Down Expand Up @@ -116,10 +116,10 @@ describe('clickhouse sql series:', () => {
});

it('should get three datapoints', () => {
expect(size(timeSeries[0].datapoints)).toBe(4);
expect(size(timeSeries[1].datapoints)).toBe(4);
expect(size(timeSeries[2].datapoints)).toBe(4);
expect(size(timeSeries[3].datapoints)).toBe(4);
expect(size(timeSeries[0].fields[0].values)).toBe(4);
expect(size(timeSeries[1].fields[0].values)).toBe(4);
expect(size(timeSeries[2].fields[0].values)).toBe(4);
expect(size(timeSeries[3].fields[0].values)).toBe(4);
});
});

Expand Down
72 changes: 13 additions & 59 deletions src/spec/sql_series_specs.jest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ describe('sql-series. toTimeSeries unit tests', () => {
selfMock.keys = [];

const result = toTimeSeries(true, selfMock);
expect(result).toEqual([{ target: 'value', datapoints: [[10, 1000]] }]);
expect(result).toEqual([{"fields": [{"config": {"links": []}, "name": "time", "type": "time", "values": [1000]}, {"config": {"links": []}, "name": "value", "values": [10]}], "length": 1, "refId": undefined}]);
});

it('should handle multiple data points correctly', () => {
Expand All @@ -329,56 +329,17 @@ describe('sql-series. toTimeSeries unit tests', () => {
selfMock.keys = [];

const result = toTimeSeries(true, selfMock);
expect(result).toEqual([
{
target: 'value',
datapoints: [
[10, 1000],
[20, 2000],
],
},
]);
expect(result).toEqual([{"fields": [{"config": {"links": []}, "name": "time", "type": "time", "values": [1000, 2000]}, {"config": {"links": []}, "name": "value", "values": [10, 20]}], "length": 2, "refId": undefined}]);
});

it('should extrapolate data points when required', () => {
let expectedDataPoints: number[][] = [];
selfMock.series = [];
for (let i = 1; i <= 10; i++) {
const t = Date.now();
selfMock.series[i - 1] = { time: t - i * 15 * 1000, value: i * 2 + 30 };
expectedDataPoints[i - 1] = [i * 2 + 30, t - i * 15 * 1000];
}

expectedDataPoints[9][0] = 48.2; // extrapolated value
selfMock.meta = [
{ name: 'time', type: 'UInt32' },
{ name: 'value', type: 'UInt64' },
];
selfMock.keys = [];
selfMock.tillNow = true;

let selfMock = {"from": 0, "keys": [], "meta": [{"name": "time", "type": "UInt32"}, {"name": "value", "type": "UInt64"}], "series": [{"time": 1736332351828, "value": 32}, {"time": 1736332336828, "value": 34}, {"time": 1736332321828, "value": 36}, {"time": 1736332306828, "value": 38}, {"time": 1736332291828, "value": 40}, {"time": 1736332276828, "value": 42}, {"time": 1736332261828, "value": 44}, {"time": 1736332246828, "value": 46}, {"time": 1736332231828, "value": 48}, {"time": 1736332216828, "value": 50}], "tillNow": true, "to": 1000}
const result = toTimeSeries(true, selfMock);
expect(result).toEqual([
{
target: 'value',
datapoints: expectedDataPoints,
},
]);

// less 10 points
selfMock.series = [];
expectedDataPoints = [];
for (let i = 1; i <= 3; i++) {
selfMock.series[i - 1] = { time: Date.now() - i * 30 * 1000, value: i * 2 + 50 };
expectedDataPoints[i - 1] = [i * 2 + 50, Date.now() - i * 30 * 1000];
}
expect(result).toEqual([{"fields": [{"config": {"links": []}, "name": "time", "type": "time", "values": [1736332351828, 1736332336828, 1736332321828, 1736332306828, 1736332291828, 1736332276828, 1736332261828, 1736332246828, 1736332231828, 1736332216828]}, {"config": {"links": []}, "name": "value", "values": [32, 34, 36, 38, 40, 42, 44, 46, 48, 48.2]}], "length": 10, "refId": undefined}]);

This comment has been minimized.

Copy link
@Slach

Slach Jan 8, 2025

Collaborator

are we sure we can't use mock here properly and need fixed values for the time?


selfMock = {"from": 0, "keys": [], "meta": [{"name": "time", "type": "UInt32"}, {"name": "value", "type": "UInt64"}], "series": [{"time": 1736332580592, "value": 52}, {"time": 1736332550592, "value": 54}, {"time": 1736332520592, "value": 56}], "tillNow": true, "to": 1000}
const resultNonExtrapolated = toTimeSeries(true, selfMock);
expect(resultNonExtrapolated).toEqual([
{
target: 'value',
datapoints: expectedDataPoints,
},
]);
expect(resultNonExtrapolated).toEqual([{"fields": [{"config": {"links": []}, "name": "time", "type": "time", "values": [1736332580592, 1736332550592, 1736332520592]}, {"config": {"links": []}, "name": "value", "values": [52, 54, 56]}], "length": 3, "refId": undefined}]);

});

Expand All @@ -396,10 +357,11 @@ describe('sql-series. toTimeSeries unit tests', () => {
selfMock.tillNow = false;

const result = toTimeSeries(true, selfMock);
expect(result).toEqual([
{ target: 'A', datapoints: [[1000, 1000], [10, 1000]] },
{ target: 'B', datapoints: [[null, 1000], [null, 1000], [2000, 2000], [20, 2000]] },
]);
expect(result).toEqual([{"fields": [{"config": {"links": []}, "name": "time", "type": "time", "values": [1000, 1000]}, {"config": {"links": []}, "name": "A", "values": [1000, 10]}], "length": 2, "refId": undefined}, {"fields": [{"config": {"links": []}, "name": "time", "type": "time", "values": [1000, 1000, 2000, 2000]}, {"config": {"links": []}, "name": "B", "values": [null, null, 2000, 20]}], "length": 4, "refId": undefined}])
// expect(result).toEqual([

This comment has been minimized.

Copy link
@Slach

Slach Jan 8, 2025

Collaborator

remove unused code

// { target: 'A', datapoints: [[1000, 1000], [10, 1000]] },
// { target: 'B', datapoints: [[null, 1000], [null, 1000], [2000, 2000], [20, 2000]] },
// ]);
});

it('should handle null values correctly', () => {
Expand All @@ -414,15 +376,7 @@ describe('sql-series. toTimeSeries unit tests', () => {
selfMock.keys = [];

const result = toTimeSeries(false, selfMock);
expect(result).toEqual([
{
target: 'value',
datapoints: [
[null, 1000],
[20, 2000],
],
},
]);
expect(result).toEqual([{"fields": [{"config": {"links": []}, "name": "time", "type": "time", "values": [1000, 2000]}, {"config": {"links": []}, "name": "value", "values": [null, 20]}], "length": 2, "refId": undefined}]);
});
});

Expand Down

0 comments on commit 7ddf223

Please sign in to comment.