-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
117 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'), | ||
]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 })); | ||
} |