Skip to content

Commit

Permalink
chore: add tests for list
Browse files Browse the repository at this point in the history
  • Loading branch information
gulkaktus committed Jan 17, 2024
1 parent 1271a79 commit b10e2db
Showing 1 changed file with 114 additions and 14 deletions.
128 changes: 114 additions & 14 deletions packages/stable/src/__tests__/api/instances.int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ const upsertSpace = async (client: CogniteClient, space: SpaceDefinition) => {
});
};

const upsertDescribable = async (
const upsertDescribables = async (
client: CogniteClient,
describable: Describable,
describables: Describable[],
view: ViewReference
) => {
await client.post(`/api/v1/projects/${client.project}/models/instances`, {
data: {
items: [
{
describables.map((describable) => ({
instanceType: 'node',
externalId: describable.externalId,
space: describable.space,
Expand All @@ -44,7 +44,7 @@ const upsertDescribable = async (
description: describable.description,
labels: describable.labels,
},
},
})),
],
},
});
Expand All @@ -65,18 +65,26 @@ describe('Instances integration test', () => {
type: 'view',
version: 'v1',
};
const describable: Describable = {
externalId: `describable_${timestamp}`,

const describable1: Describable = {
externalId: `describable_1_${timestamp}`,
space: testSpace.space,
title: `title ${timestamp}`,
description: `description ${timestamp}`,
title: `title_1_${timestamp}`,
description: `description_1_${timestamp}`,
labels: [`label1`, 'label2'],
};
const describable2: Describable = {
externalId: `describable_2_${timestamp}`,
space: testSpace.space,
title: `title_2_${timestamp}`,
description: `description_2_${timestamp}`,
labels: [`label1`, 'label2'],
};

beforeAll(async () => {
client = setupLoggedInClient();
await upsertSpace(client, testSpace);
await upsertDescribable(client, describable, view);
await upsertDescribables(client, [describable1, describable2], view);
});

afterAll(async () => {
Expand All @@ -87,7 +95,7 @@ describe('Instances integration test', () => {
items: [
{
instanceType: 'node',
externalId: describable.externalId,
externalId: describable1.externalId,
space: testSpace.space,
},
],
Expand All @@ -96,24 +104,116 @@ describe('Instances integration test', () => {
);
});

test('search nodes with limit 1', async () => {
test('list nodes from a single view with limit 1', async () => {
const response = await client.instances.list({
sources: [{ source: view }],
instanceType: 'node',
limit: 1,
});
expect(response.items).toHaveLength(2);
expect(response.items[0].externalId).toBeDefined();
expect(response.items[1].externalId).toBeDefined();
});

test('list nodes with a filter that returns 1 result', async () => {
const response = await client.instances.list({
sources: [{ source: view }],
filter: {
equals: {
property: ['title'],
value: describable1.title,
},
},
instanceType: 'node',
limit: 1000,
});
expect(response.items).toHaveLength(1);
expect(response.items[0].externalId).toBeDefined();
expect(response.items[0].externalId).toBe(describable1.externalId);
});

test('list nodes with a filter that returns no results', async () => {
const response = await client.instances.list({
sources: [{ source: view }],
filter: {
equals: {
property: ['title'],
value: 'No describable has this title',
},
},
instanceType: 'node',
limit: 1000,
});
expect(response.items).toHaveLength(0);
});

test('list nodes sorted in ascending order', async () => {
const response = await client.instances.list({
sources: [{ source: view }],
sort: [
{ property: ['title'], direction: 'ascending', nullsFirst: false },
],
instanceType: 'node',
limit: 2,
});
expect(response.items).toHaveLength(2);
expect(response.items[0].externalId).toBeDefined();
expect(response.items[1].externalId).toBeDefined();

const title1 =
response.items[0].properties![view.space][
`${view.externalId}/${view.version}`
]['title'].toString();
const title2 =
response.items[0].properties![view.space][
`${view.externalId}/${view.version}`
]['title'].toString();
expect(title1 < title2);
});

test('list nodes sorted in descending order', async () => {
const response = await client.instances.list({
sources: [{ source: view }],
sort: [
{ property: ['title'], direction: 'descending', nullsFirst: false },
],
instanceType: 'node',
limit: 2,
});
expect(response.items).toHaveLength(2);
expect(response.items[0].externalId).toBeDefined();
expect(response.items[1].externalId).toBeDefined();

const title1 =
response.items[0].properties![view.space][
`${view.externalId}/${view.version}`
]['title'].toString();
const title2 =
response.items[0].properties![view.space][
`${view.externalId}/${view.version}`
]['title'].toString();
expect(title1 > title2);
});

test('search nodes with limit 2', async () => {
const response = await client.instances.search({
view,
instanceType: 'node',
limit: 1,
});
expect(response.items).toHaveLength(1);
expect(response.items).toHaveLength(2);
expect(response.items[0].externalId).toBeDefined();
expect(response.items[1].externalId).toBeDefined();
});

test('search with query', async () => {
const response = await client.instances.search({
view,
query: describable.description,
query: describable1.description,
limit: 1,
});
expect(response.items).toHaveLength(1);
expect(response.items[0].externalId).toBe(describable.externalId);
expect(response.items[0].externalId).toBe(describable1.externalId);
});

test('search with filter', async () => {
Expand Down

0 comments on commit b10e2db

Please sign in to comment.