Skip to content

Commit

Permalink
test: fix test for pagination cursor (#1138)
Browse files Browse the repository at this point in the history
* fix: create 50 alerts for cursor pagination test

* linting

* use nextCursor

* remove comment

* linting

* use int for limit

* add client and cogniterrors in test file

* linting

* remove unnecessary expectation
  • Loading branch information
VebjornG authored Sep 4, 2024
1 parent 860f519 commit a3e9106
Showing 1 changed file with 15 additions and 36 deletions.
51 changes: 15 additions & 36 deletions packages/beta/src/__tests__/api/alertsApi.int.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright 2020 Cognite AS

import type { CogniteClient, CogniteError } from '@cognite/sdk-beta';
import { describe, expect, test, vi } from 'vitest';
import { setupLoggedInClient } from '../testUtils';

Expand Down Expand Up @@ -228,7 +229,7 @@ describe('alerts api', () => {

vi.setConfig({ testTimeout: 30_000 });

test.skip('cursor pagination', async () => {
test('cursor pagination', async () => {
// create channel for the next test
const channelsToCreate = [
{
Expand All @@ -244,55 +245,33 @@ describe('alerts api', () => {
property: 'createdTime',
order: 'desc',
},
cursor: '',
});
expect(response.items.length).toBe(1);

// create alerts in batches of 100
const totalAlerts = 1000; // Total number of alerts to create
const batchSize = 100; // Size of each batch
const totalAlerts = 50; // Total number of alerts to create

// Generate and create alerts in batches
let alertCounter = Date.now(); // Counter to ensure unique externalId

// Function to create a batch of alerts
const createBatch = async () => {
const alerts = Array.from({ length: batchSize }, () => ({
source: 'smth',
channelExternalId,
externalId: `external_id_test_cursor_${alertCounter++}`,
}));
await client.alerts.create(alerts);
};

// Create alerts in batches
const batchPromises = [];
for (let i = 0; i < Math.floor(totalAlerts / batchSize); i++) {
batchPromises.push(createBatch());
}

// Wait for all batches to complete
await Promise.all(batchPromises);
// Create alerts
const createdAlerts = Array.from({ length: totalAlerts }, () => ({
source: 'smth',
channelExternalId,
externalId: `external_id_test_cursor_${alertCounter++}`,
}));

// create one extra alert
await client.alerts.create([
{
source: 'smth',
channelExternalId,
externalId: `external_id_test_cursor_${alertCounter}`,
},
]);
await client.alerts.create(createdAlerts);

const alerts = client.alerts
const alerts = await client.alerts
.list({
sort: {
property: 'createdTime',
order: 'desc',
},
cursor: response.nextCursor,
limit: 10,
})
.autoPagingToArray({ limit: 1001 });
.autoPagingToArray({ limit: 50 });

expect((await alerts).length).toBeGreaterThan(1000);
expect((await alerts).length).toBe(50);

// clean up created alerts
await client.alerts.deleteChannels([{ externalId: channelExternalId }]);
Expand Down

0 comments on commit a3e9106

Please sign in to comment.