Skip to content
This repository has been archived by the owner on Dec 9, 2023. It is now read-only.

Commit

Permalink
🚩 test: Add test for indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
cheap-glitch committed Dec 24, 2020
1 parent ab71082 commit e7c704c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
10 changes: 3 additions & 7 deletions src/lib/resources.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Client as FaunaClient, query as q, errors as FaunaErrors } from 'faunadb';
import { FaunaResource, FaunaResourceType, FaunaQueryResult, FaunaUploadResults } from '../types';

export async function uploadResources(client: FaunaClient, type: FaunaResourceType, resources: Array<FaunaResource>): Promise<FaunaUploadResults | FaunaErrors.FaunaHTTPError> {
export async function uploadResources(client: FaunaClient, type: FaunaResourceType, resources: Array<FaunaResource>): Promise<FaunaUploadResults | FaunaErrors.InvalidValue | FaunaErrors.FaunaHTTPError> {
const [INDEX, CREATE] = (() => {
switch (type) {
case FaunaResourceType.Role: return [q.Role, q.CreateRole ];
Expand All @@ -28,11 +28,7 @@ export async function uploadResources(client: FaunaClient, type: FaunaResourceTy
))));

let response;
try {
response = (await query) as FaunaUploadResults;
} catch(error) {
return error;
}
try { response = await query; } catch(error) { return error; }

return response;
return response as FaunaUploadResults;
}
47 changes: 46 additions & 1 deletion test/resources.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { query as q } from 'faunadb';
import { query as q, errors as FaunaErrors } from 'faunadb';

import { Database } from './helpers/database';

Expand Down Expand Up @@ -91,3 +91,48 @@ test("update an existing role", async () => { // {{{
expect(result).toEqual([FaunaQueryResult.Updated]);

}); // }}}

test("create a new index", async () => { // {{{

// Setup
if (!(await db.collectionExists('spaceships'))) {
await db.createCollection('spaceships');
}

// Action
const result = await uploadResources(db.getClient(), FaunaResourceType.Index, [{
name: 'spaceship_names',
source: q.Collection('spaceships'),
unique: true,
terms: [{ field: ['data', 'name'] }]
}]);

// Tests
expect(result).toEqual([FaunaQueryResult.Created]);
await expect(db.indexExists('spaceship_names')).resolves.toBe(true);

}); // }}}

test("error when updating an existing index", async () => { // {{{

// Setup
await expect(db.collectionExists('spaceships')).resolves.toBe(true);
await expect(db.indexExists('spaceship_names')).resolves.toBe(true);

// Action
const result = await uploadResources(db.getClient(), FaunaResourceType.Index, [{
name: 'spaceship_names',
source: q.Collection('spaceships'),
unique: false,
terms: [{ field: ['data', 'name'] }],
values: [{ field: ['data', 'name'] , reverse: false }],
}]);

// Tests
expect(result).toBeInstanceOf(FaunaErrors.FaunaHTTPError);
if (result instanceof FaunaErrors.FaunaHTTPError) {
expect(result.message).toBe('validation failed');
expect(result.requestResult.responseRaw).toMatch('Index sources, terms, values, and partition count may not be updated');
}

}); // }}}

0 comments on commit e7c704c

Please sign in to comment.