Skip to content

Commit

Permalink
Update tests, actions
Browse files Browse the repository at this point in the history
  • Loading branch information
rolljee committed Oct 9, 2023
1 parent 49989c1 commit f92a42b
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 29 deletions.
1 change: 1 addition & 0 deletions .github/actions/functional-test-backend/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ runs:
shell: bash
run: |
set +e
npm ci
export CI=true
docker compose up -d
bash ./.github/actions/functional-test-backend/wait-kuzzle.sh
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,31 +96,6 @@ describe("Geofencing: create movement record measure", () => {
await moveTruck(insideIDFSud[0]);
await sdk.collection.refresh("tenant-asset_tracking-kuzzle", "measures");

const measures = await sdk.document.search<MeasureContent>(
"tenant-asset_tracking-kuzzle",
"measures",
{
query: {
equals: { type: "movementRecord" },
},
sort: { measuredAt: "asc" },
},
{ lang: "koncorde" },
);

expect(measures.hits).toHaveLength(2);
expect(measures.hits[0]._source).toMatchObject({
values: {
in: null,
},
});
expect(measures.hits[1]._source).toMatchObject({
values: {
out: null,
in: "IDFSud",
},
});

const assets = await sdk.document.get<AssetContent>(
"tenant-asset_tracking-kuzzle",
"assets",
Expand Down
8 changes: 4 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ services:
- DEBUG=${DEBUG:-none}
healthcheck:
test: ["CMD", "curl", "-f", "http://api:7512/_healthcheck"]
timeout: 30s
interval: 30s
retries: 100
start_period: 3m
timeout: 10s
interval: 10s
retries: 30
start_period: 1m

web:
image: kuzzleio/kuzzle-runner:18
Expand Down
23 changes: 23 additions & 0 deletions helpers/collections.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Kuzzle } from 'kuzzle-sdk';

export async function truncateCollection(sdk: Kuzzle, index: string, collection: string) {
await sdk.collection.refresh(index, collection);
await sdk.document.deleteByQuery(index, collection, {});
await sdk.collection.refresh(index, collection);
}

/**
* @deprecated alias to truncateCollection
*/
export const resetCollection = truncateCollection;

export async function beforeEachTruncateCollections(sdk: Kuzzle) {
await Promise.all([
truncateCollection(sdk, 'platform', 'devices'),
truncateCollection(sdk, 'tenant-asset_tracking-kuzzle', 'assets'),
truncateCollection(sdk, 'tenant-asset_tracking-kuzzle', 'devices'),
truncateCollection(sdk, 'tenant-asset_tracking-kuzzle', 'assets-history'),
truncateCollection(sdk, 'tenant-asset_tracking-kuzzle', 'measures'),
truncateCollection(sdk, 'tenant-asset_tracking-kuzzle', 'alerts'),
]);
}
5 changes: 5 additions & 0 deletions helpers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from './collections';
export * from './payloads';
export * from './setup';
export * from './tenants';
export * from './useSdk';
32 changes: 32 additions & 0 deletions helpers/payloads.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { JSONObject, Kuzzle } from 'kuzzle-sdk';

// Delay in second for dates sent between two steps so the plugin accept the new
// Add a delay for subsequent payload of the same device so the plugin accept it
const deviceDelay = {};

export async function sendPayloads(sdk: Kuzzle, action: string, payloads: JSONObject[]) {
let response;

for (let i = 0; i < payloads.length; i++) {
const payload = payloads[i];

if (deviceDelay[payload.reference]) {
deviceDelay[payload.reference] += 2;
} else {
deviceDelay[payload.reference] = 1;
}

if (!payload.date) {
const delay = deviceDelay[payload.reference] * 1000;
payload.date = new Date(Date.now() + delay);
}

response = await sdk.query({
controller: 'device-manager/payloads',
action,
body: payload,
});
}

return response;
}
22 changes: 22 additions & 0 deletions helpers/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { beforeEachTruncateCollections } from './collections';
import { beforeAllCreateTenants } from './tenants';
import { useSdk } from './useSdk';

export function setupHooks() {
const sdk = useSdk();

beforeAll(async () => {
await sdk.connect();
await beforeAllCreateTenants(sdk);
});

beforeEach(async () => {
await beforeEachTruncateCollections(sdk);
});

afterAll(async () => {
sdk.disconnect();
});

return sdk;
}
25 changes: 25 additions & 0 deletions helpers/tenants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Kuzzle } from 'kuzzle-sdk';

async function createTenantIfNotExists(sdk: Kuzzle, group: string, name: string) {
const { result } = await sdk.query<any, any>({
controller: 'multi-tenancy/tenant',
action: 'exists',
name,
group,
});

if (result.exists) {
return;
}

await sdk.query({
controller: 'multi-tenancy/tenant',
action: 'create',
name,
group,
});
}

export async function beforeAllCreateTenants(sdk: Kuzzle) {
await Promise.all([createTenantIfNotExists(sdk, 'asset_tracking', 'kuzzle')]);
}
5 changes: 5 additions & 0 deletions helpers/useSdk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Kuzzle, WebSocket } from 'kuzzle-sdk';

export function useSdk(): Kuzzle {
return new Kuzzle(new WebSocket('localhost', { port: 7512 }));
}

0 comments on commit f92a42b

Please sign in to comment.