Skip to content

Commit

Permalink
add method, add int and unit tests for method, refactor types (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsmith023 authored Aug 22, 2023
1 parent 85962c1 commit 1af7777
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 6 deletions.
17 changes: 13 additions & 4 deletions src/data/checker.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import Connection from '../connection';
import { ConsistencyLevel } from './replication';
import { ObjectsPath } from './path';
import { CommandBase } from '../validation/commandBase';

export default class Checker extends CommandBase {
private className!: string;
private consistencyLevel?: ConsistencyLevel;
private id!: string;
private tenant?: string;
private objectsPath: ObjectsPath;
Expand All @@ -28,6 +30,15 @@ export default class Checker extends CommandBase {
return this;
};

withConsistencyLevel = (consistencyLevel: ConsistencyLevel) => {
this.consistencyLevel = consistencyLevel;
return this;
};

buildPath = () => {
return this.objectsPath.buildCheck(this.id, this.className, this.consistencyLevel, this.tenant);
};

validateIsSet = (prop: string | undefined | null, name: string, setter: string) => {
if (prop == undefined || prop == null || prop.length == 0) {
this.addError(`${name} must be set - set with ${setter}`);
Expand All @@ -42,14 +53,12 @@ export default class Checker extends CommandBase {
this.validateId();
};

do = () => {
do = (): Promise<boolean> => {
if (this.errors.length > 0) {
return Promise.reject(new Error('invalid usage: ' + this.errors.join(', ')));
}
this.validate();

return this.objectsPath.buildCheck(this.id, this.className, this.tenant).then((path: string) => {
return this.client.head(path, undefined);
});
return this.buildPath().then((path: string) => this.client.head(path, undefined));
};
}
48 changes: 48 additions & 0 deletions src/data/journey.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ const thingClassName = 'DataJourneyTestThing';
const refSourceClassName = 'DataJourneyTestRefSource';
const classCustomVectorClassName = 'ClassCustomVector';

const fail = (msg: string) => {
throw new Error(msg);
};

describe('data', () => {
const client = weaviate.client({
scheme: 'http',
Expand Down Expand Up @@ -699,6 +703,23 @@ describe('data', () => {
});
});

it('forms a exists query with consistency_level set', () => {
const id = '00000000-0000-0000-0000-000000000000';

return client.data
.checker()
.withClassName(thingClassName)
.withId(id)
.withConsistencyLevel('QUORUM')
.buildPath()
.then((path: string) => {
expect(path).toContain('consistency_level=QUORUM');
})
.catch((e: WeaviateError) => {
throw new Error('it should not have errord: ' + e);
});
});

it('creates object with consistency_level set', async () => {
const id = '144d1944-3ab4-4aa1-8095-92429d6cbaba';
const properties = { foo: 'bar' };
Expand Down Expand Up @@ -1078,6 +1099,33 @@ describe('data', () => {
.catch((e) => fail('it should not have errord: ' + e));
});

it('checks an object exists with consistency_level set', async () => {
const id = 'e7c7f6d5-4c9d-4a4e-8e1b-9d3d5a0e4d9f';
const props = { stringProp: 'foobar' };

await client.data
.creator()
.withClassName(thingClassName)
.withProperties(props)
.withId(id)
.do()
.then((res) => {
expect(res.properties).toEqual(props);
expect(res.id).toEqual(id);
})
.catch((e) => fail('it should not have errord: ' + e));

return client.data
.checker()
.withId(id)
.withConsistencyLevel('QUORUM')
.do()
.then((exists) => {
expect(exists).toBe(true);
})
.catch((e) => fail('it should not have errord: ' + e));
});

it('tears down and cleans up', () => {
return Promise.all([
client.schema.classDeleter().withClassName(thingClassName).do(),
Expand Down
9 changes: 7 additions & 2 deletions src/data/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ export class ObjectsPath {
this.addQueryParams,
]);
}
buildCheck(id: string, className: string, tenant?: string): Promise<string> {
return this.build({ id, className, tenant: tenant }, [
buildCheck(
id: string,
className: string,
consistencyLevel?: ConsistencyLevel,
tenant?: string
): Promise<string> {
return this.build({ id, className, consistencyLevel, tenant }, [
this.addClassNameDeprecatedNotSupportedCheck,
this.addId,
this.addQueryParams,
Expand Down

0 comments on commit 1af7777

Please sign in to comment.